Skip to content

Commit c66d218

Browse files
committed
neural network for high school
1 parent 323ceae commit c66d218

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

WI21/nn.ipynb

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 50,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from keras.models import Sequential\n",
10+
"from keras.layers import Dense, Flatten\n",
11+
"from keras.layers import BatchNormalization as BatchNorm\n",
12+
"from keras.utils import np_utils\n",
13+
"from keras.callbacks import ModelCheckpoint\n",
14+
"import random\n",
15+
"import tensorflow as tf"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 48,
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"(60000, 28, 28)\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(path=\"mnist.npz\")\n",
33+
"print(x_train.shape)"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 49,
39+
"metadata": {},
40+
"outputs": [
41+
{
42+
"name": "stdout",
43+
"output_type": "stream",
44+
"text": [
45+
"(42000, 28, 28) (18000, 28, 28)\n"
46+
]
47+
}
48+
],
49+
"source": [
50+
"# train, validation split (70%, 30%)\n",
51+
"x_train, x_val = x_train[0:x_train.shape[0]*70//100], x_train[x_train.shape[0]*70//100:]\n",
52+
"y_train, y_val = y_train[0:y_train.shape[0]*70//100], y_train[y_train.shape[0]*70//100:]\n",
53+
"# Some other simple things you can do - Shuffle, Normalize. \n",
54+
"# Other advanced things you can do to preprocess the data - PCA, Z-score.\n",
55+
"# What other optimizations can you think of? \n",
56+
"print(x_train.shape, x_val.shape)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 58,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"# https://keras.io/api/layers/\n",
66+
"# Keras offers an API for a lot of layers with multiple optional parameters to tune the network.\n",
67+
"def create_network(network_input):\n",
68+
" model = Sequential()\n",
69+
" model.add(Flatten()) # Convert [28,28] -> [784,]\n",
70+
" model.add(Dense(25)) # [784,] -> [25,]\n",
71+
" model.add(Activation('relu'))\n",
72+
" model.add(Dense(10)) # [25,] -> [10,] FCC\n",
73+
" model.add(Activation('softmax'))\n",
74+
" \n",
75+
" #optimizer and loss.\n",
76+
" model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))\n",
77+
" return model\n"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 60,
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"Epoch 1/5\n",
90+
"5250/5250 [==============================] - 3s 538us/step - loss: 3.6693\n",
91+
"Epoch 2/5\n",
92+
"5250/5250 [==============================] - 3s 534us/step - loss: 0.9733\n",
93+
"Epoch 3/5\n",
94+
"5250/5250 [==============================] - 3s 543us/step - loss: 0.7850\n",
95+
"Epoch 4/5\n",
96+
"5250/5250 [==============================] - 3s 537us/step - loss: 0.6917\n",
97+
"Epoch 5/5\n",
98+
"5250/5250 [==============================] - 3s 544us/step - loss: 0.6751\n"
99+
]
100+
},
101+
{
102+
"data": {
103+
"text/plain": [
104+
"<tensorflow.python.keras.callbacks.History at 0x7fcd40099fd0>"
105+
]
106+
},
107+
"execution_count": 60,
108+
"metadata": {},
109+
"output_type": "execute_result"
110+
}
111+
],
112+
"source": [
113+
"model = create_network(x_train)\n",
114+
"#https://keras.io/api/models/model_training_apis/\n",
115+
"#without validation\n",
116+
"model.fit(x=x_train, y=y_train, epochs=5, batch_size = 8)"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 61,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"Epoch 1/5\n",
129+
"5250/5250 [==============================] - 4s 682us/step - loss: 4.4919 - val_loss: 1.3123\n",
130+
"Epoch 2/5\n",
131+
"5250/5250 [==============================] - 4s 669us/step - loss: 1.2269 - val_loss: 0.9553\n",
132+
"Epoch 3/5\n",
133+
"5250/5250 [==============================] - 4s 676us/step - loss: 0.9047 - val_loss: 0.7206\n",
134+
"Epoch 4/5\n",
135+
"5250/5250 [==============================] - 3s 666us/step - loss: 0.7218 - val_loss: 0.7201\n",
136+
"Epoch 5/5\n",
137+
"5250/5250 [==============================] - 4s 673us/step - loss: 0.6267 - val_loss: 0.6180\n"
138+
]
139+
},
140+
{
141+
"data": {
142+
"text/plain": [
143+
"<tensorflow.python.keras.callbacks.History at 0x7fcd307d9070>"
144+
]
145+
},
146+
"execution_count": 61,
147+
"metadata": {},
148+
"output_type": "execute_result"
149+
}
150+
],
151+
"source": [
152+
"model = create_network(x_train)\n",
153+
"#with validation\n",
154+
"model.fit(x=x_train, y=y_train, epochs=5, batch_size = 8, validation_data=(x_val, y_val))"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": []
163+
}
164+
],
165+
"metadata": {
166+
"kernelspec": {
167+
"display_name": "Python 3",
168+
"language": "python",
169+
"name": "python3"
170+
},
171+
"language_info": {
172+
"codemirror_mode": {
173+
"name": "ipython",
174+
"version": 3
175+
},
176+
"file_extension": ".py",
177+
"mimetype": "text/x-python",
178+
"name": "python",
179+
"nbconvert_exporter": "python",
180+
"pygments_lexer": "ipython3",
181+
"version": "3.8.5"
182+
}
183+
},
184+
"nbformat": 4,
185+
"nbformat_minor": 4
186+
}

0 commit comments

Comments
 (0)