-
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
5 changed files
with
221 additions
and
0 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
204 changes: 204 additions & 0 deletions
204
Course 1: Introduction to Tensorflow/Week 2/Exercise2-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,204 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"colab_type": "text", | ||
"id": "tOoyQ70H00_s" | ||
}, | ||
"source": [ | ||
"## Exercise 2\n", | ||
"In the course you learned how to do classificaiton using Fashion MNIST, a data set containing items of clothing. There's another, similar dataset called MNIST which has items of handwriting -- the digits 0 through 9.\n", | ||
"\n", | ||
"Write an MNIST classifier that trains to 99% accuracy or above, and does it without a fixed number of epochs -- i.e. you should stop training once you reach that level of accuracy.\n", | ||
"\n", | ||
"Some notes:\n", | ||
"1. It should succeed in less than 10 epochs, so it is okay to change epochs= to 10, but nothing larger\n", | ||
"2. When it reaches 99% or greater it should print out the string \"Reached 99% accuracy so cancelling training!\"\n", | ||
"3. If you add any additional variables, make sure you use the same names as the ones used in the class\n", | ||
"\n", | ||
"I've started the code for you below -- how would you finish it? " | ||
] | ||
}, | ||
{ | ||
"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\n", | ||
"from tensorflow.keras.callbacks import Callback\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": { | ||
"colab": {}, | ||
"colab_type": "code", | ||
"id": "9rvXQGAA0ssC" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# GRADED FUNCTION: train_mnist\n", | ||
"def train_mnist():\n", | ||
" # Please write your code only where you are indicated.\n", | ||
" # please do not remove # model fitting inline comments.\n", | ||
"\n", | ||
" # YOUR CODE SHOULD START HERE\n", | ||
" class myCallback(Callback):\n", | ||
" def on_epoch_end(self, epoch, logs={}):\n", | ||
" if (logs.get('acc') >= 0.99):\n", | ||
" print(\"\\nReached 99% accuracy so cancelling training!\")\n", | ||
" self.model.stop_training = True\n", | ||
" callbacks = myCallback()\n", | ||
" # YOUR CODE SHOULD END HERE\n", | ||
"\n", | ||
" mnist = tf.keras.datasets.mnist\n", | ||
"\n", | ||
" (x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)\n", | ||
" # YOUR CODE SHOULD START HERE\n", | ||
" x_train = x_train/255.0\n", | ||
" x_test = x_test/255.0\n", | ||
" # YOUR CODE SHOULD END HERE\n", | ||
" model = Sequential([\n", | ||
" # YOUR CODE SHOULD START HERE\n", | ||
" Flatten(),\n", | ||
" Dense(512, activation = 'relu'),\n", | ||
" Dense(10, activation ='softmax')\n", | ||
" # YOUR CODE SHOULD END HERE\n", | ||
" ])\n", | ||
"\n", | ||
" model.compile(optimizer='adam',\n", | ||
" loss='sparse_categorical_crossentropy',\n", | ||
" metrics=['accuracy'])\n", | ||
" \n", | ||
" # model fitting\n", | ||
" history = model.fit(# YOUR CODE SHOULD START HERE\n", | ||
" x_train, y_train, epochs = 10, callbacks = [callbacks]\n", | ||
" # YOUR CODE SHOULD END HERE\n", | ||
" )\n", | ||
" # model fitting\n", | ||
" return history.epoch, history.history['acc'][-1]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"colab": {}, | ||
"colab_type": "code", | ||
"id": "9rvXQGAA0ssC" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"WARNING: Logging before flag parsing goes to stderr.\n", | ||
"W1202 21:06:40.502089 140386063906624 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n", | ||
"Instructions for updating:\n", | ||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Epoch 1/10\n", | ||
"60000/60000 [==============================] - 10s 160us/sample - loss: 0.1986 - acc: 0.9422\n", | ||
"Epoch 2/10\n", | ||
"60000/60000 [==============================] - 9s 146us/sample - loss: 0.0801 - acc: 0.9758\n", | ||
"Epoch 3/10\n", | ||
"60000/60000 [==============================] - 9s 147us/sample - loss: 0.0514 - acc: 0.9835\n", | ||
"Epoch 4/10\n", | ||
"60000/60000 [==============================] - 9s 155us/sample - loss: 0.0363 - acc: 0.9882\n", | ||
"Epoch 5/10\n", | ||
"59744/60000 [============================>.] - ETA: 0s - loss: 0.0280 - acc: 0.9909\n", | ||
"Reached 99% accuracy so cancelling training!\n", | ||
"60000/60000 [==============================] - 9s 155us/sample - loss: 0.0280 - acc: 0.9908\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"([0, 1, 2, 3, 4], 0.99085)" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"train_mnist()" | ||
] | ||
}, | ||
{ | ||
"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": "d6dew", | ||
"launcher_item_id": "FExZ4" | ||
}, | ||
"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 | ||
} |
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.