您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何使用tensorboard展示神經(jīng)網(wǎng)絡(luò)的graph”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用tensorboard展示神經(jīng)網(wǎng)絡(luò)的graph”吧!
# 創(chuàng)建神經(jīng)網(wǎng)絡(luò), 使用tensorboard 展示graph import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 若沒有 pip install matplotlib # 定義一個神經(jīng)層 def add_layer(inputs, in_size, out_size, activation_function=None): #add one more layer and return the output of this layer with tf.name_scope('layer'): with tf.name_scope('Weights'): Weights = tf.Variable(tf.random_normal([in_size, out_size]),name='W') with tf.name_scope('biases'): biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,name='b') 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 #make up some real data x_data = np.linspace(-1, 1, 300)[:, np.newaxis] # x_data值為-1到1之間,有300個單位(例子),再加一個維度newaxis,即300行*newaxis列 noise = np.random.normal(0, 0.05, x_data.shape) # 均值為0.方差為0.05,格式和x_data一樣 y_data = np.square(x_data) - 0.5 + noise #define placeholder for inputs to network with tf.name_scope('inputs'): xs = tf.placeholder(tf.float32, [None, 1],name='x_input1') # none表示無論給多少個例子都行 ys = tf.placeholder(tf.float32, [None, 1],name='y_input1') # 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) #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])) # 對每個例子進(jìn)行求和并取平均值 reduction_indices=[1]指按行求和 with tf.name_scope('train'): train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 以0.1的學(xué)習(xí)效率對誤差進(jìn)行更正和提升 #兩種初始化的方式 #init = tf.initialize_all_variables() init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) #把整個框架加載到一個文件中去,再從文件中加載出來放到瀏覽器中查看 #writer=tf.train.SummaryWriter("logs/",sess.graph) #首先找到tensorboard.exe的路徑并進(jìn)入c:Anaconda\Scripts,執(zhí)行tensorboard.exe --logdir=代碼生成的圖像的路徑(不能帶中文) writer=tf.summary.FileWriter("../../logs/",sess.graph) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(x_data, y_data) plt.ion() plt.show() #show()是一次性的展示,為了使連續(xù)的展示,加入plt.ion() for i in range(1000): sess.run(train_step, feed_dict={xs: x_data, ys: y_data}) if i % 50 == 0: # to see the step improment 顯示實(shí)際點(diǎn)的數(shù)據(jù) # print(sess.run(loss,feed_dict = {xs:x_data,ys:y_data})) try: # 每次劃線前抹除上一條線,抹除lines的第一條線,由于lines只有一條線,則為lines[0],第一次沒有線 ax.lines.remove(lines[0]) except Exception: pass # 顯示預(yù)測數(shù)據(jù) prediction_value = sess.run(prediction, feed_dict={xs: x_data}) # 存儲 prediction_value 的值 lines = ax.plot(x_data, prediction_value, 'r-', lw=5) # 用紅色的線畫,且寬度為5 # 停止0.1秒后再畫下一條線 plt.pause(0.1)
生成的tensorboard的graph:
感謝各位的閱讀,以上就是“如何使用tensorboard展示神經(jīng)網(wǎng)絡(luò)的graph”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何使用tensorboard展示神經(jīng)網(wǎng)絡(luò)的graph這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。