import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt #
# add layer
#
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):
with tf.name_scope('Weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W') # hang lie
tf.summary.histogram(layer_name + '/weights', Weights)#保存成一个直方图,bin是取值
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name = 'b')
tf.summary.histogram(layer_name + '/biases', biases)#注意histogram的路径
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(layer_name + '/outputs', outputs)
return outputs
#
#make up some 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 #
#define placeholder
#
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') #add hidden layer
l1 = add_layer(xs, 1, 10, n_layer = 1,activation_function = tf.nn.relu)
#add output layer
prediction = add_layer(l1, 10, 1, n_layer = 2, activation_function = None) #the error between prediction and 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)#记录operation,是存储在scaler里的 with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) sess = tf.Session()
merged = tf.summary.merge_all() #所有的summary在merge以后,在一个run中就可执行
writer = tf.summary.FileWriter("logs/", sess.graph) #定义writer #import step
sess.run(tf.global_variables_initializer() ) #
# Session
# 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, # 否则要一个个run summary。
feed_dict = {xs:x_data, ys:y_data}) writer.add_summary(result, i)#按序列写入结果
print(sess.run(loss, feed_dict={xs:x_data, ys:y_data}))

  

莫烦TensorFlow_08 tensorboard可视化进阶的更多相关文章

  1. 莫烦TensorFlow_07 tensorboard可视化

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add_layer(inputs, in_ ...

  2. 莫烦TensorFlow_06 plot可视化

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add_layer(inputs, in_ ...

  3. 莫烦大大TensorFlow学习笔记(9)----可视化

      一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...

  4. tensorflow学习笔记-bili莫烦

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

  5. tensorflow 莫烦教程

    1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random ...

  6. Tensorflow 搭建神经网络及tensorboard可视化

    1. session对话控制 matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf.matmul(m ...

  7. scikit-learn学习笔记-bili莫烦

    bilibili莫烦scikit-learn视频学习笔记 1.使用KNN对iris数据分类 from sklearn import datasets from sklearn.model_select ...

  8. 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)

    莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...

  9. Tensorflow学习笔记3:TensorBoard可视化学习

    TensorBoard简介 Tensorflow发布包中提供了TensorBoard,用于展示Tensorflow任务在计算过程中的Graph.定量指标图以及附加数据.大致的效果如下所示, Tenso ...

随机推荐

  1. 《Vue 进阶系列之响应式原理及实现》

    https://www.bilibili.com/video/av51444410/?p=5 https://github.com/amandakelake/blog/issues/63 https: ...

  2. LG2495 「SDOI2011」消耗战 虚树

    问题描述 LG2495 题解 虚树 \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #define int l ...

  3. Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsigh

    java.lang.​ClassNotFo​undExcepti​on in matlabR2014a 就是MATLAB和pycharm不能同时运行.关闭pycharm然后打开MATLAB就可以了.

  4. Excel已损坏,无法打开

    突然之间,很多EXCEL文件打开时报错:"已损坏,无法打开",这些文件共同点是从邮件中下载而来,这些文件可能面临着安全威协,原来是软件设置了受保护的视图,取消即可.

  5. Linux宝塔安装步骤

    首先:先运行 X shell 一:输入命令 Centos安装命令(一般都用这个): yum install -y wget && wget -O install.sh http://d ...

  6. MySQL锁表查询SQL

    // 查看进程 SHOW PROCESSLIST; // 查看是否锁表 SHOW OPEN TABLES WHERE In_use > 0; // 查看正在锁的事务 SELECT * FROM ...

  7. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(五)

    直接贴代码了: NewsInfo 实体类: public class NewsInfo { public int NewsInfoId { get; set; } public string News ...

  8. ABAP 新语法-实例讲解

    主要内容 内联声明 构造表达式 内表操作 Open SQL 其他 本文列出了ABAP新语法的一些使用方式,供大家学习参考. 内联声明 代码实现: *&--------------------- ...

  9. Anchor 和 Dock 属性的使用

    Anchor 是一个常用属性,用来控制当窗体大小变化,控件如何自动调整自身大小和位置 一 仅设置一个值 如果此时将窗体放大,将会变成这样: 由于固定了top, 所以top不变,那么bottom自然会因 ...

  10. IntelliJ IDEA 中使用 Lambok (注解无效问题的解决)

    一,概述 Lambok可以说一个能很大提高开发效率的插件,只要在使用注解的方式就能实现很多常用的功能.如常用的@Data能在编译阶段自动生成toString方法,getter方法setter方法等. ...