20170514231642575

20170514231701560



20170514231805394


20170514231824749

20170514231840436

20170514231903729

20170514231923656





20170514232024790


"""一个简单的MNIST 分类器用来演示 TensorBoard 的 summaries 功能.

 虽然这不是一个非常好的MNIST model, 但是它可以很好的演示如何使用
tf.name_scope 来创建一个在 TensorBoard 计算图浏览器中非常容易读的图,
而且为summary tags 命名,使得它们在TensorBoard上有效的组合起来
这个例子 TensorBoard dashboard 的主要功能
"""
import argparse
import sys
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# 全局变量,用来存放基本的模型(超)参数.
FLAGS = None

# 创建一个带有合适的初始值的权重变量,不能把weights初始化成0,否则网络将会卡住(get stuck).
def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

# 创建一个带有合适的初始值的偏置变量.
def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

#对一个张量进行全面的汇总(均值,标准差,最大最小值,直方图)
# (用于 TensorBoard 可视化).
def variable_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)

#可重用代码来创建一个简单的神经网络层.
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
    """它在内部做一个矩阵乘法,偏置加法,然后使用relu来进行非线性输出映射.
    它也设置了一些名称域让产生的计算图有更好的可读性,而且添加了一些汇总节点.
    """
    # 添加一个name scope来保证当前层在计算图里的分组更加符合逻辑层次.
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            weights = weight_variable([input_dim, output_dim])
            variable_summaries(weights)
        with tf.name_scope('biases'):
            biases = bias_variable([output_dim])
            variable_summaries(biases)
        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            tf.summary.histogram('pre_activations', preactivate)
        activations = act(preactivate, name='activation')
        tf.summary.histogram('activations', activations)
        return activations

#创建日志文件夹,启动训练过程
def train():
    # 导入数据
    mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True, fake_data=FLAGS.fake_data)

    # 声明一个交互式会话
    sess = tf.InteractiveSession()

    # 输入占位符
    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 784], name='x-input')
        y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')

    with tf.name_scope('input_reshape'):
        image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
        tf.summary.image('input', image_shaped_input, 10)

    hidden1 = nn_layer(x, 784, 500, 'layer1')

    with tf.name_scope('dropout'):
        keep_prob = tf.placeholder(tf.float32)
        tf.summary.scalar('dropout_keep_probability', keep_prob)
        dropped = tf.nn.dropout(hidden1, keep_prob)

    # 这个地方不要使用 softmax 激活函数.,这个地方输出的y就是logits
    y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)

    #定义损失节点
    with tf.name_scope('cross_entropy'):
        diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
        with tf.name_scope('total'):
            cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

    #定义训练节点
    with tf.name_scope('train'):
        optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
        train_step = optimizer.minimize(cross_entropy)

    #定义评估节点
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):#当前批次样本的预测正确性
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        with tf.name_scope('accuracy'):#当前批次样本的预测正确率
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)

    # 整合所有汇总节点,将汇总事件文件写入日志logs/mnist_summaries (by default)
    merged = tf.summary.merge_all()
    #用来写入训练日志(训练日志的event文件中有计算图)
    train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
    # 用来写入测试日志
    test_writer = tf.summary.FileWriter(FLAGS.log_dir + '/test')

    #初始化所有变量
    tf.global_variables_initializer().run()

    #生成一个TensorFlow feed_dict: 将真实的张量数据映射到 Tensor placeholders.
    def feed_dict(train):
        if train or FLAGS.fake_data:
            xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data)
            k = FLAGS.dropout
        else:
            xs, ys = mnist.test.images, mnist.test.labels
            k = 1.0
        return {x: xs, y_: ys, keep_prob: k}

    #开启训练循环
    for step in range(FLAGS.max_steps):
        # 每隔10步, 计算测试集上的准确率, 然后将评估结果写入到测试event文件中
        if step % 10 == 0:
            summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
            test_writer.add_summary(summary, step)
            print('Accuracy at step %s: %s' % (step, acc))
        # 在所有其他步骤,在训练数据上执行训练节点 & 记录训练汇总信息
        else:
            if step % 100 == 99:  # 每隔100步跟踪记录一次执行状态
                run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()
                summary, _ = sess.run([merged, train_step],
                                      feed_dict=feed_dict(True),
                                      options=run_options,
                                      run_metadata=run_metadata)
                train_writer.add_run_metadata(run_metadata, 'step%03d' % step)
                train_writer.add_summary(summary, step)
                print('Adding run metadata for', step)
            else:  # Record a summary
                summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))
                train_writer.add_summary(summary, step)

    #最后要关闭FileWriter
    train_writer.close()
    test_writer.close()

#创建日志文件夹,启动训练过程
def main(_):
  if tf.gfile.Exists(FLAGS.log_dir):
    tf.gfile.DeleteRecursively(FLAGS.log_dir)
  tf.gfile.MakeDirs(FLAGS.log_dir)
  #启动训练过程
  train()

#用ArgumentParser类把模型的(超)参数全部解析到全局变量FLAGS里面
if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--fake_data', nargs='?', const=True, type=bool,
                      default=False,
                      help='If true, uses fake data for unit testing.')
  parser.add_argument('--max_steps', type=int, default=1000,
                      help='Number of steps to run trainer.')
  parser.add_argument('--learning_rate', type=float, default=0.001,
                      help='Initial learning rate')
  parser.add_argument('--dropout', type=float, default=0.9,
                      help='Keep probability for training dropout.')
  parser.add_argument('--data_dir', type=str,
                      default='MNIST_data/',
                      help='Directory for storing input data')
  parser.add_argument('--log_dir', type=str,
                      default='logs/mnist_with_summaries',
                      help='Summaries log directory')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)





20170514232941423




 收藏 (2)  打赏  点赞 (5)

爱读知行合一 6个月前


由于命令都是tf.summary.FileWriter,除了有没有计算图的区别,那训练日志和测试日志内容不是一样了嘛?

(0) 回复

Ouwen 回复 爱读知行合一 23天前

不一样的,你运行的merge信息不一样啊。

(0) 回复

菜鸟后飞 1年前


tf 1.2.0 版测试通过


(2) 回复

您可能需要以下文章!

友情介绍

powered by studyai.com 2017