【问题标题】:Loss not Converging for CNN ModelCNN 模型的损失不收敛
【发布时间】:2020-04-25 14:07:17
【问题描述】:

图像转换和批处理

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.com/thedagger/pokemon-generation-one

我们将不胜感激。 谢谢你

【问题讨论】:

    标签: machine-learning deep-learning computer-vision pytorch conv-neural-network


    【解决方案1】:

    您的网络的问题是您应用了两次softmax() - 一次在fc4() 层,一次在使用nn.CrossEntropyLoss() 时。

    根据official documentation,Pytorch 在应用nn.CrossEntropyLoss() 时会处理softmax()

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

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

    x = self.fc4(x)
    

    【讨论】:

    • 如果是这种情况,那么您必须添加更多层并使网络更加复杂。从最先进的架构(如 Resnet、Alexnet 等)中汲取灵感,制作您自己的自定义网络,或者您也可以使用迁移学习。
    猜你喜欢
    • 2018-09-16
    • 2018-05-18
    • 2019-06-03
    • 2017-02-08
    • 2019-02-20
    • 2018-01-26
    • 2018-05-03
    • 2022-11-18
    • 2020-01-08
    相关资源
    最近更新 更多