Tensorflow_v1学习笔记四 tensorboard

目前还是旧版的学习笔记
另外:tensorflow2.1 不知道为什么 tensorboad总是空白 试过各种处理和浏览器 会出现空白 重新装tensorflow1.5 才能显示 另外chorme ok 其他浏览器改为极速模式可以ok 改为ie不ok

1 命名 tf.name_scope()

先使用with tf.name_scope(NAME)
然后 在定义变量时 可加上name=名字

with tf.name_scope('inputs'):
    xs=tf.placeholder(tf.float32,[None,1],name='x_inputs')

2 保存

‘./logs/’ 是保存路径

writer=tf.summary.FileWriter('./logs/',sess.graph)

3 运行

命令行

tensorboard --logdir=logs --host=127.0.0.1

在这里插入图片描述

4 scalar和histogram

两种看板
merge也需要run run之后增加到图里

tf.summary.histogram('b',biases) #图名字 数值
tf.summary.scalar('loss',loss)#名字 数值

merged=tf.summary.merge_all()#把hist合并
#注意run merged才能记录
result=sess.run(merged,feed_dict={xs:x,ys:y})
writer.add_summary(result,i)#每隔50次 记录一下

在这里插入图片描述
在这里插入图片描述

5代码

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow.compat.v1 as tf #使用tf的v1版本
tf.disable_v2_behavior() #使用tf的v1版本
import numpy as np


#输入 输入的size 输出的size 激活函数
def add_layer(inputs,in_size,out_size,activation_function=None):
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')
            tf.summary.histogram('w',Weights)
        with tf.name_scope('biases'):
            biases=tf.Variable(tf.zeros([1,out_size])+0.1,name ='b')    
            tf.summary.histogram('b',biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b=tf.matmul(inputs,Weights)+biases
        if activation_function is None:
            outputs=Wx_plus_b
        else:
            outputs=activation_function(Wx_plus_b)
        tf.summary.histogram('o',outputs)
        return outputs


#造数据
x = np.linspace(-1, 1, 100)[:, np.newaxis] #将一行转为一列
noise = np.random.normal(0, 0.1, size=x.shape)
y = np.power(x, 2) + noise   

with tf.name_scope('inputs'):
    xs=tf.placeholder(tf.float32,[None,1],name='x_inputs')
    ys=tf.placeholder(tf.float32,[None,1],name='y_inputs')

l1=add_layer(xs,1,10,activation_function=tf.nn.relu) #隐藏层 10维
prediction=add_layer(l1,10,1,activation_function=None) #输出层 1维

with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)#scalar和histogram在两个看板里
with tf.name_scope('train'):
    train_step=tf.train.GradientDescentOptimizer(0.5).minimize(loss)

merged=tf.summary.merge_all()#把hist合并

#init=tf.initialize_all_variables() #已经不适用
init=tf.global_variables_initializer() 
sess=tf.Session()
sess.run(init)
writer=tf.summary.FileWriter('./logs/',sess.graph)

for i in range(1001):
    sess.run(train_step,feed_dict={xs:x,ys:y})
    if i%50==0:
        #merge也需要run
        result=sess.run(merged,feed_dict={xs:x,ys:y})
        writer.add_summary(result,i)#每隔50次 记录一下
        #print(sess.run(loss,feed_dict={xs:x,ys:y}))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值