CNN模型的损失不收敛

问题描述 投票:0回答:1

图像转换和批处理

transform = transforms.Compose([

                                transforms.Resize((100,100)),

                                transforms.ToTensor(),

                                transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])

                                ])

data_set = datasets.ImageFolder(root="/content/drive/My Drive/models/pokemon/dataset",transform=transform)

train_loader = DataLoader(data_set,batch_size=10,shuffle=True,num_workers=6)

以下是我的模型

class pokimonClassifier(nn.Module):

  def __init__(self):

    super().__init__()

    self.conv1 = nn.Conv2d(3,6,3,1)

    self.conv2 = nn.Conv2d(6,18,3,1)

    self.fc1 = nn.Linear(23*23*18,520)

    self.fc2 = nn.Linear(520,400)

    self.fc3 = nn.Linear(400,320)

    self.fc4 = nn.Linear(320,149)

  def forward(self,x):

    x = F.relu(self.conv1(x))

    x = F.max_pool2d(x,2,2)

    x = F.relu(self.conv2(x))  

    x = F.max_pool2d(x,2,2)

    x = x.view(-1,23*23*18)

    x = F.relu(self.fc1(x))

    x = F.relu(self.fc2(x))

    x = F.relu(self.fc3(x))

    x = F.log_softmax(self.fc4(x), dim=1)

    return x

创建模型实例,使用GPU,设置标准和优化器这里是第一个设置。lr = 0.001 后改为 0.0001

model = pokimonClassifier()
model.to('cuda')
criterion = nn.CrossEntropyLoss()

optimizer = torch.optim.Adam(model.parameters(),lr = 0.0001)

训练数据集

for e in range(epochs):

  train_crt = 0

  for b,(train_x,train_y) in enumerate(train_loader):

    b+=1

    train_x, train_y = train_x.to('cuda'), train_y.to('cuda')

    # train model

    y_preds = model(train_x)

    loss = criterion(y_preds,train_y)

    # analysis model

    predicted = torch.max(y_preds,1)[1]

    correct = (predicted == train_y).sum()

    train_crt += correct

    # print loss and accuracy

    if b%50 == 0:

        print(f'Epoch {e} batch{b} loss:{loss.item()} ')

    # updating weights and bais

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

  train_loss.append(loss)

  train_correct.append(train_crt)

我的损失值保持在4 - 3之间,其没有收敛到0.我是深度学习的超级新手,我对它了解不多。

我使用的数据集在这里。https:/www.kaggle.comthedaggerpokemon-generation-one

非常感谢你的帮助。

machine-learning deep-learning computer-vision pytorch conv-neural-network
1个回答
1
投票

你的网络的问题是你在应用 softmax() 两次--一次在 fc4() 层,再一次使用 nn.CrossEntropyLoss().

根据《公约》的规定。公文Pytorch负责 softmax() 在申请 nn.CrossEntropyLoss().

所以在你的代码中,请修改这一行

x = F.log_softmax(self.fc4(x), dim=1)

x = self.fc4(x)
© www.soinside.com 2019 - 2024. All rights reserved.