Sud*_*shu 7 python lstm recurrent-neural-network pytorch
我正在从某人的代码中使用 PyTorch 学习 LSTM。这里他clip_grad_norm_在两层 LSTM 的训练过程中使用了该函数。我想知道他为什么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()
Run Code Online (Sandbox Code Playgroud)
如果您需要有关问题的更多信息,请告诉我。