💡 모델 구조 출력
간단하게 모델 구조 출력
- 그냥 내가 만들어 놓은 (모델)클래스 이름을 print하면 된다.
torchsummary 라이브러리
! pip install torchsummary
summary(model, input_size = (channels, H, W))
- inpust_size를 넣어서 output shape와 parameter 수 확인 가능
- 단, 모델 클래스에서 def __init__() 안에 self 말고 다른 파라미터가 있다면 그 값이 뭔지 정의해 주어야 한다.
- ex) input_shape = (3, 256, 256), num_rersidual_blocks=9 처럼
pytorch_model_summary 라이브러리
! pip install pytorch-model-summary
summary(model, *inputs, batch_size = -1, show_input=Flase,
show_hierarchical=False, print_summary = Flase,
max_depth=1, show_parent_layers=False)
- *inputs : (N x C x H x W) -> (batch_size, channel, height, width)
- show_input = True이면 input shape이 나오고, False이면 output shape이 나온다.
- print_summary = True이면 print( ) 함수를 안써도 된다.
- max_depth = None, show_parent_layer = True를 설정하면 모든 레이어의 정보와 레이어의 parent layers path도 볼 수 있다.
- show_hierarchical = True로 설정하면 summary table 외에도 모델의 hierarchical view를 볼 수 있다.
- batch_size를 설정해주면 마지막 summary table에 같이 출력 된다.
torchinfo 라이브러리
! pip install torchinfo
summary(model, input_size = (batch_size, channel, height, width))
torchsummaryX 라이브러리
!pip install torchsummaryX
from torchsummaryX import summary
summary(model, torch.zeros((1, 3, 224, 224)))
- (batch, channel, height, width)와 같은 형태로 데이터 입력
- input size와 output size 둘 다 확인 가능
- depth도 설정할 수 있는 여러 옵션 존재
💡모델 변수 출력
전체 출력
- model.parameters( ) :
for parameter in model.parameters() :
print(parameter.size())
직접 출력
- model.fc1.weight
- model.fc1.bias
print(model.fc1.weight.size(), model.fc1.bias.size())
변수명까지 출력
- model.named_parameters()
for name, param in model.named_parameters() :
print(name, param.size())
'Programming > Pytorch' 카테고리의 다른 글
[Pytorch] GPU device (0) | 2023.01.28 |
---|---|
[Pytorch] Dataset, DataLoader (0) | 2023.01.27 |
[Pytorch] nn.Linear( ), MLP (0) | 2023.01.20 |
[Pytorch] 텐서(차원) 관련 함수 정리 (0) | 2023.01.18 |
[Pytorch] 모듈 및 함수 정리 (0) | 2023.01.15 |