tf.reshape(tensor, shape, name=None) 数据重定形状函数
参数:
tensor:输入数据
shape:目标形状
name:名称
返回:Tensor
例:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' 的形状就是 [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' 当前形状是 [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' 形状是 [3, 2, 3]
# pass '[-1]' 扁平化 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 也可以被用于shape中
# -1 被推断结果是 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 被推断结果是 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 被推断结果是 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]
# tensor 't' is [7]
# shape `[]` 重塑为标量,用[]的时候,t只是有一个元素,不然会报错
reshape(t, []) ==> 7
测试代码
import tensorflow as tf
t = [7]
k = tf.reshape(t,[])
sess = tf.Session()
kk = sess.run(k)
print(kk)
---------------------
作者:zj360202
来源:CSDN
原文:https://blog.csdn.net/zj360202/article/details/70256835
版权声明:本文为博主原创文章,转载请附上博文链接!
|