TensorBoard-視覺化簡單例子
阿新 • • 發佈:2019-02-08
一、程式碼例子
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 18:50:24 2017
@author: YDD
"""
'''
生成一個檔案,儲存在‘logs/’檔案裡面(此處我的logs為:G:\Spyder_檔案集\logs)
然後在命令列終端(cmd)中執行該檔案(tensorboard --logdi='logs/')回車,拷貝網址(http://169.254.61.28:6006),選擇GRAPH,即可顯示
'''
import tensorflow as tf
#import numpy as np
#import matplotlib.pyplot as plt
sess = tf.Session()
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 #未被啟用的值,存在Wx_plus_b中。
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
#自己定義了所有的資料。
#x_data = np.linspace(-1,1,300)[:,np.newaxis] #建造輸入,-1到1區間300個單位【維度】,一個特性有300個例子。
#noise = np.random.normal(0,0.05,x_data.shape) #噪聲,方差是0.05,格式和x_data一樣。
#y_data = np.square(x_data)-0.5 + noise #輸出y=x^2 -0.5+noise.
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32,[None,1],name='x_input')
ys = tf.placeholder(tf.float32,[None,1],name='y_input')
#建立一個隱藏層hidden1(10個神經元),一個輸出層prediction(1個神經元)
hidden1 = add_layer(xs,1,10,activation_function=tf.nn.relu) #啟用函式選擇tf.nn.relu。(可嘗試其他啟用函式,選擇誤差結果較小最好)
prediction = add_layer(hidden1,10,1,activation_function=None)
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),
reduction_indices=[1])) #求一個預測值的平方誤差,然後將所有的平方誤差求和,最後求和的一個平均值。
with tf.name_scope('train_step'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) #學習的速率為0.1.
# 這裡注意和教程有點差別!! #選定視覺化儲存目錄
writer = tf.summary.FileWriter("logs/",sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
二、執行程式碼,生成檔案
三、命令列終端,找到該檔案路徑下,執行檔案
四、複製網址,併網頁開啟檢視視覺化結果
三、命令列終端,找到該檔案路徑下,執行檔案
四、複製網址,併網頁開啟檢視視覺化結果