我正在尝试使用本教程加载模型:https : //pytorch.org/tutorials/beginner/saving_loading_models.html#saving-loading-model-for-inference。不幸的是,我非常初学者,我面临一些问题。
我创建了检查点:
checkpoint = {'epoch': epochs, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(),'loss': loss}
torch.save(checkpoint, 'checkpoint.pth')
Run Code Online (Sandbox Code Playgroud)
然后我为我的网络编写了类,我想加载文件:
class Network(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(9216, 4096)
self.fc2 = nn.Linear(4096, 1000)
self.fc3 = nn.Linear(1000, 102)
def forward(self, x):
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
x = self.fc3(x)
x = log(F.softmax(x, dim=1))
return x
Run Code Online (Sandbox Code Playgroud)
像那样:
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
model = Network()
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
model = load_checkpoint('checkpoint.pth')
Run Code Online (Sandbox Code Playgroud)
我收到此错误(已编辑以显示整个通信): …
python machine-learning neural-network conv-neural-network pytorch
这是我的代码:
dictionary = {}
for i in range(2, 15):
dictionary[str(i)] = 0
Run Code Online (Sandbox Code Playgroud)
是否可以用一行代码创建它?