tensorflow学习笔记(2)张量与计算图
TensorFlow简介:
官网上对TensorFlow的介绍是,一个使用数据流图(data flow graphs)技术来进行数值计算的开源软件库。数据流图中的节点,代表数值运算;节点节点之间的边,代表多维数据(tensors)之间的某种联系。我们可以在多种设备(含有CPU或GPU)上通过简单的API调用来使用该系统的功能。
TensorFlow包含构建数据流图与计算数据流图等基本步骤,图中的节点表示数学操作,图中连结各节点的边表示多维数组,即:tensors(张量)。 张量是TensorFlow最核心的组件,所有运算和优化都是基于张量进行的。张量是基于向量和矩阵的推广,可以将标量看为零阶张量,矢量看做一阶张量,矩阵看做二阶张量(后面详细介绍)。
数据流图是描述有向图中的数值计算过程。有向图中的节点通常代表数学运算,但也可以表示数据的输入、输出和读写等操作;有向图中的边表示节点之间的某种联系,它负责传输多维数据(Tensors)。图中这些tensors的flow也就是TensorFlow的命名来源。
基本使用:
将计算流程表示成图;
通过Sessions来执行图计算;
将数据表示为tensors;
使用Variables来保持状态信息;
分别使用feeds和fetches来填充数据和抓取任意的操作结果;
TensorFlow初识,简单实例
import tensorflow as tf
a =tf.placeholder("float")
b =tf.placeholder("float")
y = tf.multiply(a,b)
sess = tf.Session()
print(sess.run(y, feed_dict={a: 3, b: 3}))
上面代码中,首先导入TensorFlow,然后tf.placeholder("float")定义a和b两个浮点类型的变量,tf.multiply(a,b)表示两个变量相乘操作,常用的算术还有:
Operation | Description |
tf.add | sum |
tf.subtract | substraction |
tf.multiply | multiplication |
tf.div | division |
tf.mod | module |
tf.abs | return the absolute value |
tf.neg | return negative value |
tf.sign | return the sign |
tf.inv | returns the inverse |
tf.square | calculates the square |
tf.round | returns the nearest integer |
tf.sqrt | calculates the square root |
tf.pow | calculates the power |
tf.exp | calculates the exponential |
tf.log | calculates the logarithm |
tf.maximum | returns the maximum |
tf.minimum | returns the minimum |
tf.cos | calculates the cosine |
tf.sin | calculates the sine |
如果两种不同类型计算时会报错,需要tf.cast()转换类型,例如:
tf.subtract(tf.constant(3.0),tf.constant(1))
"""
TypeError: Input 'y' of 'Sub' Op has type int32 that does not
match type float32 of argument 'x'.
"""
上面代码需改为:
tf.subtract(tf.cast(tf.constant(3.0), tf.int32), tf.constant(1))
另外,还会用到的矩阵计算方法:
Operation | Description |
tf.diag | returns a diagonal tensor with a given diagonal values |
tf.transpose | returns the transposes of the argument |
tf.matmul | returns a tensor product of multiplying two tensors listed as arguments |
tf.matrix_determinant | returns the determinant of the square matrix specified as an argument |
tf.matrix_inverse | returns the inverse of the square matrix specified as an argument |
接下来 tf.Session()语句表示创建一个session,这是最重要的一步,它用来计算生成的符号表达式。到这一步TensorFlow代码还没有真正被执行, 而调用run()方法后算法才真正被执行。可以看出,TensorFlow既是一个表示机器学习算法的接口,又是对机器学习算法的实现。
为了抓取输出结果,在执行session的run函数后,通过print函数打印状态信息。
填充(Feeds):
TensorFlow提供的机制:先创建特定数据类型的占位符(placeholder),之后再进行数据的填充("feed_dict= ");如果不对placeholder()的变量进行数据填充,将会引发错误。
基本数据类型:
数据类型 | Python 类型 | 描述 |
---|---|---|
DT_FLOAT
|
tf.float32
|
32 位浮点数. |
DT_DOUBLE
|
tf.float64
|
64 位浮点数. |
DT_INT64
|
tf.int64
|
64 位有符号整型. |
DT_INT32
|
tf.int32
|
32 位有符号整型. |
DT_INT16
|
tf.int16
|
16 位有符号整型. |
DT_INT8
|
tf.int8
|
8 位有符号整型. |
DT_UINT8
|
tf.uint8
|
8 位无符号整型. |
DT_STRING
|
tf.string
|
可变长度的字节数组.每一个张量元素都是一个字节数组. |
DT_BOOL
|
tf.bool
|
布尔型. |
DT_COMPLEX64
|
tf.complex64
|
由两个32位浮点数组成的复数:实数和虚数. |
DT_QINT32
|
tf.qint32
|
用于量化Ops的32位有符号整型. |
DT_QINT8
|
tf.qint8
|
用于量化Ops的8位有符号整型. |
DT_QUINT8
|
tf.quint8
|
用于量化Ops的8位无符号整型. |
张量的阶
TensorFlow用张量表示所有的数据,张量可看成一个n维的数组或列表,在图中的节点之间流通。张量的维数称为阶,注:张量的阶和矩阵的阶不是同一个概念。下面的张量(使用Python的list定义)是2阶:
t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
阶 | 数学实例 | Python 例子 |
---|---|---|
0 | 纯量 (只有大小) |
s = 1
|
1 | 向量(大小和方向) |
v = [1, 2, 3]
|
2 | 矩阵(数据表) |
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
3 | 3阶张量 (数据立体) |
t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
|
n | n阶 |
....
|
张量的形状
TensorFlow使用三种记号描述张量的维度:阶,形状及维数,它们之间的关系:
阶 | 形状 | 维数 | 实例 |
---|---|---|---|
0 | [ ] | 0-D | 一个 0维张量. 一个纯量. |
1 | [D0] | 1-D | 一个1维张量的形式[5]. |
2 | [D0, D1] | 2-D | 一个2维张量的形式[3, 4]. |
3 | [D0, D1, D2] | 3-D | 一个3维张量的形式 [1, 4, 3]. |
n | [D0, D1, ... Dn] | n-D | 一个n维张量的形式 [D0, D1, ... Dn]. |
张量的一些常用操作:
Operation | Description |
tf.shape | To find a shape of a tensor |
tf.size | To find the size of a tensor |
tf.rank | To find a rank of a tensor |
tf.reshape | To change the shape of a tensor keeping the same elements contained |
tf.squeeze | To delete in a tensor dimensions of size 1 |
tf.expand_dims | To insert a dimension to a tensor |
tf.slice | To remove a portions of a tensor |
tf.split | To divide a tensor into several tensors along one dimension |
tf.tile | To create a new tensor replicating a tensor multiple times |
tf.concat | To concatenate tensors in one dimension |
tf.reverse | To reverse a specific dimension of a tensor |
tf.transpose | To transpose dimensions in a tensor |
tf.gather | To collect portions according to an index |
例如,将一个2维张量扩展为3维:
vectors = tf.constant(conjunto_puntos)
extended_vectors = tf.expand_dims(vectors, 0)
print (expanded_vectors.get_shape())
执行上面这句,可以得到扩展后张量的维度。
TensorFlow计算图:
有了张量和基于张量的各种操作,之后需要将各种操作整合起来,输出结果。但不幸的是,随着操作种类和数量的增多,有可能引发各种意想不到的问题,包括多个操作之间应该并行还是顺次执行,如何协同各种不同的底层设备,以及如何避免各种类型的冗余操作等等。这些问题有可能拉低整个深度学习网络的运行效率或者引入不必要的Bug,计算图正是为解决这一问题产生的。
论文《Learning Deep Architectures for AI》作者用不同的占位符(*,+,sin)构成操作结点,以字母x、a、b构成变量结点,以有向线段将这些结点连接起来,组成一个表征运算逻辑关系的清晰明了的“图”型数据结构,这就是最初的计算图。
计算图的引入可以让开发者从宏观上俯瞰整个神经网络的内部结构,就好像编译器可以从整个代码的角度决定如何分配寄存器那样,计算图也可以从宏观上决定代码运行时的GPU内存分配,以及分布式环境中不同底层设备间的相互协作方式。除此之外,现在也有许多深度学习框架将计算图应用于模型调试,可以实时输出当前某一操作类型的文本描述。
实例1:
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)
node1和node2是constant,常量不可改变,其输出结果:
Tensor("Const_2:0", shape=(), dtype=float32) Tensor("Const_3:0", shape=(), dtype=float32)
上面并没有直接输出3.0和4.0,而是输出可以生成3.0和4.0的两个张量,如果想要得到3.0和4.0,需要上面介绍的session和run操作:
sess = tf.Session()
print(sess.run([node1, node2]))
计算图是将节点列到一个图中的一系列操作,其输入是节点(nodes),输出也是node。或者更复杂一点,操作也是node:
实例2:
node3 = tf.add(node1, node2)
print("node3:", node3)
print("sess.run(node3):", sess.run(node3))
输出结果为:
node3: Tensor("Add_1:0", shape=(), dtype=float32)
sess.run(node3): 7.0
为了使算法容易理解,TensorFlow中的可视化工具Tensorboard包含了一些debug函数与优化程序,可以察看不同类型的参数统计结果与图中的计算细节(这部分以后参照实例学习一下)。
实例3:
输入可以是任意量,例如构建模型:y=w*x+b,w和b一定时,x是可变量:
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x + b
W和b是Variable,执行上面的语句,W和b并没有被初始化,如果执行程序,需要下面的初始化语句:
init = tf.global_variables_initializer()
完整实例如下:
import tensorflow as tf
W = tf.Variable([.5], dtype=tf.float32)
b = tf.Variable([-.5], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x + b
#print("linear_model:",linear_model)init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(linear_model, {x: [1, 2, 3, 4]}))
输出结果为:[ 0. 0.5 1. 1.5]
实例4:
import tensorflow as tfW = tf.Variable(1)
assign_W = W.assign(10)#修改变量方法,assign
with tf.Session() as sess:sess.run(W.initializer)print(W.eval())sess.run(assign_W)print(W.eval())
assign只是一个函数,并且不需要初始化,但是assign_add()和assign_sub()需要初始化。
import tensorflow as tfW = tf.Variable(10)
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
print('W add 1=',sess1.run(W.assign_add(1)))
print('W sun 2=',sess2.run(W.assign_sub(2)))
sess1.close()
sess2.close()
参考:
https://www.tensorflow.org/get_started/get_started
http://jorditorres.org/research-teaching/tensorflow/first-contact-with-tensorflow-book/first-contact-with-tensorflow/#cap1
http://blog.csdn.net/liyuqian199695/article/details/61647946
http://wiki.jikexueyuan.com/project/tensorflow-zh/resources/dims_types.html
开发丨深度学习框架太抽象?其实不外乎这五大核心组件
tensorflow学习笔记(2)张量与计算图相关推荐
- TensorFlow学习笔记--第三节张量(tensor)及其定义方法
目录 在TensorFlow中,所有的数据通过张量的形式来表示 1张量及属性: 1.1维数(阶) 1.2 形状 1.3数据类型 TensorFlow 支持以下三种类型的张量: **1.常量** **2 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记--使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- Tensorflow学习笔记2:About Session, Graph, Operation and Tensor
简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...
- Win10: tensorflow 学习笔记(3)
前言 学以致用,以学促用.输出检验,完整闭环. 怕什么真理无穷,进一寸有一寸的欢喜--胡适 经过前段时间的努力,已经在电脑上搭好了深度学习系统,接下来就要开始跑程序了,将AI落地了. 安装win10下 ...
- tensorflow学习笔记1
tensorflow学习笔记1 本文主要记录我在慕课上观看北大曹建老师的<人工智能实践:Tensorflow笔记>,链接:https://www.icourse163.org/course ...
- tensorflow学习笔记(八):LSTM手写体(MNIST)识别
文章目录 一.LSTM简介 二.主要函数 三.LSTM手写体(MNIST)识别 1.MNIST数据集简介 2.网络描述 3.项目实战 一.LSTM简介 LSTM是一种特殊的RNN,很好的解决了RNN中 ...
- 炼数成金Tensorflow学习笔记之2.4_Tensorflow简单示例
炼数成金Tensorflow学习笔记之2.4_Tensorflow简单示例 代码及分析 代码及分析 # -*- coding: utf-8 -*- """ Created ...
- tensorflow学习笔记(1)------北京大学 曹健
tensorflow学习笔记(1) 0 环境搭建 tensorfolw的环境搭建(anaconda版,一步到位) 视频里环境搭建过程很详细,就不细说了.(视频中也有配置视频,链接在文末) 另外,本笔记 ...
- tensorflow学习笔记(七):CNN手写体(MNIST)识别
文章目录 一.CNN简介 二.主要函数 三.CNN的手写体识别 1.MNIST数据集简介 2.网络描述 3.项目实战 一.CNN简介 一般的卷积神经网络由以下几个层组成:卷积层,池化层,非线性激活函数 ...
- Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题
Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题 参考文章: (1)Tensorflow学习笔记6:解决tensorflow训练过程中GPU未调用问题 (2)http ...
最新文章
- 利用Reachability实现网络状态监听
- 团队开发git使用各种问题
- c++上传oss中文路径报错_C# 从Oss对象存储器上下载文件到本地打包下载代码
- [Unity] GameFramework 学习记录 5
- nmap查看开放端口以及使用的协议
- 455 periodic strings
- Django_RBAC_demo2 升级版权限控制组件
- Halcon:基本例程
- Segger和Micrium之间的区别和联系
- 计算机专业的文献综述题目,计算机专业文献综述格式及要求
- 安装的photoshop cs2为什么一进去就说我的用户名、组织、或序列号无效或错误???
- 关于数学基础的研究现状
- 如何在Oracle数据库中使用dbms_output输出内容
- 从0到1做好KOL运营实操
- 设计一个聊天窗口java代码,Java 设计的聊天程序-完整代码
- 元界快讯|首个物联网元宇宙平台“物联森友会”即将上线
- JDK17 ReentrantLock 简述 lock()、unLock()
- 《黄帝内经.通天》阴阳五种类型的人
- 【Linux】VIM命令(全面详解)
- CSS中的BFC详解:定义、触发方式及应用场景