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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": null,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"id": "kLutYXp-ecSf"
|
| 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 = y_train % 2\n",
|
| 38 |
+
"y_train = keras.utils.to_categorical(y_train, 10)"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"source": [
|
| 44 |
+
"input_img = Input((28, 28))\n",
|
| 45 |
+
"x = Flatten()(input_img)\n",
|
| 46 |
+
"x = Dense(128, activation = 'relu')(x)\n",
|
| 47 |
+
"x = Dense(256, activation = 'relu')(x)\n",
|
| 48 |
+
"x = Dense(64, activation = 'relu')(x)\n",
|
| 49 |
+
"classif = Dense(10, activation = 'softmax')(x)"
|
| 50 |
+
],
|
| 51 |
+
"metadata": {
|
| 52 |
+
"id": "Ffd2RsvUedfQ"
|
| 53 |
+
},
|
| 54 |
+
"execution_count": null,
|
| 55 |
+
"outputs": []
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"cell_type": "code",
|
| 59 |
+
"source": [
|
| 60 |
+
"model = keras.Model(input_img, classif)"
|
| 61 |
+
],
|
| 62 |
+
"metadata": {
|
| 63 |
+
"id": "5aVLXHYNe5R_"
|
| 64 |
+
},
|
| 65 |
+
"execution_count": null,
|
| 66 |
+
"outputs": []
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"cell_type": "code",
|
| 70 |
+
"source": [
|
| 71 |
+
"model.compile(optimizer = 'adam', loss = 'categorical_crossentropy')"
|
| 72 |
+
],
|
| 73 |
+
"metadata": {
|
| 74 |
+
"id": "tG0HHttBVuxs"
|
| 75 |
+
},
|
| 76 |
+
"execution_count": null,
|
| 77 |
+
"outputs": []
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"source": [
|
| 82 |
+
"model.fit(x_train, y_train, epochs = 10, batch_size = 30, shuffle = True)"
|
| 83 |
+
],
|
| 84 |
+
"metadata": {
|
| 85 |
+
"colab": {
|
| 86 |
+
"base_uri": "https://localhost:8080/"
|
| 87 |
+
},
|
| 88 |
+
"id": "L6tEkyZdWIZy",
|
| 89 |
+
"outputId": "2a98272e-fb00-440a-e4f6-10c92a477318"
|
| 90 |
+
},
|
| 91 |
+
"execution_count": null,
|
| 92 |
+
"outputs": [
|
| 93 |
+
{
|
| 94 |
+
"output_type": "stream",
|
| 95 |
+
"name": "stdout",
|
| 96 |
+
"text": [
|
| 97 |
+
"Epoch 1/10\n",
|
| 98 |
+
"2000/2000 [==============================] - 12s 5ms/step - loss: 0.1153\n",
|
| 99 |
+
"Epoch 2/10\n",
|
| 100 |
+
"2000/2000 [==============================] - 11s 5ms/step - loss: 0.0524\n",
|
| 101 |
+
"Epoch 3/10\n",
|
| 102 |
+
"2000/2000 [==============================] - 9s 5ms/step - loss: 0.0384\n",
|
| 103 |
+
"Epoch 4/10\n",
|
| 104 |
+
"2000/2000 [==============================] - 11s 6ms/step - loss: 0.0308\n",
|
| 105 |
+
"Epoch 5/10\n",
|
| 106 |
+
"2000/2000 [==============================] - 11s 6ms/step - loss: 0.0250\n",
|
| 107 |
+
"Epoch 6/10\n",
|
| 108 |
+
"2000/2000 [==============================] - 11s 5ms/step - loss: 0.0199\n",
|
| 109 |
+
"Epoch 7/10\n",
|
| 110 |
+
"2000/2000 [==============================] - 10s 5ms/step - loss: 0.0168\n",
|
| 111 |
+
"Epoch 8/10\n",
|
| 112 |
+
"2000/2000 [==============================] - 11s 6ms/step - loss: 0.0142\n",
|
| 113 |
+
"Epoch 9/10\n",
|
| 114 |
+
"2000/2000 [==============================] - 11s 6ms/step - loss: 0.0131\n",
|
| 115 |
+
"Epoch 10/10\n",
|
| 116 |
+
"2000/2000 [==============================] - 9s 5ms/step - loss: 0.0110\n"
|
| 117 |
+
]
|
| 118 |
+
},
|
| 119 |
+
{
|
| 120 |
+
"output_type": "execute_result",
|
| 121 |
+
"data": {
|
| 122 |
+
"text/plain": [
|
| 123 |
+
"<keras.callbacks.History at 0x7f7234824ee0>"
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
"metadata": {},
|
| 127 |
+
"execution_count": 16
|
| 128 |
+
}
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"cell_type": "code",
|
| 133 |
+
"source": [
|
| 134 |
+
"model.predict(x_train[:1])"
|
| 135 |
+
],
|
| 136 |
+
"metadata": {
|
| 137 |
+
"colab": {
|
| 138 |
+
"base_uri": "https://localhost:8080/"
|
| 139 |
+
},
|
| 140 |
+
"id": "WGei66Vbdtzk",
|
| 141 |
+
"outputId": "673fe3fb-8363-427e-c753-27e6471aaf51"
|
| 142 |
+
},
|
| 143 |
+
"execution_count": null,
|
| 144 |
+
"outputs": [
|
| 145 |
+
{
|
| 146 |
+
"output_type": "stream",
|
| 147 |
+
"name": "stdout",
|
| 148 |
+
"text": [
|
| 149 |
+
"1/1 [==============================] - 0s 100ms/step\n"
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"output_type": "execute_result",
|
| 154 |
+
"data": {
|
| 155 |
+
"text/plain": [
|
| 156 |
+
"array([[1.21183645e-11, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n",
|
| 157 |
+
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n",
|
| 158 |
+
" 0.00000000e+00, 0.00000000e+00]], dtype=float32)"
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
"metadata": {},
|
| 162 |
+
"execution_count": 18
|
| 163 |
+
}
|
| 164 |
+
]
|
| 165 |
+
},
|
| 166 |
+
{
|
| 167 |
+
"cell_type": "code",
|
| 168 |
+
"source": [
|
| 169 |
+
"y_train[:1]"
|
| 170 |
+
],
|
| 171 |
+
"metadata": {
|
| 172 |
+
"colab": {
|
| 173 |
+
"base_uri": "https://localhost:8080/"
|
| 174 |
+
},
|
| 175 |
+
"id": "YkhzAnVeePCm",
|
| 176 |
+
"outputId": "c3c041c1-1ef4-441f-abb6-a771632a3617"
|
| 177 |
+
},
|
| 178 |
+
"execution_count": null,
|
| 179 |
+
"outputs": [
|
| 180 |
+
{
|
| 181 |
+
"output_type": "execute_result",
|
| 182 |
+
"data": {
|
| 183 |
+
"text/plain": [
|
| 184 |
+
"array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)"
|
| 185 |
+
]
|
| 186 |
+
},
|
| 187 |
+
"metadata": {},
|
| 188 |
+
"execution_count": 19
|
| 189 |
+
}
|
| 190 |
+
]
|
| 191 |
+
}
|
| 192 |
+
]
|
| 193 |
+
}
|