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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 26,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"id": "2F6ZW8s2TK12"
|
| 22 |
+
},
|
| 23 |
+
"outputs": [],
|
| 24 |
+
"source": [
|
| 25 |
+
"import numpy as np\n",
|
| 26 |
+
"import matplotlib.pyplot as plt\n",
|
| 27 |
+
"from tensorflow.keras.datasets import mnist\n",
|
| 28 |
+
"from tensorflow import keras\n",
|
| 29 |
+
"import keras.backend as K\n",
|
| 30 |
+
"from tensorflow.keras.layers import Dense, Flatten, Reshape, Input, Lambda, BatchNormalization, Dropout\n",
|
| 31 |
+
" \n",
|
| 32 |
+
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
|
| 33 |
+
" \n",
|
| 34 |
+
"x_train = x_train / 255\n",
|
| 35 |
+
"x_test = x_test / 255\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"y_train = keras.utils.to_categorical(y_train, 10)"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"source": [
|
| 43 |
+
"input_img = Input((28, 28))\n",
|
| 44 |
+
"x = Flatten()(input_img)\n",
|
| 45 |
+
"x = Dense(256, activation='relu')(x)\n",
|
| 46 |
+
"x = Dense(128, activation='relu')(x)\n",
|
| 47 |
+
"x = Dense(64, activation='relu')(x)\n",
|
| 48 |
+
"Classif = Dense(10, activation='softmax')(x)"
|
| 49 |
+
],
|
| 50 |
+
"metadata": {
|
| 51 |
+
"id": "llfdgGwoTcO1"
|
| 52 |
+
},
|
| 53 |
+
"execution_count": 27,
|
| 54 |
+
"outputs": []
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"source": [
|
| 59 |
+
"model = keras.Model(input_img, Classif)"
|
| 60 |
+
],
|
| 61 |
+
"metadata": {
|
| 62 |
+
"id": "2yrM66AMUa1O"
|
| 63 |
+
},
|
| 64 |
+
"execution_count": 28,
|
| 65 |
+
"outputs": []
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "code",
|
| 69 |
+
"source": [
|
| 70 |
+
"model.compile(optimizer='adam', loss='categorical_crossentropy')"
|
| 71 |
+
],
|
| 72 |
+
"metadata": {
|
| 73 |
+
"id": "FWuTEvwxVEKU"
|
| 74 |
+
},
|
| 75 |
+
"execution_count": 29,
|
| 76 |
+
"outputs": []
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "code",
|
| 80 |
+
"source": [
|
| 81 |
+
"model.fit(x_train, y_train, epochs=5, batch_size=30, shuffle=True)"
|
| 82 |
+
],
|
| 83 |
+
"metadata": {
|
| 84 |
+
"colab": {
|
| 85 |
+
"base_uri": "https://localhost:8080/"
|
| 86 |
+
},
|
| 87 |
+
"id": "j_PDLrF8VENz",
|
| 88 |
+
"outputId": "bf561cc5-36e0-47b6-a5d1-a1d7e1aa7e07"
|
| 89 |
+
},
|
| 90 |
+
"execution_count": 30,
|
| 91 |
+
"outputs": [
|
| 92 |
+
{
|
| 93 |
+
"output_type": "stream",
|
| 94 |
+
"name": "stdout",
|
| 95 |
+
"text": [
|
| 96 |
+
"Epoch 1/5\n",
|
| 97 |
+
"2000/2000 [==============================] - 16s 7ms/step - loss: 0.2133\n",
|
| 98 |
+
"Epoch 2/5\n",
|
| 99 |
+
"2000/2000 [==============================] - 12s 6ms/step - loss: 0.0902\n",
|
| 100 |
+
"Epoch 3/5\n",
|
| 101 |
+
"2000/2000 [==============================] - 12s 6ms/step - loss: 0.0650\n",
|
| 102 |
+
"Epoch 4/5\n",
|
| 103 |
+
"2000/2000 [==============================] - 12s 6ms/step - loss: 0.0508\n",
|
| 104 |
+
"Epoch 5/5\n",
|
| 105 |
+
"2000/2000 [==============================] - 13s 6ms/step - loss: 0.0389\n"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"output_type": "execute_result",
|
| 110 |
+
"data": {
|
| 111 |
+
"text/plain": [
|
| 112 |
+
"<keras.callbacks.History at 0x7fef21e73a00>"
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
"metadata": {},
|
| 116 |
+
"execution_count": 30
|
| 117 |
+
}
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "code",
|
| 122 |
+
"source": [
|
| 123 |
+
"model.predict(x_train[:1])"
|
| 124 |
+
],
|
| 125 |
+
"metadata": {
|
| 126 |
+
"colab": {
|
| 127 |
+
"base_uri": "https://localhost:8080/"
|
| 128 |
+
},
|
| 129 |
+
"id": "BQIQVSATWJAa",
|
| 130 |
+
"outputId": "81e468a9-528c-4467-84a7-e50b8167ee36"
|
| 131 |
+
},
|
| 132 |
+
"execution_count": 32,
|
| 133 |
+
"outputs": [
|
| 134 |
+
{
|
| 135 |
+
"output_type": "stream",
|
| 136 |
+
"name": "stdout",
|
| 137 |
+
"text": [
|
| 138 |
+
"1/1 [==============================] - 0s 98ms/step\n"
|
| 139 |
+
]
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"output_type": "execute_result",
|
| 143 |
+
"data": {
|
| 144 |
+
"text/plain": [
|
| 145 |
+
"array([[2.7693006e-11, 1.0839771e-06, 2.3587077e-08, 8.1381639e-03,\n",
|
| 146 |
+
" 2.0295085e-12, 9.9185455e-01, 9.6902228e-09, 1.0457582e-08,\n",
|
| 147 |
+
" 1.3851497e-06, 4.7160506e-06]], dtype=float32)"
|
| 148 |
+
]
|
| 149 |
+
},
|
| 150 |
+
"metadata": {},
|
| 151 |
+
"execution_count": 32
|
| 152 |
+
}
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "code",
|
| 157 |
+
"source": [
|
| 158 |
+
"y_train[:1]"
|
| 159 |
+
],
|
| 160 |
+
"metadata": {
|
| 161 |
+
"colab": {
|
| 162 |
+
"base_uri": "https://localhost:8080/"
|
| 163 |
+
},
|
| 164 |
+
"id": "f5Cvv8UYeUeH",
|
| 165 |
+
"outputId": "7b387d52-9ef1-4864-b3b3-f5317e9efbf8"
|
| 166 |
+
},
|
| 167 |
+
"execution_count": 33,
|
| 168 |
+
"outputs": [
|
| 169 |
+
{
|
| 170 |
+
"output_type": "execute_result",
|
| 171 |
+
"data": {
|
| 172 |
+
"text/plain": [
|
| 173 |
+
"array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]], dtype=float32)"
|
| 174 |
+
]
|
| 175 |
+
},
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"execution_count": 33
|
| 178 |
+
}
|
| 179 |
+
]
|
| 180 |
+
}
|
| 181 |
+
]
|
| 182 |
+
}
|