오류 핸들링

[error] RuntimeError: The expanded size of the tensor (*) must match the existing size (*) at non-singleton dimension *. Target sizes: [* * * *]

감자156 2023. 4. 17. 14:29
반응형

torch.expand 함수를 사용할 때, 차원이 1이 아닌 축에 expand를 적용해서 발생한 오류

 

오류 ex)

 

에서 1이 아닌 dim을 확장하려고 하면 오류 발생

 

 

차원이 1일 때만 정상 동작

 

이를 대체하여 torch.repeat과 torch.repeat_interleave를 사용할 수 있음. 

둘 다 텐서를 반복하여 축을 확장하는 함수이지만, repeat는 행, 열의 값을 무조건 1회 반복해야 함. 

 

더 자세한 예시는 아래 링크에 )

https://cccaaa.tistory.com/33

 

torch tensor 반복 확장하기 expand vs. repeat vs. repeat_interleave 차이

pytorch에서 텐서를 반복하여 확장하는 방법들을 정리하겠음. 1) torch.expand() dim=1인 축에 대해서만 확장이 가능하며, shape을 input으로 받음 사용 예) 여기서, 1이 아닌 차원을 확장하려 하면 runtime erro

cccaaa.tistory.com

 

반응형