-
-
Save kiransair/9557b4265bc55e2ef07290acc79dee62 to your computer and use it in GitHub Desktop.
SO_74413904.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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNvGUKCmzqIAQS+pC59f4VM", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/kiransair/9557b4265bc55e2ef07290acc79dee62/so_74413904.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": "w53bBLpoWZxn" | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"from tensorflow import keras\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"mnist=keras.datasets.mnist.load_data()" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "BVpvDwAhWaru", | |
"outputId": "962fce15-e1a3-4f7b-a48e-be93054186c3" | |
}, | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | |
"11490434/11490434 [==============================] - 0s 0us/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"(x_train,y_train),(x_test,y_test)=mnist" | |
], | |
"metadata": { | |
"id": "57wUoOfsWdPV" | |
}, | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x_train=x_train.astype('float32')/255\n", | |
"x_test=x_test.astype('float32')/255" | |
], | |
"metadata": { | |
"id": "yTEfUEY1WgxO" | |
}, | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"y_train=keras.utils.to_categorical(y_train,10)\n", | |
"y_test=keras.utils.to_categorical(y_test,10)" | |
], | |
"metadata": { | |
"id": "XYdNWi9eWi4p" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"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", | |
"])" | |
], | |
"metadata": { | |
"id": "MX7WjFaPW4w7" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.compile(loss=\"categorical_crossentropy\", optimizer='adam',metrics=['accuracy'])" | |
], | |
"metadata": { | |
"id": "5HF4L1ZBW9YL" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.fit(x_train,y_train,epochs=5)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "kUyEGNxPXGos", | |
"outputId": "8ff73714-576c-4de7-f4b1-686f66b4a1b0" | |
}, | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/5\n", | |
"1875/1875 [==============================] - 74s 39ms/step - loss: 0.2078 - accuracy: 0.9358\n", | |
"Epoch 2/5\n", | |
"1875/1875 [==============================] - 58s 31ms/step - loss: 0.0785 - accuracy: 0.9761\n", | |
"Epoch 3/5\n", | |
"1875/1875 [==============================] - 61s 32ms/step - loss: 0.0621 - accuracy: 0.9808\n", | |
"Epoch 4/5\n", | |
"1875/1875 [==============================] - 59s 32ms/step - loss: 0.0530 - accuracy: 0.9830\n", | |
"Epoch 5/5\n", | |
"1875/1875 [==============================] - 58s 31ms/step - loss: 0.0456 - accuracy: 0.9857\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f80cbffc3d0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 8 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"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", | |
"])" | |
], | |
"metadata": { | |
"id": "xiBbh-rKZecD" | |
}, | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model1.compile(loss=\"categorical_crossentropy\", optimizer='adam',metrics=['accuracy'])" | |
], | |
"metadata": { | |
"id": "qN8RN0b9bTLG" | |
}, | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model1.fit(x_test,y_test,epochs=5)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Nhnpot1EY660", | |
"outputId": "50f8236d-9239-4f82-ae6e-89cedff790c4" | |
}, | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/5\n", | |
"313/313 [==============================] - 11s 32ms/step - loss: 0.5796 - accuracy: 0.8143\n", | |
"Epoch 2/5\n", | |
"313/313 [==============================] - 10s 31ms/step - loss: 0.1744 - accuracy: 0.9467\n", | |
"Epoch 3/5\n", | |
"313/313 [==============================] - 10s 31ms/step - loss: 0.1216 - accuracy: 0.9613\n", | |
"Epoch 4/5\n", | |
"313/313 [==============================] - 10s 31ms/step - loss: 0.0966 - accuracy: 0.9691\n", | |
"Epoch 5/5\n", | |
"313/313 [==============================] - 10s 31ms/step - loss: 0.0808 - accuracy: 0.9735\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7f80c8826ac0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"X=model1.get_weights()[0]" | |
], | |
"metadata": { | |
"id": "AlTsKY0KZ6Ju" | |
}, | |
"execution_count": 17, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Y=model.get_weights()[0]" | |
], | |
"metadata": { | |
"id": "wkrXeIpldcHL" | |
}, | |
"execution_count": 18, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x = tf.constant([[X], [Y]]) " | |
], | |
"metadata": { | |
"id": "wJSV8H7lpXkW" | |
}, | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"tf.math.reduce_euclidean_norm(x)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "zLzVJ5MRdoj6", | |
"outputId": "89f2a072-23c2-42e6-8f26-4fb11228c4e5" | |
}, | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<tf.Tensor: shape=(), dtype=float32, numpy=4.558021>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 20 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "Yjc69jYTspq9" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment