[Pytorch]#3 Shaping Functions

Jude's Sound Lab·2025년 1월 9일

Algorithm & Coding

목록 보기
3/5

Shaping functions comparison

Expand

The expand function broadcasts a tensor to a new shape, but without duplicating its data.

Simple usage

tensor = torch.tensor([1, 2, 3])  # Shape: (3,)
expanded_tensor = tensor.expand(3, 3)  # Shape: (3, 3)
print(expanded_tensor)

tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
tensor = torch.tensor([[1], [2], [3]])  # Shape: (3, 1)
expanded_tensor = tensor.expand(3, 4)  # Shape: (3, 4)
print(expanded_tensor)
tensor([[1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]])
profile
chords & code // harmony with structure

0개의 댓글