Torch_tutorials

๊ฐœ์ธ study๋ฅผ ์œ„ํ•œ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ ๋‚ด์šฉ์— ์ž˜๋ชป๋œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

PyTorch tutorial ์„ ํ•˜๋ฉด์„œ, ์ดํ•ดํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌ๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Summary

  1. torch.tensor - โ€œN์ฐจ์› ๋ฐฐ์—ด dataโ€ + โ€œGPUโ€ + โ€œgradientโ€

    • N์ฐจ์› ๋ฐฐ์—ด data๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฉด์—์„œ, numpy.ndarray์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.

    • CPU/GPU device ์ด๋™์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. to(device)

    • autograd๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด, back propagation์„ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  2. torch.nn - Neural Networks์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

    • Module : Layer์™€ forward method๋ฅผ ํฌํ•จํ•˜๋ฉฐ, ์ฃผ๋กœ model๋ฅผ ์ƒˆ๋กœ ์ •์˜ํ• ๋•Œ, parent class ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    • Parameter : Tensor์˜ subclass(wrapper)๋กœ, Module์—์„œ ํ•™์Šต์„ ์œ„ํ•œ weight ๊ด€๋ฆฌ๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. parameters() iterator ๋กœ ๋ถˆ๋Ÿฌ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    • functional : ํ™œ์„ฑํ™” ํ•จ์ˆ˜, ์†์‹ค ํ•จ์ˆ˜ ๋“ฑ์„ ํฌํ•จํ•˜๋Š” ๋ชจ๋“ˆ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ F importํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ convolution๊ณผ linear layer์™€ ๊ฐ™์ด, ์ €์žฅ state๋ฅผ ๊ฐ€์ง€๋Š” layer์˜ non-statefule ๋ฒ„์ „ ๋ ˆ์ด์–ด๋„ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  3. torch.optim - backward() ๋‹จ๊ณ„์—์„œ, Parameter๋ฅผ updateํ•˜๋Š” optimizer๋ฅผ ํฌํ•จํ•˜๋Š” module

  4. Dataset - dataset์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณต๋˜๋Š” __len__๊ณผ __getitem__์„ ํฌํ•จํ•˜๋Š” abstract class.

  5. DataLoader - Dataset์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ๋ฐฐ์น˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” iterator.

1. torch.Tensor

numpy.ndarray์™€ ๋น„์Šทํ•˜๊ฒŒ ๋‹ค์ฐจ์› ์—ฐ์‚ฐ์„ ์œ„ํ•œ ๊ธฐ๋ณธ function๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , PyTorch๋Š” tensor์— autograd๋ฅผ ๋„ฃ์–ด ๊ฐ€์žฅ ์ค‘์‹ฌ์ด๋˜๋Š” class๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • ์ด์ „์—๋Š” torch.autograd.Variable์ด๋ผ๋Š” tensor์˜ wrapper class๊ฐ€ ์žˆ์–ด, ์—ฌ๊ธฐ์„œ operation hisotry๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, autograd๊ฐ€ tensor์—์„œ ์ž๋™ ์ง€์›๋˜๋ฉด์„œ, ๋” ์ด์ƒ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

torch.Tensor is the central class of the package.

tensor.requires_grad๋ฅผ True๋กœ ํ•˜๋ฉด, tensor๋ฅผ ์‚ฌ์šฉํ•œ ์—ฐ์‚ฐ๊ณผ์ •์„ ๊ธฐ๋ก(grad_fn)ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ์ตœ์ข…๋‹จ tensor์—์„œ .backward()๊ฐ€ ์ˆ˜ํ–‰๋˜๋ฉด, gradient๊ฐ€ ๊ณ„์‚ฐ๋˜์–ด tensor.grad์— ์ €์žฅ๋˜๊ณ , ์ด ๊ฐ’์€, grad_fn์— ๊ธฐ๋ก๋œ ์—ฐ์‚ฐ์„ ํƒ€๊ณ , back propagation ์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด code๋ฅผ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ [2],

2. torch.nn & torch.optim & Dataset & DataLoader

์ด ๋ถ€๋ถ„์€ ์‹ค์ œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ์ดํ•ดํ•˜๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.[3]

torch.nn, torch.optim, Dataset ๊ทธ๋ฆฌ๊ณ  DataLoader์—†์ด Tensor๋งŒ์„ ์ด์šฉํ•ด์„œ, ์•„๋ž˜์™€ ๊ฐ™์ด Linear regression์„ model์„ ๊ตฌํ˜„ํ•˜๊ณ  ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1) nn.functional ์‚ฌ์šฉ

์—ฌ๊ธฐ์„œ, torch.nn.functional๋ฅผ ์‚ฌ์šฉํ•ด์„œ, log_softmax() + negative_log_likelihood()๋ฅผ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2) nn.Module ์‚ฌ์šฉ

ํ•™์Šต์˜ ๋Œ€์ƒ์ด ๋˜๋Š” parameter์™€ Neural Networks model์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, nn.Module์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. nn.Module์˜ Parameters()๋ฅผ ์ด์šฉํ•ด์„œ, model์— ์†ํ•ด ์žˆ๋Š” parameter์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‰ฝ๊ฒŒ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3) nn.Linear ์‚ฌ์šฉ

๋ฏธ๋ฆฌ ์ •์˜๋œ Model๋“ค ์„ ์‚ฌ์šฉํ•ด์„œ, code๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. nn.Linear๋ฅผ ์‚ฌ์šฉํ•ด์„œ, self.weights์™€ self.bias์˜ ์ •์˜์™€ ์ดˆ๊ธฐํ™” ์ž‘์—… ๊ทธ๋ฆฌ๊ณ  xb @ self.weights + self.bias ๊ณ„์‚ฐ์„ ๋Œ€์‹  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4) optim ์‚ฌ์šฉ

๋ฏธ๋ฆฌ ๊ตฌํ˜„๋œ, optimization ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ torch.optim์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด, parameters๋ฅผ step() method๋ฅผ ์‚ฌ์šฉํ•˜ updateํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5) Dataset & DataLoader ์‚ฌ์šฉ

๊ตฌํ˜„์„ ํ•˜๋‹ค๋ณด๋ฉด, data๋ฅผ loadํ•˜๊ณ  ์ž๋ฅด๊ณ  ์„ž๋Š”๋ฐ ๋งŽ์€ code๊ฐ€ ๋“ค์–ด ๊ฐ‘๋‹ˆ๋‹ค. ์ด๋•Œ, Dataset๊ณผ DataLoader๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๋ถ€๋ถ„์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. __len__๊ณผ __getitem__๋ฅผ ๊ฐ€์ง„ class๋Š” Dataset์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ __len__๊ณผ __getitem__๋งŒ ๋งŒ๋“ค๋ฉด, customize๋œ Dataset์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Tensor๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” ๊ฒฝ์šฐ, ๋ฏธ๋ฆฌ ์ •์˜๋œ TensorDataset๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. DataLoader๋Š” batch ๊ด€๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. Dataset๊ณผ DataLoader๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊น”๋”ํ•ด ์ง‘๋‹ˆ๋‹ค.

6) ์ตœ์ข… code

torch.nn & torch.optim & Dataset & DataLoader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์•„๋ž˜์™€ ๊ฐ™์ด code๋ฅผ refactoring ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Reference

Last updated