TensorFlow学习(9)——tensorboard name_scope的使用

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


# 第一步、载入训练数据
mnist = input_data.read_data_sets(train_dir="../MNIST_data", one_hot=True)

# 设置批次大小
batch_size = 5 # 可以优化的地方,修改批次的数量
# 计算一共有多少个批次
n_batch = mnist.train.num_examples

# 第二步、定义神经网络模型
# 输入层
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("Layout"):
    with tf.name_scope("weight"):
        w = tf.Variable(tf.zeros([784, 10]), name="w")
    with tf.name_scope("biases"):
        b = tf.Variable(tf.zeros([10]), name="b")
    with tf.name_scope("prediction"):
        prediction = tf.nn.softmax(tf.matmul(x, w)+b, name="prediction")

# 第三、定义代价函数
loss = tf.reduce_mean(tf.square(y-prediction)) # 可以优化的地方,修改代价函数

# 第四、定义优化函数
optimiter = tf.train.GradientDescentOptimizer(0.2) # 可以优化的地方,使用其他优化方法,可以修改步长

# 第五、最小化代价函数
train = optimiter.minimize(loss)

# 判断每一个预测是否准确:equal()是对比两个参数,arg_max是求出一维向量中最大的值所在的位置。返回布尔值
correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1))
# 先把布尔值转为浮点型,统计准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 第六、初始化变量、开始训练
init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    write = tf.summary.FileWriter("log/", sess.graph)
    # 迭代21个周期,其实就是全部数据训练21次
    for epoch in range(1):
        # 循环n_batch个批次,每次一百
        for batch in range(n_batch):
            batch_xz, batch_yz = mnist.train.next_batch(batch_size)
            result = sess.run(train, feed_dict={x:batch_xz, y:batch_yz})
            print("---batch--->"+str(batch)+"---loss--->"+str(sess.run(loss, feed_dict={x:batch_xz, y:batch_yz})))

        acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})
        print("=======epoch=====>"+str(epoch)+"=====acc======>"+str(acc))

结束线/

欢迎大家加入Q群讨论:463255841

结束线/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值