tenflow数据集_Tensorflow实战-02-基于Mnist数据集实现Tensorboard可视化

实现Tensorboard可视化真的是踩坑无数,且耽误了很多时间,下面记录一下学习的总结和历程。供大家参考。

提取码:bcnx

Tensorflow官方推出了可视化工具Tensorboard,可以帮助我们实现以上功能,它可以将模型训练过程中的各种数据汇总起来存在自定义的路径与日志文件中,然后在指定的web端可视化地展现这些信息。

1. Tensorboard介绍

1.1 Tensorboard是什么

Tensorflow官方推出了可视化工具Tensorboard,它可以将模型训练过程中的各种数据汇总起来存在自定义的路径与日志文件中,然后在指定的web端可视化地展现这些信息Tensorboard相当于打开黑盒,是我们看清里面的信息

1.2 Tensorboard的数据形式

Tensorboard可以记录与展示以下数据形式:

(1)标量Scalars

(2)图片Images

(3)音频Audio

(4)计算图Graph

(5)数据分布Distribution

(6)直方图Histograms

(7)嵌入向量Embeddings

1.3 Tensorboard的使用方法

1.4 Tensorboard的组成及符号

总体上,目前 TensorBoard 主要包括下面几个面板:

TensorBoard 的工作原理是读取模型训练时产生的 TensorFlow events 文件,这个文件包括了一些 summary 数据(就是作图时用的数据)。

2. 基于Mnist数据集实现Tensorboard可视化操作

import os

import tensorflow as tf

import urllib

LOGDIR = './mnist/'

mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + 'data', one_hot=True)

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")

#w = tf.Variable(tf.zeros([5, 5, size_in, size_out]), 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)

tf.summary.histogram("weights", w)

#输出一个直方图的Summary protocol buffer .

tf.summary.histogram("biases", b)#加入某个参数的分布

tf.summary.histogram("activations", act)

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)

tf.summary.histogram("weights", w)

tf.summary.histogram("biases", b)

tf.summary.histogram("activations", act)

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])

tf.summary.image('input', x_image, 3)

#输出带图像的probuf,汇总数据的图像的的形式如下: ' tag /image/0', ' tag /image/1'...,如:input/image/0等

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)#tensorboard

summ = tf.summary.merge_all()#把所有要显示的东西聚到一起

embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding")

assignment = embedding.assign(embedding_input)

saver = tf.train.Saver()

sess.run(tf.global_variables_initializer())

tenboard_dir = './tensorboard/test3/'

writer = tf.summary.FileWriter(tenboard_dir + hparam)

writer.add_graph(sess.graph)

for i in range(2001):

batch = mnist.train.next_batch(100)

if i % 5 == 0:

[train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})

writer.add_summary(s, i)

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

3.Tensorboard产生的代码无法打卡

不知道抽什么风,复制地址总是打不开

要么是这样的

要么是这样的

这是自己电脑的

首先:activate tensorflow_gpuenv进入环境

然后选择位置盘

然后cd 到具体的文件加下

然后tensorboard--logdir=./test4

然后关闭wifi,复制地址到网页中打开

最终解决:关闭WIFI,可以打开网页了!!!

计算机小白表示不明白为什么这样看到这种图还是挺美的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值