Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neural network 使用Pytorch在MNIST数据集上使用SGD,损失不会减少_Neural Network_Pytorch_Mnist_Stochastic Gradient - Fatal编程技术网

Neural network 使用Pytorch在MNIST数据集上使用SGD,损失不会减少

Neural network 使用Pytorch在MNIST数据集上使用SGD,损失不会减少,neural-network,pytorch,mnist,stochastic-gradient,Neural Network,Pytorch,Mnist,Stochastic Gradient,我尝试在批量大小为32的MNIST数据集上使用SGD,但损失根本没有减少。 我检查了我的模型、损失函数并阅读了文档,但不知道我做错了什么 我定义我的神经网络如下 class classification(nn.Module): def __init__(self): super(classification, self).__init__() # construct layers for a neural network self.classifier1 = n

我尝试在批量大小为32的MNIST数据集上使用SGD,但损失根本没有减少。 我检查了我的模型、损失函数并阅读了文档,但不知道我做错了什么

我定义我的神经网络如下

class classification(nn.Module):
def __init__(self):
    super(classification, self).__init__()
    
    # construct layers for a neural network
    self.classifier1 = nn.Sequential(
        nn.Linear(in_features=28*28, out_features=20*20),
        nn.Sigmoid(),
    ) 
    self.classifier2 = nn.Sequential(
        nn.Linear(in_features=20*20, out_features=10*10),
        nn.Sigmoid(),
    ) 
    self.classifier3 = nn.Sequential(
        nn.Linear(in_features=10*10, out_features=10),
        nn.LogSoftmax(dim=1),
    ) 
    
    
def forward(self, inputs):                 # [batchSize, 1, 28, 28]
    x = inputs.view(inputs.size(0), -1)    # [batchSize, 28*28]
    x = self.classifier1(x)                # [batchSize, 20*20]
    x = self.classifier2(x)                # [batchSize, 10*10]
    out = self.classifier3(x)              # [batchSize, 10]
    
    return out
我将我的培训过程定义如下


classifier = classification().to("cuda")
#optimizer
optimizer = torch.optim.SGD(classifier.parameters(), lr=learning_rate_value)
#loss function
criterion = nn.NLLLoss()
batch_size=32
epoch = 30
#array to save loss history
loss_train_arr=np.zeros(epoch)

#used DataLoader to make split batch
batched_train = torch.utils.data.DataLoader(training_set, batch_size, shuffle=True)

for i in range(epoch):
    
    loss_train=0
    
    #train and compute loss, accuracy
    for img, label in batched_train:
        img=img.to(device)
        label=label.to(device)

        optimizer.zero_grad()
        predicted = classifier(img)
        
        label_predicted = torch.argmax(predicted,dim=1)
        loss = criterion(predicted, label)
        loss.backward
        optimizer.step()
        loss_train += loss.item()
        
    loss_train_arr[i]=loss_train/(len(batched_train.dataset)/batch_size)


我使用的是带有LogSoftmax层的模型,所以我的损失函数似乎是正确的。但是损失根本没有减少。

如果您发布的代码与您使用的代码完全相同,那么问题在于您实际上没有对损失进行反向调用(缺少括号
()
)。

以确保损失函数不是问题所在,请在最后一层使用softmax acitvation,并使用MSE作为损耗函数,并检查works@CodeYong然后请点击左边靠近答案顶部的勾号来接受答案。对不起,我不知道有“接受”按钮。帮了我很多忙!