tensorboard

import tensorflow as tf
#在2.X版本的tensorflow使用1.x版本的tenorflow
import numpy as np
#在2.X版本的tensorflow使用1.x版本的tenorflow
#activation_function=None为Linear线性函数
#添加神经层函数
def add_layer(inputs,in_size,out_size,n_layer,activation_function=None):
    layer_name = 'layer%s' % n_layer
    with tf.name_scope(layer_name):  # 创建layer框架
        with tf.name_scope('weights'): # 创建Weights框架
            Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='W')
            tf.summary.histogram(layer_name+'/weights',Weights)#观看变化的变量
            #生成随机变量矩阵有in_size行out_size列
        with tf.name_scope('biases'):
            biases=tf.Variable(tf.zeros([1,out_size])+0.1,name='b')
            tf.summary.histogram(layer_name + '/biases', biases)
             #1行out_size列的数值为0.1
        with tf.name_scope('Wx_plus_b'):
            #矩阵乘法
            Wx_plus_b=tf.matmul(tf.cast(inputs,tf.float32),Weights)+biases
        if activation_function is None:
            outputs=Wx_plus_b
        else:
            outputs=activation_function(Wx_plus_b)
            tf.summary.histogram(layer_name + '/outputs', outputs)
        return outputs
#[-1,1]区间有300个单位 功能:在指定的间隔内返回均匀间隔的数字。
##x_data 输入层
x_data=np.linspace(-1,1,300)[:,np.newaxis]
#np.newaxis 插入新维度
#np.random.normal(均值,方差,输出size)正态分布
noise=np.random.normal(0,0.05,x_data.shape)
y_data=np.square(x_data)-0.5+noise
#x的值
with tf.name_scope('inputs'):#创建inputs框架
    xs=tf.placeholder(tf.float32,[None,1],name='x_input')#行不定列为1
    #tf.placeholder(dtype,shape=None,name=None)
    #y的值
    ys=tf.placeholder(tf.float32,[None,1],name='y_input')
##隐藏层l1 input=1 output=10
l1=add_layer(x_data,1,10,n_layer=1,activation_function=tf.nn.relu)
##输出层predition
prediction=add_layer(l1,10,1,n_layer=2,activation_function=None)
#square平方reduce_sum求和之后reduce_mean求平均值
#reduction_indices=[1]维度为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)#显示loss
with tf.name_scope('train'):
    train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)#学习效率为0.1(<1) minimize(loss)减小误差
sess=tf.Session()
mereged=tf.summary.merge_all()# 打包所有的summary
writer=tf.summary.FileWriter('logs/',sess.graph)#graph是全部框架
init=tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if i%50==0:
        result=sess.run(mereged,feed_dict={xs:x_data,ys:y_data})
        writer.add_summary(result,i)#i为记录步数
#生成文件在pycharm目录下
#右键文件logsopen in terminal       cd.. 返回logs的上级
# C:\Users\yanqi\PycharmProjects\tensorflow1.0>tensorboard --logdir=logs --host=127.0.0.1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值