-
-
Save kiransair/f560548016e5227633647f967051398d to your computer and use it in GitHub Desktop.
TF_Forum_53230.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": "ABX9TyPrsik1ULc5MzXHkLI4cUlt", | |
"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/f560548016e5227633647f967051398d/tf_forum_53230.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": "TgVqDnw70c3b" | |
}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf\n", | |
"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": "fHIM9Rzu0jXB", | |
"outputId": "7d95e3ec-a9fb-4f5d-f699-6d2a91ae00e9" | |
}, | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", | |
"\u001b[1m11490434/11490434\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"(x_train,y_train),(x_test,y_test)=mnist" | |
], | |
"metadata": { | |
"id": "7wpn8-6T0lGm" | |
}, | |
"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": "cHsvwC5R0m4x" | |
}, | |
"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": "YIkk9k3H0opj" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from keras import activations" | |
], | |
"metadata": { | |
"id": "ZgFK1Z9T0qeb" | |
}, | |
"execution_count": 6, | |
"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.GlobalAveragePooling2D(),\n", | |
" keras.layers.Dropout(0.5),\n", | |
" keras.layers.Dense(10),\n", | |
" keras.layers.Activation(activations.softmax)\n", | |
"])" | |
], | |
"metadata": { | |
"id": "TevJaA9B0s0T" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"optimizer=tf.keras.optimizers.Adam()" | |
], | |
"metadata": { | |
"id": "kP5yH8lE0uY_" | |
}, | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.compile(loss=\"categorical_crossentropy\", optimizer=optimizer,metrics=['accuracy'])" | |
], | |
"metadata": { | |
"id": "UYw1wJex0wOK" | |
}, | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.fit(x_train,y_train,epochs=2)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "JlUoFbHG0yBw", | |
"outputId": "5b311bbe-d080-49cd-d7d5-6b45e7ea43e5" | |
}, | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Epoch 1/2\n", | |
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m56s\u001b[0m 29ms/step - accuracy: 0.3622 - loss: 1.7816\n", | |
"Epoch 2/2\n", | |
"\u001b[1m1875/1875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m85s\u001b[0m 30ms/step - accuracy: 0.7221 - loss: 0.8613\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.src.callbacks.history.History at 0x7cbdc341e8c0>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"logits = model.predict(x_test)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "TJ29jZon00Lm", | |
"outputId": "d4efd224-3522-40aa-d7dc-920d973386cd" | |
}, | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 15ms/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"logits[0]" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "RuTQV-Bb1Qt_", | |
"outputId": "6fd9d7e9-4208-4909-b059-d4849115f8d8" | |
}, | |
"execution_count": 12, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([2.7764330e-04, 9.4532346e-08, 2.8229714e-03, 1.0156622e-05,\n", | |
" 1.4043928e-05, 5.8893958e-04, 5.5398455e-06, 9.7335368e-01,\n", | |
" 4.8517436e-06, 2.2922000e-02], dtype=float32)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 12 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"sub_model = keras.Sequential(model.layers[:7])" | |
], | |
"metadata": { | |
"id": "z0AQof361RO1" | |
}, | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x_test_ = tf.expand_dims(x_test, axis=-1)" | |
], | |
"metadata": { | |
"id": "XBAfjlLR1XYS" | |
}, | |
"execution_count": 14, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"logits = sub_model.predict(x_test_)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Pn0s9k-E1bn4", | |
"outputId": "4cb63591-fe15-4273-e888-482b59b0a001" | |
}, | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m3s\u001b[0m 11ms/step\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"logits[0]" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "mW2aGRCe1iji", | |
"outputId": "7e902e00-52b1-43cb-c1dd-545d14b6bab4" | |
}, | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([ -2.173252 , -10.1584015 , 0.14595598, -5.481463 ,\n", | |
" -5.157399 , -1.4212654 , -6.087623 , 5.9889135 ,\n", | |
" -6.220251 , 2.2402632 ], dtype=float32)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 16 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"tf.nn.softmax(logits[0])" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ZNqTK98i1l5A", | |
"outputId": "a8b305e5-933f-4285-d560-3232d2344028" | |
}, | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<tf.Tensor: shape=(10,), dtype=float32, numpy=\n", | |
"array([2.7764330e-04, 9.4532354e-08, 2.8229717e-03, 1.0156622e-05,\n", | |
" 1.4043929e-05, 5.8893964e-04, 5.5398459e-06, 9.7335374e-01,\n", | |
" 4.8517441e-06, 2.2922002e-02], dtype=float32)>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 17 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "WEeFGO-41pJY" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment