在《【TensorFlow】TensorBoard的使用(一)》中讲述了如何简单的使用TensorBoard,但是生成的图比较乱,本文旨在对生成的图进行优化。
优化主要分为如下步骤:
本文在《【TensorFlow】TensorBoard的使用(一)》示例的基础上对TensorBoard进行优化。
实现以上两步优化,代码如下:
mnist_board_2.py:
import os
import tensorflow as tf
LOGDIR = './mnist'
mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + 'data', one_hot=True)
# 加上name值,方便在tensorboard里面查看
def conv_layer(input, size_in, size_out, name='conv'):
# 定义名字作用域
with tf.name_scope(name):
w = tf.Variable(tf.truncated_normal([5, 5, size_in, size_out], stddev=0.1), name='W')
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name='B')
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding='SAME')
act = tf.nn.relu(conv + b)
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def fc_layer(input, size_in, size_out, name='fc'):
with tf.name_scope(name):
w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name='W')
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name='B')
act = tf.nn.relu(tf.matmul(input, w) + b)
return act
def mnist_model(learning_rate, use_two_conv, use_two_fc, hparam):
tf.reset_default_graph()
sess = tf.Session()
# setup placeholders, and reshape the data
x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
x_image = tf.reshape(x, [-1, 28, 28, 1])
y = tf.placeholder(tf.float32, shape=[None, 10], name='labels')
if use_two_conv:
conv1 = conv_layer(x_image, 1, 32, 'conv1')
conv_out = conv_layer(conv1, 32, 64, 'conv2')
else:
conv1 = conv_layer(x_image, 1, 64, 'conv')
conv_out = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
flattened = tf.reshape(conv_out, [-1, 7 * 7 * 64])
if use_two_fc:
fc1 = fc_layer(flattened, 7 * 7 * 64, 1024, 'fc1')
embedding_input = fc1
embedding_size = 1024
logits = fc_layer(fc1, 1024, 10, 'fc2')
else:
embedding_input = flattened
embedding_size = 7 * 7 * 64
logits = fc_layer(flattened, 7 * 7 * 64, 10, 'fc')
with tf.name_scope('loss'):
xent = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y), name='loss')
# tf.summary.scalar('loss', xent)
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# tf.summary.scalar('accuracy', accuracy)
# summ = tf.summary.merge_all()
emdedding = tf.Variable(tf.zeros([1024, embedding_size]))
# assignment = emdedding.assign(embedding_input)
sess.run(tf.global_variables_initializer())
# 保存路径
tenboard_dir = './tensorboard/test2/'
# 指定一个文件用来保存图
writer = tf.summary.FileWriter(tenboard_dir + hparam)
# 把图add进去
writer.add_graph(sess.graph)
for i in range(2001):
batch = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})
def make_hparam_string(learning_rate, use_two_fc, use_two_conv):
conv_param = 'conv=2' if use_two_conv else 'conv=1'
fc_param = 'fc=2' if use_two_fc else 'fc=1'
return 'lr_%.0E,%s,%s' % (learning_rate, conv_param, fc_param)
def main():
# You can try adding some more learning rates
for learning_rate in [1E-4]:
# Include 'False' as a value to try different model architectures.
for use_two_fc in [True]:
for use_two_conv in [True]:
# Construct a hyperparameter string for each one(example: 'lr_1E-3,fc=2,conv=2')
hparam = make_hparam_string(learning_rate, use_two_fc, use_two_conv)
print('Starting run for %s' % hparam)
# Actually run with the new settings
mnist_model(learning_rate, use_two_fc, use_two_conv, hparam)
if __name__ == '__main__':
main()
执行上述程序,生成test2文件夹,运行TensorBoard,显示如下:
由上图可以很明显的看出,输入的x经过reshape操作后,执行conv1-->conv2-->fc1-->fc2操作,最后经过和标签labels相比对,生成loss、accuracy值,图中也展示了train环节的各张量的维度。
上图虽然能清晰的展示出train的流程,但是其展示的结果较少,用户想了解权重w和偏置b的分布情况、损失loss和精度accuracy的变化情况等,都查看不了。
下篇文章讲述如何针对上述情况进行优化——丰富TensorBoard的展示结果。