Tensorflow学习:Tensorboard网络结构

Tensorflow学习:Tensorboard网络结构

定义一个命名空间:with tf.name_scope(‘input’):

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

# 载入数据集
mnist = input_data.read_data_sets("MNST_data", one_hot=True)

# 每个批次的大小:一次性向神经网络中放入100张图片进行训练:以矩阵的形式放进去
batch_size = 100
# 计算一共多少个批次 //:整除
n_batch = mnist.train.num_examples // batch_size

# 定义一个命名空间
with tf.name_scope('input'):
    # 定义两个placeholder  None:过会以一个批次喂进去,784:28*28(把一个图片拉成一个784的向量) 10:输出时0-9的10个数
    x = tf.placeholder(tf.float32, [None, 784], name="x_input")
    y = tf.placeholder(tf.float32, [None, 10], name="y_input")

# 定义一个简单的神经网络(只有输入层784个神经元,输出层10个神经元,不用隐层)
with tf.name_scope('layer'):
    # 定义命名空间里的命名空间
    with tf.name_scope('weights'):
        # 定义权值
        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('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W)+b
    with tf.name_scope('softmax'):
        # 通过softmax函数转化为概率值
        prediction = tf.nn.softmax(wx_plus_b)

# 二次代价函数
with tf.name_scope('loss'):
    # loss = tf.reduce_mean(tf.square(y - prediction))
    # 对数似然代价函数(加快收敛的速度):label:真实标签值,logits:预测值,需要求下平均值
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
with tf.name_scope('train'):
    # 使用随机梯度下降法进行优化:是loss最小化,学习率:0.2
    # train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
    # 更换优化器
    train_step = tf.train.AdamOptimizer(0.001).minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        # 判断真实值与预测值是否相同(布尔类型),相同返回true
        # tf.arg_max(prediction,1):求概率最大的数在哪个位置,相当于他的标签(返回一维张量中最大的值所在的位置)
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.arg_max(prediction, 1))
    with tf.name_scope('accuracy'):
        # 求准确率:cast:将bool类型转换成32位float类型,然后求一个平均值(true=1,false=0)
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 进行训练
with tf.Session() as sess:
    # 初始化变量
    sess.run(init)
    #相对路径
    writer = tf.summary.FileWriter('logs/', sess.graph)
    # 训练次数:迭代1次
    for step in range(1):
        for batch in range(n_batch):
            # 获得一个批次:100张图片
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # 把数据喂给它进行训练
            sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
        # 进行完一次迭代训练打印出准确率:测试用的测试集中的图片和标签
        acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        print("Iter" + str(step) + ",Testing Accuracy:" + str(acc))

cmd:进入输出文件的路径

C:\Windows\system32>d:
D:\>cd D:\workspace\PyCharm\venv\cuiyongling\logs
D:\workspace\PyCharm\venv\cuiyongling\logs>tensorboard --logdir=D:\workspace\PyCharm\venv\cuiyongling\logs
D:\workspace\PyCharm\venv\cuiyongling\logs>tensorboard --logdir=D:\workspace\PyCharm\venv\cuiyongling\logs
d:\program files\python\lib\site-packages\numpy\_distributor_init.py:32: UserWarning: loaded more than 1 DLL from .libs:
d:\program files\python\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
d:\program files\python\lib\site-packages\numpy\.libs\libopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll
  stacklevel=1)
2020-08-03 21:20:44.471769: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-03 21:20:44.476755: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.3.0 at http://localhost:6006/ (Press CTRL+C to quit)

用谷歌浏览器打开: http://localhost:6006/
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值