๋ฐ˜์‘ํ˜•

๐Ÿ Python & library/PyTorch 18

[PyTorch] ๋ชจ๋ธ ์‹œ๊ฐํ™” ํˆด ์„ธ๊ฐ€์ง€ - Torchviz, HiddenLayer, Netron (Model visualization)

PyTorch ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ํˆด ์„ธ๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ์ถœ์ฒ˜: https://stackoverflow.com/questions/52468956/how-do-i-visualize-a-net-in-pytorch How do I visualize a net in Pytorch? import torch import torch.nn as nn import torch.optim as optim import torch.utils.data as data import torchvision.models as models import torchvision.datasets as dset import torchvision.transforms as stackoverflow.com 1. Torchviz https://git..

[PyTorch/Tensorflow v1, v2] Gradient Clipping ์ถ”๊ฐ€ํ•˜๊ธฐ

Gradient clipping์€ ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜ ์ž‘์€ gradient์˜ ๊ฐ’์„ ์ œํ•œํ•˜์—ฌ vanishing gradient๋‚˜ exploding gradient ํ˜„์ƒ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ํŠนํžˆ RNN์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ์ธ๋ฐ ์ด์™ธ์—๋„ ๊นŠ์€ ๋„คํŠธ์›Œํฌ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ค‘๊ฐ„์— loss๊ฐ€ ๋„ˆ๋ฌด ๋›ฐ๋ฉด์„œ weight update๊ฐ€ ์ด์ƒํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค๋ฉด ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์˜€๋‹ค. https://neptune.ai/blog/understanding-gradient-clipping-and-how-it-can-fix-exploding-gradients-problem Understanding Gradient Clipping (and How It Can Fix Exploding Grad..

[PyTorch] Scheduler ์‹œ๊ฐํ™”ํ•˜๊ธฐ (Visualize scheduler)

๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด PyTorch scheduler์„ ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. import matplotlib.pyplot as plt def visualize_scheduler(optimizer, scheduler, epochs): lrs = [] for _ in range(epochs): optimizer.step() lrs.append(optimizer.param_groups[0]['lr']) scheduler.step() plt.plot(lrs) plt.show() scheduler.get_lr()๋กœ learning rate๋ฅผ ์–ป์–ด์˜ค์ง€ ์•Š๊ณ  optimizer.param_groups[0]['lr']๋กœ ์–ป์–ด์˜ค๋Š” ์ด์œ ๋Š”, ReduceLROnPlateau ๋“ฑ์˜ scheduler์˜ ๊ฒฝ์šฐ get_lr() meth..

[PyTorch] ReduceLROnPlateau

ReduceLROnPlateau๋Š” ๋”์ด์ƒ ํ•™์Šต์ด ์ง„ํ–‰๋˜์ง€ ์•Š์„ ๋•Œ learning rate๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋Š” scheduler์ด๋‹ค. scheduler์— input์œผ๋กœ metric์„ ์ฃผ๋ฉด, ์ผ์ • epoch ๋™์•ˆ ๋ณ€ํ™”๊ฐ€ ์—†์„ ๋•Œ learning rate๋ฅผ ๊ฐ์†Œ์‹œํ‚จ๋‹ค. ์ฃผ์š” Parameter์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. mode: [min, max] ์ค‘ ํ•˜๋‚˜. Input์œผ๋กœ ์ฃผ๋Š” metric์ด ๋‚ฎ์„์ˆ˜๋ก ์ข‹์€์ง€, ๋†’์„์ˆ˜๋ก ์ข‹์€์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 'min' option์„ ์ฃผ๋ฉด, metric์˜ ๊ฐ์†Œ๊ฐ€ ๋ฉˆ์ถœ ๋•Œ๋งˆ๋‹ค learning rate๋ฅผ ๊ฐ์†Œ์‹œํ‚จ๋‹ค. factor: Learning rate๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋น„์œจ. new_lr = lr * factor์ด ๋œ๋‹ค. patience: Metric์ด ์–ผ๋งˆ ๋™์•ˆ ๋ณ€ํ™”๊ฐ€ ์—†์„ ๋•Œ learning rat..

[PyTorch] CosineAnnealingLR, CosineAnnealingWarmRestarts

Cosine Annealing์€ SGDR: Stochastic Gardient Descent with Warm Restarts์—์„œ ์ œ์•ˆ๋œ learning rate scheduling ๋ฐฉ๋ฒ•์ด๋‹ค. Cosine Annealing์€ learning rate๊ฐ€ cosine ํ•จ์ˆ˜์˜ ์ฃผ๊ธฐ๋ฅผ ๋”ฐ๋ผ ๊ฐ์†Œํ•˜๊ณ , ์ฆ๊ฐ€ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ์šฐ์„ , Cosine Annealing์˜ ์ˆ˜์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. $\eta_t=\eta_{min}+\frac12(\eta_{max}-\eta_{min})(1+cos(\frac{T_{cur}}{T_{max}}\pi))$ $\eta$๋Š” learning rate๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. Cosine Annealing์—์„œ ์„ค์ • ๊ฐ€๋Šฅํ•œ parameter์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. $\eta_{min}$ : ์ตœ์†Œ lea..

[PyTorch] nn.ModuleList ๊ธฐ๋Šฅ๊ณผ ์‚ฌ์šฉ ์ด์œ 

์ฐธ๊ณ  : https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463/3 When should I use nn.ModuleList and when should I use nn.Sequential? From what I see, it is interchangeable then? Unless there is some order to be followed, then we should use Sequential. Am I right? discuss.pytorch.org official docs : https://pytorch.org/docs/stable/generated/torch.nn.M..

[PyTorch] Livelossplot ์‚ฌ์šฉ์˜ˆ์ œ

www.kaggle.com/pmigdal/livelossplot-for-training-loss-tracking Livelossplot์€ ํ•™์Šต ๊ณผ์ •์—์„œ real-time์œผ๋กœ ์‹คํ–‰ ๋กœ๊ทธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํŒจํ‚ค์ง€์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. !pip install livelossplot ์•„๋ž˜๋Š” ์‚ฌ์šฉ ์˜ˆ์ œ์ด๋‹ค. from livelossplot import PlotLosses liveloss = PlotLosses() model = model.to(device) loss_F = nn.MSELoss() for epoch in range(num_epochs): logs = {} for phase in ['train', 'validation']: if phase == 'train': model.train() else..

[PyTorch] ๋ชจ๋ธ ์ €์žฅํ•˜๊ธฐ & ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

state_dict - torch.nn.Module ๋ชจ๋ธ์˜ ๊ฐ ๊ณ„์ธต์˜ ํ•™์Šต ๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜(model.parameters())๋“ค์„ ๋งคํ•‘ํ•˜๋Š” dictionary ๊ฐ์ฒด. - torch.optim ์˜ตํ‹ฐ๋งˆ์ด์ € ๊ฐ์ฒด ๋˜ํ•œ ์˜ตํ‹ฐ๋งˆ์ด์ €์˜ ์ƒํƒœ์™€ ์‚ฌ์šฉ๋œ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ state_dict๋ฅผ ๊ฐ€์ง. state_dict ์ €์žฅํ•˜๊ธฐ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์ €์žฅํ•˜๊ธฐ torch.save(model.state_dict(), PATH) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ model = TheModelClass(*args, **kwargs) model.load_sate_dict(torch.load(PATH) model.eval() - inference๋ฅผ ์œ„ํ•ด ํ•™์Šต๋œ ๋ชจ๋ธ์˜ ํ•™์Šต๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ state_dict๋ฅผ ์ด์šฉํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•. - ๋ชจ๋ธ ์ €์žฅ ์‹œ .pt ..

๋ฐ˜์‘ํ˜•