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)表示两个变量相乘操作,常用的算术还有:

OperationDescription
tf.addsum
tf.subtractsubstraction
tf.multiplymultiplication
tf.divdivision
tf.modmodule
tf.absreturn the absolute value
tf.negreturn negative value
tf.signreturn the sign
tf.invreturns the inverse
tf.squarecalculates the square
tf.roundreturns the nearest integer
tf.sqrtcalculates the square root
tf.powcalculates the power
tf.expcalculates the exponential
tf.logcalculates the logarithm
tf.maximumreturns the maximum
tf.minimumreturns the minimum
tf.coscalculates the cosine
tf.sincalculates 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))

另外,还会用到的矩阵计算方法:

OperationDescription
tf.diagreturns a diagonal tensor with a given diagonal values
tf.transposereturns the transposes of the argument
tf.matmulreturns a tensor product of multiplying two tensors listed as arguments
tf.matrix_determinantreturns the determinant of the square matrix specified as an argument
tf.matrix_inversereturns 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_FLOATtf.float3232 位浮点数.
DT_DOUBLEtf.float6464 位浮点数.
DT_INT64tf.int6464 位有符号整型.
DT_INT32tf.int3232 位有符号整型.
DT_INT16tf.int1616 位有符号整型.
DT_INT8tf.int88 位有符号整型.
DT_UINT8tf.uint88 位无符号整型.
DT_STRINGtf.string可变长度的字节数组.每一个张量元素都是一个字节数组.
DT_BOOLtf.bool布尔型.
DT_COMPLEX64tf.complex64由两个32位浮点数组成的复数:实数和虚数.
DT_QINT32tf.qint32用于量化Ops的32位有符号整型.
DT_QINT8tf.qint8用于量化Ops的8位有符号整型.
DT_QUINT8tf.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]]
33阶张量 (数据立体)t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]
nn阶 ....

张量的形状

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].

张量的一些常用操作:

OperationDescription
tf.shapeTo find a shape of a tensor
tf.sizeTo find the size of a tensor
tf.rankTo find a rank of a tensor
tf.reshapeTo change the shape of a tensor keeping the same elements contained
tf.squeezeTo delete in a tensor dimensions of size 1
tf.expand_dimsTo insert a dimension to a tensor 
tf.sliceTo remove a portions of a tensor
tf.splitTo divide a tensor into several tensors along one dimension
tf.tileTo create a new tensor replicating a tensor multiple times
tf.concatTo concatenate tensors in one dimension
tf.reverseTo reverse a specific dimension of a tensor
tf.transposeTo transpose dimensions in a tensor
tf.gatherTo 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 tf

W = 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 tf

W = 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

开发丨深度学习框架太抽象?其实不外乎这五大核心组件

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值