【发布时间】:2018-01-03 16:11:14
【问题描述】:
我有一个问题,我已经无法解决一个星期。我正在尝试构建 CIFAR-10 分类器,但是每批后我的损失值随机跳跃,即使在同一批次上,准确性也没有提高(我什至不能用一批过拟合模型),所以我猜唯一可能的原因is - 权重没有更新。
我的模块类
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv_pool = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 128, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(128, 256, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(256, 512, 3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(512, 512, 1),
nn.ReLU(),
nn.MaxPool2d(2, 2))
self.fcnn = nn.Sequential(
nn.Linear(512, 2048),
nn.ReLU(),
nn.Linear(2048, 2048),
nn.ReLU(),
nn.Linear(2048, 10)
)
def forward(self, x):
x = self.conv_pool(x)
x = x.view(-1, 512)
x = self.fcnn(x)
return x
我正在使用的优化器:
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
我的火车功能:
def train():
for epoch in range(5): # loop over the dataset multiple times
for i in range(0, df_size):
# get the data
try:
images, labels = loadBatch(ds, i)
except BaseException:
continue
# wrap
inputs = Variable(images)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, Variable(labels))
loss.backward()
optimizer.step()
acc = test(images,labels)
print("Loss: " + str(loss.data[0]) + " Accuracy %: " + str(acc) + " Iteration: " + str(i))
if i % 40 == 39:
torch.save(net.state_dict(), "model_save_cifar")
print("Finished epoch " + str(epoch))
我正在使用 batch_size = 20,image_size = 32 (CIFAR-10)
loadBatch 函数返回 LongTensor 20x3x32x32 的图像和 LongTensor 20x1 的标签的元组
如果您能帮助我或提出可能的解决方案,我将非常高兴(我猜这是因为 NN 中的顺序模块,但我传递给优化器的参数似乎是正确的)
【问题讨论】:
-
乍一看,我觉得很好。您也可以发布您的测试功能吗?
-
@blckbird 如果您有兴趣,请查看我的答案。测试函数只是遍历 test_set 并将输出标签与 train_set 标签进行比较
标签: python machine-learning computer-vision conv-neural-network pytorch