-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
Course 1: Introduction to Tensorflow/Week 3/Excercise-3-Question.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"colab_type": "text", | ||
"id": "iQjHqsmTAVLU" | ||
}, | ||
"source": [ | ||
"## Exercise 3\n", | ||
"In the videos you looked at how you would improve Fashion MNIST using Convolutions. For your exercise see if you can improve MNIST to 99.8% accuracy or more using only a single convolutional layer and a single MaxPooling 2D. You should stop training once the accuracy goes above this amount. It should happen in less than 20 epochs, so it's ok to hard code the number of epochs for training, but your training must end once it hits the above metric. If it doesn't, then you'll need to redesign your layers.\n", | ||
"\n", | ||
"I've started the code for you -- you need to finish it!\n", | ||
"\n", | ||
"When 99.8% accuracy has been hit, you should print out the string \"Reached 99.8% accuracy so cancelling training!\"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"from os import path, getcwd, chdir\n", | ||
"from tensorflow.keras.models import Sequential\n", | ||
"from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D\n", | ||
"from tensorflow.keras.callbacks import Callback\n", | ||
"\n", | ||
"# DO NOT CHANGE THE LINE BELOW. If you are developing in a local\n", | ||
"# environment, then grab mnist.npz from the Coursera Jupyter Notebook\n", | ||
"# and place it inside a local folder and edit the path to that location\n", | ||
"path = f\"{getcwd()}/../tmp2/mnist.npz\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"config = tf.ConfigProto()\n", | ||
"config.gpu_options.allow_growth = True\n", | ||
"sess = tf.Session(config=config)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# GRADED FUNCTION: train_mnist_conv\n", | ||
"def train_mnist_conv():\n", | ||
" # Please write your code only where you are indicated.\n", | ||
" # please do not remove model fitting inline comments.\n", | ||
"\n", | ||
" # YOUR CODE STARTS HERE\n", | ||
" class myCallback(Callback):\n", | ||
" def on_epoch_end(self, epoch, logs={}):\n", | ||
" if (logs.get('acc') >= 0.998):\n", | ||
" print(\"\\nReached 99% accuracy so cancelling training!\")\n", | ||
" self.model.stop_training = True\n", | ||
" callbacks = myCallback()\n", | ||
" \n", | ||
" # YOUR CODE ENDS HERE\n", | ||
"\n", | ||
" mnist = tf.keras.datasets.mnist\n", | ||
" (training_images, training_labels), (test_images, test_labels) = mnist.load_data(path=path)\n", | ||
" # YOUR CODE STARTS HERE\n", | ||
" training_images = training_images.reshape(60000, 28, 28, 1)/255.0\n", | ||
" test_images = test_images.reshape(10000, 28, 28, 1)/255.0\n", | ||
" # YOUR CODE ENDS HERE\n", | ||
"\n", | ||
" model = tf.keras.models.Sequential([\n", | ||
" # YOUR CODE STARTS HERE\n", | ||
" Conv2D(64, (3,3), activation = 'relu', input_shape = (28,28,1)),\n", | ||
" MaxPooling2D(2,2),\n", | ||
" Flatten(),\n", | ||
" Dense(128, activation = 'relu'),\n", | ||
" Dense(10, activation = 'softmax')\n", | ||
" # YOUR CODE ENDS HERE\n", | ||
" ])\n", | ||
"\n", | ||
" model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n", | ||
" # model fitting\n", | ||
" history = model.fit(\n", | ||
" # YOUR CODE STARTS HERE\n", | ||
" training_images, training_labels, epochs = 20, callbacks = [callbacks]\n", | ||
" # YOUR CODE ENDS HERE\n", | ||
" )\n", | ||
" # model fitting\n", | ||
" return history.epoch, history.history['acc'][-1]\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"_, _ = train_mnist_conv()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Now click the 'Submit Assignment' button above.\n", | ||
"# Once that is complete, please run the following two cells to save your work and close the notebook" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%javascript\n", | ||
"<!-- Save the notebook -->\n", | ||
"IPython.notebook.save_checkpoint();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%javascript\n", | ||
"IPython.notebook.session.delete();\n", | ||
"window.onbeforeunload = null\n", | ||
"setTimeout(function() { window.close(); }, 1000);" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"coursera": { | ||
"course_slug": "introduction-tensorflow", | ||
"graded_item_id": "ml06H", | ||
"launcher_item_id": "hQF8A" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
File renamed without changes
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.