본문 바로가기
Error Note

RuntimeError: view size is not compatible with input tensor's size and stride ~

by ga.0_0.ga 2023. 1. 30.
728x90
반응형
반응형

- 전체 에러 문구

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

expand(), view(), transpose() 등의 함수를 이용해 텐서의 모양을 바꿀 때 새로운 텐서를 생성하는 것이 아니라, 저장된 메모리의 주소는 그대로 둔체 텐서의 모양만 바꾸게 됩니다. 이런 경우 메모리 주소가 연속적이지 않게 변하게되므로 발생하는 에러입니다.

- 해결 방법

연속적이지 않은 메모리 주소를 연속적이게 만들어주면 됩니다.

1) contiguous() 사용하기

x.contiguous().view([-1])

 

2) reshape() 사용하기

x.reshape([-1])

메모리 주소의 연속성인 contiguous()에 대한 자세한 설명은 이곳을 참고해주세요!

 

728x90
반응형

댓글