import tensorflow as tf
import numpy as np

def add_layer(inputs,in_size,out_size,n_layer,activation_function=None):
# add one more layer and return the output of this layer
layer_name = 'layer%s' % n_layer
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(layer_name+'/weights',Weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs,Weights),biases)
tf.summary.histogram(layer_name+'/biases',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

# make up some real data
x_data =np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data)-0.5+noise

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

# create hidden layer
l1 = add_layer(xs,1,10,1,activation_function=tf.nn.relu)
# create output layer
prediction = add_layer(l1,10,1,2,activation_function=None)
# the error between prediction adn real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs/",sess.graph)

# import step
sess.run(tf.global_variables_initializer())

for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50 == 0:
result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
writer.add_summary(result,i)

莫烦tensorflow(6)-tensorboard的更多相关文章

  1. 莫烦tensorflow(9)-Save&Restore

    import tensorflow as tfimport numpy as np ##save to file#rember to define the same dtype and shape w ...

  2. 莫烦tensorflow(8)-CNN

    import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 dat ...

  3. 莫烦tensorflow(7)-mnist

    import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 dat ...

  4. 莫烦tensorflow(5)-训练二次函数模型并用matplotlib可视化

    import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt def add_layer(inputs,in_siz ...

  5. 莫烦tensorflow(4)-placeholder

    import tensorflow as tf input1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32) outpu ...

  6. 莫烦tensorflow(3)-Variable

    import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.ad ...

  7. 莫烦tensorflow(2)-Session

    import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tfmatrix1 = tf.constant([[3,3] ...

  8. 莫烦tensorflow(1)-训练线性函数模型

    import tensorflow as tfimport numpy as np #create datax_data = np.random.rand(100).astype(np.float32 ...

  9. tensorflow学习笔记-bili莫烦

    bilibili莫烦tensorflow视频教程学习笔记 1.初次使用Tensorflow实现一元线性回归 # 屏蔽警告 import os os.environ[' import numpy as ...

随机推荐

  1. Docker bridge-utils 工具简单部署

    bridge-utils 网桥查看工具 # 1.安装 查看桥接工具 yum install -y bridge-utils # 2.查看桥接 命令brctl show bridge name brid ...

  2. Angular7 表单

    Angular 表单 input.checkbox.radio. select. textarea 实现在线预约功能 html 文件 <h2>人员登记系统</h2> <d ...

  3. 在Linux下OpenCV的下载和编译

    原理上来说,和windows下没有差别,我们同样使用Cmake-GUI来解决问题. 我们推荐QT和OpenCV全部采用官方的方式重新安装一遍,否则可能会丢失一些模块,而这些都会降低开发效率. 1.参考 ...

  4. twitter ads_campaign management(图示)

    下载链接

  5. hdu 1895 Sum Zero hash

    Sum Zero Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Proble ...

  6. 页面Vue

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 浅谈String中的==和对象中引用对象类型的==

    @Test public void test02() { StringBuffer sb = new StringBuffer(); sb.append('a'); sb.append(11); Sy ...

  8. 【NET Core】.NET Core中读取json配置文件

    在.NET Framework框架下应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 Sy ...

  9. 省市区三级联选select2.js

    <div class="mui-input-row row_then" id='showCityPicker3'> <input id='cityResult3' ...

  10. style.width与offsetWidth的区别

    1. style.width只能读取内联样式,offsetWidth都可以读取: 2. style.width读取的值带“px”单位,offsetWidth读取纯数值: 3. style.width获 ...