반응형

 

torch.randint — PyTorch 1.13 documentation

Shortcuts

pytorch.org

torch.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

 

low(포함, optional)와 high(제외, 필수) 사이에서 uniformly 하게 생성된 랜덤 정수로 채워진 텐서 반환

텐서는 shape를 가지는데, size(튜플형, 필수) 변수의 크기로 정의됨

  • low (int, optional) – Lowest integer to be drawn from the distribution. Default: 0.
  • high (int) – One above the highest integer to be drawn from the distribution.
  • size (tuple) – a tuple defining the shape of the output tensor.
>>> torch.randint(3, 5, (3,))
tensor([4, 3, 4])


>>> torch.randint(10, (2, 2))
tensor([[0, 2],
        [5, 5]])
        

>>> torch.randint(3, 10, (2, 2))
tensor([[4, 5],
        [6, 7]])

 

 


 

torch.Tensor.item — PyTorch 1.13 documentation

Shortcuts

pytorch.org

Tensor.item() → number
  • 텐서를 표준 python 숫자형으로 반환
  • 크기가 (1, )인 텐서에만 작동됨. 다른 경우는 Tensor.tolist() 사용
  • 미분 불가
>>> x = torch.tensor([1.0])
>>> x.item()
1.0

 


 

 

torch.Tensor.tolist — PyTorch 1.13 documentation

Shortcuts

pytorch.org

Tensor.tolist() → list or number
  • 텐서를 중첩된(nested) 리스트로 반환
  • 스칼라의 경우 Tensor.item()과 마찬가지로 standard Python number로 반환됨.
  • 텐서는 필요한 경우 자동으로 먼저 CPU로 이동됨
  • 미분 불가
>>> a = torch.randn(2, 2)

>>> a.tolist()
[[0.012766935862600803, 0.5415473580360413],
 [-0.08909505605697632, 0.7729271650314331]]

>>> a[0,0].tolist()
0.012766935862600803

 

반응형

'Pytorch > 함수들' 카테고리의 다른 글

[PyTorch] autograd hook이란? Tensor.register_hook(hook)  (0) 2022.12.30

+ Recent posts