TensorFlow学习笔记(七)

1.TensorFlow 是一个编程系统, 使用图来表示计算任务. 图中的节点被称之为op (operation 的缩写). 一个 op获得 0 个或多个张量(Tensor) , 执行计算, 产生 0 个或多个Tensor . 每个 Tensor 是一个按类型划分的多维数组. 例如, 你可以将一小组图像集表示为一个四维浮点数数组, 这四个维度分别是[batch, height, width, channels] .
一个 TensorFlow 图描述了计算的过程. 为了进行计算, 图必须在会话里被启动. 会话将图的 op 分发到诸如 CPU 或 GPU 之类的设备上, 同时提供执行 op 的方法. 这些方法执行后, 将产生的 tensor 返回. 在 Python 语言中, 返回的 tensor 是numpy ndarray 对象; 在 C 和 C++ 语言中, 返回的 tensor 是tensorflow::Tensor 实例
TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU。一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测。如果检测到 GPU, TensorFlow 会尽可能地利用找到的第一个 GPU 来执行操作.
并行计算能让代价大的算法计算加速执行,TensorFlow也在实现上对复杂操作进行了有效的改进。大部分核相关的操作都是设备相关的实现,比如GPU。下面是一些重要的操作/核:
2.tf 函数

tf.add(x, y, name=None) 求和
tf.sub(x, y, name=None) 减法
tf.mul(x, y, name=None) 乘法
tf.div(x, y, name=None) 除法
tf.mod(x, y, name=None) 取模
tf.abs(x, name=None)    求绝对值
tf.neg(x, name=None)    取负 (y = -x).
tf.sign(x, name=None)   返回符号 y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
tf.inv(x, name=None)    取反
tf.square(x, name=None) 计算平方 (y = x * x = x^2).
tf.round(x, name=None)  舍入最接近的整数
# ‘a’ is [0.9, 2.5, 2.3, -4.4]
tf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ]
tf.sqrt(x, name=None)   开根号 (y = \sqrt{x} = x^{1/2}).
tf.pow(x, y, name=None) 幂次方 
# tensor ‘x’ is [[2, 2], [3, 3]]
# tensor ‘y’ is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]
tf.exp(x, name=None)    计算e的次方
tf.log(x, name=None)    计算log,一个输入计算e的ln,两输入以第二输入为底
tf.maximum(x, y, name=None) 返回最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) 返回最小值 (x < y ? x : y)
tf.cos(x, name=None)    三角函数cosine
tf.sin(x, name=None)    三角函数sine
tf.tan(x, name=None)    三角函数tan
tf.atan(x, name=None)   三角函数ctan

3.数据类型转换操作

操作  描述
tf.string_to_number
(string_tensor, out_type=None, name=None)   字符串转为数字
tf.to_double(x, name=’ToDouble’)    转为64位浮点类型–float64
tf.to_float(x, name=’ToFloat’)  转为32位浮点类型–float32
tf.to_int32(x, name=’ToInt32’)  转为32位整型–int32
tf.to_int64(x, name=’ToInt64’)  转为64位整型–int64
tf.cast(x, dtype, name=None)    将x或者x.values转换为dtype
# tensor a is [1.8, 2.2], dtype=tf.float
tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int32

4.形状操作

操作  描述
tf.shape(input, name=None)  返回数据的shape
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
shape(t) ==> [2, 2, 3]
tf.size(input, name=None)   返回数据的元素数量
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]
size(t) ==> 12
tf.rank(input, name=None)   返回tensor的rank
注意:此rank不同于矩阵的rank,
tensor的rank表示一个tensor需要的索引数目来唯一表示任何一个元素
也就是通常所说的 “order”, “degree”或”ndims”
#’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
# shape of tensor ‘t’ is [2, 2, 3]
rank(t) ==> 3
tf.reshape(tensor, shape, name=None)    改变tensor的形状
# tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor ‘t’ has shape [9]
reshape(t, [3, 3]) ==> 
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
#如果shape有元素[-1],表示在该维度打平至一维
# -1 将自动推导得为 9:
reshape(t, [2, -1]) ==> 
[[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
tf.expand_dims(input, dim, name=None)   插入维度1进入一个tensor中
#该操作要求-1-input.dims()
# ‘t’ is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1] <= dim <= input.dims()
•   切片与合并(Slicing and Joining)
tf.slice(input_, begin, size, name=None)    对tensor进行切片操作
其中size[i] = input.dim_size(i) - begin[i]
该操作要求 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]
#’input’ is 
#[[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]
tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> 
[[[3, 3, 3],
[4, 4, 4]]]
tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> 
[[[3, 3, 3]],
[[5, 5, 5]]]
tf.split(split_dim, num_split, value, name=’split’) 沿着某一维度将tensor分离为num_split tensors
# ‘value’ is a tensor with shape [5, 30]
# Split ‘value’ into 3 tensors along dimension 1
split0, split1, split2 = tf.split(1, 3, value)
tf.shape(split0) ==> [5, 10]
tf.concat(concat_dim, values, name=’concat’)    沿着某一维度连结tensor
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
如果想沿着tensor一新轴连结打包,那么可以:
tf.concat(axis, [tf.expand_dims(t, axis) for t in tensors])
等同于tf.pack(tensors, axis=axis)
tf.pack(values, axis=0, name=’pack’)    将一系列rank-R的tensor打包为一个rank-(R+1)的tensor
# ‘x’ is [1, 4], ‘y’ is [2, 5], ‘z’ is [3, 6]
pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] 
# 沿着第一维pack
pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]
等价于tf.pack([x, y, z]) = np.asarray([x, y, z])
tf.reverse(tensor, dims, name=None) 沿着某维度进行序列反转
其中dim为列表,元素为bool型,size等于rank(tensor)
# tensor ‘t’ is 
[[[[ 0, 1, 2, 3],
#[ 4, 5, 6, 7],

#[ 8, 9, 10, 11]],
#[[12, 13, 14, 15],
#[16, 17, 18, 19],
#[20, 21, 22, 23]]]]
# tensor ‘t’ shape is [1, 2, 3, 4]
# ‘dims’ is [False, False, False, True]
reverse(t, dims) ==>
[[[[ 3, 2, 1, 0],
[ 7, 6, 5, 4],
[ 11, 10, 9, 8]],
[[15, 14, 13, 12],
[19, 18, 17, 16],
[23, 22, 21, 20]]]]
tf.transpose(a, perm=None, name=’transpose’)    调换tensor的维度顺序
按照列表perm的维度排列调换tensor顺序,
如为定义,则perm为(n-10)
# ‘x’ is [[1 2 3],[4 5 6]]
tf.transpose(x) ==> [[1 4], [2 5],[3 6]]
# Equivalently
tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
tf.gather(params, indices, validate_indices=None, name=None)    合并索引indices所指示params中的切片

tf.one_hot
(indices, depth, on_value=None, off_value=None, 
axis=None, dtype=None, name=None)   indices = [0, 2, -1, 1]
depth = 3
on_value = 5.0 
off_value = 0.0 
axis = -1 
#Then output is [4 x 3]: 
output = 
[5.0 0.0 0.0] // one_hot(0) 
[0.0 0.0 5.0] // one_hot(2) 
[0.0 0.0 0.0] // one_hot(-1) 
[0.0 5.0 0.0] // one_hot(1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值