Tensorflow 搭建神经网络及tensorboard可视化
1. session对话控制
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2) #类似于numpy的np.dot(m1,m2)
方法1:
sess = tf.Session()
result = sess.run(product)
print(result) # [[12]]
sess.close()
方法2:
with tf.Session() as sess:#不需要手动关闭sess
result2 = sess.run(product)
print(result2) # [[12]]
2. Variable变量
state = tf.Variable(0,name='counter') #定义常量 one
one = tf.constant(1) #定义加法步骤(注:此步并没有直接计算)
new_value = tf.add(state,one) #将 State 更新成 new_value
update = tf.assign(state,new_value) # 如果定义 Variable, 就一定要 initialize
# init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer() # 替换成这样就好 with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state)) >>>1
2
3
3. placeholder
Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数据 sess.run(***, feed_dict={input: **}).
接下来, 传值的工作交给了 sess.run() , 需要传入的值放在了feed_dict={} 并一一对应每一个 input. placeholder 与 feed_dict={} 是绑定在一起出现的。
input1 = tf.placeholder(tf.float32) #大部分只能处理float32
input2 = tf.placeholder(tf.float32) #两行两列[2,2] output = tf.multiply(input1,input2) with tf.Session() as sess:
print(sess.run(output,feed_dict={input1:[2.],input2:[1.]})) >>>[2.]
4. 添加层def add_layer()
import tensorflow as tf 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')
biases = tf.Variable(tf.zeros([1,out_size])+0.1)
Wx_plus_biase = tf.add(tf.matmul(inputs,Weights),biases) if activation_function == None:
outputs = Wx_plus_biase
else:
outputs = activation_function(Wx_plus_biase) return outputs
5. 搭建神经网络
import numpy as np x_data = np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape).astype(np.float32)
y_data = np.square(x_data) - 0.5 + noise # 利用占位符定义我们所需的神经网络的输入。 tf.placeholder()就是代表占位符,这里的None代表无论输入有多少都可以,因为输入只有一个特征,所以这里是1。
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') #层
l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None) #loss
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),
reduction_indices=1)) #优化器
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss) # init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer() # 替换成这样就好
sess = tf.Session()
sess.run(init) for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i % 50 == 0:
# to see the step improvement
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
结果如下:

6. 结果可视化
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
# plt.ion() #plt.ion()用于连续显示
# plt.show() # 每隔50次训练刷新一次图形,用红色、宽度为5的线来显示我们的预测数据和输入之间的关系,并暂停0.1s。 for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i % 50 == 0:
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction,feed_dict={xs:x_data})
lines = ax.plot(x_data,prediction_value,'r-',lw=5)#线宽度=5
# ax.lines.remove(lines[0])#去除lines的第一个线段
plt.pause(0.1) #暂停0.1s
# plt.show()

7. TensorFlow的优化器
tf.train.GradientDescentOptimizer
tf.train.AdadeltaOptimizer
tf.train.AdagradDAOptimizer
tf.train.MomentumOptimizer
tf.train.AdamOptimizer
tf.train.FtrlOptimizer
tf.train.RMSPropOptimizer
8. 可视化神经网络
# 图纸搭建 指定这里名称的会将来在可视化的图层inputs中显示出来
import tensorflow as tf
with tf.name_scope('inputs'):
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,1],name='x_in')
ys = tf.placeholder(tf.float32,[None,1],name='y_in') def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(
tf.random_normal([in_size, out_size],name='W'),
name='W')
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)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs #层
l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None) # the error between prediciton and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(
tf.reduce_sum(
tf.square(ys - prediction),
# eduction_indices=[1]
)) with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) sess = tf.Session() # get session
# tf.train.SummaryWriter soon be deprecated, use following
writer = tf.summary.FileWriter("E:/logs", sess.graph)
sess.run(tf.global_variables_initializer())

inputs输入层

隐藏层layer

隐藏层layer1

损失函数

训练

9. 可视化训练过程
输入数据:
import tensorflow as tf
import numpy as np # 图纸搭建 指定这里名称的会将来在可视化的图层inputs中显示出来
with tf.name_scope('inputs'):
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,1],name='x_in')
ys = tf.placeholder(tf.float32,[None,1],name='y_in') ## make up some data
x_data= np.linspace(-1, 1, 300, dtype=np.float32)[:,np.newaxis]
noise= np.random.normal(0, 0.05, x_data.shape).astype(np.float32)
y_data= np.square(x_data) -0.5+ noise
添加层:
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_name):
with tf.name_scope('weights'):
Weights= tf.Variable(tf.random_normal([in_size, out_size]),name='W')
# tf.histogram_summary(layer_name+'/weights',Weights)
tf.summary.histogram(layer_name + '/weights', Weights) # tensorflow >= 0.12 with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1,out_size])+0.1, name='b')
# tf.histogram_summary(layer_name+'/biase',biases)
tf.summary.histogram(layer_name + '/biases', biases) # Tensorflow >= 0.12 with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs,Weights), biases) if activation_function is None: #最后一层不需要激活
outputs=Wx_plus_b
else:
outputs= activation_function(Wx_plus_b) # tf.histogram_summary(layer_name+'/outputs',outputs)
tf.summary.histogram(layer_name + '/outputs', outputs) # Tensorflow >= 0.12 return outputs
损失函数:
with tf.name_scope('loss'):
loss= tf.reduce_mean(tf.reduce_sum(
tf.square(ys- prediction), reduction_indices=[1]))
# tf.scalar_summary('loss',loss) # tensorflow < 0.12
tf.summary.scalar('loss', loss) # tensorflow >= 0.12
接下来,开始合并打包。 tf.merge_all_summaries()方法会对我们所有的summaries合并到一起。因此在原有代码片段中添加:
sess= tf.Session() # merged= tf.merge_all_summaries() # tensorflow < 0.12
merged = tf.summary.merge_all() # tensorflow >= 0.12 # writer = tf.train.SummaryWriter('logs/', sess.graph) # tensorflow < 0.12
writer = tf.summary.FileWriter("logs/", sess.graph) # tensorflow >=0.12 # sess.run(tf.initialize_all_variables()) # tf.initialize_all_variables() # tf 马上就要废弃这种写法
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)
(1)DISTRIBUTIONS

(2)EVENTS
# tf.scalar_summary('loss',loss) # tensorflow < 0.12
tf.summary.scalar('loss', loss) # tensorflow >= 0.12

(3)HISTOGRAMS
# tf.histogram_summary(layer_name+'/biase',biases) # Tensorflow < 0.12
tf.summary.histogram(layer_name + '/biases', biases) # Tensorflow >= 0.12

参考文献:
【1】莫烦Python
Tensorflow 搭建神经网络及tensorboard可视化的更多相关文章
- (转)一文学会用 Tensorflow 搭建神经网络
一文学会用 Tensorflow 搭建神经网络 本文转自:http://www.jianshu.com/p/e112012a4b2d 字数2259 阅读3168 评论8 喜欢11 cs224d-Day ...
- 用Tensorflow搭建神经网络的一般步骤
用Tensorflow搭建神经网络的一般步骤如下: ① 导入模块 ② 创建模型变量和占位符 ③ 建立模型 ④ 定义loss函数 ⑤ 定义优化器(optimizer), 使 loss 达到最小 ⑥ 引入 ...
- 一文学会用 Tensorflow 搭建神经网络
http://www.jianshu.com/p/e112012a4b2d 本文是学习这个视频课程系列的笔记,课程链接是 youtube 上的,讲的很好,浅显易懂,入门首选, 而且在github有代码 ...
- 超简单tensorflow入门优化程序&&tensorboard可视化
程序1 任务描述: x = 3.0, y = 100.0, 运算公式 x×W+b = y,求 W和b的最优解. 使用tensorflow编程实现: #-*- coding: utf-8 -*-) im ...
- kaggle赛题Digit Recognizer:利用TensorFlow搭建神经网络(附上K邻近算法模型预测)
一.前言 kaggle上有传统的手写数字识别mnist的赛题,通过分类算法,将图片数据进行识别.mnist数据集里面,包含了42000张手写数字0到9的图片,每张图片为28*28=784的像素,所以整 ...
- Tensorflow搭建神经网络及使用Tensorboard进行可视化
创建神经网络模型 1.构建神经网络结构,并进行模型训练 import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt ...
- TensorFlow基础笔记(9) Tensorboard可视化显示以及查看pb meta模型文件的方法
参考: http://blog.csdn.net/l18930738887/article/details/55000008 http://www.jianshu.com/p/19bb60b52dad ...
- tensorflow搭建神经网络
最简单的神经网络 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt date = np.linspa ...
- tensorflow搭建神经网络基本流程
定义添加神经层的函数 1.训练的数据2.定义节点准备接收数据3.定义神经层:隐藏层和预测层4.定义 loss 表达式5.选择 optimizer 使 loss 达到最小 然后对所有变量进行初始化,通过 ...
随机推荐
- {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll
Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...
- [No0000DA]WPF ControlTemplate简介
一.简介 WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下ControlTemplate.其实WPF的每一个控件都有一 ...
- Dotnetbar中如何让LabelX自动换行
把LableX标签控件的WordWrap属性设置为true. https://zhidao.baidu.com/question/207858423.html 另外lable控件可以设置AutoSiz ...
- C# RichTextBox的用法
https://www.cnblogs.com/arxive/p/5725570.html
- 2014年蓝桥杯省赛A组c++第1题(暴力求解)
/* 小明带两个妹妹参加元宵灯会.别人问她们多大了,她们调皮地说:“我们俩的年龄之积是年龄之和的6倍”. 小明又补充说:“她们可不是双胞胎,年龄差肯定也不超过8岁啊.” 请你写出:小明的较小的妹妹的年 ...
- Page13:跟踪问题、最优控制[Linear System Theory]
内容包含跟踪问题及其结构框图.内模原理.可跟踪条件 各种线性二次型最优控制问题指标含义
- LeetCode 784 Letter Case Permutation 解题报告
题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...
- CloudStack+KVM环境搭建(步骤很详细,说明ClockStack是用来管理虚拟机的)
文章目录环境准备配置本地域名解析关闭selinux安装ntp服务安装管理端安装Mysql数据库安装服务端RPM:初始化CloudStack数据库:初始化cloudstack管理服务器安装系统虚拟机安装 ...
- mysql存储引擎的简介
前言 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能,使用不同的存储引擎,还可以 ...
- python之文件操作的函数
file=open("e:\\c.py","w",encoding="utf-8")date=file.close() ...