_reshape
reshape
¶
Reshape a tensor
to conform to a given shape.
This method can be used with Numpy data:
n = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]])
b = fe.backend.reshape(n, shape=[-1]) # [1, 2, 3, 4, 5, 6, 7, 8]
b = fe.backend.reshape(n, shape=[2, 4]) # [[1, 2, 3, 4], [5, 6, 7, 8]]
b = fe.backend.reshape(n, shape=[4, 2]) # [[1, 2], [3, 4], [5, 6], [7, 8]]
b = fe.backend.reshape(n, shape=[2, 2, 2, 1]) # [[[[1], [2]], [[3], [4]]], [[[5], [6]], [[7], [8]]]]
This method can be used with TensorFlow tensors:
t = tf.constant([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]])
b = fe.backend.reshape(t, shape=[-1]) # [1, 2, 3, 4, 5, 6, 7, 8]
b = fe.backend.reshape(t, shape=[2, 4]) # [[1, 2, 3, 4], [5, 6, 7, 8]]
b = fe.backend.reshape(t, shape=[4, 2]) # [[1, 2], [3, 4], [5, 6], [7, 8]]
b = fe.backend.reshape(t, shape=[2, 2, 2, 1]) # [[[[1], [2]], [[3], [4]]], [[[5], [6]], [[7], [8]]]]
This method can be used with PyTorch tensors:
p = torch.tensor([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]])
b = fe.backend.reshape(p, shape=[-1]) # [1, 2, 3, 4, 5, 6, 7, 8]
b = fe.backend.reshape(p, shape=[2, 4]) # [[1, 2, 3, 4], [5, 6, 7, 8]]
b = fe.backend.reshape(p, shape=[4, 2]) # [[1, 2], [3, 4], [5, 6], [7, 8]]
b = fe.backend.reshape(p, shape=[2, 2, 2, 1]) # [[[[1], [2]], [[3], [4]]], [[[5], [6]], [[7], [8]]]]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
The input value. |
required |
shape |
List[int]
|
The new shape of the tensor. At most one value may be -1 which indicates that whatever values are left should be packed into that axis. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The reshaped |
Raises:
Type | Description |
---|---|
ValueError
|
If |