10分钟搞懂TensorBoard用法

作者: 林檎果 | 来源:发表于2018-03-21 11:23 被阅读58次

    基本用法

    启动采集器,将运行session环境内的参数都保存到文件里,后续就可以用

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        file_writer = tf.summary.FileWriter('./logs/1', sess.graph)
    

    后续通过TensorBoard打开这个文件,查看这个session的模型,运行

    tensorboard --logdir=./logs/1
    

    打开浏览器,通常是通过本机的6006端口访问

    tensorboard

    对模型归类

    在session中,对模型做归类

     with tf.name_scope("RNN_layers”):
            lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
            drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
            cell = tf.contrib.rnn.MultiRNNCell([drop] * num_layers)
    
    with tf.name_scope(“RNN_layers”)
    

    查看session文件效果如图


    tensorboard name space

    采集运行信息

     with tf.name_scope('logits’):
            softmax_w = tf.Variable(tf.truncated_normal((lstm_size, num_classes), stddev=0.1),
                                   name=‘softmax_w’)
            softmax_b = tf.Variable(tf.zeros(num_classes), name=‘softmax_b’)
            logits = tf.matmul(output, softmax_w) + softmax_b
            tf.summary.histogram('softmax_w', softmax_w)
            tf.summary.histogram('softmax_b', softmax_b) #以直方图采集权重 
    ….
    merged = tf.summary.merge_all() #收集全部采集点
    …..
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        train_writer = tf.summary.FileWriter('./logs/2/train', sess.graph)
        ….
        summary, batch_loss, new_state, _ = sess.run([model.merged, model.cost, 
                                                              model.final_state, model.optimizer], 
                                                              feed_dict=feed) #运行采集点的收集器merge
        ….
        train_writer.add_summary(summary, iteration) #采集到的信息写入文件
    
    tensorboard histogram

    比较模型的不同参数,调参用

    epochs = 20
    batch_size = 100
    num_steps = 100
    train_x, train_y, val_x, val_y = split_data(chars, batch_size, num_steps)
    
    for lstm_size in [128,256,512]:
        for num_layers in [1, 2]:
            for learning_rate in [0.002, 0.001]:
                log_string = 'logs/4/lr={},rl={},ru={}'.format(learning_rate, num_layers, lstm_size) #每一对参数写入一个文件
                writer = tf.summary.FileWriter(log_string)
                model = build_rnn(len(vocab), 
                        batch_size=batch_size,
                        num_steps=num_steps,
                        learning_rate=learning_rate,
                        lstm_size=lstm_size,
                        num_layers=num_layers)
                
                train(model, epochs, writer)#每个文件用采集器收集信息
    

    对每个参数配置做记录,最终可以得到他们之间对比的图案


    image

    总结:

    • 图形做归类: with tf.name_scope('logits’):
    • 埋点:tf.summary.histogram('softmax_w', softmax_w)
    • 打包点:merged = tf.summary.merge_all()
    • 设置读写文件: train_writer = tf.summary.FileWriter('./logs/2/train', sess.graph)
    • 运行时做记录:train_writer.add_summary(summary, iteration)

    关于我:

    linxinzhe,全栈工程师,目前供职于某500强通信企业。人工智能,区块链爱好者。

    GitHub:https://github.com/linxinzhe

    欢迎留言讨论,也欢迎关注我~
    我也会关注你的哦!

    相关文章

      网友评论

        本文标题:10分钟搞懂TensorBoard用法

        本文链接:https://www.haomeiwen.com/subject/kvybqftx.html