{ "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": null, "metadata": { "id": "kLutYXp-ecSf" }, "outputs": [], "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": null, "outputs": [] }, { "cell_type": "code", "source": [ "model = keras.Model(input_img, classif)" ], "metadata": { "id": "5aVLXHYNe5R_" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "model.compile(optimizer = 'adam', loss = 'categorical_crossentropy')" ], "metadata": { "id": "tG0HHttBVuxs" }, "execution_count": null, "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": "2a98272e-fb00-440a-e4f6-10c92a477318" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/10\n", "2000/2000 [==============================] - 12s 5ms/step - loss: 0.1153\n", "Epoch 2/10\n", "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0524\n", "Epoch 3/10\n", "2000/2000 [==============================] - 9s 5ms/step - loss: 0.0384\n", "Epoch 4/10\n", "2000/2000 [==============================] - 11s 6ms/step - loss: 0.0308\n", "Epoch 5/10\n", "2000/2000 [==============================] - 11s 6ms/step - loss: 0.0250\n", "Epoch 6/10\n", "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0199\n", "Epoch 7/10\n", "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0168\n", "Epoch 8/10\n", "2000/2000 [==============================] - 11s 6ms/step - loss: 0.0142\n", "Epoch 9/10\n", "2000/2000 [==============================] - 11s 6ms/step - loss: 0.0131\n", "Epoch 10/10\n", "2000/2000 [==============================] - 9s 5ms/step - loss: 0.0110\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 16 } ] }, { "cell_type": "code", "source": [ "model.predict(x_train[:1])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WGei66Vbdtzk", "outputId": "673fe3fb-8363-427e-c753-27e6471aaf51" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1/1 [==============================] - 0s 100ms/step\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "array([[1.21183645e-11, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00, 0.00000000e+00]], dtype=float32)" ] }, "metadata": {}, "execution_count": 18 } ] }, { "cell_type": "code", "source": [ "y_train[:1]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YkhzAnVeePCm", "outputId": "c3c041c1-1ef4-441f-abb6-a771632a3617" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)" ] }, "metadata": {}, "execution_count": 19 } ] } ] }