pytorch
1. 텐서 기초
륵기
2020. 3. 4. 15:46
728x90
반응형
import torch # 파이토치
import numpy as np # 행렬 연산 위함
# 배열 생성
arr = np.array([1,2,3,4,5])
# [1 2 3 4 5]
arr.dtype # 배열 타입 확인
# dtype('int32')
#토치를 이용한 배열 생성
x = torch.from_numpy(arr)
x # tensor([1, 2, 3, 4, 5], dtype=torch.int32)
torch.as_tensor(arr)
# tensor([1, 2, 3, 4, 5], dtype=torch.int32)
#2차원 배열 생성
arr2d = np.arange(0.0,12.0) # 0부터 12까지의 수로 배열 생성
x2 = torch.from_numpy(arr2d)
print(x2)
'''
tensor([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.],
[ 9., 10., 11.]], dtype=torch.float64)
'''
파이토치를 이용한 배열 생성을 알아보았다.
numpy 행렬을 torch tensor로 바꾸기 위한 함수는 아래와 같다
- from_numpy, tensor, as_tensor
728x90
반응형