Returns a tensor with the same data as the original tensor but with a different shape. `shape` can be passed as a tuple or as separate arguments. ```python exec="true" source="above" session="tensor" result="python" t = Tensor.arange(6) print(t.reshape(2, 3).numpy()) ```
(self, shape, *args)
| 154 | return self._broadcast_to(new_shape) |
| 155 | |
| 156 | def reshape(self, shape, *args) -> Self: |
| 157 | """ |
| 158 | Returns a tensor with the same data as the original tensor but with a different shape. |
| 159 | `shape` can be passed as a tuple or as separate arguments. |
| 160 | |
| 161 | ```python exec="true" source="above" session="tensor" result="python" |
| 162 | t = Tensor.arange(6) |
| 163 | print(t.reshape(2, 3).numpy()) |
| 164 | ``` |
| 165 | """ |
| 166 | # resolve None and args |
| 167 | new_shape = tuple([s if s is not None else self.shape[i] for i, s in enumerate(argfix(shape, *args))]) |
| 168 | # resolve -1 |
| 169 | if (c := new_shape.count(-1)) > 1: |
| 170 | raise RuntimeError(f"only one dimension can be inferred using -1, getting {new_shape}") |
| 171 | if c: |
| 172 | new_shape = tuple([-prod(self.shape) // prod(new_shape) if s == -1 else s for s in new_shape]) |
| 173 | if prod(self.shape) != prod(new_shape): |
| 174 | raise ValueError(f"size mismatch, can't reshape ({self.shape}) -> ({new_shape})") |
| 175 | ret = self._mop(Ops.RESHAPE, arg=new_shape) |
| 176 | return self if ret.shape == self.shape else ret |
| 177 | |
| 178 | def pad(self, arg:tuple[tuple[sint, sint] | None, ...]) -> Self: |
| 179 | if self.ndim != len(arg): |