首页 > 其他 > 详细

【4-1】Tensorboard网络结构

时间:2019-06-05 21:54:10      阅读:89      评论:0      收藏:0      [点我收藏+]

一、代码

在之前的基础之上,多加了tf.name_scope()函数,相当于给它起名字了,就可以在Tensorboard中可视化出来。

 1 import tensorflow as tf
 2 from tensorflow.examples.tutorials.mnist import input_data
 3 
 4 #载入数据集
 5 mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
 6 
 7 #每个批次的大小
 8 batch_size = 100
 9 #计算一共有多少个批次
10 n_batch = mnist.train.num_examples // batch_size
11 
12 #命名空间
13 with tf.name_scope(input):
14     #定义两个placeholder
15     x = tf.placeholder(tf.float32,[None,784],name=x-input)
16     y = tf.placeholder(tf.float32,[None,10],name=y-input)
17 
18     
19 with tf.name_scope(layer):
20     #创建一个简单的神经网络
21     with tf.name_scope(wights):
22         W = tf.Variable(tf.zeros([784,10]),name=W)
23     with tf.name_scope(biases):    
24         b = tf.Variable(tf.zeros([10]),name=b)
25     with tf.name_scope(wx_plus_b):
26         wx_plus_b = tf.matmul(x,W) + b
27     with tf.name_scope(softmax):
28         prediction = tf.nn.softmax(wx_plus_b)
29 
30 #二次代价函数
31 # loss = tf.reduce_mean(tf.square(y-prediction))
32 with tf.name_scope(loss):
33     loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
34 with tf.name_scope(train):
35     #使用梯度下降法
36     train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
37 
38 #初始化变量
39 init = tf.global_variables_initializer()
40 
41 with tf.name_scope(accuracy):
42     with tf.name_scope(correct_prediction):
43         #结果存放在一个布尔型列表中
44         correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
45     with tf.name_scope(accuracy):
46         #求准确率
47         accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
48 
49 with tf.Session() as sess:
50     sess.run(init)
51     writer = tf.summary.FileWriter(logs/,sess.graph)
52     for epoch in range(1):
53         for batch in range(n_batch):
54             batch_xs,batch_ys =  mnist.train.next_batch(batch_size)
55             sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
56         
57         acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
58         print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

然后会在当前路径下,生成logs文件夹和里面的文件,在命令行输入命令:tensorboard --logdir=所在路径

技术分享图片

在谷歌浏览器上打开链接就能看到结果了。

参考:https://www.cnblogs.com/fydeblog/p/7429344.html

 

【4-1】Tensorboard网络结构

原文:https://www.cnblogs.com/direwolf22/p/10975713.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!