-
-
Save kiransair/dd07ba560b5907c23349b869ea20fb52 to your computer and use it in GitHub Desktop.
TF_Forum_10130.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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/kiransair/dd07ba560b5907c23349b869ea20fb52/tf_forum_10130.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"id": "Da9J0GgbIy85" | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"from tensorflow import keras\n", | |
"import numpy as np\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "kvwi47x3I891", | |
"outputId": "970abb7d-265f-42c0-e059-89aeca9348e6" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | |
"11493376/11490434 [==============================] - 0s 0us/step\n", | |
"11501568/11490434 [==============================] - 0s 0us/step\n" | |
] | |
} | |
], | |
"source": [ | |
"mnist=keras.datasets.mnist.load_data()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"id": "2u_jnfKII-xM" | |
}, | |
"outputs": [], | |
"source": [ | |
"(x_train,y_train),(x_test,y_test)=mnist" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"id": "bbMrsrdAJAqr" | |
}, | |
"outputs": [], | |
"source": [ | |
"x_train=x_train.astype('float32')/255\n", | |
"x_test=x_test.astype('float32')/255" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"id": "FZLaP3LfJCsC" | |
}, | |
"outputs": [], | |
"source": [ | |
"y_train=keras.utils.to_categorical(y_train,10)\n", | |
"y_test=keras.utils.to_categorical(y_test,10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"id": "mIys4cNkJE2i" | |
}, | |
"outputs": [], | |
"source": [ | |
"model=keras.Sequential([\n", | |
" keras.Input(shape=(28,28,1)),\n", | |
" keras.layers.Conv2D(32,kernel_size=(3,3),activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n", | |
" keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n", | |
" keras.layers.Flatten(),\n", | |
" keras.layers.Dropout(0.5),\n", | |
" keras.layers.Dense(10,activation='softmax')\n", | |
"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"id": "fO3ihOooJGr7" | |
}, | |
"outputs": [], | |
"source": [ | |
"model.compile(loss=\"categorical_crossentropy\", optimizer='adam',metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"id": "GeETDC_jInTy" | |
}, | |
"outputs": [], | |
"source": [ | |
"checkpoint_path = \"training_1/cp.ckpt\"\n", | |
"checkpoint_dir = os.path.dirname(checkpoint_path)\n", | |
"\n", | |
"# Create a callback that saves the model's weights\n", | |
"cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n", | |
" save_weights_only=True,\n", | |
" verbose=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "EdQrdnD9LZus", | |
"outputId": "db6b2c08-221e-4ed6-a520-0569e76d1093" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/3\n", | |
"1874/1875 [============================>.] - ETA: 0s - loss: 0.2130 - accuracy: 0.9354\n", | |
"Epoch 1: saving model to training_1/cp.ckpt\n", | |
"1875/1875 [==============================] - 64s 33ms/step - loss: 0.2130 - accuracy: 0.9354\n", | |
"Epoch 2/3\n", | |
"1875/1875 [==============================] - ETA: 0s - loss: 0.0802 - accuracy: 0.9754\n", | |
"Epoch 2: saving model to training_1/cp.ckpt\n", | |
"1875/1875 [==============================] - 60s 32ms/step - loss: 0.0802 - accuracy: 0.9754\n", | |
"Epoch 3/3\n", | |
"1874/1875 [============================>.] - ETA: 0s - loss: 0.0615 - accuracy: 0.9807\n", | |
"Epoch 3: saving model to training_1/cp.ckpt\n", | |
"1875/1875 [==============================] - 60s 32ms/step - loss: 0.0615 - accuracy: 0.9807\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7ff417df0dd0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
], | |
"source": [ | |
"model.fit(x_train,y_train,epochs=3,callbacks=[cp_callback])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"id": "X23_FrwiJMx0" | |
}, | |
"outputs": [], | |
"source": [ | |
"model1=keras.Sequential([\n", | |
" keras.Input(shape=(28,28,1)),\n", | |
" keras.layers.Conv2D(32,kernel_size=(3,3),activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n", | |
" keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),\n", | |
" keras.layers.MaxPooling2D(pool_size=(2,2)),\n", | |
" keras.layers.Flatten(),\n", | |
" keras.layers.Dropout(0.5),\n", | |
" keras.layers.Dense(10,activation='softmax')\n", | |
"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"id": "g_VjwvccJQfe", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"outputId": "3ce85db3-24e8-4bb2-fbdf-0e2bf6499073" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x7ff416356310>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
], | |
"source": [ | |
"model1.load_weights(checkpoint_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"id": "ro-AlXgyJSdN" | |
}, | |
"outputs": [], | |
"source": [ | |
"model1.compile(loss=\"categorical_crossentropy\", optimizer='adam',metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "M73f0xxdJU0N", | |
"outputId": "5e2e7b26-62e8-4225-c4af-f22b3f5afb7a" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/3\n", | |
"1875/1875 [==============================] - 60s 32ms/step - loss: 0.0547 - accuracy: 0.9829\n", | |
"Epoch 2/3\n", | |
"1875/1875 [==============================] - 61s 33ms/step - loss: 0.0464 - accuracy: 0.9855\n", | |
"Epoch 3/3\n", | |
"1875/1875 [==============================] - 58s 31ms/step - loss: 0.0435 - accuracy: 0.9867\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7ff4145f0e90>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 13 | |
} | |
], | |
"source": [ | |
"model1.fit(x_train,y_train,epochs=3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"id": "98LpOPeDJW30" | |
}, | |
"outputs": [], | |
"source": [ | |
"" | |
] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"name": "TF_Forum_10130.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyO0DwnktPlrjy2/n9tPuvp7", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3", | |
"name": "python3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment