model.state_dict()
时间: 2023-10-30 08:15:21 浏览: 29
This method returns a dictionary containing the state of the model. The state dict contains all the trainable parameters of the model as key-value pairs. The keys are the parameter names and the values are the parameter values. The state dict can be used to save and load the model state.
Example:
```python
import torch.nn as nn
# Define a simple model
model = nn.Sequential(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 2),
)
# Print the state dict of the model
print(model.state_dict())
```
Output:
```
OrderedDict([('0.weight', tensor([[-0.1464, -0.0292, -0.0428, -0.1395, -0.2927, 0.0024, -0.1274, 0.2838,
-0.1555, -0.1155],
[ 0.0485, -0.0121, 0.2262, 0.1230, 0.1632, -0.1716, 0.0260, -0.1388,
-0.0559, -0.0088],
[ 0.1007, -0.0886, -0.0397, -0.2816, 0.1102, 0.1846, 0.2877, -0.1633,
-0.0046, -0.2268],
[ 0.2967, 0.2078, 0.2856, 0.1374, 0.1577, -0.0576, -0.2108, -0.1999,
0.2785, -0.0573],
[-0.0491, -0.0572, 0.0629, 0.2515, 0.2247, -0.1601, -0.2762, 0.0459,
-0.2034, 0.0762]])), ('0.bias', tensor([-0.2045, -0.2039, -0.0141, -0.0620, 0.2876])), ('2.weight', tensor([[ 0.0092, 0.2682, -0.3807, -0.3963, -0.4518],
[ 0.5443, -0.2556, 0.3782, -0.1193, -0.1083]])), ('2.bias', tensor([ 0.2886, -0.0211]))])
```