Deep learning 培训损失完全不变(PyTorch)

Deep learning 培训损失完全不变(PyTorch),deep-learning,neural-network,nlp,pytorch,lstm,Deep Learning,Neural Network,Nlp,Pytorch,Lstm,我试图解决一个文本分类问题。我的训练数据以80个数字的顺序输入,每个数字代表一个单词,目标值仅为1到3之间的数字。 我通过这个模型传递它: class Model(nn.Module): def __init__(self, tokenize_vocab_count): super().__init__() self.embd = nn.Embedding(tokenize_vocab_count+1, 300) self.embd_dro

我试图解决一个文本分类问题。我的训练数据以80个数字的顺序输入,每个数字代表一个单词,目标值仅为1到3之间的数字。 我通过这个模型传递它:

class Model(nn.Module):
    def __init__(self, tokenize_vocab_count):
        super().__init__()
        self.embd = nn.Embedding(tokenize_vocab_count+1, 300)
        self.embd_dropout = nn.Dropout(0.3)
        self.LSTM = nn.LSTM(input_size=300, hidden_size=100, dropout=0.3, batch_first=True)
        self.lin1 = nn.Linear(100, 1024)
        self.lin2 = nn.Linear(1024, 512)
        self.lin_dropout = nn.Dropout(0.8)
        self.lin3 = nn.Linear(512, 3)

    def forward(self, inp):
        inp = self.embd_dropout(self.embd(inp))
        inp, (h_t, h_o) = self.LSTM(inp)
        h_t = F.relu(self.lin_dropout(self.lin1(h_t)))
        h_t = F.relu(self.lin_dropout(self.lin2(h_t)))
        out = F.softmax(self.lin3(h_t))
        return out
我的培训循环如下:

model = Model(tokenizer_obj.count+1).to('cuda')

optimizer = optim.AdamW(model.parameters(), lr=1e-2)
loss_fn = nn.CrossEntropyLoss()

EPOCH = 10

for epoch in range(0, EPOCH):
     for feature, target in tqdm(author_dataloader):
         train_loss = loss_fn(model(feature.to('cuda')).view(-1,  3), target.to('cuda'))
         optimizer.zero_grad()
         train_loss.backward()
         optimizer.step()
      print(f"epoch: {epoch + 1}\tTrain Loss : {train_loss}")
torch.Size([64, 80]) torch.Size([64])
我打印出了特征和目标尺寸,如下所示:

model = Model(tokenizer_obj.count+1).to('cuda')

optimizer = optim.AdamW(model.parameters(), lr=1e-2)
loss_fn = nn.CrossEntropyLoss()

EPOCH = 10

for epoch in range(0, EPOCH):
     for feature, target in tqdm(author_dataloader):
         train_loss = loss_fn(model(feature.to('cuda')).view(-1,  3), target.to('cuda'))
         optimizer.zero_grad()
         train_loss.backward()
         optimizer.step()
      print(f"epoch: {epoch + 1}\tTrain Loss : {train_loss}")
torch.Size([64, 80]) torch.Size([64])
这里64是批量大小。 我现在没有做任何验证。 当我训练时,我得到一个恒定的损失值,没有变化

/home/koushik/Software/miniconda3/envs/fastai/lib/python3.7/site-packages/torch/nn/modules/rnn.py:50: UserWarning: dropout option adds dropout after all but last recurrent layer, so non-zero dropout expects num_layers greater than 1, but got dropout=0.3 and num_layers=1
  "num_layers={}".format(dropout, num_layers))
  0%|                                                                                                                                                 | 0/306 [00:00<?, ?it/s]/media/koushik/Backup Plus/Code/Machine Deep Learning/NLP/src/Deep Learning/model.py:20: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.
  out = F.softmax(self.lin3(h_t))
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 89.36it/s]
epoch: 1        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 89.97it/s]
epoch: 2        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 89.35it/s]
epoch: 3        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 89.17it/s]
epoch: 4        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 88.72it/s]
epoch: 5        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 87.75it/s]
epoch: 6        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 85.67it/s]
epoch: 7        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 85.40it/s]
epoch: 8        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 84.49it/s]
epoch: 9        Train Loss : 1.0986120700836182
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 306/306 [00:03<00:00, 84.21it/s]
epoch: 10       Train Loss : 1.0986120700836182
/home/koushik/Software/miniconda3/envs/fastai/lib/python3.7/site packages/torch/nn/modules/rnn.py:50:UserWarning:dropout选项会在最后一个循环层之后添加dropout,因此非零dropout要求num_layers大于1,但得到的dropout=0.3,num_layers=1
“num_layers={}”。格式(辍学,num_layers))
您正在使用的0%| | 0/306[00:00应用了log softmax,但您也在模型中应用了softmax:

out=F.softmax(self.lin3(h\u t))

模型的输出应该是原始登录,没有
F.softmax

应用日志softmax,但是您也将softmax应用于模型的输出,这是有害的,您应该将其删除。另外,将线性退出设置为0.8相当高,可以先以较低的退出概率尝试它,或者完全禁用它,仅为v验证该模型是否有效。此外,学习率相当高,您可能希望将其设置为低10/30倍或soThanks@MichaelJungo。问题在于模型输出中的softmax。我减少了辍学率,并降低了学习率。现在模型似乎在训练,但损失不断上升和下降。有什么建议吗?@MichaelJungo为这个问题添加一个答案,我将其标记为答案:)