Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions 00-install-torch.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 파이토치 설치하기

```{r, echo=FALSE, message=FALSE, warning=FALSE, include=FALSE}
Sys.setenv(RETICULATE_PYTHON = "/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python")
library(reticulate)
use_python("/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python", required = T)
```

파이토치(PyTorch)는 Python 환경에서 설치할 수 있다. 공식 웹사이트에서 제공하는 명령어를 참고하면 쉽게 설치할 수 있다.

1. 파이토치 공식 설치 명령어 찾기

파이토치 공식 웹사이트에서 운영 체제와 컴퓨팅 환경에 맞는 명령어를 선택하면 된다. [파이토치홈페이지](https://pytorch.org/get-started/locally/)로 가서 아래 항목을 선택해보자.

- PyTorch 빌드: Stable (안정된 버전) 또는 Nightly (개발 버전)

- 운영 체제: Windows, macOS, Linux

- 패키지 관리자: pip 또는 conda

- 컴퓨팅 플랫폼: CPU 또는 CUDA(GPU 버전)

2. 설치 명령어 예제

<!-- -->

(1) CPU만 사용하는 경우

Python과 pip를 사용한다면:

```{python}
pip install torch torchvision torchaudio
```

conda를 사용한다면:

```{python}
conda install pytorch torchvision torchaudio cpuonly -c pytorch
```

(2) GPU(CUDA) 사용하는 경우

CUDA 11.8을 사용하는 GPU 환경이라면:

pip install torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu118>

CUDA 11.7을 사용하는 경우:

pip install torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu117>

conda를 사용하려면:

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

3. 설치 확인

설치가 제대로 되었는지 확인하려면 아래 코드를 실행해보자.

```{python}
import torch
print(torch.__version__) # PyTorch 버전을 출력
print(torch.cuda.is_available()) # GPU 사용 가능 여부 확인
```

torch.cuda.is_available()가 True라면 GPU 설정까지 완료된 것이다.

4. 문제 해결

설치 중 문제가 생기면 다음을 확인해보자.

- Python 버전은 3.9 이상이어야 한다.

- GPU 드라이버가 최신 버전인지 확인해야 한다.

- CUDA Toolkit 설치가 필요하다면 CUDA Toolkit 다운로드로 가서 설치한다.

특정 버전을 설치하려면 아래 명령어를 사용하면 된다.
77 changes: 77 additions & 0 deletions 00-install-torch.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 파이토치 설치하기

```{r, echo=FALSE, message=FALSE, warning=FALSE, include=FALSE}
Sys.setenv(RETICULATE_PYTHON = "/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python")
library(reticulate)
use_python("/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python", required = T)
```

파이토치(PyTorch)는 Python 환경에서 설치할 수 있다. 공식 웹사이트에서 제공하는 명령어를 참고하면 쉽게 설치할 수 있다.

1. 파이토치 공식 설치 명령어 찾기

파이토치 공식 웹사이트에서 운영 체제와 컴퓨팅 환경에 맞는 명령어를 선택하면 된다. [파이토치홈페이지](https://pytorch.org/get-started/locally/)로 가서 아래 항목을 선택해보자.

- PyTorch 빌드: Stable (안정된 버전) 또는 Nightly (개발 버전)

- 운영 체제: Windows, macOS, Linux

- 패키지 관리자: pip 또는 conda

- 컴퓨팅 플랫폼: CPU 또는 CUDA(GPU 버전)

2. 설치 명령어 예제

<!-- -->

(1) CPU만 사용하는 경우

Python과 pip를 사용한다면:

```{python}
pip install torch torchvision torchaudio
```

conda를 사용한다면:

```{python}
conda install pytorch torchvision torchaudio cpuonly -c pytorch
```

(2) GPU(CUDA) 사용하는 경우

CUDA 11.8을 사용하는 GPU 환경이라면:

pip install torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu118>

CUDA 11.7을 사용하는 경우:

pip install torch torchvision torchaudio --index-url <https://download.pytorch.org/whl/cu117>

conda를 사용하려면:

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

3. 설치 확인

설치가 제대로 되었는지 확인하려면 아래 코드를 실행해보자.

```{python}
import torch
print(torch.__version__) # PyTorch 버전을 출력
print(torch.cuda.is_available()) # GPU 사용 가능 여부 확인
```

torch.cuda.is_available()가 True라면 GPU 설정까지 완료된 것이다.

4. 문제 해결

설치 중 문제가 생기면 다음을 확인해보자.

- Python 버전은 3.9 이상이어야 한다.

- GPU 드라이버가 최신 버전인지 확인해야 한다.

- CUDA Toolkit 설치가 필요하다면 CUDA Toolkit 다운로드로 가서 설치한다.

특정 버전을 설치하려면 아래 명령어를 사용하면 된다.
122 changes: 122 additions & 0 deletions 01-intro-to-tensor.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# 딥러닝 첫걸음, 텐서 (tensor) 만들기 {#intro}

```{r, echo=FALSE, message=FALSE, warning=FALSE, include=FALSE}
Sys.setenv(RETICULATE_PYTHON = "/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python")
library(reticulate)
use_python("/opt/homebrew/Caskroom/miniconda/base/envs/torch/bin/python", required = T)
```

## torch와의 첫 만남

PyTorch를 설치했으니, 한번 만나보자. 아래 명령어로 torch를 불러온다.

```{python}
import torch
```


## 텐서(Tensor) 만들기

텐서는 다차원 배열이다. 우리가 많이 사용하는 행렬(matrix)의 개념을 확장한 것이다. Python의 numpy 배열과 비슷하지만, GPU 계산이 가능하다는 점에서 차별화된다.

## 빈 텐서 만들기

5행 3열의 빈 텐서를 선언한다. 빈 텐서는 초기화되지 않은 임의의 값으로 채워진다.

```{python}
x = torch.empty(5, 3)
# 텐서 x값 확인
print(x)
# 텐서 x의 크기 확인
print(x.size())
```

empty 텐서는 초기화되지 않은 값이 채워진다. 이후 값이 정의되기 전에는 신뢰할 수 없는 데이터가 포함되어 있으니 주의하자.

### 랜덤 텐서

0과 1 사이의 난수로 채워진 텐서를 선언한다.

```{python}
rand_tensor = torch.rand(5, 3)
print(rand_tensor)
```

Python에서는 리스트와 비슷한 문법을 사용해 텐서에 접근할 수 있다.

```{python}
print(rand_tensor[:, 1]) # 두 번째 열
print(rand_tensor[:3, :]) # 첫 3행
print(rand_tensor[2:4, [0, 2]]) # 3~4행의 1, 3열
```


### 단위 텐서

4행 4열의 단위 텐서를 선언한다.

```{python}
x = torch.eye(4)
print(x)
```


### 영(0) 텐서

모든 값이 0으로 채워진 3행 5열 텐서를 선언한다.

```{python}
x = torch.zeros(3, 5)
print(x)
```

## 고급 기술: 영리하게 만들기

지금까지는 초기화 함수들로 텐서를 선언했지만, 직접 값을 지정해 텐서를 선언할 수도 있다.

### 텐서 직접 선언

리스트 또는 2D 배열로 텐서를 만들 수 있다.

```{python}
y = torch.tensor([[1, 2], [3, 4], [5, 6]])
print(y)
```

### range 함수 사용

Python의 range를 사용해 텐서를 선언해보자.

```{python}
y = torch.tensor([i for i in range(1, 7)]).reshape(3, 2)
print(y)
```

### torch.linspace 함수 사용

torch.linspace를 사용하면 특정 범위의 값을 지정해 텐서를 만들 수 있다.

```{python}
y = torch.linspace(0.1, 1, steps=10).reshape(5, 2)
print(y)
```

## 텐서와 행렬은 같을까?

PyTorch의 텐서와 Numpy의 행렬은 비슷하지만 동일하지 않다. PyTorch 텐서는 GPU 연산에 최적화되어 있다. 다만 단순한 행렬곱 연산자 \@와 토치 연산자인 torch.matmul은 같은 결과를 도출한다.

```{python}
x = torch.zeros(3, 5)

# 행렬 곱
result = x @ x.T
print(result)

# 텐서 연산을 위해 torch.matmul() 사용
result = torch.matmul(x, x.T)
print(result)
```

## 텐서를 다룰 때 주의사항

PyTorch 텐서의 인덱싱은 Python 리스트와 동일하게 0부터 시작한다. R과는 다르니 주의하자. 다음 장에서는 텐서의 연산에 대해 더 자세히 다뤄보자.
Loading