{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "kLutYXp-ecSf", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "dd3f2061-b234-4c54-9a85-91ac3fadf6e5" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", "11490434/11490434 [==============================] - 1s 0us/step\n" ] } ], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from tensorflow.keras.datasets import mnist\n", "from tensorflow import keras\n", "import keras.backend as K\n", "from tensorflow.keras.layers import Dense, Flatten, Reshape, Input, Lambda, BatchNormalization, Dropout\n", "\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "x_train = x_train / 255\n", "x_test = x_test/ 255\n", "\n", "y_train = y_train % 2\n", "y_train = keras.utils.to_categorical(y_train, 10)" ] }, { "cell_type": "code", "source": [ "input_img = Input((28, 28))\n", "x = Flatten()(input_img)\n", "x = Dense(128, activation = 'relu')(x)\n", "x = Dense(256, activation = 'relu')(x)\n", "x = Dense(64, activation = 'relu')(x)\n", "classif = Dense(10, activation = 'softmax')(x)" ], "metadata": { "id": "Ffd2RsvUedfQ" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "model = keras.Model(input_img, classif)" ], "metadata": { "id": "5aVLXHYNe5R_" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])" ], "metadata": { "id": "tG0HHttBVuxs" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "model.fit(x_train, y_train, epochs = 10, batch_size = 30, shuffle = True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "L6tEkyZdWIZy", "outputId": "ab46112c-85ee-4d43-eeb3-4657296ef823" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/10\n", "2000/2000 [==============================] - 12s 5ms/step - loss: 0.1117 - accuracy: 0.9597\n", "Epoch 2/10\n", "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0523 - accuracy: 0.9825\n", "Epoch 3/10\n", "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0389 - accuracy: 0.9862\n", "Epoch 4/10\n", "2000/2000 [==============================] - 9s 5ms/step - loss: 0.0304 - accuracy: 0.9895\n", "Epoch 5/10\n", "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0250 - accuracy: 0.9915\n", "Epoch 6/10\n", "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0203 - accuracy: 0.9929\n", "Epoch 7/10\n", "2000/2000 [==============================] - 9s 4ms/step - loss: 0.0162 - accuracy: 0.9945\n", "Epoch 8/10\n", "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0148 - accuracy: 0.9947\n", "Epoch 9/10\n", "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0117 - accuracy: 0.9961\n", "Epoch 10/10\n", "2000/2000 [==============================] - 9s 4ms/step - loss: 0.0114 - accuracy: 0.9960\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "tf.keras.utils.plot_model(model, show_shapes= True, show_layer_names= True, show_layer_activations= True)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 518 }, "id": "WGei66Vbdtzk", "outputId": "1d66ceeb-7a58-489a-ec83-6a46a3b507fa" }, "execution_count": 7, "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeras\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_shapes\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_layer_names\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_layer_activations\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'tf' is not defined" ] } ] }, { "cell_type": "code", "source": [ "model.save('drive/MyDrive/my_model')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YkhzAnVeePCm", "outputId": "88492cf4-5d9d-4a4e-ca91-690740e40961" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "WARNING:absl:Found untraced functions such as _update_step_xla while saving (showing 1 of 1). These functions will not be directly callable after loading.\n" ] } ] }, { "cell_type": "code", "source": [ "model.summary()" ], "metadata": { "id": "H4_sMVCpvNUG" }, "execution_count": null, "outputs": [] } ] }