PyTorch Tensor의 차원 변환
파이토치에서 사용하는 텐서는 여러가지 형태로 차원을 변환할 수 있다. 차원의 추가, 삭제도 포함이다. 여기서는 squeeze, unsqueeze, view, reshape, traspose, permute를 알아본다. 1. unsqueeze 지정된 위치에 크기가 1일 차원을 추가 import torchx = torch.tensor([[1, 2, 3], [4, 5, 6]])x.shape>> torch.Size([2, 3])y = x.unsqueeze(0)y, y.shape>> (tensor([[[1, 2, 3], [4, 5, 6]]]), torch.Size([1, 2, 3])) y = x.unsqueeze(1)y, y.shape>>(tensor([[[1, 2..
2025. 4. 16.
PyTorch repeat, repeat_interleave, expand 차이
PyTorch의 repeat와 repeat_interleave와 expand의 주요 차이점을 설명한다.1. torch.repeat:1차원 텐서 x = torch.tensor([1, 2, 3])result = x.repeat(3)>> tensor([1, 2, 3, 1, 2, 3, 1, 2, 3]) 2차원 텐서 x = torch.tensor([[1, 2, 3], [4, 5, 6]])x.repeat(2, 1)>> tensor([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]) 이때 repeat 안에 들어가는 숫자는 복사하고자 하는 텐서의 차원이 같아야 한다. 2차원 텐서에 대해서 아래와 같이 입력하면 에러..
2024. 11. 1.