{ "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": 26, "metadata": { "id": "2F6ZW8s2TK12" }, "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 = keras.utils.to_categorical(y_train, 10)" ] }, { "cell_type": "code", "source": [ "input_img = Input((28, 28))\n", "x = Flatten()(input_img)\n", "x = Dense(256, activation='relu')(x)\n", "x = Dense(128, activation='relu')(x)\n", "x = Dense(64, activation='relu')(x)\n", "Classif = Dense(10, activation='softmax')(x)" ], "metadata": { "id": "llfdgGwoTcO1" }, "execution_count": 27, "outputs": [] }, { "cell_type": "code", "source": [ "model = keras.Model(input_img, Classif)" ], "metadata": { "id": "2yrM66AMUa1O" }, "execution_count": 28, "outputs": [] }, { "cell_type": "code", "source": [ "model.compile(optimizer='adam', loss='categorical_crossentropy')" ], "metadata": { "id": "FWuTEvwxVEKU" }, "execution_count": 29, "outputs": [] }, { "cell_type": "code", "source": [ "model.fit(x_train, y_train, epochs=5, batch_size=30, shuffle=True)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j_PDLrF8VENz", "outputId": "bf561cc5-36e0-47b6-a5d1-a1d7e1aa7e07" }, "execution_count": 30, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Epoch 1/5\n", "2000/2000 [==============================] - 16s 7ms/step - loss: 0.2133\n", "Epoch 2/5\n", "2000/2000 [==============================] - 12s 6ms/step - loss: 0.0902\n", "Epoch 3/5\n", "2000/2000 [==============================] - 12s 6ms/step - loss: 0.0650\n", "Epoch 4/5\n", "2000/2000 [==============================] - 12s 6ms/step - loss: 0.0508\n", "Epoch 5/5\n", "2000/2000 [==============================] - 13s 6ms/step - loss: 0.0389\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "" ] }, "metadata": {}, "execution_count": 30 } ] }, { "cell_type": "code", "source": [ "model.predict(x_train[:1])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BQIQVSATWJAa", "outputId": "81e468a9-528c-4467-84a7-e50b8167ee36" }, "execution_count": 32, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1/1 [==============================] - 0s 98ms/step\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "array([[2.7693006e-11, 1.0839771e-06, 2.3587077e-08, 8.1381639e-03,\n", " 2.0295085e-12, 9.9185455e-01, 9.6902228e-09, 1.0457582e-08,\n", " 1.3851497e-06, 4.7160506e-06]], dtype=float32)" ] }, "metadata": {}, "execution_count": 32 } ] }, { "cell_type": "code", "source": [ "y_train[:1]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "f5Cvv8UYeUeH", "outputId": "7b387d52-9ef1-4864-b3b3-f5317e9efbf8" }, "execution_count": 33, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]], dtype=float32)" ] }, "metadata": {}, "execution_count": 33 } ] } ] }