我想使用 pretrain inceptionV3 训练一个 100 类的模型,但是在我的训练代码中,我遇到了一个奇怪的错误,显示如下错误:
我的培训代码是:
step = -1
print_inter=50
val_inter=400
train_size = ceil(len(data_set['train']) / dataloader['train'].batch_size)
for epoch in range(50):
# train phase
exp_lr_scheduler.step(epoch)
inception_v3.train(True)
for batch_cnt, data in enumerate(dataloader['train']):
step += 1
inception_v3.train(True)
inputs, labels = data
inputs = torch.autograd.Variable(inputs.cuda())
labels = torch.autograd.Variable(torch.from_numpy(np.array(labels)).long().cuda())
outputs = inception_v3(inputs)
# zero the parameter gradients
optimizer.zero_grad()
outputs = inception_v3(inputs)
print(inputs.shape)
print(outputs[0].shape)
print(outputs[1].shape)
print(labels.shape)
loss = criterion(outputs[0], labels)
loss += criterion(outputs[1], labels)
outputs = (outputs[0] + outputs[1]) / 2
_, preds = torch.max(outputs, 1)
loss.backward()
optimizer.step()
# batch loss
inception_v3.train(False) # Set model to evaluate mode
for batch_cnt_val, data_val in enumerate(dataloader['val']):
# print data
inputs, labels = data_val
inputs = Variable(inputs.cuda())
labels = Variable(torch.from_numpy(np.array(labels)).long().cuda())
# forward
outputs = inception_v3(inputs)
print(inputs.shape)
print(outputs[0].shape)
print(outputs[1].shape)
print(labels.shape)
loss = criterion(outputs[0], labels)
loss += criterion(outputs[1], labels)
outputs = (outputs[0] + outputs[1]) / 2
运行此代码后,得到以下结果:
torch.Size([8, 3, 299, 299])
torch.Size([8, 100])
torch.Size([8, 100])
torch.Size([8])
torch.Size([8, 3, 299, 299])
torch.Size([100])
torch.Size([100])
torch.Size([8])
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-21-0cfbe57aae73> in <module>()
49 print(outputs[1].shape)
50 print(labels.shape)
---> 51 loss = criterion(outputs[0], labels)
52 loss += criterion(outputs[1], labels)
53 outputs = (outputs[0] + outputs[1]) / 2
~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
757 _assert_no_grad(target)
758 return F.cross_entropy(input, target, self.weight, self.size_average,
--> 759 self.ignore_index, self.reduce)
760
761
~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce)
1440 >>> loss.backward()
1441 """
-> 1442 return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
1443
1444
~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel)
942 if dim is None:
943 dim = _get_softmax_dim('log_softmax', input.dim(), _stacklevel)
--> 944 return torch._C._nn.log_softmax(input, dim)
945
946
RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1)
您可以看到相同的模型获得相同的输入大小,但我的第二个输出错过了批处理维度。原谅我蹩脚的英文