Tensorflow-tensorboard展現網絡結構

tensorboard展現網絡結構

代碼

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tf.compat.v1.disable_eager_execution()

#載入數據集
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)

#每一個批次大小
batch_size=100
#計算一共有多少個批次
n_bath=mnist.train.num_examples // batch_size
print(n_bath)

with tf.name_scope('input'):
    #定義兩個placeholder
    x=tf.compat.v1.placeholder(tf.float32,[None,784],name='x-input')
    y=tf.compat.v1.placeholder(tf.float32,[None,10],name='y-input')


with tf.name_scope('layer'):
    #建立一個簡單的神經網絡
    with tf.name_scope('wights'):
        W=tf.Variable(tf.zeros([784,10]),name='W')
    with tf.name_scope('biases'):
        b=tf.Variable(tf.zeros([10]),name='b')
    with tf.name_scope('wx_plus_b'):
        wx_plus_b=tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        prediction=tf.nn.softmax(wx_plus_b)


with tf.name_scope('loss'):
    #二次代價函數
    loss=tf.reduce_mean(tf.square(y-prediction))

with tf.name_scope('train'):
    #梯度降低
    train_step=tf.compat.v1.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化變量
init=tf.compat.v1.global_variables_initializer()

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        #結果存放在一個布爾型列表中
        #返回的是一系列的True或False argmax返回一維張量中最大的值所在的位置,對比兩個最大位置是否一致
        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    with tf.name_scope('accuracy'):
        #求準確率
        #cast:將布爾類型轉換爲float,將True爲1.0,False爲0,而後求平均值
        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))


with tf.compat.v1.Session() as sess:
    sess.run(init)
    writer=tf.compat.v1.summary.FileWriter('logs/',sess.graph)
    for epoch in range(1):
        for batch in range(n_bath):
            #得到一批次的數據,batch_xs爲圖片,batch_ys爲圖片標籤
            batch_xs,batch_ys=mnist.train.next_batch(batch_size)
            #進行訓練
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        #訓練完一遍後,測試下準確率的變化

        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print("Iter "+str(epoch)+",Testing Accuracy "+str(acc))

會生成logs/目錄,而且目錄下的文件咱們須要這樣子打開python

打開圖的方法

1.找到「tensorboard.exe」所在目錄(找不到直接用搜索,通常在python運行環境目錄中),輸入cmd回車打開。

 

 

2.找到運行程序的日誌輸出路徑(通常在項目目錄中),複製路徑地址。

3.cmd框中鍵入命令:tensorboard --logdir=日誌路徑地址,回車,把紅框內的網址複製到瀏覽器打開,搞定。

 

 

 圖形化展現

點擊對應的模塊,會展現詳細的數據信息以及相應的結構展現瀏覽器

相關文章
相關標籤/搜索