一、tensorflow
1.結果的可視化
莫煩大佬的代碼如下:
from __future__ import print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def add_layer(inputs, in_size, out_size, activation_function=None):
# 增加一層隱藏層,返回隱藏層的輸出值
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size])+0.1) # 加0.1使得初始化片偏差不爲零
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# Make up some real data
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
# define placeholder for inputs for network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# define the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# initialize_all_variables()
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()
# 訓練2000次
for i in range(2000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to visualize the result and improvement
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
# plot the prediction
lines = ax.plot(x_data, prediction_value, '-r', lw=5)
plt.pause(1)
對以上代碼有如下筆記:
1.plt.ion():在plt.show()之後,程序會暫停到那兒,並不會繼續執行下去。使用plt.ion()這個函數,使matplotlib的顯示模式轉換爲交互(interactive)模式。即使在腳本中遇到plt.show(),代碼還是會繼續執行。具體參考這篇文章。
2.try except:屬於python的異常處理機制之一,具體參考這篇文章。
2.tensorboard簡單使用
同煩大佬的代碼;
from __future__ import print_function
import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
# 增加一層隱藏層,返回隱藏層的輸出值
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size])+0.1) # 加0.1使得初始化片偏差不爲零
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# define placeholder for inputs for network
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# define the error between prediction and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# initialize_all_variables()
init = tf.global_variables_initializer()
sess = tf.Session()
# tf.train.SummaryWriter soon be deprecated, use following
writer = tf.summary.FileWriter("logs/", sess.graph)
sess.run(init)
打開tensorboard的步驟:
1.首先運行代碼生成log文件之後,activate虛擬環境。
2.然後,在terminal中cd到tensorboard所在的文件夾下。
3.再運行tensorboard --logdir logs
即可。
4.複製得到的這個鏈接,再瀏覽器中打開即可。
··········································································································
實踐證明上述方法目前來看只是能獲得一個鏈接而已,但是似乎並沒有看到和莫煩大佬視頻中類似的效果。並且第二段代碼如下(雖說失敗了,但是先記錄一下,之後再看):
(之後再看點開,解決方法就可以)
from __future__ import print_function
import tensorflow as tf
import numpy as np
def add_layer(inputs, in_size, out_size, n_layer, activation_function=None):
# 增加一層隱藏層,返回隱藏層的輸出值
layer_name = 'layer %s' % n_layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random.normal([in_size, out_size]), name='W')
tf.compat.v1.summary.histogram(layer_name + 'weights', Weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b') # 加0.1使得初始化片偏差不爲零
tf.compat.v1.summary.histogram(layer_name + '/biases', biases)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
tf.compat.v1.summary.histogram(layer_name + '/outputs', outputs)
return outputs
# Make up some real data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
# define placeholder for inputs for network
with tf.name_scope('inputs'):
xs = tf.compat.v1.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.compat.v1.placeholder(tf.float32, [None, 1], name='y_input')
# add hidden layer
l1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, n_layer=2, activation_function=None)
# define the error between prediction and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
tf.compat.v1.summary.scalar('loss', loss)
with tf.name_scope('train'):
train_step = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.compat.v1.Session()
merged = tf.compat.v1.summary.merge_all()
# tf.train.SummaryWriter soon be deprecated, use following
writer = tf.compat.v1.summary.FileWriter("logs/", sess.graph)
# initialize_all_variables()
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
# 訓練2000次
for i in range(2000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
result = sess.run(merged, feed_dict={xs: x_data, ys: y_data})
writer.add_summary(result, i)
二、C++
1.指針(pointer)和引用(reference)的區別
指針是一個變量,只不過這個變量存儲的是一個地址,指向內存的一個存儲單元;而引用跟原來的變量實質上是同一個東西,只不過是原變量的一個別名而已。具體參考這篇文章。
2.明確堆(heap)和棧(stack)的區別
heap:是由new之類函數分配的空間所在地,需要通過delete釋放。地址是由低向高增長的。
stack:是自動分配變量,以及函數調用的時候所使用的一些空間。地址是由高向低減少的。
近期學習總結:
1.C++可以:
按照北大mooc所講順序,看入門書籍,當看書喫力可以結合視頻,互補學習。
2.強化學習可以繼續按照:
1.學習tensorflow(學到Saver那節先)。
2.運行《python強化學習實戰》六、七章代碼。
3.then:進行DQN(莫煩視頻and兩本書)。