티스토리 뷰
torchscript는 list를 생성하게 되면 기본적으로
List[T], 즉, tensor를 member로 가지는 list가 생성이 된다.
그냥 python code를 실행할 때는 문제가 되지 않는 코드이다.
tmp = []
for i in range(0, 10, 1):
tmp.append(i)
new_tmp = torch.tensor(tmp)
==> 빈 list를 하나 선언해주고 그 안에 int형을 담은 다음 list를 tensor로 변환해주는 torch.tensor 함수를 사용.
그러나 이 부분을 torchscript로 변환하려고 하면 error가 뜬다.
잘 변환이 되도 C++에서 불러와 쓰려고 하면 저 부분에서 다음과 같은 runtime error가 뜬다.
RuntimeError: Input must be of ints, floats, or bools, got Tensor
이를 어떻게 해결하나 봤더니,
python code 상에서 list를 선언할 때 int 형을 받도록 하는 code를 추가해줘야 했다.
tmp = [] (x)
tmp: List[int] = [] (o)
tmp: List[int] = [] 을 사용하면 int를 받는 list를 선언한 것과 동일한 효과가 난다.
참고로 List를 쓸 때는
from typing import List
을 이용하여 import를 해줘야한다.
https://pytorch.org/docs/stable/jit_language_reference.html
TorchScript Language Reference — PyTorch 1.9.1 documentation
TorchScript is a statically typed subset of Python that can either be written directly (using the @torch.jit.script decorator) or generated automatically from Python code via tracing. When using tracing, code is automatically converted into this subset of
pytorch.org
공식문서의 Default Types 부분을 참고하면 더 도움이 될 듯! (예시 코드도 있음)
'이것저것 자료 > Python' 카테고리의 다른 글
- Total
- Today
- Yesterday
- numpy
- pytorch
- 설치
- 설치하기
- tensorflow
- 다익스트라
- BFS
- torch
- version
- Python
- 프로그래머스
- 백트래킹
- 이것이코딩테스트다
- dfs
- shellscript
- n과m
- CUDA
- notfound
- 카카오
- 백준
- 파이썬
- LGSVL
- 최소신장트리
- torchscript
- matplotlib
- PIP
- 코딩테스트
- 동적프로그래밍
- docker
- error
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |