<jupyter_start><jupyter_text>ref: https://github.com/Harry24k/Pytorch-Basic  How to use Colab? Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud.With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser. Advantages- The best thing is that it is free!- It gives you costly GPUs and working envriornment, independent of what personal machine you are using.- It also runs on Google servers using virtual machines and you don’t need to install any packages, which sometimes create difficulty you are using different operating systems such as MAC, Windows, and Linux.  Hotkeys (change as you want) | hotkey | action || --- | --- || ctrl + M H | Open hotkey configuration || ctrl + O | Open note || ctrl + S | Save note || ctrl + F9 | Run all cells || ctrl + shift + enter | Run cell || shift + enter | Run cell and move on to the next || alt + enter | Run cell and add a new one below || ctrl + M I | Stop running execution || ctrl + M A | Add new code cell above || ctrl + M B | Add new code cell below || ctrl + M D | Delete cell || ctrl + M K | Move cell up || ctrl + M J | Move cell down || ctrl + M Y | Change to code cell || ctrl + M M | Change to markdown cell || ctrl + M Z | Undo last cell execution || ctrl + H | Search || ctrl + / | Comment out or undo |  Terminal commands<jupyter_code># Get OS info
!cat /etc/issue.net
# Get CPU info
!cat /proc/cpuinfo | grep 'processor\|model name\|cpu cores'
# Get memory info
!free -h
# Get disk info
!df -h
# Get GPU info
!nvidia-smi
# Get python version
!python --version
# Get current directory
!pwd
%cd
# Get files/folders list
!ls -lah
!mv
!cp
!pip show torch<jupyter_output>Name: torch
Version: 1.10.0+cu111
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: https://pytorch.org/
Author: PyTorch Team
Author-email: <EMAIL>
License: BSD-3
Location: /usr/local/lib/python3.7/dist-packages
Requires: typing-extensions
Required-by: torchvision, torchtext, torchaudio, fastai<jupyter_text>File I/O<jupyter_code># Upload file to Colab workspace
# [NOTE] Uploaded files will be gone when runtime resets
import os
from google.colab import files

uploaded = files.upload()

for f in uploaded.keys():
    print('User uploaded file "{}" at "{}"'.format(f, os.getcwd()))

# Or simply open file tab then drag-and-drop
# Download file from Colab workspace
# [NOTE] Enable third party cookies and use Chrome browser
# files.download('./sample_data/mnist_train_small.csv')

# Or simply open file tab and right click a file then download <-- Recommended
# Mount Google drive
from google.colab import drive

drive.mount('/content/gdrive')<jupyter_output>Mounted at /content/gdrive<jupyter_text>1. Tensor<jupyter_code>import torch
print(torch.__version__)<jupyter_output>1.10.0+cu111<jupyter_text>1.1 Tensor Basic<jupyter_code>#Tensor의 기초적 선언
#torch.FloatTensor와 같음
# x = torch.Tensor(행, 열)
x = torch.Tensor(2, 3)
print(x)
#Tesnor의 정보 확인
print("Tensor Type : ", type(x))
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size()) # x.shape<jupyter_output>Tensor Type :  <class 'torch.Tensor'>
Tensor Type :  torch.FloatTensor
Tensor Size :  torch.Size([2, 3])<jupyter_text>1.2 Tensor with characteristics<jupyter_code>#0으로 채워진 Tensor 선언
x = torch.zeros(2, 3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
x = torch.zeros(2,3, dtype=torch.int32)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#1로 채워진 Tensor 선언
x = torch.ones(2, 3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#0, 1 사이의 랜덤 숫자로 채워진 Tensor 선언
x = torch.rand(2, 3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#평균이 0이고 분산이 1을 따르는 정규분포에서 추출된 값으로 채워진 Tensor 선언
x = torch.randn(2, 3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#정방행렬과 같은 Tensor 선언
x = torch.eye(2, 3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#0부터 2까지 0.5씩 증가하는 Tensor 선언
x = torch.arange(0, 2, step=0.5)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#직접 값을 입력하여 선언
x = torch.Tensor([[1,2,3],[1,2,3]])
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#32-bit floating point
x = torch.FloatTensor(2,3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#32-bit integer (signed)
x = torch.IntTensor(2,3)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())<jupyter_output>tensor([[638048512,     21937, 528784896],
        [    21937,        24,         0]], dtype=torch.int32)
Tensor Type :  torch.IntTensor
Tensor Size :  torch.Size([2, 3])<jupyter_text>1.3 Tensor and Numpy<jupyter_code>import numpy as np
#numpy 형식의 배열
x = np.array([[1,2,3],[1,2,3]])
print(x, type(x))
#numpy를 tensor로 변환
x = torch.from_numpy(x) # x = torch.Tensor(x)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#tensor를 numpy로 변환
x = x.numpy()
print(x, type(x))<jupyter_output>[[1 2 3]
 [1 2 3]] <class 'numpy.ndarray'><jupyter_text>1.4 Tensor Slicing<jupyter_code>x = torch.Tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(x)
#모든 원소 출력
x[:, :]
#2행부터 출력
x[1:, :]
#2열부터 출력
x[:, 1:]
#2행, 2열부터 3열까지 출력
x[1:, 1:3]
#1행 출력
x[0, :]
#x를 1행씩 잘라서 배열로 저장
x_rows = torch.split(x, split_size_or_sections=1, dim=0)
print(x_rows)
#x를 2열씩 잘라서 배열로 저장
x_cols = torch.split(x, split_size_or_sections=2, dim=1)
print(x_cols)
#위와 같은 결과
torch.chunk(x, chunks=2, dim=1)
#x_cols의 첫번째 배열 중 특정 부분만 추출하기
torch.masked_select(x_cols[0], torch.BoolTensor([[0,1],[1,0],[0,1]]).bool())<jupyter_output><empty_output><jupyter_text>1.5 Tensor Merging<jupyter_code>#x_rows를 행으로 이어 붙이기
torch.cat(x_rows, dim=0)
#x_rows를 열로 이어 붙이기
torch.cat(x_rows, dim=1)
#x_cols를 쌓아올리기
x_new = torch.stack(x_cols, dim=0)
print(x_new)<jupyter_output>tensor([[[ 1.,  2.],
         [ 5.,  6.],
         [ 9., 10.]],

        [[ 3.,  4.],
         [ 7.,  8.],
         [11., 12.]]])<jupyter_text>1.6 Tensor Reshaping<jupyter_code>x = torch.Tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(x)
#4행 3열로 바꾸기
x.view(4, 3)
#2열로 바꾸기
x.view(-1, 2)
x.view(2, 1, -1)
#차원이 1인 값을 축소
x.view(2, 1, -1).squeeze()
#dim=1 자리에 차원을 추가
x.view(2, 1, -1).squeeze().unsqueeze(dim=1)<jupyter_output><empty_output><jupyter_text>1.7 Tensor Calculation<jupyter_code>x = torch.Tensor([[1,2,3],[4,5,6]])
y = torch.Tensor([[1,1,1],[2,2,2]])
print("x :", x)
print("y :", y)
# x와 y의 합
z = torch.add(x, y)
print("z :", z)
print("x+y :", x+y)
# x의 모든 원소에서 2를 뺌
print(x - 2)

# x의 모든 원소에 2를 곱함
print(2*x)
#x의 원소와 y의 원소를 각각 곱함
z = torch.mul(x, y)
print("z :", z)
print("x*y :", x*y)
#x의 원소를 각각 제곱함
z = torch.pow(x, 2)
print("z :", z)
print("x**2 :", x**2)
#x의 원소에 log를 씌움
z = torch.log(x)
print("z :", z)
print("x.log() :", x.log())
#x의 원소에 루트를 씌움
z = torch.sqrt(x)
print("z :", z)
print("x.sqrt() :", x.sqrt())
#x의 원소에 나머지(mod) 계산
z = x % 2
print(z)
#x에 절댓값
z = torch.abs(x)
print("z :", z)
print("x.abs() :", x.abs())<jupyter_output>z : tensor([[1., 2., 3.],
        [4., 5., 6.]])
x.abs() : tensor([[1., 2., 3.],
        [4., 5., 6.]])<jupyter_text>1.8 Tensor Casting<jupyter_code>x = torch.Tensor([[1,2,3],[4,5,6]])
y = torch.Tensor([[1,1,1],[2,2,2]])
print("x :", x)
print("y :", y)
x.type(torch.DoubleTensor)
x.double()
x.type(torch.IntTensor)
x.int()<jupyter_output><empty_output><jupyter_text>1.9 Tensor Statistic<jupyter_code>x = torch.Tensor([[-1,2,-3],[4,-5,6]])
print(x)
print("x의 합 :", x.sum())
print("x의 최대값 :", x.max())
print("x의 최소값 :", x.min())
print("x의 분산 :", x.var())
# Scalar Tensor
x.sum().size()
# item의 활용
print("x의 합 :", x.sum().item())
print("x의 최대값 :", x.max().item())
print("x의 최소값 :", x.min().item())
print("x의 분산 :", x.var().item())
#행(dim=0)을 기준으로 열의 최댓값을 출력
value, index = x.max(dim=0)
value, index
#열(dim=1)을 기준으로 행의 최댓값을 출력
value, index = x.max(dim=1)
value, index

# torch.max(x, dim=1)과 같은 결과
# 이는 추후에 계속 등장하므로 매우 중요!
#열(dim=1)을 기준으로 각 행을 정렬
value, index = x.sort(dim=1)
value<jupyter_output><empty_output><jupyter_text>1.10 Tensor *_like function<jupyter_code>x = torch.Tensor([[-1,2,-3],[4,-5,6]])
print(x)
y = torch.zeros_like(x)
print(y)
y = torch.ones_like(x)
print(y)
y = torch.rand_like(x)
print(y)<jupyter_output>tensor([[0.7551, 0.0692, 0.4790],
        [0.1638, 0.4956, 0.0169]])<jupyter_text>1.11 Tensor for GPU<jupyter_code>x = torch.Tensor([[1,2,3],[1,2,3]])
print(x)
# cuda 확인
print("현재 Cuda 사용 가능한 Device 개수 :", torch.cuda.device_count())
print("현재 Cuda 사용 가능 여부 :", torch.cuda.is_available())
print("현재 사용 중인 Cuda Device :", torch.cuda.current_device())
print("현재 Cuda Device 이름 :", torch.cuda.get_device_name(0))
print("현재 Cuda Device의 사용 가능한 메모리 :", torch.cuda.max_memory_allocated(device=0), "bytes")
print("현재 Cuda Device의 사용 중인 메모리 :", torch.cuda.memory_allocated(device=0), "bytes")
# cuda 세팅
torch.cuda.set_device(0)
#cpu를 gpu로 전환
gpu_x = x.cuda()
print(gpu_x)
print("Tensor Type : ", gpu_x.type())
print("Tensor Size : ", gpu_x.size())
#cpu를 gpu로 전환 2
device = torch.device("cuda")
gpu_x = x.to(device)
print(gpu_x)
print("Tensor Type : ", gpu_x.type())
print("Tensor Size : ", gpu_x.size())
#gpu를 cpu로 전환
x = gpu_x.cpu()
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
#gpu를 cpu로 전환 2
device = torch.device("cpu")
x = gpu_x.to(device)
print(x)
print("Tensor Type : ", x.type())
print("Tensor Size : ", x.size())
# gpu tensor와 cpu tensor의 연산
gpu_x + x<jupyter_output><empty_output><jupyter_text>1.12 Tensor Calculation as Matrix (참고)<jupyter_code>x = torch.Tensor([[1,1],[2,2]])
y = torch.Tensor([[1,2],[3,4]])
print("x :", x)
print("y :", y)
#x와 y의 행렬곱
torch.mm(x, y)
#x와 z의 행렬벡터곱
z = torch.Tensor([1, 2])
torch.mv(x, z)
#z와 z의 내적
torch.dot(z, z)
#x의 전치(transpose)
x.t()
# y의 역행렬(inverse)
y.inverse()
# 텐서의 배치 행렬곱
x1 = torch.FloatTensor(3,3,2)
x2 = torch.FloatTensor(3,2,3)

torch.bmm(x1,x2).size()
x1
# 텐서의 전치
x1.transpose(0,2)
eigenvalue, eigenvector = torch.eig(x,eigenvectors=True)
print("eigenvalue :", eigenvalue)
print("eigenvector :", eigenvector)
#행렬의 QR factorization
Q,R = torch.qr(x)
print("Q :", Q)
print("R :", R)
#행렬의 SVD factorization
S,V,D = torch.svd(x)
print("S :", S)
print("V :", V)
print("D :", D)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18599689681923973, 'fraction_numerical': 0.03529868114817688, 'mean_word_length': 4.629366812227074, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '478962', 'license': 'MIT', 'n_tokens_mistral': 4771, 'n_tokens_neox': 5023, 'n_words': 1139, 'path': 'Day 6/6_1. Tensor.ipynb', 'repo_name': 'SLCFLAB/Data-Science-Python', 'size': '10325'}