rueki

3. 파이토치 기본 연산 본문

pytorch

3. 파이토치 기본 연산

륵기 2020. 3. 5. 16:57
728x90
반응형
import numpy as np
import torch

x = torch.arange(6).reshape(3,2)
x
'''
tensor([[0, 1],
        [2, 3],
        [4, 5]])
'''

x[:,1] # tensor([1, 3, 5]), 1번째 열 원소만 슬라이싱


x = torch.arange(10)
x
# tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# view 함수
x.view(2,5)
'''
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
'''

# reshape 함수
x.reshape(2,5)
'''
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
'''

a = torch.tensor([1.,2.,3.])
b= torch.tensor([4.,5.,6.])

a + b #tensor([5., 7., 9.])
torch.add(a,b) #tensor([5., 7., 9.])

a.mul(b) #tensor([ 4., 10., 18.])

# 행렬곱연산
a.dot(b) # tensor(32.)


#2x3 생성
a = torch.tensor([[0,2,4],[1,3,5]])
#3x2 생성
b = torch.tensor([[6,7],[8,9],[10,11]])


torch.mm(a,b )#matrix multiplication
'''
tensor([[56, 62],
        [80, 89]])
'''

a @ b
'''
tensor([[56, 62],
        [80, 89]])
'''

x.numel # 원소개수
# 4

view = reshape , 똑같이 출력되는 것 확인

numel = 원소 개수 출력

torch.add , torch.mul = 덧셈과 곱셈

torch.dot = 행렬곱연산

torch.mm = 행렬 곱셈

728x90
반응형

'pytorch' 카테고리의 다른 글

6. Pytorch를 이용한 ANN 구현  (0) 2020.03.07
5. pytorch를 이용한 Linear Regression  (0) 2020.03.07
4. Gradient Descent  (0) 2020.03.05
2. 텐서 기초2  (0) 2020.03.04
1. 텐서 기초  (0) 2020.03.04
Comments