Artificial Intelligence/Framework

[PyTorch] class ๊ธฐ๋ฐ˜ ๋ชจ๋ธ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

geum 2022. 9. 16. 13:40

https://www.assemblyai.com/blog/pytorch-vs-tensorflow-in-2022/

 

class ModelName(nn.Module):
    def __init__(self):
                super().__init__()
                # ์ฝ”๋“œ ์ž‘์„ฑ ๋ถ€๋ถ„

    def forward(self):
                # ์ฝ”๋“œ ์ž‘์„ฑ ๋ถ€๋ถ„

 

nn.Module

- ๋ชจ๋“  ์‹ ๊ฒฝ๋ง ๋ชจ๋“ˆ์˜ ๊ธฐ๋ณธ์ด ๋˜๋Š” ํด๋ž˜์Šค

- ๋ชจ๋ธ ๊ตฌํ˜„ ์‹œ ์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ(should)

- nn.Module ํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ nn.Module ์•ˆ์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ

- Linear, Dropout, LayerNorm ๋“ฑ ๋ชจ๋ธ ์ƒ์„ฑ์— ์“ฐ์ด๋Š” ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ ํฌํ•จ

 

__init__

- ์‹ ๊ฒฝ๋ง ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

- โœ… super().__init__(): ๋ถ€๋ชจ ํด๋ž˜์Šค(nn.Module)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์—ญํ• ๋กœ ์ด ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  self.๋ณ€์ˆ˜๋ช… = ~ ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ

 

forward

- ๋ชจ๋ธ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด forward ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  output์„ ๋ฐ˜ํ™˜

- __init__ ์—์„œ ์ •์˜ํ•œ ๋ชจ๋“ˆ์„ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ๋ชจ๋“ˆ ๊ฐ„ ์—ฐ์‚ฐ์„ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„ 

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

https://wikidocs.net/book/2788

https://tutorials.pytorch.kr/

https://pytorch.org/docs/stable/index.html

https://medium.com/dejunhuang/learning-day-22-what-is-nn-module-in-pytorch-ecf8400f411a

https://goldenrabbit.co.kr/2022/07/30/%ED%8C%8C%EC%9D%B4%ED%86%A0%EC%B9%98-%EC%BD%94%EB%94%A9-%EA%B6%8C%EA%B3%A0-%EC%8A%A4%ED%83%80%EC%9D%BC-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0/