YouTip LogoYouTip

Pytorch Torch Reshape

```html PyTorch torch.reshape Function | Rookie Tutorial

[Rookie Tutorial -- Learning not only technology, but also dreams!](https://example.com/)

PyTorch Tutorial

### torch.reshape

The `torch.reshape` function in PyTorch is used to change the shape of a tensor without changing its data. It returns a new tensor with the same data as the input tensor but with a different shape.

Syntax:

torch.reshape(input, shape)

Parameters:

  • input: The input tensor to be reshaped.
  • shape: The desired shape of the output tensor. The total number of elements must remain the same.

Returns:

  • A new tensor with the specified shape.

Example:

import torch

# Create a tensor
x = torch.arange(6)

# Reshape the tensor
y = torch.reshape(x, (2, 3))

print(y)

Output:

tensor([[0, 1, 2],
        [3, 4, 5]])

This example demonstrates how to use `torch.reshape` to change the shape of a tensor from a 1D array of 6 elements to a 2D tensor of shape (2, 3).

```
← Pytorch Torch RollPytorch Torch Randperm β†’