-
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
9 changed files
with
293 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
236 changes: 236 additions & 0 deletions
236
Course 1: Introduction to Tensorflow/Week 4/Exercise4-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,236 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"colab_type": "text", | ||
"id": "UncprnB0ymAE" | ||
}, | ||
"source": [ | ||
"Below is code with a link to a happy or sad dataset which contains 80 images, 40 happy and 40 sad. \n", | ||
"Create a convolutional neural network that trains to 100% accuracy on these images, which cancels training upon hitting training accuracy of >.999\n", | ||
"\n", | ||
"Hint -- it will work best with 3 convolutional layers." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"import os\n", | ||
"import zipfile\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 happy-or-sad.zip 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/happy-or-sad.zip\"\n", | ||
"\n", | ||
"zip_ref = zipfile.ZipFile(path, 'r')\n", | ||
"zip_ref.extractall(\"/tmp/h-or-s\")\n", | ||
"zip_ref.close()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# GRADED FUNCTION: train_happy_sad_model\n", | ||
"def train_happy_sad_model():\n", | ||
" # Please write your code only where you are indicated.\n", | ||
" # please do not remove # model fitting inline comments.\n", | ||
"\n", | ||
" DESIRED_ACCURACY = 0.999\n", | ||
"\n", | ||
" class myCallback(Callback): # your code\n", | ||
" def on_epoch_end(self, epoch, logs={}):\n", | ||
" if (logs.get('acc') > DESIRED_ACCURACY):\n", | ||
" print(\"\\nReached ~100% accuracy so cancelling training!\")\n", | ||
" self.model.stop_training = True\n", | ||
" # Your Code\n", | ||
"\n", | ||
" callbacks = myCallback()\n", | ||
" \n", | ||
" # This Code Block should Define and Compile the Model. Please assume the images are 150 X 150 in your implementation.\n", | ||
" model = Sequential([\n", | ||
" # Your Code Here\n", | ||
" Conv2D(16, (3,3), activation = 'relu', input_shape = (150,150,3)),\n", | ||
" MaxPooling2D(2,2),\n", | ||
" Conv2D(32, (3,3), activation = 'relu'),\n", | ||
" MaxPooling2D(2,2),\n", | ||
" Conv2D(64, (3,3), activation = 'relu'),\n", | ||
" MaxPooling2D(2,2),\n", | ||
" Flatten(),\n", | ||
" Dense(512, activation = 'relu'),\n", | ||
" Dense(1, activation = 'sigmoid')\n", | ||
" ])\n", | ||
"\n", | ||
" from tensorflow.keras.optimizers import RMSprop\n", | ||
"\n", | ||
" model.compile(optimizer= RMSprop(lr = 0.001), loss='binary_crossentropy', metrics=['acc'])# Your Code Here #)\n", | ||
" \n", | ||
"\n", | ||
" # This code block should create an instance of an ImageDataGenerator called train_datagen \n", | ||
" # And a train_generator by calling train_datagen.flow_from_directory\n", | ||
"\n", | ||
" from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", | ||
"\n", | ||
" train_datagen = ImageDataGenerator(rescale = 1./255) # Your Code Here\n", | ||
"\n", | ||
" # Please use a target_size of 150 X 150.\n", | ||
" train_generator = train_datagen.flow_from_directory(\n", | ||
" # Your Code Here)\n", | ||
" # Expected output: 'Found 80 images belonging to 2 classes'\n", | ||
" '/tmp/h-or-s/',\n", | ||
" target_size = (150, 150),\n", | ||
" batch_size = 128,\n", | ||
" class_mode = 'binary'\n", | ||
" )\n", | ||
"\n", | ||
" # This code block should call model.fit_generator and train for\n", | ||
" # a number of epochs.\n", | ||
" # model fitting\n", | ||
" history = model.fit_generator(\n", | ||
" # Your Code Here)\n", | ||
" # model fitting\n", | ||
" train_generator,\n", | ||
" steps_per_epoch = 8,\n", | ||
" epochs = 15,\n", | ||
" callbacks = [callbacks],\n", | ||
" verbose = 2\n", | ||
" )\n", | ||
" return history.history['acc'][-1]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"WARNING: Logging before flag parsing goes to stderr.\n", | ||
"W1202 23:25:19.177921 140608713762624 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", | ||
"W1202 23:25:19.543609 140608713762624 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n", | ||
"Instructions for updating:\n", | ||
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 80 images belonging to 2 classes.\n", | ||
"Epoch 1/15\n", | ||
"8/8 - 6s - loss: 2.5840 - acc: 0.6453\n", | ||
"Epoch 2/15\n", | ||
"8/8 - 2s - loss: 0.5867 - acc: 0.7781\n", | ||
"Epoch 3/15\n", | ||
"8/8 - 2s - loss: 0.4023 - acc: 0.8141\n", | ||
"Epoch 4/15\n", | ||
"8/8 - 2s - loss: 0.1744 - acc: 0.9625\n", | ||
"Epoch 5/15\n", | ||
"8/8 - 2s - loss: 0.1311 - acc: 0.9688\n", | ||
"Epoch 6/15\n", | ||
"8/8 - 2s - loss: 0.1083 - acc: 0.9672\n", | ||
"Epoch 7/15\n", | ||
"8/8 - 2s - loss: 0.1950 - acc: 0.9281\n", | ||
"Epoch 8/15\n", | ||
"8/8 - 2s - loss: 0.0395 - acc: 0.9953\n", | ||
"Epoch 9/15\n", | ||
"8/8 - 2s - loss: 0.0208 - acc: 0.9937\n", | ||
"Epoch 10/15\n", | ||
"\n", | ||
"Reached ~100% accuracy so cancelling training!\n", | ||
"8/8 - 2s - loss: 0.0090 - acc: 1.0000\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1.0" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# The Expected output: \"Reached 99.9% accuracy so cancelling training!\"\"\n", | ||
"train_happy_sad_model()" | ||
] | ||
}, | ||
{ | ||
"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": "1kAlw", | ||
"launcher_item_id": "PNLYD" | ||
}, | ||
"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.
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