YouTip LogoYouTip

Pytorch Torch Atleast_2D

# PyTorch torch.atleast_2d Function * * PyTorch torch Reference Manual](#) `torch.atleast_2d` is a PyTorch function that converts input tensors to at least 2 dimensions. If the input has fewer than 2 dimensions, new dimensions will be automatically added to make it 2D. ### Function Definition torch.atleast_2d(*tensors) * * * ## Usage Examples ## Example import torch # Convert scalar to 2D tensor x = torch.atleast_2d(5) print("After scalar conversion:", x,"Shape:", x.shape) # Output: After scalar conversion: tensor([]) Shape: torch.Size([1, 1]) # Convert 1D tensor to 2D x = torch.tensor([1,2,3]) y = torch.atleast_2d(x) print("1D to 2D conversion:", y,"Shape:", y.shape) # Output: 1D to 2D conversion: tensor([[1, 2, 3]]) Shape: torch.Size([1, 3]) # 2D tensors remain unchanged x = torch.tensor([[1,2],[3,4]]) y = torch.atleast_2d(x) print("2D tensor:", y,"Shape:", y.shape) * * PyTorch torch Reference Manual](#)
← Pytorch Torch BernoulliPytorch Torch Atan β†’