[파이토치로 시작하는 딥러닝 기초]01_Tensor Manipulation(텐서 조작)

2020. 11. 26. 15:42·AI Study/DL_Basic
반응형
 

Tensor Manipulation(텐서 조작)¶

Pytorch Basic Tensor Manipulation¶

  • Vector, Matrix and Tensor
  • Numpy Review
  • Pytorch Tensor Allocation
  • Matrix Multiplication
  • Other Basic Ops

Vector, Matrix and Tensor¶

  • 스칼라(Scaler) : 차원이 없는 값(0차원)
  • 벡터(Vector) : 1차원으로 이루어져있는 값
  • 행렬(Matrix) : 2차원으로 이루어져 있는 값
  • 텐서(Tensor) : 3차원 이상으로 이루어져 있는 값

Pytorch Tensor Shape Convention¶

텐서의 크기 표현하는 방법

  • 2D Tensor (Typical Simple Setting)

    • |t| = (atchsize,dim)
    •  
  • 3D Tensor (Typical Computer Vision, 이미지 데이터 분석)

    • |t|= (batchsize,width,heigbatchsize,width,height) )
    •  
  • 3D Tensor (Typical Natural Language Processing, NLP 저연어 처리 분석)

    • |t|= (batchsize,length,dim)
    •  
In [1]:
import numpy as np
import torch
 

1D Array with Numpy¶

In [2]:
t = np.array([0., 1., 2., 3., 4., 5., 6. ])
print(t)
 
[0. 1. 2. 3. 4. 5. 6.]
In [3]:
print('Rank  of t : ', t.ndim) ## 몇개의 차원으로 되어있는가?
print('Shape of t : ', t.shape) ## 어떤 형태를 가지고 있니?
 
Rank  of t :  1
Shape of t :  (7,)
In [4]:
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
 
t[0], t[1], t[-1] =  0.0 1.0 6.0
t[2:5], t[4:-1]   =  [2. 3. 4.] [4. 5.]
t[:2], t[3:]      =  [0. 1.] [3. 4. 5. 6.]
 

2D Array with Numpy¶

In [5]:
t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]])
print(t)
 
[[ 1.  2.  3.]
 [ 4.  5.  6.]
 [ 7.  8.  9.]
 [10. 11. 12.]]
In [6]:
print('Rank  of t : ', t.ndim) ## 몇개의 차원으로 되어있는가?
print('Shape of t : ', t.shape) ## 어떤 형태를 가지고 있니?
 
Rank  of t :  2
Shape of t :  (4, 3)
 

1D Array with Pytorch¶

In [7]:
t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6. ]) ## numpy와 작동방식이 동일
print(t)
 
tensor([0., 1., 2., 3., 4., 5., 6.])
In [8]:
print(t.dim())   # rank
print(t.shape)   # shape
print(t.size())  # shape
print(t[0], t[1], t[-1]) # Element
print(t[2:5], t[4:-1])   # Slicing
print(t[:2], t[3:])      # Slicing
 
1
torch.Size([7])
torch.Size([7])
tensor(0.) tensor(1.) tensor(6.)
tensor([2., 3., 4.]) tensor([4., 5.])
tensor([0., 1.]) tensor([3., 4., 5., 6.])
 

2D Array with Pytorch¶

In [9]:
t = torch.FloatTensor([[1., 2., 3.],
                       [4., 5., 6.], 
                       [7., 8., 9.], 
                       [10., 11., 12.]
                      ])
print(t)
 
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.],
        [10., 11., 12.]])
In [10]:
print(t.dim())   # rank
print(t.size())  # shape
print(t[:, 1])
print(t[:, 1].size())
print(t[:, :-1])
 
2
torch.Size([4, 3])
tensor([ 2.,  5.,  8., 11.])
torch.Size([4])
tensor([[ 1.,  2.],
        [ 4.,  5.],
        [ 7.,  8.],
        [10., 11.]])
 

Broadcasting¶

파이토치를 어떻게 다루는가?

  • 행렬을 다룰 때의 규칙
    • 덧셈이나 뺄셈을 할 때에는 애초에 두 텐서 간의 크기가 같아야 한다.
    • 행렬 곱을 수행할 때 마지막 차원과 첫번째 차원이 일치해야 한다.

그러나 다른 형태의 행렬을 불가피하게 사칙연산을 수행해야 하는 경우가 있는데, 이 때 broadcasting을 통해서 자동적으로 size를 맞춰 진행할 수 있다.

In [11]:
# Same shpae
m1 = torch.FloatTensor([[3, 3]])  ## 1X2 matrix
m2 = torch.FloatTensor([[2, 2]])  ## 1X2 matrix
print(m1 + m2)
 
tensor([[5., 5.]])
In [12]:
# Vector + scalar
m1 = torch.FloatTensor([[1, 2]])
m2 = torch.FloatTensor([3])  # 3 -> [[3, 3]]
print(m1 + m2)
 
tensor([[4., 5.]])
In [13]:
# 2 X 1 Vectpr + 1 X 2 Vector
m1 = torch.FloatTensor([[1, 2]])  ## [[1, 2]] -> [[1, 2],[1, 2]]
m2 = torch.FloatTensor([[3], [4]])  ## [[3], [4]] -> [[3, 3],[4, 4]]
print(m1 + m2)
 
tensor([[4., 5.],
        [5., 6.]])
 

Broadcasting은 자동으로 실행되는 것이기 때문에 실수를 하지는 않는지 조심해서 확인해야한다.

 

Multiplication vs Matrix Multiplication¶

딥러닝은 행렬 곱 연산을 굉장히 많이 사용하는 알고리즘이다. 그래서 행렬곱을 구현하는 것이 매우 중요하다.

  • 일반 matrix 곱 : 각 원소끼리의 곱. 일반적으로 행렬의 형태가 같아야 곱셈이 되지만, 행렬의 형태가 다른 경우 broadcasting 되어서 곱해진다.
  • 행렬 곱(내적, inner product) : 가장 마지막의 차원과 첫번째의 차원이 같아야 행렬곱이 가능해짐.
In [14]:
print()
print('-------------')
print('Mul vs Matmul')
print('-------------')
m1 = torch.FloatTensor([[1, 2], [3, 4]])
m2 = torch.FloatTensor([[1], [2]])
print('Shape or Matrix 1 : ',m1.shape)  # 2 X 2
print('Shape or Matrix 2 : ',m2.shape)  # 2 X 1
print(m1.matmul(m2)) # 2 X 1

m1 = torch.FloatTensor([[1, 2], [3, 4]])
m2 = torch.FloatTensor([[1], [2]])
print('Shape or Matrix 1 : ',m1.shape)  # 2 X 2
print('Shape or Matrix 2 : ',m2.shape)  # 2 X 1
print(m1.mul(m2))
 
-------------
Mul vs Matmul
-------------
Shape or Matrix 1 :  torch.Size([2, 2])
Shape or Matrix 2 :  torch.Size([2, 1])
tensor([[ 5.],
        [11.]])
Shape or Matrix 1 :  torch.Size([2, 2])
Shape or Matrix 2 :  torch.Size([2, 1])
tensor([[1., 2.],
        [6., 8.]])
 

Mean¶

행렬의 평균 구하기

In [15]:
t = torch.FloatTensor([1, 2])
print(t.mean())
 
tensor(1.5000)
In [16]:
# LongTensor에 대해서는 평균을 구할 수 없다. 
# Can't use mean() on integers
t = torch.LongTensor([1, 2])
try : 
    print(t.mean())
except Exception as exc:
    print(exc)
 
Can only calculate the mean of floating types. Got Long instead.
 

우리가 원하는 차원에 대해서만 평균을 구할 수 있다.

You can also use t.mean for higer rank tensors to get mean of all elements or mean by particuler dimension.

In [17]:
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t)
 
tensor([[1., 2.],
        [3., 4.]])
In [18]:
print(t.mean())           # 모든 원소값의 평균
print(t.mean(dim = 0))    # 0차원을 없애겠어. 2 x 2 -> 1 x 2
print(t.mean(dim = 1))    # 1차원을 없애겠어. 2 x 2 -> 2 x 1
print(t.mean(dim = -1))   # 가장 마지막 차원을 없애겠어
 
tensor(2.5000)
tensor([2., 3.])
tensor([1.5000, 3.5000])
tensor([1.5000, 3.5000])
 

sum¶

In [19]:
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t)
 
tensor([[1., 2.],
        [3., 4.]])
In [20]:
print(t.sum())           # 모든 원소값의 합
print(t.sum(dim = 0))    # 0차원을 없애겠어. 2 x 2 -> 1 x 2
print(t.sum(dim = 1))    # 1차원을 없애겠어. 2 x 2 -> 2 x 1
print(t.sum(dim = -1))   # 가장 마지막 차원을 없애겠어
 
tensor(10.)
tensor([4., 6.])
tensor([3., 7.])
tensor([3., 7.])
 

Max and Argmax¶

  • Max : 어떤 텐서나 행렬에 대해 가장 큰 값을 찾아주는 것을 말한다.
  • Argmax : max값의 index를 리턴해주는 경우
In [21]:
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t)
 
tensor([[1., 2.],
        [3., 4.]])
In [22]:
print(t.max()) # Returns one value : max
 
tensor(4.)
In [23]:
print(t.max(dim = 0)) # max값과 그에 해당하는 index를 알려줌
print('Max : ', t.max(dim = 0)[0])
print('Argmax : ', t.max(dim = 0)[1])
 
torch.return_types.max(
values=tensor([3., 4.]),
indices=tensor([1, 1]))
Max :  tensor([3., 4.])
Argmax :  tensor([1, 1])
 

View (Reshape)¶

view는 numpy에서 reshape라는 함수와 같은 역할을 한다.
Reshape를 다시 만들어준다는 의미와 같다.

In [24]:
t = np.array([[[0, 1, 2], 
               [3, 4, 5]],
             
              [[6, 7, 8],
               [9, 10, 11]]])
ft = torch.FloatTensor(t)
print(ft.shape)
 
torch.Size([2, 2, 3])
In [25]:
print(ft.view([-1, 3])) # 두개의 차원인데, 뒷 차원만 3으로 맞춰줘
print(ft.view([-1, 3]).shape)
 
tensor([[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [ 6.,  7.,  8.],
        [ 9., 10., 11.]])
torch.Size([4, 3])
In [26]:
print(ft.view([-1, 1, 3])) # 3개의 차원인데 2번째, 3번재 dim을 1, 3 으로 맞춰줘
print(ft.view([-1, 1, 3]).shape)
 
tensor([[[ 0.,  1.,  2.]],

        [[ 3.,  4.,  5.]],

        [[ 6.,  7.,  8.]],

        [[ 9., 10., 11.]]])
torch.Size([4, 1, 3])
 

Squeeze¶

기본적으로 view와 비슷하나 자동으로 내가 원하는 dimension의 element 개수가 1인 경우에 해당 demension을 없애줌

In [27]:
ft = torch.FloatTensor([[0],[1],[2]])
print(ft)
print(ft.shape)
 
tensor([[0.],
        [1.],
        [2.]])
torch.Size([3, 1])
In [28]:
print(ft.squeeze())
print(ft.squeeze().shape)
 
tensor([0., 1., 2.])
torch.Size([3])
 

Unsqueeze¶

내가 원하는 dimension에 1을 넣어주는 함수

In [29]:
ft = torch.Tensor([0, 1, 2])
print(ft.shape)
 
torch.Size([3])
In [30]:
print(ft.unsqueeze(0)) ## 괄호 안에 차원의 rank를 넣어줌
print(ft.unsqueeze(0).shape) # 3 -> 1 x 3
 
tensor([[0., 1., 2.]])
torch.Size([1, 3])
In [31]:
print(ft.view(1, -1)) # 두개의 차원을 만들 건데, 0번째 차원의 dimension은 1이야.
print(ft.view(1, -1).shape) # 3 -> 1 x 3
 
tensor([[0., 1., 2.]])
torch.Size([1, 3])
In [32]:
print(ft.unsqueeze(1)) # 1번째 차원의 dimeansion에 1을 넣어줌
print(ft.unsqueeze(1).shape) # 3 -> 3 x 1
 
tensor([[0.],
        [1.],
        [2.]])
torch.Size([3, 1])
In [33]:
print(ft.unsqueeze(-1)) # 가장 마지막 차원에 1을 넣어줌
print(ft.unsqueeze(-1).shape) # 3 -> 3 x 1
 
tensor([[0.],
        [1.],
        [2.]])
torch.Size([3, 1])
 

Type Casting¶

텐서의 타입을 바꿔주는 것

In [34]:
#integer 형태의 tesnor
lt = torch.LongTensor([1, 2, 3, 4])
print(lt)
 
tensor([1, 2, 3, 4])
In [35]:
print(lt.float())
 
tensor([1., 2., 3., 4.])
In [36]:
# Boolean 형태의 tensor
bt = torch.ByteTensor([True, False, False, True])
print(bt)
 
tensor([1, 0, 0, 1], dtype=torch.uint8)
In [37]:
print(bt.long())
print(bt.float())
 
tensor([1, 0, 0, 1])
tensor([1., 0., 0., 1.])
 

Concatenate¶

두개의 텐서를 이어붙이는 함수

In [38]:
x = torch.FloatTensor([[1, 2], [3, 4]]) # 2 x 2
y = torch.FloatTensor([[5, 6], [7, 8]]) # 2 x 2
In [39]:
print(torch.cat([x, y], dim = 0)) # 0번째 차원을 기준으로 concatenate
print(torch.cat([x, y], dim = 1)) # 1번째 차원을 기준으로 concatenate
 
tensor([[1., 2.],
        [3., 4.],
        [5., 6.],
        [7., 8.]])
tensor([[1., 2., 5., 6.],
        [3., 4., 7., 8.]])
 

Stacking¶

concatenate와 비슷한 함수. 몇가지를 함축시켜놓음

In [40]:
# x, y, z shape : (2,)
x = torch.FloatTensor([1, 4])
y = torch.FloatTensor([2, 5])
z = torch.FloatTensor([3, 6])
In [41]:
print(torch.stack([x, y, z]))
print(torch.stack([x, y, z], dim = 1))
 
tensor([[1., 4.],
        [2., 5.],
        [3., 6.]])
tensor([[1., 2., 3.],
        [4., 5., 6.]])
In [42]:
# 위 stack 함수를 cat으로 구현하면 아래와 같음
print(torch.cat([x.unsqueeze(0), y.unsqueeze(0), z.unsqueeze(0)], dim = 0))
 
tensor([[1., 4.],
        [2., 5.],
        [3., 6.]])
 

Ones and Zeros¶

정의한 dimension 만큼 원소가 0 또는 1로만 이루어진 행렬을 생성하는 함수

In [43]:
x = torch.FloatTensor([[0, 1, 2], [2, 1, 0]])
print(x)
 
tensor([[0., 1., 2.],
        [2., 1., 0.]])
In [44]:
print(torch.ones_like(x))  ## x와 동일한 shape의 0으로만 이루어진 행렬 생성
print(torch.zeros_like(x)) ## x와 동일한 shape의 1로만 이루어진 행렬 생성
 
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor([[0., 0., 0.],
        [0., 0., 0.]])
 

In-place Operation¶

  • inplace 선언문으로 메모리에 새로 선언하지 않고 기존 메모리 텐서에 정답 값을 넣는 연산
  • 함수 뒤에 '_'를 붙여준다.
In [45]:
x = torch.FloatTensor([[1, 2], [3, 4]])
In [46]:
print(x.mul(2.))
print(x)
print(x.mul_(2.))  ## in-place operation
print(x)
 
tensor([[2., 4.],
        [6., 8.]])
tensor([[1., 2.],
        [3., 4.]])
tensor([[2., 4.],
        [6., 8.]])
tensor([[2., 4.],
        [6., 8.]])

 

출처 :www.edwith.org/boostcourse-dl-pytorch/lecture/42283/

 

[LECTURE] Lab-01-2 Tensor Manipulation 2 : edwith

학습목표 지난 시간에 이어서 텐서 조작(Tensor Manipulation)에 대해 계속 알아본다. 핵심키워드 텐서(Tensor) 넘파이(NumPy) 텐서 조작(Tensor Man... - 양동현

www.edwith.org

 

 

반응형

'AI Study > DL_Basic' 카테고리의 다른 글

[파이토치로 시작하는 딥러닝 기초]05_ Logistic Regression  (0) 2020.12.22
[파이토치로 시작하는 딥러닝 기초]04.02_Loading Data  (0) 2020.12.21
[파이토치로 시작하는 딥러닝 기초]04.01_Multivariable_Linear_regression  (0) 2020.12.21
[파이토치로 시작하는 딥러닝 기초]03_Deeper Look at Gradient Descent  (0) 2020.12.21
[파이토치로 시작하는 딥러닝 기초]02_Linear Regression  (0) 2020.12.21
'AI Study/DL_Basic' 카테고리의 다른 글
  • [파이토치로 시작하는 딥러닝 기초]04.02_Loading Data
  • [파이토치로 시작하는 딥러닝 기초]04.01_Multivariable_Linear_regression
  • [파이토치로 시작하는 딥러닝 기초]03_Deeper Look at Gradient Descent
  • [파이토치로 시작하는 딥러닝 기초]02_Linear Regression
자동화먹
자동화먹
많은 사람들에게 도움이 되는 생산적인 기록하기
    반응형
  • 자동화먹
    자동화먹의 생산적인 기록
    자동화먹
  • 전체
    오늘
    어제
    • 분류 전체보기 (144)
      • 생산성 & 자동화 툴 (30)
        • Notion (24)
        • Obsidian (0)
        • Make.com (1)
        • tips (5)
      • Programming (37)
        • Python (18)
        • Oracle (6)
        • Git (13)
      • AI Study (65)
        • DL_Basic (14)
        • ML_Basic (14)
        • NLP (21)
        • Marketing&Recommend (4)
        • chatGPT (0)
        • etc (12)
      • 주인장의 생각서랍 (10)
        • 생각정리 (4)
        • 독서기록 (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    기초
    파이토치
    git
    빅데이터분석
    python기초
    LSTM
    Github
    nlp
    노션첫걸음
    노션
    pytorch
    머신러닝
    git commit
    notion
    데이터분석
    seq2seq
    gcp
    딥러닝
    GPT
    Jupyter notebook
    Transformer
    데이터베이스
    dl
    cnn
    빅데이터
    ML
    파이토치로 시작하는 딥러닝 기초
    자연어처리
    Google Cloud Platform
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
자동화먹
[파이토치로 시작하는 딥러닝 기초]01_Tensor Manipulation(텐서 조작)

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.