r"""Reshapes a tensor. 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` o
(tensor, shape, name=None)
| 58 | |
| 59 | @tf_export("reshape", v1=["reshape", "manip.reshape"]) |
| 60 | def reshape(tensor, shape, name=None): # pylint: disable=redefined-outer-name |
| 61 | r"""Reshapes a tensor. |
| 62 | |
| 63 | Given `tensor`, this operation returns a tensor that has the same values |
| 64 | as `tensor` with shape `shape`. |
| 65 | |
| 66 | If one component of `shape` is the special value -1, the size of that |
| 67 | dimension is computed so that the total size remains constant. In particular, |
| 68 | a `shape` of `[-1]` flattens into 1-D. At most one component of `shape` can |
| 69 | be -1. |
| 70 | |
| 71 | If `shape` is 1-D or higher, then the operation returns a tensor with shape |
| 72 | `shape` filled with the values of `tensor`. In this case, the number of |
| 73 | elements implied by `shape` must be the same as the number of elements in |
| 74 | `tensor`. |
| 75 | |
| 76 | For example: |
| 77 | |
| 78 | ``` |
| 79 | # tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 80 | # tensor 't' has shape [9] |
| 81 | reshape(t, [3, 3]) ==> [[1, 2, 3], |
| 82 | [4, 5, 6], |
| 83 | [7, 8, 9]] |
| 84 | |
| 85 | # tensor 't' is [[[1, 1], [2, 2]], |
| 86 | # [[3, 3], [4, 4]]] |
| 87 | # tensor 't' has shape [2, 2, 2] |
| 88 | reshape(t, [2, 4]) ==> [[1, 1, 2, 2], |
| 89 | [3, 3, 4, 4]] |
| 90 | |
| 91 | # tensor 't' is [[[1, 1, 1], |
| 92 | # [2, 2, 2]], |
| 93 | # [[3, 3, 3], |
| 94 | # [4, 4, 4]], |
| 95 | # [[5, 5, 5], |
| 96 | # [6, 6, 6]]] |
| 97 | # tensor 't' has shape [3, 2, 3] |
| 98 | # pass '[-1]' to flatten 't' |
| 99 | reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] |
| 100 | |
| 101 | # -1 can also be used to infer the shape |
| 102 | |
| 103 | # -1 is inferred to be 9: |
| 104 | reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], |
| 105 | [4, 4, 4, 5, 5, 5, 6, 6, 6]] |
| 106 | # -1 is inferred to be 2: |
| 107 | reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], |
| 108 | [4, 4, 4, 5, 5, 5, 6, 6, 6]] |
| 109 | # -1 is inferred to be 3: |
| 110 | reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1], |
| 111 | [2, 2, 2], |
| 112 | [3, 3, 3]], |
| 113 | [[4, 4, 4], |
| 114 | [5, 5, 5], |
| 115 | [6, 6, 6]]] |
| 116 | |
| 117 | # tensor 't' is [7] |