Skip to content

Commit b5ebfdf

Browse files
author
ugik
committed
improved simple 2-layer
1 parent b41bd8f commit b5ebfdf

File tree

6 files changed

+267
-117
lines changed

6 files changed

+267
-117
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 9,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"name": "stdout",
12+
"output_type": "stream",
13+
"text": [
14+
"Random starting synaptic weights: \n",
15+
"[[-0.16595599]\n",
16+
" [ 0.44064899]\n",
17+
" [-0.99977125]]\n",
18+
"error after 0 iterations: 0.578374046722\n",
19+
"error after 1000 iterations: 0.0353771814512\n",
20+
"error after 2000 iterations: 0.024323319584\n",
21+
"error after 3000 iterations: 0.0196075022358\n",
22+
"error after 4000 iterations: 0.016850233908\n",
23+
"error after 5000 iterations: 0.014991814044\n",
24+
"error after 6000 iterations: 0.0136320935305\n",
25+
"error after 7000 iterations: 0.01258242301\n",
26+
"error after 8000 iterations: 0.0117408289409\n",
27+
"error after 9000 iterations: 0.0110467781322\n",
28+
"New synaptic weights after training: \n",
29+
"[[ 12.79547496]\n",
30+
" [ -4.2162058 ]\n",
31+
" [ -4.21608782]]\n",
32+
"Considering new situation [1, 0, 0] -> ?: \n",
33+
"[ 0.99999723]\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"# from https://iamtrask.github.io//2015/07/12/basic-python-network/\n",
39+
"import numpy as np\n",
40+
"\n",
41+
"class NeuralNetwork():\n",
42+
" def __init__(self):\n",
43+
" # Seed the random number generator, so it generates the same numbers\n",
44+
" # every time the program runs.\n",
45+
" np.random.seed(1)\n",
46+
"\n",
47+
" # We model a single neuron, with 3 input connections and 1 output connection.\n",
48+
" # We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1\n",
49+
" # and mean 0.\n",
50+
" self.synaptic_weights = 2 * np.random.random((3, 1)) - 1\n",
51+
"\n",
52+
" # The Sigmoid function, which describes an S shaped curve.\n",
53+
" # We pass the weighted sum of the inputs through this function to\n",
54+
" # normalise them between 0 and 1.\n",
55+
" def __sigmoid(self, x):\n",
56+
" return 1 / (1 + np.exp(-x))\n",
57+
"\n",
58+
" # The derivative of the Sigmoid function.\n",
59+
" # This is the gradient of the Sigmoid curve.\n",
60+
" # It indicates how confident we are about the existing weight.\n",
61+
" def __sigmoid_derivative(self, x):\n",
62+
" return x * (1 - x)\n",
63+
"\n",
64+
" # We train the neural network through a process of trial and error.\n",
65+
" # Adjusting the synaptic weights each time.\n",
66+
" def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):\n",
67+
" for iteration in iter(range(number_of_training_iterations)):\n",
68+
" # Pass the training set through our neural network (a single neuron).\n",
69+
" output = self.think(training_set_inputs)\n",
70+
"\n",
71+
" # Calculate the error (The difference between the desired output\n",
72+
" # and the predicted output).\n",
73+
" error = training_set_outputs - output\n",
74+
"\n",
75+
" # Multiply the error by the input and again by the gradient of the Sigmoid curve.\n",
76+
" # This means less confident weights are adjusted more.\n",
77+
" # This means inputs, which are zero, do not cause changes to the weights.\n",
78+
" adjustment = np.dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))\n",
79+
"\n",
80+
" # Adjust the weights.\n",
81+
" self.synaptic_weights += adjustment\n",
82+
" if (iteration % 1000 == 0):\n",
83+
" print (\"error after %s iterations: %s\" % (iteration, str(numpy.mean(numpy.abs(error)))))\n",
84+
"\n",
85+
" # The neural network thinks.\n",
86+
" def think(self, inputs):\n",
87+
" # Pass inputs through our neural network (our single neuron).\n",
88+
" return self.__sigmoid(np.dot(inputs, self.synaptic_weights))\n",
89+
"\n",
90+
"\n",
91+
"if __name__ == \"__main__\":\n",
92+
"\n",
93+
" #Intialise a single neuron neural network.\n",
94+
" neural_network = NeuralNetwork()\n",
95+
"\n",
96+
" print (\"Random starting synaptic weights: \")\n",
97+
" print (neural_network.synaptic_weights)\n",
98+
"\n",
99+
" # The training set. We have 4 examples, each consisting of 3 input values\n",
100+
" # and 1 output value.\n",
101+
" training_set_inputs = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 0]])\n",
102+
" training_set_outputs = np.array([[0, 1, 1, 0]]).T\n",
103+
"\n",
104+
" # Train the neural network using a training set.\n",
105+
" # Do it 10,000 times and make small adjustments each time.\n",
106+
" neural_network.train(training_set_inputs, training_set_outputs, 10000)\n",
107+
"\n",
108+
" print (\"New synaptic weights after training: \")\n",
109+
" print (neural_network.synaptic_weights)\n",
110+
"\n",
111+
" # Test the neural network with a new pattern\n",
112+
" test = [1, 0, 0]\n",
113+
" print (\"Considering new situation %s -> ?: \" % test )\n",
114+
" print (neural_network.think(np.array(test)))\n"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 14,
120+
"metadata": {
121+
"collapsed": false
122+
},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"input:\n",
129+
"[[0 0 1]\n",
130+
" [1 1 1]\n",
131+
" [1 0 1]\n",
132+
" [0 1 0]]\n",
133+
"truth:\n",
134+
"[[0]\n",
135+
" [1]\n",
136+
" [1]\n",
137+
" [0]]\n",
138+
"dot-product:\n",
139+
"[[-0.99977125]\n",
140+
" [-0.72507825]\n",
141+
" [-1.16572724]\n",
142+
" [ 0.44064899]]\n",
143+
"output:\n",
144+
"[[ 0.2689864 ]\n",
145+
" [ 0.3262757 ]\n",
146+
" [ 0.23762817]\n",
147+
" [ 0.60841366]]\n",
148+
"error:\n",
149+
"[[ 0.2689864 ]\n",
150+
" [-0.6737243 ]\n",
151+
" [-0.76237183]\n",
152+
" [ 0.60841366]]\n",
153+
"derivative:\n",
154+
"[[-0.28621005]\n",
155+
" [-0.00314557]\n",
156+
" [-0.23331852]]\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"# a look inside one iteration\n",
162+
"def sigmoid(x):\n",
163+
" return 1 / (1 + np.exp(-x))\n",
164+
"\n",
165+
"def sigmoid_derivative(x):\n",
166+
" return x * (1 - x)\n",
167+
" \n",
168+
"sweights = [[-0.16595599],[ 0.44064899],[-0.99977125]]\n",
169+
"print (\"input:\")\n",
170+
"print (training_set_inputs)\n",
171+
"print (\"truth:\")\n",
172+
"print (training_set_outputs)\n",
173+
"print (\"dot-product:\")\n",
174+
"print (np.dot(training_set_inputs, sweights) )\n",
175+
"output = sigmoid(np.dot(training_set_inputs, sweights))\n",
176+
"print (\"output:\")\n",
177+
"print (output)\n",
178+
"error = sigmoid(np.dot(training_set_inputs, sweights)) - training_set_outputs\n",
179+
"print (\"error:\")\n",
180+
"print (error)\n",
181+
"print (\"derivative:\")\n",
182+
"print (np.dot(training_set_inputs.T, error * sigmoid_derivative(output)) )"
183+
]
184+
}
185+
],
186+
"metadata": {
187+
"kernelspec": {
188+
"display_name": "Python 3",
189+
"language": "python",
190+
"name": "python3"
191+
},
192+
"language_info": {
193+
"codemirror_mode": {
194+
"name": "ipython",
195+
"version": 3
196+
},
197+
"file_extension": ".py",
198+
"mimetype": "text/x-python",
199+
"name": "python",
200+
"nbconvert_exporter": "python",
201+
"pygments_lexer": "ipython3",
202+
"version": "3.5.2"
203+
}
204+
},
205+
"nbformat": 4,
206+
"nbformat_minor": 0
207+
}

.ipynb_checkpoints/Tensorflow ANN-checkpoint.ipynb

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,17 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 33,
5+
"execution_count": 63,
66
"metadata": {
77
"collapsed": false
88
},
99
"outputs": [
1010
{
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"
11+
"name": "stdout",
12+
"output_type": "stream",
13+
"text": [
14+
"[0, 1, 0, 1, 1] [0]\n"
15+
]
5416
}
5517
],
5618
"source": [
@@ -102,12 +64,12 @@
10264
"if __name__ == '__main__':\n",
10365
" train_x,train_y,test_x,test_y = create_feature_sets_and_labels()\n",
10466
"\n",
105-
"train_x, train_y"
67+
"print(train_x[0], train_y[0]"
10668
]
10769
},
10870
{
10971
"cell_type": "code",
110-
"execution_count": 57,
72+
"execution_count": 79,
11173
"metadata": {
11274
"collapsed": false
11375
},
@@ -150,7 +112,7 @@
150112
"output_layer = {'f_fum':None,\n",
151113
" 'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),\n",
152114
" 'bias':tf.Variable(tf.random_normal([n_classes])),}\n",
153-
"\n",
115+
" \n",
154116
"\n",
155117
"def neural_network_model(data):\n",
156118
"\n",
@@ -166,6 +128,7 @@
166128
"\n",
167129
"def train_neural_network(x):\n",
168130
" prediction = neural_network_model(x)\n",
131+
"\n",
169132
" cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )\n",
170133
" #optimizer = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999).minimize(cost)\n",
171134
" optimizer = tf.train.GradientDescentOptimizer(1).minimize(cost)\n",

.ipynb_checkpoints/Tensorflow bow-checkpoint.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 9,
66
"metadata": {
77
"collapsed": false
88
},
@@ -119,7 +119,7 @@
119119
"n_nodes_hl2 = 250\n",
120120
"\n",
121121
"n_classes = 2\n",
122-
"hm_epochs = 5000\n",
122+
"hm_epochs = 10\n",
123123
"\n",
124124
"x = tf.placeholder('float')\n",
125125
"y = tf.placeholder('float')\n",
@@ -170,14 +170,15 @@
170170
" epoch_loss += c\n",
171171
" last_cost = c\n",
172172
"\n",
173-
" if (epoch% 500) == 0 and epoch > 50:\n",
173+
" if (epoch% 2) == 0 and epoch > 1:\n",
174174
" print('Epoch', epoch+1, 'completed out of',hm_epochs,'cost:', last_cost)\n",
175175
"\n",
176176
" correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))\n",
177177
" accuracy = tf.reduce_mean(tf.cast(correct, 'float'))\n",
178178
"\n",
179179
" print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))\n",
180180
"\n",
181+
"print(x)\n",
181182
"train_neural_network(x)"
182183
]
183184
}

0 commit comments

Comments
 (0)