tf.reshape(tensor, shape, name=None)
- 第1个参数tensor为被调整维度的张量。
- 第2个参数shape为要调整为的形状。
- 返回一个shape形状的新tensor
注意shape里最多有一个维度的值可以填写为-1,表示自动计算此维度。
Reshapes a tensor.
Given tensor, this operation returns a tensor that has the same values as tensor with shape shape.
If shape is the special value {[-1], then tensor is flattened and the operation outputs a 1-D tensor with all elements of tensor.
If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.
1 # tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
2 # tensor ‘t’ has shape [9]
3 reshape(t, [3, 3]) ==> [[1, 2, 3]
4 [4, 5, 6]
5 [7, 8, 9]]
6
7 # tensor ‘t’ is [[[1, 1], [2, 2]]
8 # [[3, 3], [4, 4]]]
9 # tensor ‘t’ has shape [2, 2]
10 reshape(t, [2, 4]) ==> [[1, 1, 2, 2]
11 [3, 3, 4, 4]]
12
13 # tensor ‘t’ is [[[1, 1, 1],
14 # [2, 2, 2]],
15 # [[3, 3, 3],
16 # [4, 4, 4]],
17 # [[5, 5, 5],
18 # [6, 6, 6]]]
19 # tensor ‘t’ has shape [3, 2, 3]
20 # pass ‘[¡1]’ to flatten ‘t’
21 reshape(t, [¡1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6,
6, 6]
tf.Tensor.get_shape()