TensorBoard網路執行

2021-08-15 21:52:35 字數 3092 閱讀 1442

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# 載入資料集

mnist = input_data.read_data_sets("mnist_data", one_hot=true)

# 每個批次的大小

batch_size = 100

# 計算一共有多少個批次

n_batch = mnist.train.num_examples // batch_size

defvariable_summaries

(var):

with tf.name_scope("summaries"):

mean = tf.reduce_mean(var)

tf.summary.scalar('mean', mean) # 平均值

with tf.name_scope('stddev'):

stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))

tf.summary.scalar('stddev', stddev) # 標準差

tf.summary.scalar('max', tf.reduce_max(var)) # 最大值

tf.summary.scalar('min', tf.reduce_min(var)) # 最小值

tf.summary.histogram('histogram', var) # 直方圖

# 定義命名空間

with tf.name_scope('input'):

# 定義兩個placeholder

x = tf.placeholder(tf.float32, [none, 784], name='x-input')

y = tf.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')

variable_summaries(w)

with tf.name_scope('biases'):

b = tf.variable(tf.zeros([1, 10]), name='b')

variable_summaries(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))

tf.summary.scalar('loss', loss)

with tf.name_scope('train'):

# 交叉熵

# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels= y, logits= prediction))

# 定義梯度下降法

train_step = tf.train.gradientdescentoptimizer(0.2).minimize(loss)

# 初始化變數

init = tf.global_variables_initializer()

with tf.name_scope('accuracy'):

with tf.name_scope('correct_prediction'):

# 結果儲存在乙個布林型列表中

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction, 1))

with tf.name_scope('accuracy'):

# 求準確率

accuracy= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

tf.summary.scalar('accuracy', accuracy)

# 合併所有的summary

tensorboard網路結構

import tensorflow as tf import numpy as np out tf.layers.conv2d x,filter nums,filter size,use bias false,kernel initializer tf.truncated normal initia...

5 2Tensorboard網路結構

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data 載入資料集 mnist input data.read data sets mnist data one hot true 定義每個批...

tensorboard使用 視覺化神經網路結構

直接解除安裝tensorboard和tensorboard plugin wit pip uninstall tensorboard pip uninstall tensorboard plugin wit 然後重灌tensorboard,注意 這裡自動安裝tensorboard plugin wi...