๐ฅ๏ธ Programming/Pytorch
ํ์ด์ฌ ์ฝ๋๋ฅผ ๋ณด๋ฉด `if __name__ == '__main__':` ์ด๋ฐ ์ฝ๋๋ฅผ ํ์ธํ ์ ์๋ค. ์ด๋ ํ์ด์ฌ ์ฝ๋๋ฅผ ๋ชจ๋๋ก ์ฌ์ฉํ ๋์ ์ง์ ์คํํ ๋๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์ฝ๋ ํจํด์ผ๋ก. ํด๋น ๋ชจ๋์ด ์ํฌํธ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ผ ์ธํฐํ๋ฆฌํฐ์์ ์ง์ ์คํ๋ ๊ฒฝ์ฐ์๋ง if๋ฌธ ์ฝ๋๋ฅผ ์คํํ๋ผ๋ ๋ช
๋ น์ด์ด๋ค. ๋ค์๊ณผ ๊ฐ์ ์ฝ๋ ์์๊ฐ ์๋ค๊ณ ํ์. # my_module.py def my_function(): print("๋ชจ๋ ๋ด ํจ์ ์คํ") if __name__ == '__main__': print("์ง์ ์คํ๋จ") ์ฌ๊ธฐ์ ๋ค๋ฅธ ๋ชจ๋ `another_module.py`์์ ๋ค์๊ณผ ๊ฐ์ด `my_function()`์ด๋ผ๋ ํจ์๋ฅผ ํธ์ถํ๋ฉด "๋ชจ๋ ๋ด ํจ์ ์คํ"์ด๋ผ๋ ์คํ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค. # another_mo..
๐ฅ๏ธ Programming/Pytorch
def training(epoch, model, trainloader, validloader): correct = 0 total = 0 running_loss = 0 model.train() for b in trainloader: x, y = b.text, b.label x, y = x.to(device), y.to(device) # ๋ฐ๋์ ๋ชจ๋ธ๊ณผ ๊ฐ์ device y_pred = model(x) loss = loss_fn(y_pred, y) optimizer.zero_grad() loss.backward() optimizer.step() with torch.no_grad(): # ์ถ๋ก y_pred = torch.argmax(y_pred, dim=1) correct += (y_pred == y).sum(..
๐ฅ๏ธ Programming/Pytorch
nn.ReLU() ํ์ดํ ์น์ ์ ๊ฒฝ๋ง ๋ชจ๋ ์ค ํ๋์ธ ํ์ฑํ ํจ์ ๋ชจ๋ `nn.Sequential()` ๋ชจ๋ธ์ ์ถ๊ฐํ ์ ์๋ `nn.Module`์ ๋ง๋ฌ ๋ชจ๋ธ์ `__init__` ๋ฉ์๋์์ ๋ ์ด์ด๋ก ์ด๊ธฐํ๋๋ฉฐ, ๊ทธ ๋ ์ด์ด๊ฐ `forward()` ๋ฉ์๋์์ ์ฌ์ฉ๋ ๋ ํ์ฑํ ํจ์๊ฐ ์ ์ฉ F.relu() ํ์ดํ ์น์ ํจ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ `torch.nn.functional`์ ํจ์ ํจ์๋ก์ ์ฃผ๋ก ๋ชจ๋ธ์ forward ์ฐ์ฐ ๋ด์์ ํ์ฑํ ํจ์๋ฅผ ์ ์ฉํ๋ ๋ฐ ์ฌ์ฉ ๋ชจ๋ธ์ forward ๋ฉ์๋์์ ํจ์๋ก ์ง์ ํธ์ถ https://discuss.pytorch.org/t/whats-the-difference-between-nn-relu-vs-f-relu/27599 https://asidefine.tistory.com/80