-
-
Save kiransair/d630702b1363f5053487034fe76da73a to your computer and use it in GitHub Desktop.
TF_Forum_7381.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": "ABX9TyO3Oe77J6T9XOGJHviR8171", | |
"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/d630702b1363f5053487034fe76da73a/tf_forum_7381.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": "oNXut5uYwtFw" | |
}, | |
"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": { | |
"id": "jVrOH5Nowyhx" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"(x_train,y_train),(x_test,y_test)=mnist" | |
], | |
"metadata": { | |
"id": "TtdvZDYqw0Ke" | |
}, | |
"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": "99EIKooAw1_H" | |
}, | |
"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": "LcBwx_Oqw4AY" | |
}, | |
"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": "6jSvd5QUw6DK" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.compile(loss=\"categorical_crossentropy\", optimizer='adam',metrics=['accuracy'])" | |
], | |
"metadata": { | |
"id": "pTGkDkr4w8Dy" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from keras.callbacks import Callback\n", | |
"\n", | |
"class Callback(Callback):\n", | |
" def __init__(self, x_train):\n", | |
" self.x_train = x_train\n", | |
" def on_train_begin(self, logs=None):\n", | |
" shapeofxtrain = self.x_train.shape\n", | |
" print(shapeofxtrain)\n" | |
], | |
"metadata": { | |
"id": "AYFcRAhlw-rY" | |
}, | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.fit(x_train,y_train,validation_data=(x_test, y_test),epochs=1,callbacks=[Callback(x_train)])" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ixLnTQa6xFUZ", | |
"outputId": "5370c1df-a2d9-4bff-e7de-a24a21bb1bbd" | |
}, | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"(60000, 28, 28)\n", | |
"1875/1875 [==============================] - 71s 37ms/step - loss: 0.2074 - accuracy: 0.9364 - val_loss: 0.0586 - val_accuracy: 0.9823\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7fa37c6c5610>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "rZqe1wivxKq-" | |
}, | |
"execution_count": 9, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment