tf.reshape
Stay organized with collections
Save and categorize content based on your preferences.
Reshapes a tensor.
tf.reshape(
tensor, shape, name=None
)
Given tensor
, this operation returns a tensor that has the same values
as tensor
with shape shape
.
If one component of shape
is the special value -1, the size of that
dimension is computed so that the total size remains constant. In particular,
a shape
of [-1]
flattens into 1-D. At most one component of shape
can
be -1.
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
.
For example:
# 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], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [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' 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]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 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 `[]` reshapes to a scalar
reshape(t, []) ==> 7
Args |
tensor
|
A Tensor .
|
shape
|
A Tensor . Must be one of the following types: int32 , int64 .
Defines the shape of the output tensor.
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor . Has the same type as tensor .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.reshape\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/reshape) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/array_ops.py#L59-L133) |\n\nReshapes a tensor.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.manip.reshape`](/api_docs/python/tf/reshape), [`tf.compat.v1.reshape`](/api_docs/python/tf/reshape)\n\n\u003cbr /\u003e\n\n tf.reshape(\n tensor, shape, name=None\n )\n\nGiven `tensor`, this operation returns a tensor that has the same values\nas `tensor` with shape `shape`.\n\nIf one component of `shape` is the special value -1, the size of that\ndimension is computed so that the total size remains constant. In particular,\na `shape` of `[-1]` flattens into 1-D. At most one component of `shape` can\nbe -1.\n\nIf `shape` is 1-D or higher, then the operation returns a tensor with shape\n`shape` filled with the values of `tensor`. In this case, the number of\nelements implied by `shape` must be the same as the number of elements in\n`tensor`.\n\n#### For example:\n\n # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]\n # tensor 't' has shape [9]\n reshape(t, [3, 3]) ==\u003e [[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]]\n\n # tensor 't' is [[[1, 1], [2, 2]],\n # [[3, 3], [4, 4]]]\n # tensor 't' has shape [2, 2, 2]\n reshape(t, [2, 4]) ==\u003e [[1, 1, 2, 2],\n [3, 3, 4, 4]]\n\n # tensor 't' is [[[1, 1, 1],\n # [2, 2, 2]],\n # [[3, 3, 3],\n # [4, 4, 4]],\n # [[5, 5, 5],\n # [6, 6, 6]]]\n # tensor 't' has shape [3, 2, 3]\n # pass '[-1]' to flatten 't'\n reshape(t, [-1]) ==\u003e [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]\n\n # -1 can also be used to infer the shape\n\n # -1 is inferred to be 9:\n reshape(t, [2, -1]) ==\u003e [[1, 1, 1, 2, 2, 2, 3, 3, 3],\n [4, 4, 4, 5, 5, 5, 6, 6, 6]]\n # -1 is inferred to be 2:\n reshape(t, [-1, 9]) ==\u003e [[1, 1, 1, 2, 2, 2, 3, 3, 3],\n [4, 4, 4, 5, 5, 5, 6, 6, 6]]\n # -1 is inferred to be 3:\n reshape(t, [ 2, -1, 3]) ==\u003e [[[1, 1, 1],\n [2, 2, 2],\n [3, 3, 3]],\n [[4, 4, 4],\n [5, 5, 5],\n [6, 6, 6]]]\n\n # tensor 't' is [7]\n # shape `[]` reshapes to a scalar\n reshape(t, []) ==\u003e 7\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-----------------------------------------------------------------------------------------------------------|\n| `tensor` | A `Tensor`. |\n| `shape` | A `Tensor`. Must be one of the following types: `int32`, `int64`. Defines the shape of the output tensor. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same type as `tensor`. ||\n\n\u003cbr /\u003e"]]