tensorflow1.1/tensorboard可视化

环境:python 3 , tensorflow 1.1

#coding:utf-8
"""
python 3
tensorflow 1.1
"""
import tensorflow as tf
import numpy as np

#设置随机种子
tf.set_random_seed(100)
np.random.seed(100)

#数据
x = np.linspace(-1,1,100)[:,np.newaxis]
noise = np.random.normal(0,0.1,x.shape)
y = np.power(x,3) + noise

#输入可视化(tf.name_scope())
with tf.variable_scope('inputs'):
    xs = tf.placeholder(tf.float32,x.shape,name='x')
    ys = tf.placeholder(tf.float32,y.shape,name='y')

#神经网络可视化
with tf.variable_scope('neural_network'):
    l1 = tf.layers.dense(xs,10,tf.nn.relu,name='hidden_layer')
    output = tf.layers.dense(l1,1,name='output_layer')
    #变量值统计
    tf.summary.histogram('layer1',l1)
    tf.summary.histogram('output',output)

#计算误差scope = 'loss'
loss = tf.losses.mean_squared_error(y,output,scope='loss')
#梯度下降
train = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)
#loss的统计用tf.summary.scalar()
tf.summary.scalar('loss',loss)

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    #tf.summary.merge_all()将所有统计信息合并(tf.summary.histogram,tf.summary_scalar)
    merged = tf.summary.merge_all()
    #tf.summary.FileWriter()将所有信息写入文件
    writer = tf.summary.FileWriter('./tensorflow1.1_logs',sess.graph)   
    for step in range(100):
        #merged也要训练
        _,result = sess.run([train,merged],feed_dict={xs:x,ys:y})
        writer.add_summary(result,step)

运行结果

这里写图片描述

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值