This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for key in model.fc.state_dict(): | |
| print('key: ', key) | |
| param = model.fc.state_dict()[key] | |
| print('param.shape: ', param.shape) | |
| print('param.requires_grad: ', param.requires_grad) | |
| print('param.shape, param.requires_grad: ', param.shape, param.requires_grad) | |
| print('isinstance(param, nn.Module) ', isinstance(param, nn.Module)) | |
| print('isinstance(param, nn.Parameter) ', isinstance(param, nn.Parameter)) | |
| print('isinstance(param, torch.Tensor): ', isinstance(param, torch.Tensor)) | |
| print('=====') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| torch.save(model.state_dict(), 'weights_only.pth') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model_new = NeuralNet() | |
| model_new.load_state_dict(torch.load('weights_only.pth')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name, param in model_new.named_parameters(): | |
| print(name, ':', param.requires_grad) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| torch.save(model, 'entire_model.pth') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model_new = torch.load('entire_model.pth') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name, param in model_new.named_parameters(): | |
| print(name, ':', param.requires_grad) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name , param in model.named_parameters(): | |
| print('type(param): ', type(param)) | |
| print('isinstance(param, nn.Module): ', isinstance(param, nn.Module)) | |
| print('isinstance(param, nn.Parameter): ', isinstance(param, nn.Parameter)) | |
| print('isinstance(param, torch.Tensor) ', isinstance(param, torch.Tensor)) | |
| print('=====') | |
| # Also try model.parameters(). It doesn't return the name of the parameters but just the parameters. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for param in model.layer1.parameters(): | |
| param.requires_grad = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name, child in model.named_children(): | |
| print('name: ', name) | |
| print('isinstance(child, nn.Module): ', isinstance(child, nn.Module)) | |
| print('isinstance(child, nn.Parameter): ', isinstance(child, nn.Parameter)) | |
| print('isinstance(child, torch.Tensor) ', isinstance(child, torch.Tensor)) | |
| print('=====') | |
| # Also try model.children(). It doesn't return the name of the children, but the children (nn.Module objects) |