Skip to content

Commit 2994972

Browse files
authored
Merge pull request #1 from Naveendurai/master
Init commit 🔆
2 parents 6450d22 + c43d13f commit 2994972

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

01_basics.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Basics of Neural Networking
2+
import numpy as np
3+
4+
no_inputs = 5
5+
x_data = np.array([1, 2, 3, 4, 5])
6+
y_data = np.array([2, 4, 6, 8, 10])
7+
weight = 1
8+
bias = 0.0
9+
learning_rate = 0.01
10+
11+
# The basic formula is Y = ( w * X ) + B
12+
# Y is the outputs
13+
# w is the weight
14+
# X is the inputs
15+
# B is the bias
16+
17+
for i in range(10):
18+
print 'Step', i, ':'
19+
output = x_data * weight + bias
20+
# [ 0.3 0.6 0.9 1.2 1.5]
21+
22+
difference = y_data - output
23+
24+
gradient = 0
25+
for i in range(no_inputs):
26+
gradient += difference[i] * x_data[i]
27+
28+
gradient /= no_inputs
29+
30+
loss = (difference * difference) / no_inputs
31+
print 'Loss:', np.sum(loss)
32+
33+
weight = weight + (learning_rate * gradient)
34+
35+
print 'Weight: ', weight
36+
print ''

02_tf_basics.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Basics of Tensorflow
2+
3+
import tensorflow as tf
4+
5+
x_input = tf.constant([1, 2, 3, 4])
6+
# print x_input
7+
# Tensor("Const:0", shape=(4,), dtype=int32)
8+
9+
sess = tf.Session()
10+
print sess.run(x_input)

03_tf_network.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Basics NeuralNetwork of Tensorflow
2+
import tensorflow as tf
3+
import numpy as np
4+
x_data = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0])
5+
y_data = tf.constant([2.0, 4.0, 6.0, 8.0, 10.0])
6+
7+
x = tf.placeholder(tf.float32)
8+
y = tf.placeholder(tf.float32)
9+
10+
weight = tf.Variable([1.0])
11+
bias = tf.Variable([0.0])
12+
13+
init = tf.initialize_all_variables()
14+
predict = (x * weight)
15+
loss = tf.reduce_mean(tf.square(predict - y) )
16+
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss/2)
17+
steps = 300
18+
with tf.Session() as sess:
19+
sess.run(init)
20+
X = sess.run(x_data)
21+
Y = sess.run(y_data)
22+
for i in range(steps):
23+
print 'Step: ', i, ':'
24+
print 'Loss: ', sess.run(loss, {x: X, y: Y})
25+
sess.run(optimizer, {x: X, y: Y})
26+
print 'THE FINAL WEIGHT IS', sess.run(weight)[0]
27+

04_tf_with_hidden.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Tensorflow Basics on NeuralNetwork with hidden layer
2+
3+
import tensorflow as tf
4+
import numpy as np
5+
6+
INPUT_NEURONS = 2
7+
HIDDEN_LAYER_1_NEURONS = 4
8+
OUTPUT_NEURONS = 1
9+
x_data = np.array([[0, 0], [0, 1], [1, 1]])
10+
11+
y_data = np.array([0, 1, 1])
12+
13+
x = tf.placeholder(tf.float32)
14+
y = tf.placeholder(tf.float32)
15+
16+
h1_layer = {'weight': tf.Variable(np.random.rand(INPUT_NEURONS, HIDDEN_LAYER_1_NEURONS).astype(np.float32)),
17+
'bias': tf.constant(0.0)}
18+
output_layer = {'weight': tf.Variable(np.random.rand(HIDDEN_LAYER_1_NEURONS, OUTPUT_NEURONS).astype(np.float32)),
19+
'bias': tf.constant(0.0)}
20+
21+
22+
h1 = tf.matmul(x, h1_layer['weight']) + h1_layer['bias']
23+
h1 = tf.sigmoid(h1)
24+
25+
predict = tf.matmul(h1, output_layer['weight']) + output_layer['bias']
26+
27+
loss = tf.reduce_mean(tf.square(predict - y))
28+
optimizer = tf.train.GradientDescentOptimizer(0.00001).minimize(loss)
29+
30+
sess = tf.Session()
31+
init = tf.initialize_all_variables()
32+
sess.run(init)
33+
34+
for i in range(50000):
35+
if i % 5000 == 0:
36+
print 'Loss: ', sess.run(loss, {x: x_data, y: y_data})
37+
sess.run(optimizer, {x: x_data, y: y_data})
38+
39+
print sess.run(predict, {x: [[1, 1]] })

0 commit comments

Comments
 (0)