tf.reshape
Stay organized with collections
Save and categorize content based on your preferences.
Reshapes a tensor.
tf.reshape(
tensor, shape, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
Given tensor
, this operation returns a new tf.Tensor
that has the same
values as tensor
in the same order, except with a new shape given by
shape
.
t1 = [[1, 2, 3],
[4, 5, 6]]
print(tf.shape(t1).numpy())
[2 3]
t2 = tf.reshape(t1, [6])
t2
<tf.Tensor: shape=(6,), dtype=int32,
numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
tf.reshape(t2, [3, 2])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4],
[5, 6]], dtype=int32)>
The tf.reshape
does not change the order of or the total number of elements
in the tensor, and so it can reuse the underlying data buffer. This makes it
a fast operation independent of how big of a tensor it is operating on.
tf.reshape([1, 2, 3], [2, 2])
Traceback (most recent call last):
InvalidArgumentError: Input to reshape is a tensor with 3 values, but the
requested shape has 4
To instead reorder the data to rearrange the dimensions of a tensor, see
tf.transpose
.
t = [[1, 2, 3],
[4, 5, 6]]
tf.reshape(t, [3, 2]).numpy()
array([[1, 2],
[3, 4],
[5, 6]], dtype=int32)
tf.transpose(t, perm=[1, 0]).numpy()
array([[1, 4],
[2, 5],
[3, 6]], dtype=int32)
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.
t = [[1, 2, 3],
[4, 5, 6]]
tf.reshape(t, [-1])
<tf.Tensor: shape=(6,), dtype=int32,
numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
tf.reshape(t, [3, -1])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4],
[5, 6]], dtype=int32)>
tf.reshape(t, [-1, 2])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4],
[5, 6]], dtype=int32)>
tf.reshape(t, [])
reshapes a tensor t
with one element to a scalar.
tf.reshape([7], []).numpy()
7
More examples:
t = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(tf.shape(t).numpy())
[9]
tf.reshape(t, [3, 3])
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=int32)>
t = [[[1, 1], [2, 2]],
[[3, 3], [4, 4]]]
print(tf.shape(t).numpy())
[2 2 2]
tf.reshape(t, [2, 4])
<tf.Tensor: shape=(2, 4), dtype=int32, numpy=
array([[1, 1, 2, 2],
[3, 3, 4, 4]], dtype=int32)>
t = [[[1, 1, 1],
[2, 2, 2]],
[[3, 3, 3],
[4, 4, 4]],
[[5, 5, 5],
[6, 6, 6]]]
print(tf.shape(t).numpy())
[3 2 3]
# Pass '[-1]' to flatten 't'.
tf.reshape(t, [-1])
<tf.Tensor: shape=(18,), dtype=int32,
numpy=array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],
dtype=int32)>
# -- Using -1 to infer the shape --
# Here -1 is inferred to be 9:
tf.reshape(t, [2, -1])
<tf.Tensor: shape=(2, 9), dtype=int32, numpy=
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>
# -1 is inferred to be 2:
tf.reshape(t, [-1, 9])
<tf.Tensor: shape=(2, 9), dtype=int32, numpy=
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>
# -1 is inferred to be 3:
tf.reshape(t, [ 2, -1, 3])
<tf.Tensor: shape=(2, 3, 3), dtype=int32, numpy=
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]], dtype=int32)>
Args |
tensor
|
A Tensor .
|
shape
|
A Tensor . Must be one of the following types: int32 , int64 .
Defines the shape of the output tensor.
|
name
|
Optional string. A name for the operation.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.reshape\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L63-L201) |\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.reshape`](https://www.tensorflow.org/api_docs/python/tf/reshape)\n\n\u003cbr /\u003e\n\n tf.reshape(\n tensor, shape, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Introduction to Tensors](https://www.tensorflow.org/guide/tensor) - [Advanced automatic differentiation](https://www.tensorflow.org/guide/advanced_autodiff) - [DTensor concepts](https://www.tensorflow.org/guide/dtensor_overview) - [Multilayer perceptrons for digit recognition with Core APIs](https://www.tensorflow.org/guide/core/mlp_core) - [Distributed training with Core APIs and DTensor](https://www.tensorflow.org/guide/core/distribution) | - [Learned data compression](https://www.tensorflow.org/tutorials/generative/data_compression) - [Adversarial example using FGSM](https://www.tensorflow.org/tutorials/generative/adversarial_fgsm) - [Convolutional Variational Autoencoder](https://www.tensorflow.org/tutorials/generative/cvae) - [Load CSV data](https://www.tensorflow.org/tutorials/load_data/csv) - [Load text](https://www.tensorflow.org/tutorials/load_data/text) |\n\nGiven `tensor`, this operation returns a new [`tf.Tensor`](../tf/Tensor) that has the same\nvalues as `tensor` in the same order, except with a new shape given by\n`shape`. \n\n t1 = [[1, 2, 3],\n [4, 5, 6]]\n print(tf.shape(t1).numpy())\n [2 3]\n t2 = tf.reshape(t1, [6])\n t2\n \u003ctf.Tensor: shape=(6,), dtype=int32,\n numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)\u003e\n tf.reshape(t2, [3, 2])\n \u003ctf.Tensor: shape=(3, 2), dtype=int32, numpy=\n array([[1, 2],\n [3, 4],\n [5, 6]], dtype=int32)\u003e\n\nThe [`tf.reshape`](../tf/reshape) does not change the order of or the total number of elements\nin the tensor, and so it can reuse the underlying data buffer. This makes it\na fast operation independent of how big of a tensor it is operating on. \n\n tf.reshape([1, 2, 3], [2, 2])\n Traceback (most recent call last):\n\n InvalidArgumentError: Input to reshape is a tensor with 3 values, but the\n requested shape has 4\n\nTo instead reorder the data to rearrange the dimensions of a tensor, see\n[`tf.transpose`](../tf/transpose). \n\n t = [[1, 2, 3],\n [4, 5, 6]]\n tf.reshape(t, [3, 2]).numpy()\n array([[1, 2],\n [3, 4],\n [5, 6]], dtype=int32)\n tf.transpose(t, perm=[1, 0]).numpy()\n array([[1, 4],\n [2, 5],\n [3, 6]], dtype=int32)\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\n t = [[1, 2, 3],\n [4, 5, 6]]\n tf.reshape(t, [-1])\n \u003ctf.Tensor: shape=(6,), dtype=int32,\n numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)\u003e\n tf.reshape(t, [3, -1])\n \u003ctf.Tensor: shape=(3, 2), dtype=int32, numpy=\n array([[1, 2],\n [3, 4],\n [5, 6]], dtype=int32)\u003e\n tf.reshape(t, [-1, 2])\n \u003ctf.Tensor: shape=(3, 2), dtype=int32, numpy=\n array([[1, 2],\n [3, 4],\n [5, 6]], dtype=int32)\u003e\n\n`tf.reshape(t, [])` reshapes a tensor `t` with one element to a scalar. \n\n tf.reshape([7], []).numpy()\n 7\n\n#### More examples:\n\n t = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n print(tf.shape(t).numpy())\n [9]\n tf.reshape(t, [3, 3])\n \u003ctf.Tensor: shape=(3, 3), dtype=int32, numpy=\n array([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]], dtype=int32)\u003e\n\n t = [[[1, 1], [2, 2]],\n [[3, 3], [4, 4]]]\n print(tf.shape(t).numpy())\n [2 2 2]\n tf.reshape(t, [2, 4])\n \u003ctf.Tensor: shape=(2, 4), dtype=int32, numpy=\n array([[1, 1, 2, 2],\n [3, 3, 4, 4]], dtype=int32)\u003e\n\n t = [[[1, 1, 1],\n [2, 2, 2]],\n [[3, 3, 3],\n [4, 4, 4]],\n [[5, 5, 5],\n [6, 6, 6]]]\n print(tf.shape(t).numpy())\n [3 2 3]\n # Pass '[-1]' to flatten 't'.\n tf.reshape(t, [-1])\n \u003ctf.Tensor: shape=(18,), dtype=int32,\n numpy=array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],\n dtype=int32)\u003e\n # -- Using -1 to infer the shape --\n # Here -1 is inferred to be 9:\n tf.reshape(t, [2, -1])\n \u003ctf.Tensor: shape=(2, 9), dtype=int32, numpy=\n array([[1, 1, 1, 2, 2, 2, 3, 3, 3],\n [4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)\u003e\n # -1 is inferred to be 2:\n tf.reshape(t, [-1, 9])\n \u003ctf.Tensor: shape=(2, 9), dtype=int32, numpy=\n array([[1, 1, 1, 2, 2, 2, 3, 3, 3],\n [4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)\u003e\n # -1 is inferred to be 3:\n tf.reshape(t, [ 2, -1, 3])\n \u003ctf.Tensor: shape=(2, 3, 3), dtype=int32, numpy=\n array([[[1, 1, 1],\n [2, 2, 2],\n [3, 3, 3]],\n [[4, 4, 4],\n [5, 5, 5],\n [6, 6, 6]]], dtype=int32)\u003e\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` | Optional string. A name for the operation. |\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"]]