Tensorflow学习-工具相关
Tensorflow
版本(# 2017-06-24
):1.2.0
Python
版本:3.5.3
- 包括:
Tensorboard
可视化tfdbg
调试- 常用的高级函数
一、TensorBoard
可视化
1、可视化计算图
- 全部代码:点击查看
- 数据集使用
MNIST
手写数字 - 加载数据
1
2
3
4
5
6
|
'''加载数据'''
data = input_data.read_data_sets('MNIST_data', one_hot=True)
print("Size of:")
print("\t\t training set:\t\t{}".format(len(data.train.labels)))
print("\t\t test set: \t\t\t{}".format(len(data.test.labels)))
print("\t\t validation set:\t{}".format(len(data.validation.labels)))
|
(1) 全连接网络
- 超参数
1
2
3
4
5
6
|
'''超参数'''
img_size = 28
img_flatten_size = img_size ** 2
img_shape = (img_size, img_size)
num_classes = 10
learning_rate = 1e-4
|
定义添加一层的函数
num_layer
指定是第几层activation
指定激励函数,若不指定跳过
1234567891011121314'''定义添加一层'''def add_fully_layer(inputs, input_size, output_size, num_layer, activation=None):with tf.name_scope('layer_'+num_layer):with tf.name_scope('Weights'):W = tf.Variable(initial_value=tf.random_normal(shape=[input_size, output_size]), name='W')with tf.name_scope('biases'):b = tf.Variable(initial_value=tf.zeros(shape=[1, output_size]) + 0.1, name='b')with tf.name_scope('Wx_plus_b'):Wx_plus_b = tf.matmul(inputs, W) + bif activation is not None:outputs = activation(Wx_plus_b)else:outputs = Wx_plus_breturn outputs
定义输入,计算图结构,loss和优化器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
'''placehoder'''
with tf.name_scope('inputs'):
x = tf.placeholder(tf.float32, shape=[None, img_flatten_size], name='x')
y = tf.placeholder(tf.float32, shape=[None, num_classes], name='y')
'''结构'''
hidden_layer1 = add_fully_layer(x, img_flatten_size, 20, '1', activation=tf.nn.relu)
logits = add_fully_layer(hidden_layer1, 20, num_classes, '2')
predictions = tf.nn.softmax(logits)
'''loss'''
#cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logits)
cross_entropy = -tf.reduce_sum(y*tf.log(predictions), reduction_indices=[1])
with tf.name_scope('losses'):
losses = tf.reduce_mean(cross_entropy)
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(learning_rate).minimize(losses)
|
- 定义
Session
和tf.summary.FileWriter
1
2
3
4
|
'''session'''
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('logs', sess.graph) # 将计算图写入文件
|
最后在
logs
的上级目录打开命令行输入:tensorboard --logdir=logs/
,浏览器中输入网址:http://localhost:6006
即可查看结果
- 自定义的
cross_entropy = -tf.reduce_sum(y*tf.log(predictions), reduction_indices=[1])
- 使用
tensorflow
中自带的cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predictions)
- 自定义的
- 可以看出
tf.name_scope
定义的名字就是其中的方框,点击里面的可以查看里面对应的内容
(2) CNN
卷积神经网络
添加一层卷积层和
pooling
层- 这里默认
pooling
使用maxpooling
, 大小为2
12345678910111213'''CNN 定义添加一层卷积层,包括pooling(使用maxpooling, size=2)'''def add_conv_layer(inputs, filter_size, input_channels, output_channels, num_layer, activation=tf.nn.relu):with tf.name_scope('conv_layer_'+num_layer):with tf.name_scope('Weights'):Weights = tf.Variable(tf.truncated_normal(stddev=0.1, shape=[filter_size, filter_size, input_channels, output_channels]), name='W')with tf.name_scope('biases'):b = tf.Variable(tf.constant(0.1, shape=[output_channels]))with tf.name_scope('conv2d'):conv2d_plus_b = tf.nn.conv2d(inputs, Weights, strides=[1,1,1,1], padding='SAME', name='conv') + bactivation_conv_outputs = activation(conv2d_plus_b)with tf.name_scope('max_pool'):max_pool_outputs = tf.nn.max_pool(activation_conv_outputs, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')return max_pool_outputs
- 这里默认
将卷积层展开
- 返回展开层和数量(因为全连接会用到)
123456789'''将卷积层展开'''def flatten_layer(layer):'''@param layer: the conv layer'''layer_shape = layer.get_shape() # 获取形状(layer_shape == [num_images, img_height, img_width, num_channels])num_features = layer_shape[1:4].num_elements() # [1:4] 是最后3个维度,就是展开的长度layer_flat = tf.reshape(layer, [-1, num_features]) # 展开return layer_flat, num_features
- 返回展开层和数量(因为全连接会用到)
定义输入
- 需要将
x
转成图片矩阵的形式12345'''placehoder'''with tf.name_scope('inputs'):x = tf.placeholder(tf.float32, shape=[None, img_flatten_size], name='x')y = tf.placeholder(tf.float32, shape=[None, num_classes], name='y')x_image = tf.reshape(x, shape = [-1, img_size, img_size, n_channels], name='x_images')
- 需要将
定义计算图结构
1
2
3
4
5
6
7
8
9
10
11
12
13
|
'''CNN卷积网络结构'''
conv_layer1 = add_conv_layer(x_image, filter_size=5, input_channels=1,
output_channels=32,
num_layer='1')
conv_layer2 = add_conv_layer(conv_layer1, filter_size=5, input_channels=32,
output_channels=64,
num_layer='2')
'''全连接层'''
conv_layer2_flat, num_features = flatten_layer(conv_layer2) # 将最后操作的数据展开
hidden_layer1 = add_fully_layer(conv_layer2_flat, num_features, 1000, num_layer='1', activation=tf.nn.relu)
logits = add_fully_layer(hidden_layer1, 1000, num_classes, num_layer='2')
predictions = tf.nn.softmax(logits)
|
- 结果
CNN
总结构
- 第一层卷积和
pooling
内部结构
(3) RNN_LSTM
循环神经网络
声明
placeholder
- 图片中每一行当做当前的输入,共有
n_steps=28
步遍历完一张图片,所以输入x
的shape=(batch_size, n_steps, n_inputs)
n_inputs
就是一行的像素值
12345'''placehoder'''with tf.name_scope('inputs'):'''RNN'''x = tf.placeholder(tf.float32, shape=[batch_size, n_steps, n_inputs], name='x')y = tf.placeholder(tf.float32, shape=[batch_size, num_classes], name='y')
- 图片中每一行当做当前的输入,共有
添加一层
cell
- 我们最后只需要遍历
n_steps
之后的输出即可(遍历完一张图然后分类),所以对应的是final_state[1]
(有两个state
, 一个是c state
,一个是h state
, 输出是h state
) - 12345678910111213'''RNN 添加一层cell'''def add_RNN_Cell(inputs):with tf.name_scope('RNN_LSTM_Cell'):with tf.name_scope('weights'):weights = tf.Variable(tf.random_normal(shape=[state_size, num_classes]), name='W')with tf.name_scope('biases'):biases = tf.Variable(tf.constant(0.1, shape=[num_classes,]), name='b')cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=state_size)init_state = cell.zero_state(batch_size, dtype=tf.float32)rnn_outputs, final_state = tf.nn.dynamic_rnn(cell=cell, inputs=x,initial_state=init_state)logits = tf.matmul(final_state[1], weights) + biasesreturn logits
- 我们最后只需要遍历
网络结果和
loss
1
2
3
4
5
6
7
8
9
|
'''RNN网络结构'''
logits = add_RNN_Cell(inputs=x)
predictions = tf.nn.softmax(logits)
'''loss'''
#cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predictions)
cross_entropy = -tf.reduce_sum(y*tf.log(predictions), reduction_indices=[1])
with tf.name_scope('losses'):
losses = tf.reduce_mean(cross_entropy)
|
- 结果
- 总体结构
RNN
内部结构
- 总体结构
2、可视化训练过程
- 全部代码:点击查看
(1) 权重
weights
,偏置biases
,损失值loss
加入一层全连接层的函数变成这样
- 加入
tf.summary.histogram(name=layer_name+'/Weights', values=W)
即可123456789101112131415161718192021222324'''定义添加一层全连接层'''def add_fully_layer(inputs, input_size, output_size, num_layer, activation=None):layer_name = 'layer_' + num_layerwith tf.name_scope(layer_name):with tf.name_scope('Weights'):low = -4*np.sqrt(6.0/(input_size + output_size)) # use 4 for sigmoid, 1 for tanh activationhigh = 4*np.sqrt(6.0/(input_size + output_size))#'''xavier方法初始化'''##sigmoid#Weights = tf.Variable(tf.random_uniform(shape=[input_size, output_size], minval=low, maxval=high, dtype=tf.float32), name='W')##reluW = tf.Variable(initial_value=tf.random_uniform(shape=[input_size, output_size], minval=low, maxval=high, dtype=tf.float32)/2, name='W')tf.summary.histogram(name=layer_name+'/Weights', values=W) # summary.histogramwith tf.name_scope('biases'):b = tf.Variable(initial_value=tf.zeros(shape=[1, output_size]) + 0.1, name='b')tf.summary.histogram(name=layer_name+'/biases', values=b) # summary.histogramwith tf.name_scope('Wx_plus_b'):Wx_plus_b = tf.matmul(inputs, W) + bif activation is not None:outputs = activation(Wx_plus_b)else:outputs = Wx_plus_btf.summary.histogram(name=layer_name+'/outputs', values=outputs) # summary.histogramreturn outputs
- 加入
损失
- 损失因为是个具体的数,所以使用
scalar
(上面的权重和偏置都是矩阵,向量)1tf.summary.scalar(name='loss_value', tensor=losses)
- 损失因为是个具体的数,所以使用
训练时
merge
merged = tf.summary.merge_all()
合并所有的summary
- 对于
loss
, 训练时执行merge
, 然后随步数不断加入merged_result = sess.run(merged, feed_dict=feed_dict_train) # 执行merged
writer.add_summary(summary=merged_result, global_step=i)
123456789101112131415'''训练'''def optimize(n_epochs):with tf.Session() as sess:merged = tf.summary.merge_all()writer = tf.summary.FileWriter('logs', sess.graph) # 将计算图写入文件sess.run(tf.global_variables_initializer())for i in range(n_epochs):batch_x, batch_y = data.train.next_batch(batch_size)feed_dict_train = {x: batch_x, y: batch_y}sess.run(train_step, feed_dict=feed_dict_train)if i % 20 == 0:print("epoch:{0}, accuracy:{1}".format(i, sess.run(accuracy, feed_dict=feed_dict_train)))merged_result = sess.run(merged, feed_dict=feed_dict_train) # 执行mergedwriter.add_summary(summary=merged_result, global_step=i) # 加入到writeroptimize(1001)
结果
loss
- 权重和偏置的数据分布
二、Tensorflow
调试tfdbg
- 官网教程:点击查看
Youtube
视频简短教程:点击查看Tensorflow Debugger (tfdbg)
是专业的调试工具,可以查看计算图中内容部的数据等
1、本地调试
(1) 加入代码
- 导入调试的包:
from tensorflow.python import debug as tf_debug
Wrapper Session
和添加filter
:filter
也可以自己定义123with tf.Session() as sess:sess = tf_debug.LocalCLIDebugWrapperSession(sess)sess.add_tensor_filter(filter_name='inf or nan', tensor_filter=tf_debug.has_inf_or_nan)
(2) 运行
- 命令行中执行:
python xxx.py --debug
即可进入调试- 支持鼠标点击的可以直接点击查看变量的信息
run
或者r
可以查看所有的tensor
的名字等信息- 第一次
run
还没有初始化变量,pt tenser_name
打印tensor
的信息
- 第一次
- 再执行一次就是初始化变量
- 可以进行
slice
- 可以进行
- 更多命令行
原文地址: http://lawlite.me/2017/06/24/Tensorflow%E5%AD%A6%E4%B9%A0-%E5%B7%A5%E5%85%B7%E7%9B%B8%E5%85%B3/
Tensorflow学习-工具相关相关推荐
- 【干货】史上最全的Tensorflow学习资源汇总,速藏!
一 .Tensorflow教程资源: 1)适合初学者的Tensorflow教程和代码示例:(https://github.com/aymericdamien/TensorFlow-Examples)该 ...
- tensorflow学习入门笔记
<div class="note"><div class="post"><div class="article" ...
- 基于PyTorch、易上手,细粒度图像识别深度学习工具库Hawkeye开源
转载自丨机器之心 鉴于当前领域内尚缺乏该方面的深度学习开源工具库,南京理工大学魏秀参教授团队用时近一年时间,开发.打磨.完成了 Hawkeye--细粒度图像识别深度学习开源工具库,供相关领域研究人员和 ...
- TensorFlow 学习指南:深度学习系统构建详解
内容简介 面向广泛的技术受众(从数据科学家.工程师到学生和研究人员),本书介绍了 TensorFlow 的基本原理和实践方法.从 TensorFlow 中的一些基本示例开始,深入探讨诸如神经网络体系结 ...
- 怎么学习机械学习相关的技术?
学习机器学习相关技术的过程可以分为以下几个步骤: 掌握基本数学和统计知识: 机器学习建立在数学和统计学的基础上,了解线性代数.概率论.统计学等基本概念和方法对于理解机器学习算法至关重要. 学习编程和数 ...
- 基于OpenCL的深度学习工具:AMD MLP及其使用详解
from:http://www.csdn.net/article/2015-08-05/2825390 [编者按]深度学习是近年来迅速发展和突破的机器学习领域,具有非常广泛的应用前景.将服务器GPU应 ...
- 数据结构学习工具总结
图解程序员画流程图.数据结构图及各种复杂图形的Graphviz工具入门 http://blog.csdn.net/jin13277480598/article/details/51305674 安装G ...
- 线性代数的学习及相关资源
线性代数的学习及相关资源 本来是想写"Coursera公开课笔记: 斯坦福大学机器学习第三课"线性代数回顾(Linear Algebra Review)"的,但是这一课仅 ...
- Win10: tensorflow 学习笔记(3)
前言 学以致用,以学促用.输出检验,完整闭环. 怕什么真理无穷,进一寸有一寸的欢喜--胡适 经过前段时间的努力,已经在电脑上搭好了深度学习系统,接下来就要开始跑程序了,将AI落地了. 安装win10下 ...
最新文章
- GitHub万星的中文机器学习资源:路线图、视频、电子书、学习建议全在这
- windows程序消息机制(Winform界面更新有关)--转
- insight切换窗口 source_Source Insight函数调用关系显示设置(示例代码)
- SpringBoot 集成ip2region
- 并发insert情况下会发生重复的数据插入问题
- android getdecorview 出现空指针,android – 为什么我从TabWidget得到一个空指针异常?...
- linux学习之lvm-逻辑卷管理器
- 在QML应用中实现threading多任务
- 【学习率调整】学习率衰减之周期余弦退火 (cyclic cosine annealing learning rate schedule)
- 逐渐“狗化”!网易云音乐控诉酷狗像素级抄袭,酷狗高管反击...
- python图像分类需要什么环境_Python学习简单图像识别分类,python
- Java常量不能二次赋值与常量不能修改值意思一样吗?
- VS C++ 重新编译
- js打开新页面的两种方式
- iOS界面调试工具Reveal
- 《佛密诸事》第二章 宇宙诸现象
- WAF绕过技术系列文章(一)
- 下一个十年:数据信仰与认知智能
- ISO26262解析(四)——FMEDA
- MySQL——SQLyog如何导出、导入数据库