zoukankan      html  css  js  c++  java
  • tensorboard可视化

     1 import tensorflow as tf
     2 import numpy as np
     3 import matplotlib.pyplot as plt
     4 def add_layer(inputs, in_size, out_size,n_layer,activation_function=None):
     5     layer_name='layer%s'%n_layer
     6     with tf.name_scope('layer'):
     7         with tf.name_scope('weights'):
     8             Weights = tf.Variable(tf.random_normal([in_size, out_size]),name='W')
     9             tf.summary.histogram(layer_name+'/weights',Weights)
    #tensorflow中提供了
    tf.summary.histogram()方法,用来绘制图片, 第一个参数是图表的名称, 第二个参数是图表要记录的变量

    10 with tf.name_scope('biases'):
    11 biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,name='b')
    12 tf.summary.histogram(layer_name+'/biases',biases)
    13 with tf.name_scope('Wx_plus_b'):
    14 Wx_plus_b = tf.matmul(inputs, Weights) + biases
    15 if activation_function is None:
    16 outputs = Wx_plus_b
    17 else:
    18 outputs = activation_function(Wx_plus_b)
    19 tf.summary.histogram(layer_name+'/outputs',outputs)
    20 return outputs
    22 x_data=np.linspace(-1,1,300,dtype=np.float32)[:,np.newaxis]
    23 noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
    24 y_data=np.square(x_data)-0.5+noise 25 with tf.name_scope('inputs'):
    26 xs=tf.placeholder(tf.float32,[None,1],name='x_input')
    27 ys=tf.placeholder(tf.float32,[None,1],name='y_input')
    29 l1=add_layer(xs,1,10,n_layer=1,activation_function=tf.nn.relu) #隐藏层
    30 prediction=add_layer(l1,10,1,n_layer=2,activation_function=None) #输出层
    31 with tf.name_scope('loss'):
    32 loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),
    33 reduction_indices=[1]))
    34 tf.summary.scalar('loss',loss)
    #Loss的变化图和之前设置的方法略有不同.loss是在tesnorBorad 的scalars下面的, 这是由于我们使用的是tf.summary.scalar()方法.
    35 with tf.name_scope('train'): 
    36 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    37 init = tf.global_variables_initializer()
    38 sess = tf.Session()
    39 merged=tf.summary.merge_all()
    #tf.summary.merge_all()方法会对我们所有的summary合并到一起. 

    40 writer=tf.summary.FileWriter('logs/',sess.graph)
    41 sess.run(init)
    42 for i in range(1000):
    43 sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    44 if i%50==0:
    45 result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
    46 writer.add_summary(result,i)
  • 相关阅读:
    使用Python创建简单的HTTP和FTP服务
    Xps实现文档显示、套打功能
    时间测试
    微信公众账号 开发教程
    竞赛快速及常用(后续更新)
    第十届蓝桥杯JavaC组省赛真题
    第十届蓝桥杯JavaC组省赛真题
    第十届蓝桥杯JavaC组省赛真题
    C# Winform学习(六)
    C# Winform学习(六)
  • 原文地址:https://www.cnblogs.com/Lazycat1206/p/11927666.html
Copyright © 2011-2022 走看看