๋ฐ์ํ
pytorch ๋ชจ๋ธ์ ์ ๊ทผํ๊ณ ํน์ layer ๋๋ layer ๋ด๋ถ์ ๋ชจ๋์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํ๋ค.
- ์์ ๋ชจ๋ธ : resnet50
import torch.nn as nn
import torchvision.models as models
model = models.resnet50(pretrained=True)
1. self.named_parameters()
for name, param in model.layer1.named_parameters():
print(name,param.shape,sep=" ")
2. self.named_children()
for name,ch in model.layer1.named_children():
print("name :",name)
print("child :", ch)
print('===========================')
๋ฐ์ํ
3. Model layer ๋ณ๊ฒฝ
๋ชจ๋ธ์ ๋ฉค๋ฒ๋ณ์๋ฅผ return ํด์ฃผ๋ named_children()์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์ ํ๋ layer๋ฅผ ์ฐพ๊ณ , ํด๋์ค์ ๋ชจ๋๋ค์ return ํด์ฃผ๋ _modules๋ฅผ ์ฌ์ฉํ์ฌ ์ ๊ทผ.
print('model.layer1._modules[\'0\'] ๋ณ๊ฒฝ ์ :', model.layer1._modules['0'],sep ='\n')
model.layer1._modules['0']._modules['conv1'] = nn.Conv2d(77,77,7)
print('model.layer1._modules[\'0\'] ๋ณ๊ฒฝ ํ :', model.layer1._modules['0'],sep ='\n')
print('model.layer1._modules[\'0\']._modules[\'conv1\'] ๋ณ๊ฒฝ ํ :',model.layer1._modules['0']._modules['conv1'],sep ='\n')
* ์ ๊ทผ ์์
Resnet50 ๋ชจ๋ธ์์ layer1์ 0๋ฒ์งธ bottleneck์ downsample์ Conv2d ์์ ์ ๊ทผํ๊ธฐ ์ํด์๋ ์๋์ ๊ฐ์ด -
model.layer1._modules['0']._modules['downsample']._modules['0']
๋ฐ์ํ