Alim2003 commited on
Commit
684cdbc
·
1 Parent(s): b13b20c

Upload model_code.txt

Browse files
Files changed (1) hide show
  1. model_code.txt +207 -0
model_code.txt ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": 1,
20
+ "metadata": {
21
+ "id": "kLutYXp-ecSf",
22
+ "colab": {
23
+ "base_uri": "https://localhost:8080/"
24
+ },
25
+ "outputId": "dd3f2061-b234-4c54-9a85-91ac3fadf6e5"
26
+ },
27
+ "outputs": [
28
+ {
29
+ "output_type": "stream",
30
+ "name": "stdout",
31
+ "text": [
32
+ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
33
+ "11490434/11490434 [==============================] - 1s 0us/step\n"
34
+ ]
35
+ }
36
+ ],
37
+ "source": [
38
+ "import numpy as np\n",
39
+ "import matplotlib.pyplot as plt\n",
40
+ "from tensorflow.keras.datasets import mnist\n",
41
+ "from tensorflow import keras\n",
42
+ "import keras.backend as K\n",
43
+ "from tensorflow.keras.layers import Dense, Flatten, Reshape, Input, Lambda, BatchNormalization, Dropout\n",
44
+ "\n",
45
+ "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
46
+ "\n",
47
+ "x_train = x_train / 255\n",
48
+ "x_test = x_test/ 255\n",
49
+ "\n",
50
+ "y_train = y_train % 2\n",
51
+ "y_train = keras.utils.to_categorical(y_train, 10)"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "source": [
57
+ "input_img = Input((28, 28))\n",
58
+ "x = Flatten()(input_img)\n",
59
+ "x = Dense(128, activation = 'relu')(x)\n",
60
+ "x = Dense(256, activation = 'relu')(x)\n",
61
+ "x = Dense(64, activation = 'relu')(x)\n",
62
+ "classif = Dense(10, activation = 'softmax')(x)"
63
+ ],
64
+ "metadata": {
65
+ "id": "Ffd2RsvUedfQ"
66
+ },
67
+ "execution_count": 2,
68
+ "outputs": []
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "source": [
73
+ "model = keras.Model(input_img, classif)"
74
+ ],
75
+ "metadata": {
76
+ "id": "5aVLXHYNe5R_"
77
+ },
78
+ "execution_count": 3,
79
+ "outputs": []
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "source": [
84
+ "model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])"
85
+ ],
86
+ "metadata": {
87
+ "id": "tG0HHttBVuxs"
88
+ },
89
+ "execution_count": 4,
90
+ "outputs": []
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "source": [
95
+ "model.fit(x_train, y_train, epochs = 10, batch_size = 30, shuffle = True)"
96
+ ],
97
+ "metadata": {
98
+ "colab": {
99
+ "base_uri": "https://localhost:8080/"
100
+ },
101
+ "id": "L6tEkyZdWIZy",
102
+ "outputId": "ab46112c-85ee-4d43-eeb3-4657296ef823"
103
+ },
104
+ "execution_count": 5,
105
+ "outputs": [
106
+ {
107
+ "output_type": "stream",
108
+ "name": "stdout",
109
+ "text": [
110
+ "Epoch 1/10\n",
111
+ "2000/2000 [==============================] - 12s 5ms/step - loss: 0.1117 - accuracy: 0.9597\n",
112
+ "Epoch 2/10\n",
113
+ "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0523 - accuracy: 0.9825\n",
114
+ "Epoch 3/10\n",
115
+ "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0389 - accuracy: 0.9862\n",
116
+ "Epoch 4/10\n",
117
+ "2000/2000 [==============================] - 9s 5ms/step - loss: 0.0304 - accuracy: 0.9895\n",
118
+ "Epoch 5/10\n",
119
+ "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0250 - accuracy: 0.9915\n",
120
+ "Epoch 6/10\n",
121
+ "2000/2000 [==============================] - 10s 5ms/step - loss: 0.0203 - accuracy: 0.9929\n",
122
+ "Epoch 7/10\n",
123
+ "2000/2000 [==============================] - 9s 4ms/step - loss: 0.0162 - accuracy: 0.9945\n",
124
+ "Epoch 8/10\n",
125
+ "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0148 - accuracy: 0.9947\n",
126
+ "Epoch 9/10\n",
127
+ "2000/2000 [==============================] - 11s 5ms/step - loss: 0.0117 - accuracy: 0.9961\n",
128
+ "Epoch 10/10\n",
129
+ "2000/2000 [==============================] - 9s 4ms/step - loss: 0.0114 - accuracy: 0.9960\n"
130
+ ]
131
+ },
132
+ {
133
+ "output_type": "execute_result",
134
+ "data": {
135
+ "text/plain": [
136
+ "<keras.callbacks.History at 0x7fb0108d3a90>"
137
+ ]
138
+ },
139
+ "metadata": {},
140
+ "execution_count": 5
141
+ }
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "source": [
147
+ "tf.keras.utils.plot_model(model, show_shapes= True, show_layer_names= True, show_layer_activations= True)\n"
148
+ ],
149
+ "metadata": {
150
+ "colab": {
151
+ "base_uri": "https://localhost:8080/",
152
+ "height": 518
153
+ },
154
+ "id": "WGei66Vbdtzk",
155
+ "outputId": "1d66ceeb-7a58-489a-ec83-6a46a3b507fa"
156
+ },
157
+ "execution_count": 7,
158
+ "outputs": [
159
+ {
160
+ "output_type": "error",
161
+ "ename": "NameError",
162
+ "evalue": "ignored",
163
+ "traceback": [
164
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
165
+ "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
166
+ "\u001b[0;32m<ipython-input-7-668ba8cae1eb>\u001b[0m in \u001b[0;36m<cell line: 1>\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",
167
+ "\u001b[0;31mNameError\u001b[0m: name 'tf' is not defined"
168
+ ]
169
+ }
170
+ ]
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "source": [
175
+ "model.save('drive/MyDrive/my_model')"
176
+ ],
177
+ "metadata": {
178
+ "colab": {
179
+ "base_uri": "https://localhost:8080/"
180
+ },
181
+ "id": "YkhzAnVeePCm",
182
+ "outputId": "88492cf4-5d9d-4a4e-ca91-690740e40961"
183
+ },
184
+ "execution_count": 8,
185
+ "outputs": [
186
+ {
187
+ "output_type": "stream",
188
+ "name": "stderr",
189
+ "text": [
190
+ "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"
191
+ ]
192
+ }
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "code",
197
+ "source": [
198
+ "model.summary()"
199
+ ],
200
+ "metadata": {
201
+ "id": "H4_sMVCpvNUG"
202
+ },
203
+ "execution_count": null,
204
+ "outputs": []
205
+ }
206
+ ]
207
+ }