Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- torch
- 표준편차
- leetcode
- GRU
- 설명의무
- 자연어 논문
- sigmoid
- Window Function
- 카이제곱분포
- nlp논문
- 논문리뷰
- sql
- 자연어 논문 리뷰
- 코딩테스트
- 자연어처리
- 그룹바이
- Statistics
- t분포
- HackerRank
- LSTM
- SQL코테
- update
- CASE
- airflow
- inner join
- 짝수
- NLP
- MySQL
- SQL 날짜 데이터
- 서브쿼리
Archives
- Today
- Total
HAZEL
[Torch_Basic ML 01 ] Tensor Manipulation( 텐서 조작하기 ) 본문
DATA ANALYSIS/ML & DL
[Torch_Basic ML 01 ] Tensor Manipulation( 텐서 조작하기 )
Rmsid01 2021. 10. 13. 20:56boostcourse 의 파이토치로 시작하는 딥러닝 기초 강의를 듣고 정리한 내용입니다.
1. 텐서 ( Tensor )
- 2D Tensor = ( batch size , dim )
- 3D Tensor = ( batch size, width, height ) - Computer vision
- 3D Tensor = ( batch size, length, dim ) - 순차정보, NLP : batch size 만큼 문장이 존재한다는 의미
- 1D Array with PyTorch
# torch 를 선언하기
t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6.])
print('t : ',t)
print('몇개의 차원으로 이루어졌나? : ', t.dim()) # rank
print('Shape : ', t.shape)
print('Shape : ', t.size())
print()
print('t[0], t[1], t[-1] = ', t[0], t[1], t[-1]) # Element
print('t[2:5] t[4:-1] = ', t[2:5], t[4:-1]) # Slicing
print('t[:2] t[3:] = ', t[:2], t[3:]) # # Slicing
- 2D Array with PyTorch
t= torch.FloatTensor([[1,2,3], [4,5,6],[7,8,9], [10,11,12]])
print('t : ','\n', t)
print()
print('몇개의 차원으로 이루어졌나? : ', t.dim())
print('Shape : ', t.shape)
print('Shape : ', t.size())
print()
print(t[:,1]) # 첫번째 차원은 다 가져오고, 두번째꺼에서는 1 번째만 가져오기
print(t[:,1].size()) # 벡터
print(t[:, :-1])
- Broadcasting
- pytorch에는 Broadcasting기능을 제공한다.
- 자동적으로 사이즈를 맞춰서 연산을 수행한다.
- Broadcasting 는 자동적으로 실행되기 때문에, 사용자 입장에서 조심스럽게 사용해야한다.
원하는 용도로 계산되는 것이 아니라, 에러 없이 계산되면 그게 더 문제일 수도 있다.
# same shape
m1 = torch.FloatTensor([[3,3]])
m2 = torch.FloatTensor([[2,2]])
print(m1 + m2) # tensor([[5., 5.]])
print()
# vector + scalar
# 원래는 크기가 안맞아서 연산이 안되지만, 자동으로 사이즈를 맞춰줌
m1 = torch.FloatTensor([[1,2]])
m2 = torch.FloatTensor([[3]]) # 3 - > [[3,3]]
print(m1 + m2) # [[1,2]] + [[3,3]] = [[4, 5]] # tensor([[4., 5.]])
print()
# 2 x 1 vector + 1 x 2 vector
# 텐서간의 연산에도 자동으로 1이 들어있는 차원을 늘려주어서 적용됨
m1 = torch.FloatTensor([[1,2]])
m2 = torch.FloatTensor([[3],[4]])
print(m1 + m2)
# [[1,2],[1,2]] + [[3,3],[4,4]] = [ [4,5],[5,6]]
# tensor([[4., 5.],
# [5., 6.]])
- Multiplication vs Matrix Multiplication ( 곱셈 vs 행렬 곱셈 )
print('일반적인 곱셈')
print('--------------')
m1 = torch.FloatTensor([[1,2],[3,4]])
m2 = torch.FloatTensor([[1],[2]])
print('Shape of Matrix 1 :', m1.shape) # 2 x 2
print('Shape of Matrix 1 :', m2.shape) # 2 x 1 -> 브로드 캐스팅 2 X 2 됨
print('mul : ','\n',m1 * m2) # 2 x 2
# [[1,2],[3,4]] X [[1,1], [2,2]]= [[1,2],[6,8]]
print()
print(m1.mul(m2))
print()
print('행렬 곱셈')
print('--------------')
m1 = torch.FloatTensor([[1,2],[3,4]])
m2 = torch.FloatTensor([[1],[2]])
print('Shape of Matrix 1 :', m1.shape) # 2 x 2
print('Shape of Matrix 1 :', m2.shape) # 2 x 1
# 행렬곱은 뒤의 차원과 앞의 차원의 크기가 같으면 됨
print('mul : ','\n',m1.matmul(m2)) # 2 x 1
- Mean
t= torch.FloatTensor([1,2])
print('mean :', t.mean()) # mean : tensor(1.5000)
# longTensor 에 대해서는 mean을 잘 수행하지 못한다.
t = torch.LongTensor([1,2])
try :
print(t.mean)
except Exception as exc:
print(exc)
# <built-in method mean of Tensor object at 0x00000216EC1A6048>
t = torch.FloatTensor([[1,2],[3,4]])
print(t)
# tensor([[1., 2.],
# [3., 4.]])
print(t.mean()) # tensor(2.5000)
print(t.mean(dim=0)) # dimension argument - dim 0을 없앤다고 생각하면 됨 # tensor([2., 3.]) # tensor([1.5000, 3.5000])
print(t.mean(dim=1)) # tensor([1.5000, 3.5000])
print(t.mean(dim=-1)) # tensor([1.5000, 3.5000])
- Sum
t = torch.FloatTensor([[1,2],[3,4]])
print(t)
# tensor([[1., 2.],
# [3., 4.]])
print(t.sum()) # tensor(10.)
print(t.sum(dim=0)) # tensor([4., 6.])
print(t.sum(dim=1)) # tensor([3., 7.])
print(t.sum(dim=-1)) # tensor([3., 7.])
- Max and Argmax
- max : tensor나 행렬에 대해서 가장 큰값을 찾아주는 것
- argmax : 그 인덱스 값을 리턴해주는 것
t = torch.FloatTensor([[1,2],[3,4]])
print(t)
print(t.max()) # tensor(4.)
print()
# dimension 의 정보도 줄 수 있음
print(t.max(dim=0)) # max 의 값 뿐만 아니라, 인덱스값도 같이 받아줌
# torch.return_types.max(
# values=tensor([3., 4.]),
# indices=tensor([1, 1]))
print()
print('MAX : ', t.max(dim=0)[0]) # MAX : tensor([3., 4.])
print()
print('Argmax : ', t.max(dim=0)[1]) # Argmax : tensor([1, 1])
print()
print(t.max(dim=1))
# torch.return_types.max(
# values=tensor([2., 4.]),
# indices=tensor([1, 1]))
print()
print(t.max(dim=-1))