Skip to content

Commit 2cdcf07

Browse files
author
Mofan Zhou
committed
create tf 12
1 parent 3c72afd commit 2cdcf07

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# View more python learning tutorial on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
import tensorflow as tf
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
10+
def add_layer(inputs, in_size, out_size, activation_function=None):
11+
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
12+
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
13+
Wx_plus_b = tf.matmul(inputs, Weights) + biases
14+
if activation_function is None:
15+
outputs = Wx_plus_b
16+
else:
17+
outputs = activation_function(Wx_plus_b)
18+
return outputs
19+
20+
# Make up some real data
21+
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
22+
noise = np.random.normal(0, 0.05, x_data.shape)
23+
y_data = np.square(x_data) - 0.5 + noise
24+
25+
##plt.scatter(x_data, y_data)
26+
##plt.show()
27+
28+
# define placeholder for inputs to network
29+
xs = tf.placeholder(tf.float32, [None, 1])
30+
ys = tf.placeholder(tf.float32, [None, 1])
31+
# add hidden layer
32+
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
33+
# add output layer
34+
prediction = add_layer(l1, 10, 1, activation_function=None)
35+
36+
# the error between prediciton and real data
37+
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
38+
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
39+
# important step
40+
init = tf.initialize_all_variables()
41+
sess= tf.Session()
42+
sess.run(init)
43+
44+
for i in range(1000):
45+
# training
46+
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
47+
if i % 50 == 0:
48+
# to see the step improvement
49+
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# View more python learning tutorial on my Youtube and Youku channel!!!
2+
3+
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
4+
# Youku video tutorial: http://i.youku.com/pythontutorial
5+
6+
import tensorflow as tf
7+
import numpy as np
8+
import matplotlib.pyplot as plt
9+
10+
def add_layer(inputs, in_size, out_size, activation_function=None):
11+
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
12+
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
13+
Wx_plus_b = tf.matmul(inputs, Weights) + biases
14+
if activation_function is None:
15+
outputs = Wx_plus_b
16+
else:
17+
outputs = activation_function(Wx_plus_b)
18+
return outputs
19+
20+
# Make up some real data
21+
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
22+
noise = np.random.normal(0, 0.05, x_data.shape)
23+
y_data = np.square(x_data) - 0.5 + noise
24+
25+
##plt.scatter(x_data, y_data)
26+
##plt.show()
27+
28+
# define placeholder for inputs to network
29+
xs = tf.placeholder(tf.float32, [None, 1])
30+
ys = tf.placeholder(tf.float32, [None, 1])
31+
# add hidden layer
32+
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
33+
# add output layer
34+
prediction = add_layer(l1, 10, 1, activation_function=None)
35+
36+
# the error between prediciton and real data
37+
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
38+
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
39+
# important step
40+
init = tf.initialize_all_variables()
41+
sess= tf.Session()
42+
sess.run(init)
43+
44+
# plot the real data
45+
fig = plt.figure()
46+
ax = fig.add_subplot(1,1,1)
47+
ax.scatter(x_data, y_data)
48+
plt.ion()
49+
plt.show()
50+
51+
52+
for i in range(1000):
53+
# training
54+
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
55+
if i % 50 == 0:
56+
# to visualize the result and improvement
57+
try:
58+
ax.lines.remove(lines[0])
59+
except Exception:
60+
pass
61+
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
62+
# plot the prediction
63+
lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
64+
plt.pause(1)
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+

0 commit comments

Comments
 (0)