1. Pytorch Tensor[파이토치, 텐서]
딥러닝 프레임워크인 Pytorch의 기본 개념인 Tensor에 대해서 정리해보자.
Tensor는 numpy의 ndarray와 비슷하며, GPU를 사용한 연산 가속이 가능하다.
-생성(torch. + empty, ones, rand, zeros, tensor )
#empty
x = torch.empty(5,5)
print(x)
#ones
y = torch.ones(5,5)
print(y)
#zeros
z = torch.zeros(3,3)
print(z)
#rand
a = torch.rand(4,2)
print(a)
#torch.tensor
b = torch.tensor([[1.1,2.2],[2.2,3.3]])
print(b)
#x.size
q = torch.tensor([[1.1,2.2,4.4],[2.2,3.3,5.5]])
print(q.size())
#type(x)
w = torch.tensor([[1.1,2.2,4.4],[2.2,3.3,5.5]])
print(type(w))
-연산부분
x = torch.rand(2,2)
y = torch.rand(2,2)
print(x+y)
x = torch.rand(2,2)
y = torch.rand(2,2)
print(y.add(x))
print(y.add_(x))
y = torch.rand(2,2)
print(y[1,1])
print(y[:,1])
#view
x= torch.rand(8,8)
print(x.view(64))
#torch <-> numpy 형변환
x = torch.rand(2,2)
print(type(x))
y = x.numpy()
print(type(y))
#item -> tensor가 스칼라 값일 떄 사용가능
x = torch.ones(1)
print(x)
print(x.item())
'AI 기술 정리 > Pytorch' 카테고리의 다른 글
2. Pytorch 미분 연산 개념 (0) | 2021.01.20 |
---|
댓글