简体   繁体   English

CNN Model 的损失不收敛

[英]Loss not Converging for CNN Model

Image Transformation and Batch图像转换和批处理

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)

Below is my Model下面是我的 Model

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

Creating Instance of model, Use GPU, Set Criterion and optimizer Here is firsr set lr = 0.001 then later changed to 0.0001创建 model 的实例,使用 GPU,设置标准和优化器 这是第一个设置lr = 0.001然后后来更改为0.0001

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

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

Training Dataset训练数据集

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)

My loss value remains between 4 - 3 and its not converging to 0. I am super new to deep learning and I don't know much about it.我的损失值保持在 4 - 3 之间,并且没有收敛到 0。我对深度学习非常陌生,对此我了解不多。

The dataset I am using is here: https://www.kaggle.com/thedagger/pokemon-generation-one我使用的数据集在这里: https://www.kaggle.com/thedagger/pokemon-generation-one

A help will be much appreciated.非常感谢您的帮助。 Thank You谢谢你

The problem with your network is that you are applying softmax() twice - once at fc4() layer and once more while using nn.CrossEntropyLoss() .您的网络的问题是您应用了两次softmax() - 一次在fc4()层,一次在使用nn.CrossEntropyLoss()时。

According to the official documentation , Pytorch takes care of softmax() while applying nn.CrossEntropyLoss() .根据官方文档, Pytorch 在应用nn.CrossEntropyLoss() ) 时会处理softmax() ) 。

So in your code, please change this line所以在你的代码中,请改变这一行

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

to

x = self.fc4(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM