我正在使用PyTorch线性回归损失,但我的SGD损失没有收敛。 用例-
model = nn.Linear(input_size,num_classes)
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Reshape images to (batch_size, input_size)
images = images.reshape(-1, 28*28)
#Convert labels from 0,1 to -1,1
labels = Variable(2*(labels.float()-0.5))
# Forward pass
outputs = model(images)
# we need maximum value of two class prediction
oneout = torch.max(outputs.data,1)[0]
loss = loss_criteria(oneout, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
损输出:0.8445,0.6883,0.7976,0.8133,0.8289,0.7195。如您所见,损失没有收敛。 预期结果 : 0.8445,0.8289,0.8133,0.7976,0.7195,0.6883一路零....