为什么这里要用到clip_grad_norm_函数呢?

Why is the clip_grad_norm_ function used here?

我正在从某人的代码中使用 PyTorch 学习 LSTM。这里他在两层LSTM的训练过程中使用了clip_grad_norm_函数。我想知道他为什么在这里使用 clip_grad_norm_ 函数,这样我就可以正确理解整个代码(他在倒数第二行使用它)。

for x, y in get_batches(data, batch_size, seq_length):
    counter += 1
                            
    x = one_hot_encode(x, n_chars)
    inputs, targets = torch.from_numpy(x), torch.from_numpy(y)
            
    if(train_on_gpu):
        inputs, targets = inputs.cuda(), targets.cuda()

    h = tuple([each.data for each in h])                    
    net.zero_grad()
                            
    output, h = net(inputs, h)                
            
    loss = criterion(output, targets.view(batch_size*seq_length).long())
    loss.backward()

    nn.utils.clip_grad_norm_(net.parameters(), clip)
    opt.step() 

如果您需要有关问题的更多信息,请告诉我。

torch.nn.utils.clip_grad_norm_ performs gradient clipping. It is used to mitigate the problem of exploding gradients,这是循环网络(LSTM 是其中的一种)特别关注的问题。

可以在 original paper 中找到更多详细信息。