Function 1 — torch.tensor
t1 = torch.tensor([[21,39],[31,30],[23,43],[11,46],[26,46],[31,25],[21,38],[22,39],[22,19],[18, 14]])
t1

t2 = torch.tensor([])
t2
t2.size()


Function 2 — torch.from_numpy
a1 = np.array([[1,2,3],[4,5,6]])
a1.dtype
t1 = torch.from_numpy(a1)
t1.dtype

Hight_Weight = np.array([[161,67],[154,76],[172, 61]])
Heart_Rate = np.array([78,89,72])
(Hight_Weight.dtype, Heart_Rate.dtype)
l2 = (Hight_Weight, Heart_Rate)
for i in l2 :
tensor = torch.from_numpy(i)
print("Type of the element\n {}\n is {}\n".format(i, tensor.dtype))

A NumPy array containing string elements cannot be converted to a tensor. The only supported types for a tensor are : float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.
Function 6 — torch.eye
torch.eye(n=4, m=5)
torch.eye(n=3)


Function 7 — torch.full
torch.full(size=(3,2), fill_value=10)
torch.full(size=[2, 3, 4], fill_value=5)


Function 11 — torch.view
# Example 1 - working
random_tensor = torch.arange(1., 17.)
print(random_tensor)
random_tensor.view(8,2)
tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.,
15., 16.])
tensor([[ 1., 2.],
[ 3., 4.],
[ 5., 6.],
[ 7., 8.],
[ 9., 10.],
[11., 12.],
[13., 14.],
[15., 16.]])
z = x.view(-1, 8)
[Pytorch] torch 유용한 함수 정리하기
유용한 함수들을 발견하게 되면 정리해보기 개인적으로 중요하다고 생각하는 것에 ★ 표시 import torch import numpy as np Function 1 — torch.tensor t1 = torch.tensor([[21,39],[31,30],[23,43],[11,46],[26..
data-newbie.tistory.com
'AI > PyTorch' 카테고리의 다른 글
[PyTorch] Linear Regression 작성하기 (0) | 2021.06.01 |
---|---|
[PyTorch] Tensor와 NumPy (0) | 2021.06.01 |
[PyTorch] package (0) | 2021.05.24 |
[pyTorch] 이미지 여는 방법 (0) | 2021.05.12 |
[PyTorch] torch view, squeeze, unsqueeze (0) | 2021.04.27 |