Skip to content

Commit b41bd8f

Browse files
author
ugik
committed
toy ANN tensorflow example
1 parent 1702571 commit b41bd8f

File tree

6 files changed

+11538
-0
lines changed

6 files changed

+11538
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 33,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"data": {
12+
"text/plain": [
13+
"([[0, 0, 1, 0, 1],\n",
14+
" [0, 0, 0, 0, 0],\n",
15+
" [1, 1, 0, 1, 1],\n",
16+
" [0, 1, 0, 1, 1],\n",
17+
" [1, 1, 0, 1, 0],\n",
18+
" [0, 1, 1, 1, 1],\n",
19+
" [1, 0, 1, 1, 0],\n",
20+
" [0, 0, 0, 0, 1],\n",
21+
" [1, 0, 1, 1, 1],\n",
22+
" [1, 1, 0, 0, 1],\n",
23+
" [0, 0, 0, 1, 1],\n",
24+
" [1, 0, 0, 1, 1],\n",
25+
" [1, 0, 1, 0, 1],\n",
26+
" [0, 0, 1, 1, 1],\n",
27+
" [1, 1, 1, 1, 0],\n",
28+
" [1, 1, 0, 0, 0],\n",
29+
" [1, 0, 0, 0, 0],\n",
30+
" [1, 1, 1, 1, 1]],\n",
31+
" [[0],\n",
32+
" [0],\n",
33+
" [1],\n",
34+
" [0],\n",
35+
" [0],\n",
36+
" [0],\n",
37+
" [0],\n",
38+
" [0],\n",
39+
" [1],\n",
40+
" [1],\n",
41+
" [0],\n",
42+
" [1],\n",
43+
" [1],\n",
44+
" [0],\n",
45+
" [0],\n",
46+
" [0],\n",
47+
" [0],\n",
48+
" [1]])"
49+
]
50+
},
51+
"execution_count": 33,
52+
"metadata": {},
53+
"output_type": "execute_result"
54+
}
55+
],
56+
"source": [
57+
"import numpy as np\n",
58+
"import random\n",
59+
"from collections import Counter\n",
60+
"\n",
61+
"\n",
62+
"def create_feature_sets_and_labels(test_size = 0.2):\n",
63+
"\n",
64+
" # 5 features\n",
65+
" features = []\n",
66+
" features.append([[0, 0, 0, 0, 0], [0]])\n",
67+
" features.append([[0, 0, 0, 0, 1], [0]])\n",
68+
" features.append([[0, 0, 0, 1, 1], [0]])\n",
69+
" features.append([[0, 0, 1, 1, 1], [0]])\n",
70+
" features.append([[0, 1, 1, 1, 1], [0]])\n",
71+
" features.append([[1, 1, 1, 1, 0], [0]])\n",
72+
" features.append([[1, 1, 1, 0, 0], [0]])\n",
73+
" features.append([[1, 1, 0, 0, 0], [0]])\n",
74+
" features.append([[1, 0, 0, 0, 0], [0]])\n",
75+
" features.append([[1, 0, 0, 1, 0], [0]])\n",
76+
" features.append([[1, 0, 1, 1, 0], [0]])\n",
77+
" features.append([[1, 1, 0, 1, 0], [0]])\n",
78+
" features.append([[0, 1, 0, 1, 1], [0]])\n",
79+
" features.append([[0, 0, 1, 0, 1], [0]])\n",
80+
" # output of [1] of positions [0,4]==1\n",
81+
" features.append([[1, 0, 0, 0, 1], [1]])\n",
82+
" features.append([[1, 1, 0, 0, 1], [1]])\n",
83+
" features.append([[1, 1, 1, 0, 1], [1]])\n",
84+
" features.append([[1, 1, 1, 1, 1], [1]])\n",
85+
" features.append([[1, 0, 0, 1, 1], [1]])\n",
86+
" features.append([[1, 0, 1, 1, 1], [1]])\n",
87+
" features.append([[1, 1, 0, 1, 1], [1]])\n",
88+
" features.append([[1, 0, 1, 0, 1], [1]])\n",
89+
"\n",
90+
" random.shuffle(features)\n",
91+
" features = np.array(features)\n",
92+
"\n",
93+
" testing_size = int(test_size*len(features))\n",
94+
"\n",
95+
" train_x = list(features[:,0][:-testing_size])\n",
96+
" train_y = list(features[:,1][:-testing_size])\n",
97+
" test_x = list(features[:,0][-testing_size:])\n",
98+
" test_y = list(features[:,1][-testing_size:])\n",
99+
"\n",
100+
" return train_x,train_y,test_x,test_y\n",
101+
"\n",
102+
"if __name__ == '__main__':\n",
103+
" train_x,train_y,test_x,test_y = create_feature_sets_and_labels()\n",
104+
"\n",
105+
"train_x, train_y"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 57,
111+
"metadata": {
112+
"collapsed": false
113+
},
114+
"outputs": [
115+
{
116+
"name": "stdout",
117+
"output_type": "stream",
118+
"text": [
119+
"Epoch 3 completed out of 10 cost: 0.0\n",
120+
"Epoch 5 completed out of 10 cost: 0.0\n",
121+
"Epoch 7 completed out of 10 cost: 0.0\n",
122+
"Epoch 9 completed out of 10 cost: 0.0\n",
123+
"Accuracy: 1.0\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"import tensorflow as tf\n",
129+
"import numpy as np\n",
130+
"\n",
131+
"train_x,train_y,test_x,test_y = create_feature_sets_and_labels()\n",
132+
"\n",
133+
"n_nodes_hl1 = 20\n",
134+
"n_nodes_hl2 = 20\n",
135+
"\n",
136+
"n_classes = 1\n",
137+
"hm_epochs = 10\n",
138+
"\n",
139+
"x = tf.placeholder('float')\n",
140+
"y = tf.placeholder('float')\n",
141+
"\n",
142+
"hidden_1_layer = {'f_fum':n_nodes_hl1,\n",
143+
" 'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),\n",
144+
" 'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}\n",
145+
"\n",
146+
"hidden_2_layer = {'f_fum':n_nodes_hl2,\n",
147+
" 'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),\n",
148+
" 'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}\n",
149+
"\n",
150+
"output_layer = {'f_fum':None,\n",
151+
" 'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),\n",
152+
" 'bias':tf.Variable(tf.random_normal([n_classes])),}\n",
153+
"\n",
154+
"\n",
155+
"def neural_network_model(data):\n",
156+
"\n",
157+
" l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])\n",
158+
" l1 = tf.sigmoid(l1)\n",
159+
"\n",
160+
" l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])\n",
161+
" l2 = tf.sigmoid(l2)\n",
162+
"\n",
163+
" output = tf.matmul(l2,output_layer['weight']) + output_layer['bias']\n",
164+
"\n",
165+
" return output\n",
166+
"\n",
167+
"def train_neural_network(x):\n",
168+
" prediction = neural_network_model(x)\n",
169+
" cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )\n",
170+
" #optimizer = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999).minimize(cost)\n",
171+
" optimizer = tf.train.GradientDescentOptimizer(1).minimize(cost)\n",
172+
"\n",
173+
" with tf.Session() as sess:\n",
174+
" sess.run(tf.global_variables_initializer())\n",
175+
" \n",
176+
" for epoch in range(hm_epochs):\n",
177+
" epoch_loss = 0\n",
178+
" i=0\n",
179+
" while i < len(train_x):\n",
180+
" start = i\n",
181+
" end = i+batch_size\n",
182+
" batch_x = np.array(train_x[start:end])\n",
183+
" batch_y = np.array(train_y[start:end])\n",
184+
"\n",
185+
" _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})\n",
186+
" epoch_loss += c\n",
187+
" i+=batch_size\n",
188+
" last_cost = c\n",
189+
"\n",
190+
" if (epoch% 2) == 0 and epoch > 1:\n",
191+
" print('Epoch', epoch+1, 'completed out of',hm_epochs,'cost:', last_cost)\n",
192+
"\n",
193+
" correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))\n",
194+
" accuracy = tf.reduce_mean(tf.cast(correct, 'float'))\n",
195+
"\n",
196+
" print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))\n",
197+
"\n",
198+
"train_neural_network(x)"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"metadata": {
205+
"collapsed": false
206+
},
207+
"outputs": [],
208+
"source": []
209+
}
210+
],
211+
"metadata": {
212+
"kernelspec": {
213+
"display_name": "Python 3",
214+
"language": "python",
215+
"name": "python3"
216+
},
217+
"language_info": {
218+
"codemirror_mode": {
219+
"name": "ipython",
220+
"version": 3
221+
},
222+
"file_extension": ".py",
223+
"mimetype": "text/x-python",
224+
"name": "python",
225+
"nbconvert_exporter": "python",
226+
"pygments_lexer": "ipython3",
227+
"version": "3.5.2"
228+
}
229+
},
230+
"nbformat": 4,
231+
"nbformat_minor": 1
232+
}

0 commit comments

Comments
 (0)