now is better than never
[인공지능(AI) 기초 다지기] 5. 딥러닝 핵심 기초 (1) 본문
https://www.boostcourse.org/ai100/joinLectures/233691?isDesc=false
인공지능(AI) 기초 다지기
부스트코스 무료 강의
www.boostcourse.org
1. Machine Learning & PyTorch Basic
1) Tensor Manipulation 1
- Vector, Matrix and Tensor
- vector : 1차원 (차원이 없으면 scalar)
matrix : 2차원
tensor : 3차원
- 2D Tensor (Typical Simple Setting)
|t| = (batch size, dim)
(= batch size * dim) (64 * 256) - 3D Tensor (Typical Computer Vision)
|t| = (batch size, width, height) - 3D Tensor (Typical Natural Language Processing) : 시계열/seqeuncial 데이터
|t| = (batch size, length, dim)
하나의 문장(dim)이 시간(timestamp=length)만큼 쌓여있다(batchsize)
- PyTorch Tensor Allocation
# numpy
t = np.array([0., 1., 2., 3., 4., 5., 6.])
# pytorch
t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6.])
- Broadcasting : 자동적으로 사이즈를 맞춰서 연산
# vector + scalar
m1 = torch.FloatTensor([[1, 2]])
m2 = torch.FloatTensor([3])# scalar [3] -> [[3, 3]]
print(m1 + m2)
# tensor([[4., 5.]])
# 2x1 vector + 1x2 vector
m1 = torch.FloatTensor([[1, 2]]) # (1, 2) -> (2, 2)
m2 = torch.FloatTensor([[3], [4]]) # (2, 1) -> (2, 2)
print(m1 + m2)
# tensor([[4., 5.],
# [5., 6.]])
- Matrix Multiplication (행렬곱)
- Multiplication
같은 위치의 행렬 요소끼리 곱합 - Matrix Multiplication
m1 = torch.FloatTensor([[1, 2], [3, 4]])
m2 = torch.FloatTensor([[1], [2]]) # broadcasting -> [[1, 1], [2, 2]]
print(m1 * m2) # multiplication
print(m1.mul(m2))
'''
tensor([[1., 2.],
[6., 8.]])
'''
print(m1.matmul(m2)) # matrix multiplication
'''
tensor([[ 5.],
[11.]])
'''
- Mean
sum 도 똑같음
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t.mean())
print(t.mean(dim=0))
print(t.mean(dim=1))
print(t.mean(dim=-1))
'''
tensor(2.500) # 전체 element
tensor([2., 3.]) # 행 (2, 2) -> (1, 2) = (2,)
tensor([1.5000, 3.5000]) # 열
tensor([1.5000, 3.5000])
'''
- Max ans Argmax
Argmax : max의 index 값 리턴
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t.max(dim=0)) # [?, ?] 값으로 리턴 <- 열 방향에서 max값 찾아서 리턴
# (tensor([3., 4.]), tensor([1, 1]))
# tensor(max value), tensor(max index) = max, argmax
print(t.max(dim=1)) # 세로로 리턴 <- 행 방향에서 max값 찾아서 리턴
print(t.max(dim=-1))
# (tensor([2., 4.]), tensor([1, 1]))
'Python > [코칭스터디 9기] 인공지능 AI 기초 다지기' 카테고리의 다른 글
[인공지능(AI) 기초 다지기] 4. 기초튼튼, 수학튼튼 (5) (0) | 2023.02.22 |
---|---|
[인공지능(AI) 기초 다지기] 4. 기초튼튼, 수학튼튼 (4) (0) | 2023.02.20 |
[인공지능(AI) 기초 다지기] 4. 기초튼튼, 수학튼튼 (3) (0) | 2023.02.17 |
[인공지능(AI) 기초 다지기] 4. 기초튼튼, 수학튼튼 (2) (0) | 2023.02.14 |
[인공지능(AI) 기초 다지기] 4. 기초튼튼, 수학튼튼 (1) (0) | 2023.02.14 |