From 71d21247ee3fab065d7fa2f634ce52db418862e9 Mon Sep 17 00:00:00 2001 From: Morvan Zhou Date: Thu, 11 May 2017 15:52:04 +1000 Subject: [PATCH] update --- README.md | 73 +++++++++-------- .../{session.py => 201_session.py} | 0 .../{placeholder.py => 202_placeholder.py} | 2 - tutorial-contents/203_variable.py | 20 +++++ tutorial-contents/204_activation.py | 48 +++++++++++ tutorial-contents/301_simple_regression.py | 4 +- .../302_simple_classification.py | 4 +- tutorial-contents/303_save_reload.py | 80 +++++++++++++++++++ tutorial-contents/304_optimizer.py | 69 ++++++++++++++++ tutorial-contents/305_tensorboard.py | 49 ++++++++++++ .../405_DQN_reinforcement_learning.py | 13 +-- tutorial-contents/install | 7 -- 12 files changed, 316 insertions(+), 53 deletions(-) rename tutorial-contents/{session.py => 201_session.py} (100%) rename tutorial-contents/{placeholder.py => 202_placeholder.py} (97%) create mode 100644 tutorial-contents/203_variable.py create mode 100644 tutorial-contents/204_activation.py create mode 100644 tutorial-contents/303_save_reload.py create mode 100644 tutorial-contents/304_optimizer.py create mode 100644 tutorial-contents/305_tensorboard.py delete mode 100644 tutorial-contents/install diff --git a/README.md b/README.md index 9526a12..a2d6cf8 100644 --- a/README.md +++ b/README.md @@ -18,69 +18,74 @@ In these tutorials, we will build our first Neural Network and try to build some All methods mentioned below have their video and text tutorial in Chinese. Visit [莫烦 Python](https://morvanzhou.github.io/tutorials/) for more. - + # Donation diff --git a/tutorial-contents/session.py b/tutorial-contents/201_session.py similarity index 100% rename from tutorial-contents/session.py rename to tutorial-contents/201_session.py diff --git a/tutorial-contents/placeholder.py b/tutorial-contents/202_placeholder.py similarity index 97% rename from tutorial-contents/placeholder.py rename to tutorial-contents/202_placeholder.py index c37872d..8bce564 100644 --- a/tutorial-contents/placeholder.py +++ b/tutorial-contents/202_placeholder.py @@ -4,8 +4,6 @@ Dependencies: tensorflow: 1.1.0 -matplotlib -numpy """ import tensorflow as tf diff --git a/tutorial-contents/203_variable.py b/tutorial-contents/203_variable.py new file mode 100644 index 0000000..f77d95f --- /dev/null +++ b/tutorial-contents/203_variable.py @@ -0,0 +1,20 @@ +""" +Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/ +My Youtube Channel: https://www.youtube.com/user/MorvanZhou + +Dependencies: +tensorflow: 1.1.0 +""" +import tensorflow as tf + +var = tf.Variable(0) # our first variable in the "global_variable" set + +add_operation = tf.add(var, 1) +update_operation = tf.assign(var, add_operation) + +with tf.Session() as sess: + # once define variables, you have to initialize them by doing this + sess.run(tf.global_variables_initializer()) + for _ in range(3): + sess.run(update_operation) + print(sess.run(var)) \ No newline at end of file diff --git a/tutorial-contents/204_activation.py b/tutorial-contents/204_activation.py new file mode 100644 index 0000000..1532fdf --- /dev/null +++ b/tutorial-contents/204_activation.py @@ -0,0 +1,48 @@ +""" +Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/ +My Youtube Channel: https://www.youtube.com/user/MorvanZhou + +Dependencies: +tensorflow: 1.1.0 +matplotlib +""" +import tensorflow as tf +import numpy as np +import matplotlib.pyplot as plt + +# fake data +x = np.linspace(-5, 5, 200) # x data, shape=(100, 1) + +# following are popular activation functions +y_relu = tf.nn.relu(x) +y_sigmoid = tf.nn.sigmoid(x) +y_tanh = tf.nn.tanh(x) +y_softplus = tf.nn.softplus(x) +# y_softmax = tf.nn.softmax(x) softmax is a special kind of activation function, it is about probability + +sess = tf.Session() +y_relu, y_sigmoid, y_tanh, y_softplus = sess.run([y_relu, y_sigmoid, y_tanh, y_softplus]) + +# plt to visualize these activation function +plt.figure(1, figsize=(8, 6)) +plt.subplot(221) +plt.plot(x, y_relu, c='red', label='relu') +plt.ylim((-1, 5)) +plt.legend(loc='best') + +plt.subplot(222) +plt.plot(x, y_sigmoid, c='red', label='sigmoid') +plt.ylim((-0.2, 1.2)) +plt.legend(loc='best') + +plt.subplot(223) +plt.plot(x, y_tanh, c='red', label='tanh') +plt.ylim((-1.2, 1.2)) +plt.legend(loc='best') + +plt.subplot(224) +plt.plot(x, y_softplus, c='red', label='softplus') +plt.ylim((-0.2, 6)) +plt.legend(loc='best') + +plt.show() \ No newline at end of file diff --git a/tutorial-contents/301_simple_regression.py b/tutorial-contents/301_simple_regression.py index 32e4fc3..aabb352 100644 --- a/tutorial-contents/301_simple_regression.py +++ b/tutorial-contents/301_simple_regression.py @@ -23,8 +23,8 @@ plt.scatter(x, y) plt.show() -tf_x = tf.placeholder(tf.float32, x.shape, 'x') # input x -tf_y = tf.placeholder(tf.float32, y.shape, 'y') # input y +tf_x = tf.placeholder(tf.float32, x.shape) # input x +tf_y = tf.placeholder(tf.float32, y.shape) # input y # neural network layers l1 = tf.layers.dense(tf_x, 10, tf.nn.relu) # hidden layer diff --git a/tutorial-contents/302_simple_classification.py b/tutorial-contents/302_simple_classification.py index 84d46ae..be767ac 100644 --- a/tutorial-contents/302_simple_classification.py +++ b/tutorial-contents/302_simple_classification.py @@ -27,8 +27,8 @@ plt.scatter(x[:, 0], x[:, 1], c=y, s=100, lw=0, cmap='RdYlGn') plt.show() -tf_x = tf.placeholder(tf.float32, x.shape, 'x') # input x -tf_y = tf.placeholder(tf.int32, y.shape, 'y') # input y +tf_x = tf.placeholder(tf.float32, x.shape) # input x +tf_y = tf.placeholder(tf.int32, y.shape) # input y # neural network layers l1 = tf.layers.dense(tf_x, 10, tf.nn.relu) # hidden layer diff --git a/tutorial-contents/303_save_reload.py b/tutorial-contents/303_save_reload.py new file mode 100644 index 0000000..8b6620c --- /dev/null +++ b/tutorial-contents/303_save_reload.py @@ -0,0 +1,80 @@ +""" +Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/ +My Youtube Channel: https://www.youtube.com/user/MorvanZhou + +Dependencies: +tensorflow: 1.1.0 +matplotlib +numpy +""" +import tensorflow as tf +import matplotlib.pyplot as plt +import numpy as np + +tf.set_random_seed(1) +np.random.seed(1) + +# fake data +x = np.linspace(-1, 1, 100)[:, np.newaxis] # shape (100, 1) +noise = np.random.normal(0, 0.1, size=x.shape) +y = np.power(x, 2) + noise # shape (100, 1) + some noise + + +def save(): + print('This is save') + # build neural network + tf_x = tf.placeholder(tf.float32, x.shape) # input x + tf_y = tf.placeholder(tf.float32, y.shape) # input y + l = tf.layers.dense(tf_x, 10, tf.nn.relu) # hidden layer + o = tf.layers.dense(l, 1) # output layer + loss = tf.losses.mean_squared_error(tf_y, o) # compute cost + train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss) + + sess = tf.Session() + sess.run(tf.global_variables_initializer()) # initialize var in graph + + saver = tf.train.Saver() # define a saver for saving and restoring + + for step in range(100): # train + sess.run(train_op, {tf_x: x, tf_y: y}) + + saver.save(sess, 'params', write_meta_graph=False) # meta_graph is not recommended + + # plotting + pred, l = sess.run([o, loss], {tf_x: x, tf_y: y}) + plt.figure(1, figsize=(10, 5)) + plt.subplot(121) + plt.scatter(x, y) + plt.plot(x, pred, 'r-', lw=5) + plt.text(-1, 1.2, 'Save Loss=%.4f' % l, fontdict={'size': 15, 'color': 'red'}) + + +def reload(): + print('This is reload') + # build entire net again and restore + tf_x = tf.placeholder(tf.float32, x.shape) # input x + tf_y = tf.placeholder(tf.float32, y.shape) # input y + l_ = tf.layers.dense(tf_x, 10, tf.nn.relu) # hidden layer + o_ = tf.layers.dense(l_, 1) # output layer + loss_ = tf.losses.mean_squared_error(tf_y, o_) # compute cost + + sess = tf.Session() + # don't need to initialize variables, just restoring trained variables + saver = tf.train.Saver() # define a saver for saving and restoring + saver.restore(sess, 'params') + + # plotting + pred, l = sess.run([o_, loss_], {tf_x: x, tf_y: y}) + plt.subplot(122) + plt.scatter(x, y) + plt.plot(x, pred, 'r-', lw=5) + plt.text(-1, 1.2, 'Reload Loss=%.4f' % l, fontdict={'size': 15, 'color': 'red'}) + plt.show() + + +save() + +# destroy previous net +tf.reset_default_graph() + +reload() diff --git a/tutorial-contents/304_optimizer.py b/tutorial-contents/304_optimizer.py new file mode 100644 index 0000000..4aa2203 --- /dev/null +++ b/tutorial-contents/304_optimizer.py @@ -0,0 +1,69 @@ +""" +Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/ +My Youtube Channel: https://www.youtube.com/user/MorvanZhou + +Dependencies: +tensorflow: 1.1.0 +matplotlib +numpy +""" +import tensorflow as tf +import matplotlib.pyplot as plt +import numpy as np + +tf.set_random_seed(1) +np.random.seed(1) + +LR = 0.01 +BATCH_SIZE = 32 + +# fake data +x = np.linspace(-1, 1, 100)[:, np.newaxis] # shape (100, 1) +noise = np.random.normal(0, 0.1, size=x.shape) +y = np.power(x, 2) + noise # shape (100, 1) + some noise + +# plot dataset +plt.scatter(x, y) +plt.show() + +# default network +class Net: + def __init__(self, opt, **kwargs): + self.x = tf.placeholder(tf.float32, [None, 1]) + self.y = tf.placeholder(tf.float32, [None, 1]) + l = tf.layers.dense(self.x, 20, tf.nn.relu) + out = tf.layers.dense(l, 1) + self.loss = tf.losses.mean_squared_error(self.y, out) + self.train = opt(LR, **kwargs).minimize(self.loss) + +# different nets +net_SGD = Net(tf.train.GradientDescentOptimizer) +net_Momentum = Net(tf.train.MomentumOptimizer, momentum=0.9) +net_RMSprop = Net(tf.train.RMSPropOptimizer) +net_Adam = Net(tf.train.AdamOptimizer) +nets = [net_SGD, net_Momentum, net_RMSprop, net_Adam] + +sess = tf.Session() +sess.run(tf.global_variables_initializer()) + +losses_his = [[], [], [], []] # record loss + +# training +for step in range(300): # for each training step + index = np.random.randint(0, x.shape[0], BATCH_SIZE) + b_x = x[index] + b_y = y[index] + + for net, l_his in zip(nets, losses_his): + _, l = sess.run([net.train, net.loss], {net.x: b_x, net.y: b_y}) + l_his.append(l) # loss recoder + +# plot loss history +labels = ['SGD', 'Momentum', 'RMSprop', 'Adam'] +for i, l_his in enumerate(losses_his): + plt.plot(l_his, label=labels[i]) +plt.legend(loc='best') +plt.xlabel('Steps') +plt.ylabel('Loss') +plt.ylim((0, 0.2)) +plt.show() \ No newline at end of file diff --git a/tutorial-contents/305_tensorboard.py b/tutorial-contents/305_tensorboard.py new file mode 100644 index 0000000..73e7546 --- /dev/null +++ b/tutorial-contents/305_tensorboard.py @@ -0,0 +1,49 @@ +""" +Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/ +My Youtube Channel: https://www.youtube.com/user/MorvanZhou + +Dependencies: +tensorflow: 1.1.0 +numpy +""" +import tensorflow as tf +import numpy as np + +tf.set_random_seed(1) +np.random.seed(1) + +# fake data +x = np.linspace(-1, 1, 100)[:, np.newaxis] # shape (100, 1) +noise = np.random.normal(0, 0.1, size=x.shape) +y = np.power(x, 2) + noise # shape (100, 1) + some noise + +with tf.variable_scope('Inputs'): + tf_x = tf.placeholder(tf.float32, x.shape, name='x') + tf_y = tf.placeholder(tf.float32, y.shape, name='y') + +with tf.variable_scope('Net'): + l1 = tf.layers.dense(tf_x, 10, tf.nn.relu, name='hidden_layer') + output = tf.layers.dense(l1, 1, name='output_layer') + + # add to histogram summary + tf.summary.histogram('h_out', l1) + tf.summary.histogram('pred', output) + +loss = tf.losses.mean_squared_error(tf_y, output, scope='loss') +train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss) +tf.summary.scalar('loss', loss) # add loss to scalar summary + +sess = tf.Session() +sess.run(tf.global_variables_initializer()) + +writer = tf.summary.FileWriter('./log', sess.graph) # write to file +merge_op = tf.summary.merge_all() # operation to merge all summary + +for step in range(100): + # train and net output + _, result = sess.run([train_op, merge_op], {tf_x: x, tf_y: y}) + writer.add_summary(result, step) + +# Lastly, in your terminal or CMD, type this : +# $ tensorboard --logdir path/to/log +# open you google chrome, type the link shown on your terminal or CMD. (something like this: http://0.0.0.0:6006) \ No newline at end of file diff --git a/tutorial-contents/405_DQN_reinforcement_learning.py b/tutorial-contents/405_DQN_reinforcement_learning.py index 2dde648..59a6b82 100644 --- a/tutorial-contents/405_DQN_reinforcement_learning.py +++ b/tutorial-contents/405_DQN_reinforcement_learning.py @@ -7,6 +7,7 @@ tensorflow: 1.1.0 matplotlib numpy +gym: 0.8.1 """ import tensorflow as tf import numpy as np @@ -40,11 +41,11 @@ l_eval = tf.layers.dense(tf_s, 10, tf.nn.relu, kernel_initializer=tf.random_normal_initializer(0, 0.1)) q = tf.layers.dense(l_eval, N_ACTIONS, kernel_initializer=tf.random_normal_initializer(0, 0.1)) -with tf.variable_scope('q_next'): # target network - l_target = tf.layers.dense(tf_s_, 10, tf.nn.relu) - q_next = tf.layers.dense(l_target, N_ACTIONS) +with tf.variable_scope('q_next'): # target network, not to train + l_target = tf.layers.dense(tf_s_, 10, tf.nn.relu, trainable=False) + q_next = tf.layers.dense(l_target, N_ACTIONS, trainable=False) -q_target = tf.stop_gradient(tf_r + GAMMA * tf.reduce_max(q_next, axis=1)) # shape=(None, ), not need gradient for q_next +q_target = tf_r + GAMMA * tf.reduce_max(q_next, axis=1) # shape=(None, ), a_one_hot = tf.one_hot(tf_a, depth=N_ACTIONS, dtype=tf.float32) q_wrt_a = tf.reduce_sum(q * a_one_hot, axis=1) # shape=(None, ), q for current state @@ -79,8 +80,8 @@ def learn(): # update target net global LEARNING_STEP_COUNTER if LEARNING_STEP_COUNTER % TARGET_REPLACE_ITER == 0: - t_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='q_next') - e_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='q') + t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_next') + e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q') sess.run([tf.assign(t, e) for t, e in zip(t_params, e_params)]) LEARNING_STEP_COUNTER += 1 diff --git a/tutorial-contents/install b/tutorial-contents/install deleted file mode 100644 index 8856537..0000000 --- a/tutorial-contents/install +++ /dev/null @@ -1,7 +0,0 @@ -compile from source, 折腾的方式. -但是快. -https://www.tensorflow.org/install/install_sources - -装 brew & bazel - -https://bazel.build/versions/master/docs/install-os-x.html#install-with-installer-mac-os-x \ No newline at end of file