Skip to content

Commit 0cc1179

Browse files
committed
RELU, and working with models save/restore features
1 parent b8c6fd9 commit 0cc1179

15 files changed

+846
-0
lines changed

009 - RELU.ipynb

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"name": "stdout",
12+
"output_type": "stream",
13+
"text": [
14+
"[[ 5.11000013 8.44000053]\n",
15+
" [ 0. 0. ]\n",
16+
" [ 24.01000214 38.23999786]]\n"
17+
]
18+
}
19+
],
20+
"source": [
21+
"import tensorflow as tf\n",
22+
"\n",
23+
"output = None\n",
24+
"hidden_layer_weights = [\n",
25+
" [0.1, 0.2, 0.4],\n",
26+
" [0.4, 0.6, 0.6],\n",
27+
" [0.5, 0.9, 0.1],\n",
28+
" [0.8, 0.2, 0.8]]\n",
29+
"out_weights = [\n",
30+
" [0.1, 0.6],\n",
31+
" [0.2, 0.1],\n",
32+
" [0.7, 0.9]]\n",
33+
"\n",
34+
"# Weights and biases\n",
35+
"weights = [\n",
36+
" tf.Variable(hidden_layer_weights),\n",
37+
" tf.Variable(out_weights)]\n",
38+
"biases = [\n",
39+
" tf.Variable(tf.zeros(3)),\n",
40+
" tf.Variable(tf.zeros(2))]\n",
41+
"\n",
42+
"# Input\n",
43+
"features = tf.Variable([[1.0, 2.0, 3.0, 4.0], [-1.0, -2.0, -3.0, -4.0], [11.0, 12.0, 13.0, 14.0]])\n",
44+
"\n",
45+
"hidden_layer = tf.add( tf.matmul(features, weights[0]), biases[0])\n",
46+
"app_relu = tf.nn.relu(hidden_layer)\n",
47+
"output = tf.add( tf.matmul(app_relu, weights[1]), biases[1])\n",
48+
"\n",
49+
"init = tf.global_variables_initializer()\n",
50+
"\n",
51+
"with tf.Session() as sess:\n",
52+
" sess.run(init)\n",
53+
" result = sess.run(output)\n",
54+
" \n",
55+
"print(result)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {
62+
"collapsed": true
63+
},
64+
"outputs": [],
65+
"source": []
66+
}
67+
],
68+
"metadata": {
69+
"kernelspec": {
70+
"display_name": "Python 3",
71+
"language": "python",
72+
"name": "python3"
73+
},
74+
"language_info": {
75+
"codemirror_mode": {
76+
"name": "ipython",
77+
"version": 3
78+
},
79+
"file_extension": ".py",
80+
"mimetype": "text/x-python",
81+
"name": "python",
82+
"nbconvert_exporter": "python",
83+
"pygments_lexer": "ipython3",
84+
"version": "3.6.0"
85+
}
86+
},
87+
"nbformat": 4,
88+
"nbformat_minor": 2
89+
}

010 - Deep Neural Networks.ipynb

+272
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# TensorFlow MNIST"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 20,
13+
"metadata": {
14+
"collapsed": false
15+
},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"Extracting ./datasets/ud730/mnist/train-images-idx3-ubyte.gz\n",
22+
"Extracting ./datasets/ud730/mnist/train-labels-idx1-ubyte.gz\n",
23+
"Extracting ./datasets/ud730/mnist/t10k-images-idx3-ubyte.gz\n",
24+
"Extracting ./datasets/ud730/mnist/t10k-labels-idx1-ubyte.gz\n"
25+
]
26+
}
27+
],
28+
"source": [
29+
"from tensorflow.examples.tutorials.mnist import input_data\n",
30+
"\n",
31+
"mnist = input_data.read_data_sets('./datasets/ud730/mnist', one_hot=True, reshape=False)"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"# Learning Parameters"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 21,
44+
"metadata": {
45+
"collapsed": true
46+
},
47+
"outputs": [],
48+
"source": [
49+
"import tensorflow as tf\n",
50+
"\n",
51+
"# Parameters\n",
52+
"learning_rate = 0.001\n",
53+
"training_epochs = 20\n",
54+
"batch_size = 128 # Decrease batch size if you don't have enough memory\n",
55+
"display_step = 1\n",
56+
"\n",
57+
"n_input = 784 # MNIST data input (img shape: 28*28)\n",
58+
"n_classes = 10 # MNIST total classes (0-9 digits)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"# Hidden Layer Parameters"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 22,
71+
"metadata": {
72+
"collapsed": true
73+
},
74+
"outputs": [],
75+
"source": [
76+
"n_hidden_layer = 256 # layer number of features"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"# Weights and Biases"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 23,
89+
"metadata": {
90+
"collapsed": true
91+
},
92+
"outputs": [],
93+
"source": [
94+
"weights = {\n",
95+
" 'hidden_layer': tf.Variable(tf.random_normal([n_input, n_hidden_layer])),\n",
96+
" 'out': tf.Variable(tf.random_normal([n_hidden_layer, n_classes]))\n",
97+
"}\n",
98+
"biases = {\n",
99+
" 'hidden_layer': tf.Variable(tf.random_normal([n_hidden_layer])),\n",
100+
" 'out': tf.Variable(tf.random_normal([n_classes]))\n",
101+
"}"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"# Input"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 24,
114+
"metadata": {
115+
"collapsed": true
116+
},
117+
"outputs": [],
118+
"source": [
119+
"x = tf.placeholder(\"float\", [None, 28, 28, 1])\n",
120+
"y = tf.placeholder(\"float\", [None, n_classes])\n",
121+
"\n",
122+
"x_flat = tf.reshape(x, [-1, n_input])"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"# Multilayer Perceptron"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": 25,
135+
"metadata": {
136+
"collapsed": true
137+
},
138+
"outputs": [],
139+
"source": [
140+
"# Hidden layer with RELU activation\n",
141+
"layer_1 = tf.add(tf.matmul(x_flat, weights['hidden_layer']),biases['hidden_layer'])\n",
142+
"layer_1 = tf.nn.relu(layer_1)\n",
143+
"# Output layer with linear activation\n",
144+
"logits = tf.add(tf.matmul(layer_1, weights['out']), biases['out'])"
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {},
150+
"source": [
151+
"# Optimizer"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 26,
157+
"metadata": {
158+
"collapsed": true
159+
},
160+
"outputs": [],
161+
"source": [
162+
"# Define loss and optimizer\n",
163+
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))\n",
164+
"optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)"
165+
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"metadata": {},
170+
"source": [
171+
"# Session"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": 27,
177+
"metadata": {
178+
"collapsed": false
179+
},
180+
"outputs": [
181+
{
182+
"name": "stdout",
183+
"output_type": "stream",
184+
"text": [
185+
"Epoch: 0001 cost= 37.288757324\n",
186+
"Epoch: 0002 cost= 28.099151611\n",
187+
"Epoch: 0003 cost= 21.492095947\n",
188+
"Epoch: 0004 cost= 15.777803421\n",
189+
"Epoch: 0005 cost= 13.104373932\n",
190+
"Epoch: 0006 cost= 16.443382263\n",
191+
"Epoch: 0007 cost= 12.179491997\n",
192+
"Epoch: 0008 cost= 11.330167770\n",
193+
"Epoch: 0009 cost= 8.374776840\n",
194+
"Epoch: 0010 cost= 10.616512299\n",
195+
"Epoch: 0011 cost= 8.541707993\n",
196+
"Epoch: 0012 cost= 5.481113434\n",
197+
"Epoch: 0013 cost= 5.806009293\n",
198+
"Epoch: 0014 cost= 8.566146851\n",
199+
"Epoch: 0015 cost= 10.841135025\n",
200+
"Epoch: 0016 cost= 8.520166397\n",
201+
"Epoch: 0017 cost= 6.733136177\n",
202+
"Epoch: 0018 cost= 4.299148560\n",
203+
"Epoch: 0019 cost= 5.420190334\n",
204+
"Epoch: 0020 cost= 8.197362900\n",
205+
"Optimization Finished!\n",
206+
"Accuracy: 0.839844\n"
207+
]
208+
}
209+
],
210+
"source": [
211+
"# Initializing the variables\n",
212+
"init = tf.global_variables_initializer()\n",
213+
"\n",
214+
"\n",
215+
"with tf.Session() as sess:\n",
216+
" sess.run(init)\n",
217+
" # Training cycle\n",
218+
" for epoch in range(training_epochs):\n",
219+
" total_batch = int(mnist.train.num_examples/batch_size)\n",
220+
" # Loop over all batches\n",
221+
" for i in range(total_batch):\n",
222+
" batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
223+
" # Run optimization op (backprop) and cost op (to get loss value)\n",
224+
" sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})\n",
225+
" # Display logs per epoch step\n",
226+
" if epoch % display_step == 0:\n",
227+
" c = sess.run(cost, feed_dict={x: batch_x, y: batch_y})\n",
228+
" print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n",
229+
" \"{:.9f}\".format(c))\n",
230+
" print(\"Optimization Finished!\")\n",
231+
"\n",
232+
" # Test model\n",
233+
" correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))\n",
234+
" # Calculate accuracy\n",
235+
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
236+
" # Decrease test_size if you don't have enough memory\n",
237+
" test_size = 256\n",
238+
" print(\"Accuracy:\", accuracy.eval({x: mnist.test.images[:test_size], y: mnist.test.labels[:test_size]}))"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"metadata": {
245+
"collapsed": true
246+
},
247+
"outputs": [],
248+
"source": []
249+
}
250+
],
251+
"metadata": {
252+
"kernelspec": {
253+
"display_name": "Python 3",
254+
"language": "python",
255+
"name": "python3"
256+
},
257+
"language_info": {
258+
"codemirror_mode": {
259+
"name": "ipython",
260+
"version": 3
261+
},
262+
"file_extension": ".py",
263+
"mimetype": "text/x-python",
264+
"name": "python",
265+
"nbconvert_exporter": "python",
266+
"pygments_lexer": "ipython3",
267+
"version": "3.6.0"
268+
}
269+
},
270+
"nbformat": 4,
271+
"nbformat_minor": 2
272+
}

0 commit comments

Comments
 (0)