Skip to content

Commit 9f4b057

Browse files
MorvanZhouMorvan Zhou
authored andcommitted
update
1 parent e54b520 commit 9f4b057

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

tutorial-contents/401_CNN.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tensorflow as tf
1111
from tensorflow.examples.tutorials.mnist import input_data
1212
import numpy as np
13+
import matplotlib.pyplot as plt
1314

1415
tf.set_random_seed(1)
1516
np.random.seed(1)
@@ -21,6 +22,13 @@
2122
test_x = mnist.test.images[:2000]
2223
test_y = mnist.test.labels[:2000]
2324

25+
# plot one example
26+
print(mnist.train.images.shape) # (55000, 28 * 28)
27+
print(mnist.train.labels.shape) # (55000, 10)
28+
plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
29+
plt.title('%i' % np.argmax(mnist.train.labels[0]))
30+
plt.show()
31+
2432
tf_x = tf.placeholder(tf.float32, [None, 28*28])/255. # normalize to range (0, 1)
2533
image = tf.reshape(tf_x, [-1, 28, 28, 1]) # (batch, height, width, channel)
2634
tf_y = tf.placeholder(tf.int32, [None, 10]) # input y
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/
3+
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
4+
5+
Dependencies:
6+
tensorflow: 1.1.0
7+
matplotlib
8+
numpy
9+
"""
10+
import tensorflow as tf
11+
from tensorflow.examples.tutorials.mnist import input_data
12+
import numpy as np
13+
import matplotlib.pyplot as plt
14+
15+
tf.set_random_seed(1)
16+
np.random.seed(1)
17+
18+
# Hyper Parameters
19+
BATCH_SIZE = 64
20+
TIME_STEP = 28 # rnn time step / image height
21+
INPUT_SIZE = 28 # rnn input size / image width
22+
LR = 0.01 # learning rate
23+
24+
# data
25+
mnist = input_data.read_data_sets('./mnist', one_hot=True)
26+
test_x = mnist.test.images[:2000]
27+
test_y = mnist.test.labels[:2000]
28+
29+
# plot one example
30+
print(mnist.train.images.shape) # (55000, 28 * 28)
31+
print(mnist.train.labels.shape) # (55000, 10)
32+
plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
33+
plt.title('%i' % np.argmax(mnist.train.labels[0]))
34+
plt.show()
35+
36+
# tensorflow placeholders
37+
tf_x = tf.placeholder(tf.float32, [None, TIME_STEP * INPUT_SIZE])/255. # shape(batch, 784), normalize to range (0, 1)
38+
image = tf.reshape(tf_x, [-1, TIME_STEP, INPUT_SIZE]) # (batch, height, width, channel)
39+
tf_y = tf.placeholder(tf.int32, [None, 10]) # input y
40+
41+
# RNN
42+
rnn_cell = tf.contrib.rnn.BasicLSTMCell(num_units=64)
43+
outputs, (h_c, h_n) = tf.nn.dynamic_rnn(
44+
rnn_cell, # cell you have chosen
45+
image, # input
46+
initial_state=None, # the initial hidden state
47+
dtype=tf.float32, # must given if set initial_state = None
48+
time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
49+
)
50+
output = tf.layers.dense(outputs[:, -1, :], 10) # output based on the last output step
51+
52+
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # compute cost
53+
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
54+
55+
accuracy = tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables
56+
labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
57+
58+
sess = tf.Session()
59+
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
60+
sess.run(init_op) # initialize var in graph
61+
62+
for step in range(1200): # training
63+
b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
64+
_, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y})
65+
if step % 50 == 0: # testing
66+
accuracy_ = sess.run(accuracy, {tf_x: test_x, tf_y: test_y})
67+
print('train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)
68+
69+
# print 10 predictions from test data
70+
test_output = sess.run(output, {tf_x: test_x[:10]})
71+
pred_y = np.argmax(test_output, 1)
72+
print(pred_y, 'prediction number')
73+
print(np.argmax(test_y[:10], 1), 'real number')
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/
3+
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
4+
5+
Dependencies:
6+
tensorflow: 1.1.0
7+
matplotlib
8+
numpy
9+
"""
10+
import tensorflow as tf
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
14+
15+
# Hyper Parameters
16+
TIME_STEP = 10 # rnn time step
17+
INPUT_SIZE = 1 # rnn input size
18+
CELL_SIZE = 32 # rnn cell size
19+
LR = 0.02 # learning rate
20+
21+
# show data
22+
steps = np.linspace(0, np.pi*2, 100, dtype=np.float32)
23+
x_np = np.sin(steps) # float32 for converting torch FloatTensor
24+
y_np = np.cos(steps)
25+
plt.plot(steps, y_np, 'r-', label='target (cos)')
26+
plt.plot(steps, x_np, 'b-', label='input (sin)')
27+
plt.legend(loc='best')
28+
plt.show()
29+
30+
# tensorflow placeholders
31+
tf_x = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE]) # shape(batch, 5, 1)
32+
tf_y = tf.placeholder(tf.float32, [None, TIME_STEP, INPUT_SIZE]) # input y
33+
34+
# RNN
35+
rnn_cell = tf.contrib.rnn.BasicRNNCell(num_units=CELL_SIZE)
36+
init_s = rnn_cell.zero_state(batch_size=1, dtype=tf.float32) # very first hidden state
37+
outputs, final_s = tf.nn.dynamic_rnn(
38+
rnn_cell, # cell you have chosen
39+
tf_x, # input
40+
initial_state=init_s, # the initial hidden state
41+
time_major=False, # False: (batch, time step, input); True: (time step, batch, input)
42+
)
43+
outs2D = tf.reshape(outputs, [-1, CELL_SIZE]) # reshape 3D output to 2D for fully connected layer
44+
net_outs2D = tf.layers.dense(outs2D, INPUT_SIZE)
45+
outs = tf.reshape(net_outs2D, [-1, TIME_STEP, INPUT_SIZE]) # reshape back to 3D
46+
47+
loss = tf.losses.mean_squared_error(labels=tf_y, predictions=outs) # compute cost
48+
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
49+
50+
sess = tf.Session()
51+
init_op = tf.group(tf.global_variables_initializer())
52+
sess.run(init_op) # initialize var in graph
53+
54+
plt.figure(1, figsize=(12, 5))
55+
plt.ion() # continuously plot
56+
plt.show()
57+
58+
for step in range(60):
59+
start, end = step * np.pi, (step+1)*np.pi # time steps
60+
# use sin predicts cos
61+
steps = np.linspace(start, end, TIME_STEP)
62+
x = np.sin(steps)[np.newaxis, :, np.newaxis] # shape (batch, time_step, input_size)
63+
y = np.cos(steps)[np.newaxis, :, np.newaxis]
64+
if 'final_s_' not in globals(): # first state, no any hidden state
65+
feed_dict = {tf_x: x, tf_y: y}
66+
else: # has hidden state, so pass it to rnn
67+
feed_dict = {tf_x: x, tf_y: y, init_s: final_s_}
68+
_, pred_, final_s_ = sess.run([train_op, outs, final_s], feed_dict) # train
69+
70+
# plotting
71+
plt.plot(steps, y.flatten(), 'r-')
72+
plt.plot(steps, pred_.flatten(), 'b-')
73+
plt.ylim((-1.2, 1.2))
74+
plt.draw()
75+
plt.pause(0.05)
76+
77+
plt.ioff()
78+
plt.show()

tutorial-contents/DQN.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
l_target = tf.layers.dense(tf_s_, 10, tf.nn.relu)
4545
q_next = tf.layers.dense(l_target, N_ACTIONS)
4646

47-
q_target = tf.stop_gradient(tf_r + GAMMA * tf.reduce_max(q_next, axis=1)) # shape=(None, ), not need any gradient
47+
q_target = tf.stop_gradient(tf_r + GAMMA * tf.reduce_max(q_next, axis=1)) # shape=(None, ), not need gradient for q_next
4848
a_one_hot = tf.one_hot(tf_a, depth=N_ACTIONS, dtype=tf.float32)
49-
q_wrt_a = tf.reduce_sum(q * a_one_hot, axis=1) # shape=(None, ), q for current state
49+
q_wrt_a = tf.reduce_sum(q * a_one_hot, axis=1) # shape=(None, ), q for current state
5050

5151
loss = tf.reduce_mean(tf.squared_difference(q_target, q_wrt_a))
5252
train_op = tf.train.AdamOptimizer(LR).minimize(loss)

0 commit comments

Comments
 (0)