Frequently Used Tensor Operation
PyTorch Tensor Manipulation Methods
This document summarizes commonly used PyTorch tensor manipulation functions, their purposes, and examples.
1. squeeze
Function: Removes dimensions of size 1 from a tensor.
Example:
tensor = torch.randn(1, 3, 4, 1)
tensor_squeezed = tensor.squeeze() # Removes all dimensions of size 1, resulting shape: (3, 4)
tensor_squeezed_dim = tensor.squeeze(0) # Removes size 1 from the 0th dimension, resulting shape: (3, 4, 1)
2. view
Function: Reshapes a tensor without changing its data storage order.
Example:
tensor = torch.randn(4, 4)
tensor_reshaped = tensor.view(2, 8) # Reshapes to shape: (2, 8)
3. reshape
Function: Similar to view
, reshapes a tensor, but may create a copy of the data if necessary.
Example:
tensor = torch.randn(4, 4)
tensor_reshaped = tensor.reshape(2, 8) # Reshapes to shape: (2, 8)
4. permute
Function: Reorders the dimensions of a tensor.
Example:
tensor = torch.randn(2, 3, 4)
tensor_permuted = tensor.permute(2, 0, 1) # Changes dimension order, resulting shape: (4, 2, 3)
5. transpose
Function: Swaps two specified dimensions of a tensor.
Example:
tensor = torch.randn(2, 3, 4)
tensor_transposed = tensor.transpose(1, 2) # Swaps the 1st and 2nd dimensions, resulting shape: (2, 4, 3)
6. expand
and expand_as
Function: Expands a tensor to match a specified shape without copying data, useful for broadcasting.
Example:
tensor = torch.randn(1, 3)
tensor_expanded = tensor.expand(4, 3) # Expands to shape: (4, 3)
7. repeat
Function: Duplicates tensor data and repeats it along specified dimensions.
Example:
tensor = torch.randn(2, 2)
tensor_repeated = tensor.repeat(2, 3) # Results in shape: (4, 6)
8. flatten
Function: Flattens a tensor into one dimension.
Example:
tensor = torch.randn(2, 3, 4)
tensor_flattened = tensor.flatten() # Results in shape: (24,)
These tensor manipulation methods help reshape, reorder, and expand tensors efficiently to fit different computational needs and network architectures.