Skip to content

Commit d88dac1

Browse files
author
ugik
committed
Tensorflow 2-layer perceptron toy example
1 parent b5ebfdf commit d88dac1

File tree

5 files changed

+421
-102
lines changed

5 files changed

+421
-102
lines changed

.ipynb_checkpoints/Tensorflow ANN-checkpoint.ipynb

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 63,
5+
"execution_count": 187,
66
"metadata": {
77
"collapsed": false
88
},
9-
"outputs": [
10-
{
11-
"name": "stdout",
12-
"output_type": "stream",
13-
"text": [
14-
"[0, 1, 0, 1, 1] [0]\n"
15-
]
16-
}
17-
],
9+
"outputs": [],
1810
"source": [
1911
"import numpy as np\n",
2012
"import random\n",
@@ -25,29 +17,30 @@
2517
"\n",
2618
" # 5 features\n",
2719
" features = []\n",
28-
" features.append([[0, 0, 0, 0, 0], [0]])\n",
29-
" features.append([[0, 0, 0, 0, 1], [0]])\n",
30-
" features.append([[0, 0, 0, 1, 1], [0]])\n",
31-
" features.append([[0, 0, 1, 1, 1], [0]])\n",
32-
" features.append([[0, 1, 1, 1, 1], [0]])\n",
33-
" features.append([[1, 1, 1, 1, 0], [0]])\n",
34-
" features.append([[1, 1, 1, 0, 0], [0]])\n",
35-
" features.append([[1, 1, 0, 0, 0], [0]])\n",
36-
" features.append([[1, 0, 0, 0, 0], [0]])\n",
37-
" features.append([[1, 0, 0, 1, 0], [0]])\n",
38-
" features.append([[1, 0, 1, 1, 0], [0]])\n",
39-
" features.append([[1, 1, 0, 1, 0], [0]])\n",
40-
" features.append([[0, 1, 0, 1, 1], [0]])\n",
41-
" features.append([[0, 0, 1, 0, 1], [0]])\n",
20+
" features.append([[0, 0, 0, 0, 0], [0,1]])\n",
21+
" features.append([[0, 0, 0, 0, 1], [0,1]])\n",
22+
" features.append([[0, 0, 0, 1, 1], [0,1]])\n",
23+
" features.append([[0, 0, 1, 1, 1], [0,1]])\n",
24+
" features.append([[0, 1, 1, 1, 1], [0,1]])\n",
25+
" features.append([[1, 1, 1, 1, 0], [0,1]])\n",
26+
" features.append([[1, 1, 1, 0, 0], [0,1]])\n",
27+
" features.append([[1, 1, 0, 0, 0], [0,1]])\n",
28+
"# features.append([[1, 0, 0, 0, 0], [0,1]])\n",
29+
"# features.append([[1, 0, 0, 1, 0], [0,1]])\n",
30+
"# features.append([[1, 0, 1, 1, 0], [0,1]])\n",
31+
"# features.append([[1, 1, 0, 1, 0], [0,1]])\n",
32+
"# features.append([[0, 1, 0, 1, 1], [0,1]])\n",
33+
"# features.append([[0, 0, 1, 0, 1], [0,1]])\n",
34+
"\n",
4235
" # output of [1] of positions [0,4]==1\n",
43-
" features.append([[1, 0, 0, 0, 1], [1]])\n",
44-
" features.append([[1, 1, 0, 0, 1], [1]])\n",
45-
" features.append([[1, 1, 1, 0, 1], [1]])\n",
46-
" features.append([[1, 1, 1, 1, 1], [1]])\n",
47-
" features.append([[1, 0, 0, 1, 1], [1]])\n",
48-
" features.append([[1, 0, 1, 1, 1], [1]])\n",
49-
" features.append([[1, 1, 0, 1, 1], [1]])\n",
50-
" features.append([[1, 0, 1, 0, 1], [1]])\n",
36+
" features.append([[1, 0, 0, 0, 1], [1,0]])\n",
37+
" features.append([[1, 1, 0, 0, 1], [1,0]])\n",
38+
" features.append([[1, 1, 1, 0, 1], [1,0]])\n",
39+
" features.append([[1, 1, 1, 1, 1], [1,0]])\n",
40+
" features.append([[1, 0, 0, 1, 1], [1,0]])\n",
41+
"# features.append([[1, 0, 1, 1, 1], [1,0]])\n",
42+
"# features.append([[1, 1, 0, 1, 1], [1,0]])\n",
43+
"# features.append([[1, 0, 1, 0, 1], [1,0]])\n",
5144
"\n",
5245
" random.shuffle(features)\n",
5346
" features = np.array(features)\n",
@@ -62,30 +55,16 @@
6255
" return train_x,train_y,test_x,test_y\n",
6356
"\n",
6457
"if __name__ == '__main__':\n",
65-
" train_x,train_y,test_x,test_y = create_feature_sets_and_labels()\n",
66-
"\n",
67-
"print(train_x[0], train_y[0]"
58+
" train_x,train_y,test_x,test_y = create_feature_sets_and_labels()\n"
6859
]
6960
},
7061
{
7162
"cell_type": "code",
72-
"execution_count": 79,
63+
"execution_count": null,
7364
"metadata": {
7465
"collapsed": false
7566
},
76-
"outputs": [
77-
{
78-
"name": "stdout",
79-
"output_type": "stream",
80-
"text": [
81-
"Epoch 3 completed out of 10 cost: 0.0\n",
82-
"Epoch 5 completed out of 10 cost: 0.0\n",
83-
"Epoch 7 completed out of 10 cost: 0.0\n",
84-
"Epoch 9 completed out of 10 cost: 0.0\n",
85-
"Accuracy: 1.0\n"
86-
]
87-
}
88-
],
67+
"outputs": [],
8968
"source": [
9069
"import tensorflow as tf\n",
9170
"import numpy as np\n",
@@ -95,8 +74,9 @@
9574
"n_nodes_hl1 = 20\n",
9675
"n_nodes_hl2 = 20\n",
9776
"\n",
98-
"n_classes = 1\n",
99-
"hm_epochs = 10\n",
77+
"n_classes = 2\n",
78+
"hm_epochs = 50\n",
79+
"batch_size = 4\n",
10080
"\n",
10181
"x = tf.placeholder('float')\n",
10282
"y = tf.placeholder('float')\n",
@@ -150,15 +130,18 @@
150130
" i+=batch_size\n",
151131
" last_cost = c\n",
152132
"\n",
153-
" if (epoch% 2) == 0 and epoch > 1:\n",
154-
" print('Epoch', epoch+1, 'completed out of',hm_epochs,'cost:', last_cost)\n",
133+
" if (epoch% 10) == 0 and epoch > 1:\n",
134+
" print('Epoch', epoch, 'completed out of',hm_epochs,'cost:', last_cost)\n",
155135
"\n",
156136
" correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))\n",
157137
" accuracy = tf.reduce_mean(tf.cast(correct, 'float'))\n",
158-
"\n",
159138
" print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))\n",
160139
"\n",
161-
"train_neural_network(x)"
140+
" output = prediction.eval(feed_dict = {x: [[1, 0, 0, 0, 0]]})\n",
141+
" print(output)\n",
142+
" print(tf.sigmoid(output[0][0]).eval(), tf.sigmoid(output[0][1]).eval())\n",
143+
" \n",
144+
"train_neural_network(x)\n"
162145
]
163146
},
164147
{
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"name": "stdout",
12+
"output_type": "stream",
13+
"text": [
14+
"Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
15+
"Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
16+
"Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
17+
"Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n",
18+
"WARNING:tensorflow:From <ipython-input-1-a908ec74eb20>:64 in <module>.: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
19+
"Instructions for updating:\n",
20+
"Use `tf.global_variables_initializer` instead.\n",
21+
"Epoch: 0001 cost= 162.871711167\n",
22+
"Epoch: 0002 cost= 39.616753785\n",
23+
"Epoch: 0003 cost= 24.902709368\n",
24+
"Epoch: 0004 cost= 17.502213440\n",
25+
"Epoch: 0005 cost= 12.720457985\n",
26+
"Epoch: 0006 cost= 9.343551102\n",
27+
"Epoch: 0007 cost= 7.050432922\n",
28+
"Epoch: 0008 cost= 5.099601904\n",
29+
"Epoch: 0009 cost= 3.807331494\n",
30+
"Epoch: 0010 cost= 2.852063462\n"
31+
]
32+
}
33+
],
34+
"source": [
35+
"#A Multilayer Perceptron implementation example using TensorFlow library.\n",
36+
"#This example is using the MNIST database of handwritten digits\n",
37+
"#Author: Mateus Alvarenga\n",
38+
"\n",
39+
"\n",
40+
"\n",
41+
"from __future__ import print_function\n",
42+
"\n",
43+
"# Import MNIST data\n",
44+
"from tensorflow.examples.tutorials.mnist import input_data\n",
45+
"mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\n",
46+
"\n",
47+
"import tensorflow as tf\n",
48+
"\n",
49+
"# Parameters\n",
50+
"learning_rate = 0.001\n",
51+
"training_epochs = 15\n",
52+
"batch_size = 100\n",
53+
"display_step = 1\n",
54+
"\n",
55+
"# Network Parameters\n",
56+
"n_hidden_1 = 256 # 1st layer number of features\n",
57+
"n_hidden_2 = 256 # 2nd layer number of features\n",
58+
"n_input = 784 # MNIST data input (img shape: 28*28)\n",
59+
"n_classes = 10 # MNIST total classes (0-9 digits)\n",
60+
"\n",
61+
"# tf Graph input\n",
62+
"x = tf.placeholder(\"float\", [None, n_input])\n",
63+
"y = tf.placeholder(\"float\", [None, n_classes])\n",
64+
"\n",
65+
"\n",
66+
"# Create model\n",
67+
"def multilayer_perceptron(x, weights, biases):\n",
68+
" # Hidden layer with RELU activation\n",
69+
" layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])\n",
70+
" layer_1 = tf.nn.relu(layer_1)\n",
71+
" # Hidden layer with RELU activation\n",
72+
" layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])\n",
73+
" layer_2 = tf.nn.relu(layer_2)\n",
74+
" # Output layer with linear activation\n",
75+
" out_layer = tf.matmul(layer_2, weights['out']) + biases['out']\n",
76+
" return out_layer\n",
77+
"\n",
78+
"# Store layers weight & bias\n",
79+
"weights = {\n",
80+
" 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n",
81+
" 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n",
82+
" 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))\n",
83+
"}\n",
84+
"biases = {\n",
85+
" 'b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
86+
" 'b2': tf.Variable(tf.random_normal([n_hidden_2])),\n",
87+
" 'out': tf.Variable(tf.random_normal([n_classes]))\n",
88+
"}\n",
89+
"\n",
90+
"# Construct model\n",
91+
"pred = multilayer_perceptron(x, weights, biases)\n",
92+
"\n",
93+
"# Define loss and optimizer\n",
94+
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))\n",
95+
"optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
96+
"\n",
97+
"# Initializing the variables\n",
98+
"#init = tf.initialize_all_variables()\n",
99+
"init = tf.global_variables_initializer()\n",
100+
"\n",
101+
"# Launch the graph\n",
102+
"with tf.Session() as sess:\n",
103+
" sess.run(init)\n",
104+
"\n",
105+
" # Training cycle\n",
106+
" for epoch in range(training_epochs):\n",
107+
" avg_cost = 0.\n",
108+
" total_batch = int(mnist.train.num_examples/batch_size)\n",
109+
" # Loop over all batches\n",
110+
" for i in range(total_batch):\n",
111+
" batch_x, batch_y = mnist.train.next_batch(batch_size)\n",
112+
" # Run optimization op (backprop) and cost op (to get loss value)\n",
113+
" _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,\n",
114+
" y: batch_y})\n",
115+
" # Compute average loss\n",
116+
" avg_cost += c / total_batch\n",
117+
" # Display logs per epoch step\n",
118+
" if epoch % display_step == 0:\n",
119+
" print(\"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n",
120+
" \"{:.9f}\".format(avg_cost))\n",
121+
" print(\"Optimization Finished!\")\n",
122+
"\n",
123+
" # Test model\n",
124+
" correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
125+
" # Calculate accuracy\n",
126+
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
127+
" print(\"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))\n"
128+
]
129+
}
130+
],
131+
"metadata": {
132+
"kernelspec": {
133+
"display_name": "Python 3",
134+
"language": "python",
135+
"name": "python3"
136+
},
137+
"language_info": {
138+
"codemirror_mode": {
139+
"name": "ipython",
140+
"version": 3
141+
},
142+
"file_extension": ".py",
143+
"mimetype": "text/x-python",
144+
"name": "python",
145+
"nbconvert_exporter": "python",
146+
"pygments_lexer": "ipython3",
147+
"version": "3.5.2"
148+
}
149+
},
150+
"nbformat": 4,
151+
"nbformat_minor": 1
152+
}

0 commit comments

Comments
 (0)