TensorFlow 1.1 Python API
来源:互联网 发布:矩阵范数怎么计算 编辑:程序博客网 时间:2024/06/04 18:48
TensorFlow 1.1 Python API (一)
Tensor操作
- 切片与融合
# ‘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(value, num_or_size_splits, axis=0, num=None, name=’split’) 将tensor切割成subtensor,其中当num_or_size_splits为标量的时候,表示沿着axis将tensor均匀分成若干等分,当num_or_size_splits为tensor的时候,表示将tensor沿着axisa按照num_or_size_splits进行分割
# ‘value’ is a tensor with shape [5, 30]
# Split ‘value’ into 3 tensors with sizes [4, 15, 11] along dimension 1;
split0, split1, split2 = tf.split(value, [4, 15, 11], 1)
tf.shape(split0) ==> [5, 4]
tf.shape(split1) ==> [5, 15]
tf.shape(split2) ==> [5, 11]
# Split ‘value’ into 3 tensors along dimension 1 split0, split1, split2 = tf.split(value, num_or_size_splits=3, axis=1)
tf.shape(split0) ==> [5, 10] tf.pad(tensor,paddings,mode=’CONSTANT’,name=None) 在原有的矩阵上进行填充
# ‘t’ is [[1, 2, 3], [4, 5, 6]].
# ‘paddings’ is [[1, 1,], [2, 2]].
# rank of ‘t’ is 2.
pad(t, paddings, “CONSTANT”) ==> [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 2, 3, 0, 0],
[0, 0, 4, 5, 6, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
pad(t, paddings, “REFLECT”) ==> [[6, 5, 4, 5, 6, 5, 4],
[3, 2, 1, 2, 3, 2, 1],
[6, 5, 4, 5, 6, 5, 4],
[3, 2, 1, 2, 3, 2, 1]]
pad(t, paddings, “SYMMETRIC”) ==> [[2, 1, 1, 2, 3, 3, 2],
[2, 1, 1, 2, 3, 3, 2],
[5, 4, 4, 5, 6, 6, 5],
[5, 4, 4, 5, 6, 6, 5]] tf.concat(values,axis,name=’concat’) 沿着axis将tensor进行拼接
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6],
[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]]
# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3]
tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6] tf.stack(values,axis=0,name=’stack’) 将列表中所有tensor沿着axis进行拼接
# ‘x’ is [1, 4]
# ‘y’ is [2, 5]
# ‘z’ is [3, 6]
stack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.
stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] tf.reverse(tensor,axis,name=None) 沿着某个axis进行反转
# 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 [3] or ‘dims’ is -1
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]]]]
# ‘dims’ is ‘[1]’ (or ‘dims’ is ‘[-3]’)
reverse(t, dims) ==> [[[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]
[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]]]
# ‘dims’ is ‘[2]’ (or ‘dims’ is ‘[-2]’)
reverse(t, dims) ==> [[[[8, 9, 10, 11],
[4, 5, 6, 7],
[0, 1, 2, 3]]
[[20, 21, 22, 23],
[16, 17, 18, 19],
[12, 13, 14, 15]]]] tf.transpose(a,perm=None,name=’transpose’) 将tensor进行转置
# ‘x’ is [[1 2 3]
# [4 5 6]]
tf.transpose(x) ==> [[1 4]
[2 5]
[3 6]
# ‘x’ is [[[1 2 3]
# [4 5 6]]
# [[7 8 9]
# [10 11 12]]]
tf.transpose(x, perm=[0, 2, 1]) ==> [[[1 4]
[2 5]
[3 6]]
[[7 10]
[8 11]
[9 12]]] tf.one_hot(indices,depth,on_value=None,off_value=None,axis=None, dtype=None,name=None) 返回一个one-hot tensor,在参数indices中的值表示tenosor中该位置的值取on_value,其余的位置取值为off_value。
如果on_value和off_value没有指定,那么on_value默认为1,off_value默认为0。
如果indices的rank为N,则输出的rank为N+1
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)
indices = [[0, 2], [1, -1]]
depth = 3
on_value = 1.0
off_value = 0.0
axis = -1
Then output is [2 x 2 x 3]:
output =
[
[1.0, 0.0, 0.0] // one_hot(0)
[0.0, 0.0, 1.0] // one_hot(2)
][
[0.0, 1.0, 0.0] // one_hot(1)
[0.0, 0.0, 0.0] // one_hot(-1)
]
indices = [0, 1, 2]
depth = 3
The output will be
output =
[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]] tf.gather(params, indices, validate_indices=None, name=None) 根据indices从params中选出slices聚合成新的tensor
- 数据类型转换Casting
# tensor a is [1.8, 2.2], dtype=tf.float
tf.cast(a, tf.int32) ==> [1, 2], dtype=tf.int32
- 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,out_type=tf.int32) 返回tensor中元素的个数
# ‘t’ is [[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]]]
size(t) ==> 12 tf.rank(input,name=None) 返回tensor的rank,tensor的rank和矩阵的rank不同,它是指唯一表示tensor中所有元素所需的索引个个数,即常见的“ndims”,“degree”,“order”
# ‘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转换成指定的shape
# 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]]
# tensor ‘t’ is [[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]]
# tensor ‘t’ has shape [3, 2, 3] pass ‘[-1]’ to flatten ‘t’,
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] tf.stack(values,axis=0,name=’stack’) 将values中的tensor沿着axis堆叠起来生成一个高纬度的tensor,
# ‘x’ is [1, 4]
# ‘y’ is [2, 5]
# ‘z’ is [3, 6]
stack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.
stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] tf.squeeze(input,axis=None,name=None,squeeze_dims=None) 将tensor中size为1的维度移除
# ‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3];
# ‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1] tf.expand_dims(input,axis=None,name=None,dim=None) 在tensor中axis中插入size为1的一个维度
# ‘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] tf.meshgrid(*args, **kwargs) 根据向量创建矩阵
#Calling X, Y = meshgrid(x, y) with the tensors
x = [1, 2, 3]
y = [4, 5, 6]
results in
X = [[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]
Y = [[4, 5, 6],
[4, 5, 6],
[4, 5, 6]],
其中生成矩阵X的列向量为x的复制,生成矩阵Y的行向量为y的复制
阅读全文
1 0
- TensorFlow 1.1 Python API
- TensorFlow API 树 (Python)
- TensorFlow(五)TensorFlow Python API (nn模块)
- tensorflow image recognition python api
- tensorflow的官方python的API地址
- TensorFlow(三) 常用的 Python API
- TensorFlow 1.0更新python相关api更改
- 极简Tensorflow API速查--Python语言
- Tensorflow api
- Tensorflow API
- TensorFlow Python API解析:图的核心数据结构
- TensorFlow Python API解析:图的核心数据结构
- tensorflow API r1.0
- tensorflow math api 汇总表
- tensorflow高阶API
- tensorflow object_detection API使用
- Tensorflow API Math
- tensorflow:常用API-'a'
- 骑兵变步兵?10 分钟搞明白如何在直播中去马赛克
- 2440移植Linux Kernel笔记(三)------制作jffs2映像文件
- Kotlin 类
- Android TextView 开源控件大全
- linux c++使用libevent编写http server + http client
- TensorFlow 1.1 Python API
- [后台运维] 服务器开发运维之道
- Kotlin 类属性
- ajaxFileupload多文件上传
- Android面试题—校招—360
- mysql常用函数汇总
- Java8学习笔记 — 其它新特性【接口默认方法(静态方法)、重复注解、类型注解】
- Kotlin 可见度
- 欢迎使用CSDN-markdown编辑器