1. Tensors

ingsol·2023년 1월 7일
0

PyTorch

목록 보기
1/8
post-thumbnail

0. PyTroch란?

1. Tensor란?

  • Tensor는 pytorch의 자료 형
  • Tensor는 단일 데이터 타입으로 된 자료들의 다차원 행렬
  • Tensor는 간단한 명령어를 통해서 GPU로 연산을 수행하게 만들 수 있음
  • Tensor 변수 뒤에 .cuda()를 추가하며 됨

2. Tensors의 종류

3. Tensor의 선언

  • torch.Tensor(tensor의 크기)
x = torch.Tensor(3,3,3)
    
# 0~1사이의 uniform distribution random 값
torch.rand(3,3)
    
# 평균0, 분산 1인 normal distribution random 값
torch.randn(3,3)
    
# Tensor에서 Numpy로
a = np.array([1,2,3,4])
b = torch.Tensor(a)
    
# Tensor에서 Numpy로
a = torch.rand(3,3)
b = a.numpy()
    
# Tensor의 형태 변환(view)
a = torch.rand(3,3)
a = a.view(1,1,3,3)
    
# Tensor 합치기
torch.cat((Tensor_A, Tensor_B), dim)
a = torch.randn(1,1,3,3)
b = torch.randn(1,1,3,3)
c = torch.cat((a,b), 0)
    
# Tensor 계산을 GPU로
x = torch.rand(3,3)
y = torch.rand(3,3)
if torch.cuda.is_available():
	x = x.cuda() #선언된 x를 GPU에 올려줌
    y = y.cuda()
   	sum = x + y #이 연산을 GPU로 계산
        
# 그 외
a.mean()
a.sum()

0개의 댓글