Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
palash04 committed Sep 21, 2020
1 parent 0996bff commit 751eab6
Showing 1 changed file with 266 additions and 0 deletions.
266 changes: 266 additions & 0 deletions Tensorflow_Series/_05_Model_Subclasing.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "_05_Model_Subclasing.ipynb",
"provenance": [],
"mount_file_id": "1-k_qtZZcggknjSVXp9p7CXO8wEn-sbky",
"authorship_tag": "ABX9TyNC1qEIuBtkJBTNtm3CNxk1",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/palash04/Artificial-Intelligence/blob/master/Tensorflow_Series/_05_Model_Subclasing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "UxOBaIw9X1FQ",
"colab_type": "code",
"colab": {}
},
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers\n",
"from tensorflow.keras.datasets import mnist"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fCQOP7sTYcZX",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "1ddb4bb8-be48-4dad-bbe0-05c5d5745fe2"
},
"source": [
"(x_train, y_train), (x_test, y_test) = mnist.load_data()"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 0s 0us/step\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "bZlapcutYkpd",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "fb2f4212-1f32-48b8-e568-b7673751a990"
},
"source": [
"x_train.shape"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(60000, 28, 28)"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oL17bPyWYqhI",
"colab_type": "code",
"colab": {}
},
"source": [
"x_train = x_train.reshape(-1, 28, 28, 1).astype(\"float32\") / 255.0\n",
"x_test = x_test.reshape(-1, 28, 28, 1).astype(\"float32\") / 255.0"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "YLUO1utUY4km",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "c2b16757-35b2-4b30-d0ba-ab8cfa66e082"
},
"source": [
"x_train.shape"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(60000, 28, 28, 1)"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "aRTfpyl6Y5Y_",
"colab_type": "code",
"colab": {}
},
"source": [
"class CNNBLOCK(layers.Layer):\n",
" def __init__(self, out_channels, kernel_size=3):\n",
" super(CNNBLOCK, self).__init__()\n",
" self.conv = layers.Conv2D(out_channels, kernel_size, padding='same')\n",
" self.bn = layers.BatchNormalization()\n",
" \n",
" def call(self, input_tensor, training=False):\n",
" x = self.conv(input_tensor)\n",
" x = self.bn(x, training=training)\n",
" x = tf.nn.relu(x)\n",
" return x"
],
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "xmG4bMWOZLGP",
"colab_type": "code",
"colab": {}
},
"source": [
"model = keras.Sequential(\n",
" [\n",
" CNNBLOCK(32),\n",
" CNNBLOCK(64),\n",
" CNNBLOCK(128),\n",
" layers.Flatten(),\n",
" layers.Dense(10),\n",
" ]\n",
")"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-FlPxSGiaMDQ",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 391
},
"outputId": "0863d794-c03f-4030-8d37-3cbba17a7eec"
},
"source": [
"model.compile(\n",
" optimizer = keras.optimizers.Adam(lr=0.001),\n",
" loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
" metrics=[\"accuracy\"]\n",
")\n",
"model.fit(x_train, y_train, batch_size=64, epochs=10,verbose=1)\n",
"model.evaluate(x_test,y_test,batch_size=64,verbose=1)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.5911 - accuracy: 0.9479\n",
"Epoch 2/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0937 - accuracy: 0.9813\n",
"Epoch 3/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0367 - accuracy: 0.9892\n",
"Epoch 4/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0266 - accuracy: 0.9912\n",
"Epoch 5/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0253 - accuracy: 0.9922\n",
"Epoch 6/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0222 - accuracy: 0.9924\n",
"Epoch 7/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0197 - accuracy: 0.9935\n",
"Epoch 8/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0160 - accuracy: 0.9946\n",
"Epoch 9/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0134 - accuracy: 0.9956\n",
"Epoch 10/10\n",
"938/938 [==============================] - 11s 11ms/step - loss: 0.0108 - accuracy: 0.9962\n",
"157/157 [==============================] - 1s 5ms/step - loss: 0.0472 - accuracy: 0.9874\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0.04721549525856972, 0.9873999953269958]"
]
},
"metadata": {
"tags": []
},
"execution_count": 11
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Rpbe8CvXaRQh",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}

0 comments on commit 751eab6

Please sign in to comment.