Pytorch Torch Flatten
# PyTorch torch.flatten Function
* * Pytorch torch Reference Manual](#)
`torch.flatten` is a function in PyTorch used to flatten tensors.
### Function Definition
torch.flatten(input, start_dim, end_dim, out)
* * *
## Usage Example
## Example
import torch
x = torch.randn(2,3,4)
# Flatten to 1D
y = torch.flatten(x)
print("Original shape:", x.shape)
print("After flattening:", y.shape)
# Flatten from specified dimension
z = torch.flatten(x, start_dim=1)
print("From dim=1 Flatten:", z.shape)
The output result is:
Original shape: torch.Size([2, 3, 4])After flattening: torch.Size()From dim=1 Flatten: torch.Size([2, 12])
* * Pytorch torch Reference Manual](#)
YouTip