我正在火炬上运行 BERT 模型。这是一个多类情感分类任务,大约有 30,000 行。我已经把所有东西都放在了 cuda 上,但不知道为什么会出现以下运行时错误。这是我的代码:
for epoch in tqdm(range(1, epochs+1)):
model.train()
loss_train_total = 0
progress_bar = tqdm(dataloader_train, desc='Epoch {:1d}'.format(epoch), leave=False, disable=False)
for batch in progress_bar:
model.zero_grad()
batch = tuple(b.to(device) for b in batch)
inputs = {'input_ids': batch[0],
'attention_mask': batch[1],
'labels': batch[2],
}
outputs = model(**inputs)
loss = outputs[0]
loss_train_total += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
progress_bar.set_postfix({'training_loss': '{:.3f}'.format(loss.item()/len(batch))})
torch.save(model.state_dict(), f'finetuned_BERT_epoch_{epoch}.model')
tqdm.write(f'\nEpoch {epoch}')
loss_train_avg = loss_train_total/len(dataloader_train)
tqdm.write(f'Training loss: {loss_train_avg}')
val_loss, predictions, true_vals = evaluate(dataloader_validation)
val_f1 = f1_score_func(predictions, true_vals)
tqdm.write(f'Validation …Run Code Online (Sandbox Code Playgroud) 我有以下文本,我想从字符串向量中提取特定单词后面的 5 个单词:
\nmy_text <- "The World Cup 2022 winners, Argentina, have failed to dislodge Brazil from the top of the Fifa men\xe2\x80\x99s world rankings as England remains fifth in the post-Qatar standings.\nHad Argentina won the final within 90 minutes, they would have taken the top spot from Brazil. In the last eight tournaments going back to USA 94, no team leading the rankings at kick-off has won the tournament, with only Brazil, the 1998 finalists, getting beyond the …Run Code Online (Sandbox Code Playgroud)