【发布时间】:2021-01-04 02:42:45
【问题描述】:
我在运行大型 RNN 网络时遇到了一些内存问题 (GPU),但我想保持我的批量大小合理,所以我想尝试梯度累积。在一个一次性预测输出的网络中,这似乎是不言而喻的,但在 RNN 中,您为每个输入步骤执行多个前向传递。因此,我担心我的实现无法按预期工作。我从用户 albanD 的优秀示例 here 开始,但我认为在使用 RNN 时应该对其进行修改。我认为这是因为你积累了更多的梯度,因为你在每个序列中进行了多次转发。
我当前的实现看起来像这样,同时允许在 PyTorch 1.6 中使用 AMP,这似乎很重要 - 一切都需要在正确的位置调用。请注意,这只是一个抽象版本,看起来可能有很多代码,但主要是 cmets。
def train(epochs):
"""Main training loop. Loops for `epoch` number of epochs. Calls `process`."""
for epoch in range(1, epochs + 1):
train_loss = process("train")
valid_loss = process("valid")
# ... check whether we improved over earlier epochs
if lr_scheduler:
lr_scheduler.step(valid_loss)
def process(do):
"""Do a single epoch run through the dataloader of the training or validation set.
Also takes care of optimizing the model after every `gradient_accumulation_steps` steps.
Calls `step` for each batch where it gets the loss from."""
if do == "train":
model.train()
torch.set_grad_enabled(True)
else:
model.eval()
torch.set_grad_enabled(False)
loss = 0.
for batch_idx, batch in enumerate(dataloaders[do]):
step_loss, avg_step_loss = step(batch)
loss += avg_step_loss
if do == "train":
if amp:
scaler.scale(step_loss).backward()
if (batch_idx + 1) % gradient_accumulation_steps == 0:
# Unscales the gradients of optimizer's assigned params in-place
scaler.unscale_(optimizer)
# clip in-place
clip_grad_norm_(model.parameters(), 2.0)
scaler.step(optimizer)
scaler.update()
model.zero_grad()
else:
step_loss.backward()
if (batch_idx + 1) % gradient_accumulation_steps == 0:
clip_grad_norm_(model.parameters(), 2.0)
optimizer.step()
model.zero_grad()
# return average loss
return loss / len(dataloaders[do])
def step():
"""Processes one step (one batch) by forwarding multiple times to get a final prediction for a given sequence."""
# do stuff... init hidden state and first input etc.
loss = torch.tensor([0.]).to(device)
for i in range(target_len):
with torch.cuda.amp.autocast(enabled=amp):
# overwrite previous decoder_hidden
output, decoder_hidden = model(decoder_input, decoder_hidden)
# compute loss between predicted classes (bs x classes) and correct classes for _this word_
item_loss = criterion(output, target_tensor[i])
# We calculate the gradients for the average step so that when
# we do take an optimizer.step, it takes into account the mean step_loss
# across batches. So basically (A+B+C)/3 = A/3 + B/3 + C/3
loss += (item_loss / gradient_accumulation_steps)
topv, topi = output.topk(1)
decoder_input = topi.detach()
return loss, loss.item() / target_len
上述方法似乎没有像我希望的那样工作,即它仍然很快遇到内存不足的问题。我想原因是step已经积累了这么多信息,但我不确定。
【问题讨论】:
标签: deep-learning pytorch recurrent-neural-network gradient-descent