TensorFlow学习笔记--mnist可视化版本

主要代码tensorflow的官网上都有,这个版本主要是增加了一些可视化的东西。方便观察一些变量。

注:以下代码为1.0版本下

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
sess = tf.InteractiveSession()
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#set parameter
learning_rate=0.001
#set placeholder
with tf.name_scope('inputs'):
    x_input = tf.placeholder('float',[None,784],name='x_input')
    y_input = tf.placeholder('float',[None,10],name='y_input')
#set variable
with tf.name_scope('weights'):
    W = tf.Variable(tf.random_normal([784,10]), name='W')
    tf.summary.histogram('weights', W)
with tf.name_scope('b'):
    b = tf.Variable(tf.random_normal([10]),name='b')
    tf.summary.histogram('b', b)
#set graph
with tf.name_scope('layer'):
    pred_y = tf.nn.softmax(tf.matmul(x_input,W)+b)
    tf.summary.histogram('out',pred_y)
#set cost
with tf.name_scope('loss'):
    loss =-tf.reduce_sum(y_input*tf.log(pred_y))
    tf.summary.scalar('loss', loss)

with tf.name_scope('train'):
    train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
    
    
sess = tf.Session()
merged = tf.summary.merge_all()
# save the logs
train_writer = tf.summary.FileWriter("mnist_logs/train", sess.graph)
test_writer = tf.summary.FileWriter("mnist_logs/test", sess.graph)
#run
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(1000):
        batch_xs,batch_ys = mnist.train.next_batch(100)
        sess.run(train,feed_dict={x_input:batch_xs,y_input:batch_ys})
        if i % 50 == 0:
            correct_prediction = tf.equal(tf.argmax(pred_y,1),tf.argmax(y_input,1))
            correct_rate = tf.reduce_mean(tf.cast(correct_prediction,'float32'))
            print sess.run(loss,feed_dict={x_input:mnist.test.images,y_input:mnist.test.labels})
            print sess.run(correct_rate,feed_dict={x_input:batch_xs,y_input:batch_ys})
            train_result = sess.run(merged,feed_dict={x_input:batch_xs,y_input:batch_ys})
            train_writer.add_summary(train_result, i)
            test_result = sess.run(merged,feed_dict={x_input:mnist.test.images,y_input:mnist.test.labels})
            test_writer.add_summary(test_result, i)


可以看到一个logs的文件,然后在命令窗口中打以下命令

$ tensorboard --logdir=mnist_logs

打开链接 http://0.0.0.0:6006







  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值