ttributeError: 'function' object has no attribute 'state_dict'
时间: 2023-12-19 20:31:59 浏览: 255
以下是解决AttributeError: 'function' object has no attribute 'state_dict'的两种方法:
1. 使用torch.load和load_state_dict加载模型参数:
```python
import torch
import torch.nn as nn
# 定义模型
class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.fc = nn.Linear(10, 5)
model = NeuralNet()
# 保存模型参数
torch.save(model.state_dict(), '/output/model_state_dict.pth')
# 加载模型参数
model.load_state_dict(torch.load('/output/model_state_dict.pth'))
```
2. 确保在调用load_state_dict时,括号里面要带上torch.load:
```python
import torch
import torch.nn as nn
# 定义模型
class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.fc = nn.Linear(10, 5)
model = NeuralNet()
# 保存模型参数
torch.save(model.state_dict(), '/output/model_state_dict.pth')
# 加载模型参数
model.load_state_dict(torch.load('/output/model_state_dict.pth'))
```
阅读全文
相关推荐













