# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="14_15jz1Y5ci" executionInfo={"status": "ok", "timestamp": 1618127092906, "user_tz": -540, "elapsed": 33786, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="8f51b9f2-e3ff-44f1-d546-90c2d40fcea7"
from google.colab import drive
drive.mount('/content/drive')

# + [markdown] id="b1lJAliTC-36"
# ### 연결된 그래픽 카드와 CUDA 버전 확인하기

# + colab={"base_uri": "https://localhost:8080/"} id="fuew_O1NDW3M" executionInfo={"status": "ok", "timestamp": 1618127111784, "user_tz": -540, "elapsed": 790, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="5e0539bc-3de2-405d-8cd7-471cbba89055"
# !nvidia-smi

# + colab={"base_uri": "https://localhost:8080/"} id="iN450VNkZ890" executionInfo={"status": "ok", "timestamp": 1618127119367, "user_tz": -540, "elapsed": 772, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="8a9594b7-cb7a-4c5f-d12e-00e7424b55f0"
# !nvcc -V

# + [markdown] id="PRVQBTNmEvBx"
# ### torch, torchvision, torchtext, 버전 확인

# + colab={"base_uri": "https://localhost:8080/"} id="6Nv3d3ZCbYyp" executionInfo={"status": "ok", "timestamp": 1618130616789, "user_tz": -540, "elapsed": 937, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="43f989ad-e37e-4010-ba77-b4789a98b692"
import torch
import torchvision
import torchtext

print(f'torch version: {torch.__version__}')
print(f'torchvision version: {torchvision.__version__}')
print(f'torchtext version: {torchtext.__version__}')


# + [markdown] id="EgnhL2gHHUz-"
# # Basic 2. Tensor  
# Tensor vs. Ndarray

# + id="SIbOE4RhHod2" executionInfo={"status": "ok", "timestamp": 1618130620559, "user_tz": -540, "elapsed": 767, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}}
import torch
import numpy as np

# + colab={"base_uri": "https://localhost:8080/"} id="UJqfHpUXHyA3" executionInfo={"status": "ok", "timestamp": 1618130622004, "user_tz": -540, "elapsed": 982, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="5b08d8a5-fe27-4aad-cda3-b3d0b9977df5"
# make random 2D-array data
import random as r
pick=lambda :r.randint(5,10)
R,C=pick(),pick()
base = [[r.randint(0,100) for __ in range(C)] for _ in range(R)]
                                          
print('random base data')
for row in base:
  print(*row)

# + colab={"base_uri": "https://localhost:8080/"} id="4w-ZmrX3eRE8" executionInfo={"status": "ok", "timestamp": 1618130624109, "user_tz": -540, "elapsed": 743, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="98e5243d-0115-4007-8923-cf00c5e14999"
as_ndarray = np.array(base)
as_tensor  = torch.tensor(base,dtype=torch.int)

print("casted to ndarray:")
print(as_ndarray)
print()
print("="*50)
print()
print("casted to tensor:")
print(as_tensor)

# + [markdown] id="YKJEXCXIeRmv"
# ### Dimension, Shape   
# > np.reshape  == torch.view  
#

# + colab={"base_uri": "https://localhost:8080/"} id="QXShb3SkeR2n" executionInfo={"status": "ok", "timestamp": 1618130628574, "user_tz": -540, "elapsed": 715, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="446f6db6-e603-4824-95e2-15e3ab0b845c"
print(f'shape of as_tensor, {as_tensor.shape}') # or .size()
for row in as_tensor:
  print(row)
print()
reshaped_tensor=as_tensor.view(3,2,-1)
print(f'shape of reshaped_tensor, {reshaped_tensor.shape}')
for row in reshaped_tensor:
  print(row)

# + [markdown] id="gXJReSitAnJD"
# > np.new_axis == torch.unsqeeze

# + colab={"base_uri": "https://localhost:8080/"} id="r-PJSzhO__7B" executionInfo={"status": "ok", "timestamp": 1618130632136, "user_tz": -540, "elapsed": 740, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="676918fc-0a69-4b38-a468-bd986cea0d5a"
print(f'shape of as_tensor, {as_tensor.shape}')
for row in as_tensor:
  print(row)
print()
unsqeezed_tensor=as_tensor.unsqueeze(1)
print(f'shape of reshaped_tensor, {unsqeezed_tensor.shape}')
for row in unsqeezed_tensor:
  print(row)

# + [markdown] id="I4_Tta-beSAI"
# ### Concatenation , Stacking

# + [markdown] id="dH-rcsMIBdul"
# > np.concat([] , axis= ) == torch.cat([] , dim= )

# + colab={"base_uri": "https://localhost:8080/"} id="3ADr496heSMn" executionInfo={"status": "ok", "timestamp": 1618130639890, "user_tz": -540, "elapsed": 1503, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="ca703ac5-371b-4948-b756-d3c7219e158e"
x = torch.Tensor([[1, 2], [3, 4]])
y = torch.Tensor([[5, 6], [7, 8]])

dim0_concat = torch.cat([x,y],dim=0) # 행이 늘어난다.
dim1_concat = torch.cat([x,y],dim=1) # 열이 늘어난다.

print("concat with dim=0")
for row in dim0_concat:
  print(row)
print()
print("concat with dim=1")
for row in dim1_concat:
  print(row)

# + [markdown] id="XY89px-FCNMG"
# > torch.stack() : unsqeeze 작업을 자동으로 수행 가능.

# + colab={"base_uri": "https://localhost:8080/"} id="TlHAZig6CNrk" executionInfo={"status": "ok", "timestamp": 1618129739235, "user_tz": -540, "elapsed": 814, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="d319254f-b819-432a-e237-ed61a343fcd4"
x = torch.FloatTensor([1, 4])
y = torch.FloatTensor([2, 5])
z = torch.FloatTensor([3, 6])
k=torch.stack([x,y,z],axis=0)
print(k)
print(k.shape)

# + [markdown] id="h6ZDCr8ueSVT"
# ### ones_like , zeros_like
# > 행렬 shape을 참고하여 0이나 1로 초기화 시키는 함수, numpy 와 비슷하다.

# + colab={"base_uri": "https://localhost:8080/"} id="zo9s4SEqeSb6" executionInfo={"status": "ok", "timestamp": 1618129918422, "user_tz": -540, "elapsed": 749, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="5b3a9163-cf7c-495e-9e61-b598115615bc"
sample = torch.Tensor([[0, 1, 2], [2, 1, 0]])

print(torch.ones_like(sample))
print()
print(torch.zeros_like(sample))

# + [markdown] id="t7rIXPCoDxai"
# ### In-place Operation (중요! ★)
# > C의 ++ 나, Python의 += 연산 같은 덮어쓰는 연산을 수행하며 그 `기록 정보`를 유지하고 있는것이 특징이다.  
# (이후 인공신경망 생성시에 역전파의 디버깅이 가능한 구조이다.)

# + colab={"base_uri": "https://localhost:8080/"} id="ByKnj4NQEMzN" executionInfo={"status": "ok", "timestamp": 1618130163970, "user_tz": -540, "elapsed": 766, "user": {"displayName": "\uc774\ub3d9\uc7ac", "photoUrl": "https://lh5.googleusercontent.com/-PSXAf5Em3Nc/AAAAAAAAAAI/AAAAAAAAGm0/Tht8Fgt81gI/s64/photo.jpg", "userId": "04556005387042920958"}} outputId="c210538c-3d6c-44a7-ab63-103ddf4a598d"
sample = torch.Tensor([[1, 2], [3, 4]])

print(sample.mul(2))
print(sample) # sample 자료는 변하지 않음
print()
# 어떤 연산이든 뒤에 언더바(_)를 추가하면 In-place operation이 된다.
print(sample.mul_(2))
print(sample)

StarcoderdataJupyterScriptsDedupFiltered