forked from hunkim/DeepLearningZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab-09-6-multi-linear_back_prop.py
61 lines (47 loc) · 1.45 KB
/
lab-09-6-multi-linear_back_prop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# http://blog.aloni.org/posts/backprop-with-tensorflow/
# https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b#.b3rvzhx89
# WIP
import tensorflow as tf
tf.set_random_seed(777) # reproducibility
# tf Graph Input
x_data = [[73., 80., 75.],
[93., 88., 93.],
[89., 91., 90.],
[96., 98., 100.],
[73., 66., 70.]]
y_data = [[152.],
[185.],
[180.],
[196.],
[142.]]
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])
# Set wrong model weights
W = tf.Variable(tf.truncated_normal([3, 1]))
b = tf.Variable(5.)
# Forward prop
hypothesis = tf.matmul(X, W) + b
print(hypothesis.shape, Y.shape)
# diff
assert hypothesis.shape.as_list() == Y.shape.as_list()
diff = (hypothesis - Y)
# Back prop (chain rule)
d_l1 = diff
d_b = d_l1
d_w = tf.matmul(tf.transpose(X), d_l1)
print(X, d_l1, d_w)
# Updating network using gradients
learning_rate = 1e-6
step = [
tf.assign(W, W - learning_rate * d_w),
tf.assign(b, b - learning_rate * tf.reduce_mean(d_b)),
]
# 7. Running and testing the training process
RMSE = tf.reduce_mean(tf.square((Y - hypothesis)))
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(10000):
print(i, sess.run([step, RMSE], feed_dict={X: x_data, Y: y_data}))
print(sess.run(hypothesis, feed_dict={X: x_data}))