๋ฐ˜์‘ํ˜•

๐Ÿ Python & library 49

[PyTorch] Weight clipping

์ด์ „ ๊ธ€์—์„œ PyTorch๋ฅผ ์ด์šฉํ•œ gradient clipping์„ ์†Œ๊ฐœํ–ˆ๋Š”๋ฐ, gradient๊ฐ€ ์•„๋‹ˆ๋ผ weight ๊ฐ’ ์ž์ฒด๋ฅผ ์ผ์ • ๋ฒ”์œ„ ์•ˆ์œผ๋กœ ์ œํ•œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ Weight clipping์„ ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š” class๋ฅผ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถœ์ฒ˜: https://discuss.pytorch.org/t/set-constraints-on-parameters-or-layers/23620 Set constraints on parameters or layers Hi, are there any ways in Pytorch to set the range of parameters or values in each layer? For example, is it able to constrain th..

[PyTorch] Enable anomaly detection (torch.autograd.detect_anomaly() / torch.autograd.set_detect_anomaly(True))

๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋˜ ์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [CUDAComplexFloatType [1, 256, 232]], which is output 0 of SubBackward0, is at version 10; expected version 9 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True). ..

[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..

[h5py] hdf5 ์†Œ๊ฐœ, h5py ์‚ฌ์šฉ๋ฒ• ๊ฐ„๋‹จ ์ •๋ฆฌ

Manual: https://docs.h5py.org/en/stable/ HDF5 for Python — h5py 3.5.0 documentation © Copyright 2014, Andrew Collette and contributors Revision fb9989a7. docs.h5py.org h5py๋Š” HDF5 ๋ฐ์ดํ„ฐ ํฌ๋งท์„ Python์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€์ด๋‹ค. HDF5๋ฅผ ์ด์šฉํ•˜๋ฉด ๋Œ€๋Ÿ‰์˜ NumPy ๋ฐ์ดํ„ฐ ๋“ฑ์„ ์†์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. About HDF5 HDF๋Š” Hierarchical Data Format์˜ ์•ฝ์ž์ด๋‹ค. HDF5์˜ ๋ชจ๋“  object๋Š” ๊ฐ์ž์˜ 'name'์ด ์žˆ๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌ๋œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์šด์˜์ฒด์ œ์˜ ํŒŒ์ผ์‹œ์Šคํ…œ์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ํด๋”์™€ ํŒŒ์ผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”..

[numpy] ๋‚˜๋งŒ ๋ชฐ๋ž๋˜ ์œ ์šฉํ•œ ํ•จ์ˆ˜ ๋ชจ์Œ

์„ธ์ƒ์— ์ด๋Ÿฐ ํŽธ๋ฆฌํ•œ ํ•จ์ˆ˜๋„ ์žˆ์—ˆ๋‹ค๋‹ˆ.. ํ•˜๋Š” ํ•จ์ˆ˜๋“ค ๋ชจ์Œ ๐Ÿ™Š (๋ชจ์œผ๋Š” ์ค‘) 1. numpy.rint(a) array์˜ ๊ฐ ์›์†Œ๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•œ๋‹ค. a = np.array([-1.1, -2.7, 3.0, 4.3, 5.5, 6.5]) print(np.rint(a)) >> [-1. -3. 3. 4. 6. 6.] 2. numpy.around(a, decimals=0) array์˜ ๊ฐ ์›์†Œ๋ฅผ ์ฃผ์–ด์ง„ ์†Œ์ˆ˜์  ์œ„์น˜(decimals)์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•œ๋‹ค. a = np.array([1.2345, 2.3456, 3.4567, -4.5678]) print(np.around(a, 2)) >> [ 1.23 2.35 3.46 -4.57] 3. numpy.clip(a, a_min, a_max) array์˜ ๊ฐ ์›์†Œ๋ฅผ ์ฃผ์–ด์ง„ ๋ฒ”์œ„๋กœ clipํ•œ..

Python print ์˜ต์…˜ 'sep'

Python์˜ print ํ•จ์ˆ˜์—๋Š” ์ถœ๋ ฅํ•  ๋‚ด์šฉ ์™ธ์—๋„ ๋‹ค๋ฅธ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ค‘ ํ•˜๋‚˜์ธ 'sep'์€ ์ถœ๋ ฅํ•  ๋‚ด์šฉ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋ถ„๋ฆฌํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ' ' ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค. ๋งŒ์•ฝ '\n'์œผ๋กœ sep ์˜ต์…˜์„ ์„ค์ •ํ•˜๋ฉด, ์ž…๋ ฅ๋ฐ›์€ ์ธ์ž๋“ค์„ ์ค„๋ฐ”๊ฟˆ์œผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ถœ๋ ฅํ•ด ์ค€๋‹ค.

[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..

๋ฐ˜์‘ํ˜•