小编Roy*_*Roy的帖子

PyTorch:运行时错误:输入、输出和索引必须在当前设备上

我正在火炬上运行 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)

python nlp pytorch bert-language-model

13
推荐指数
1
解决办法
2万
查看次数

如何提取特定单词后有字数限制的短语?

我有以下文本,我想从字符串向量中提取特定单词后面的 5 个单词:

\n
my_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)

regex string r

6
推荐指数
2
解决办法
115
查看次数

标签 统计

bert-language-model ×1

nlp ×1

python ×1

pytorch ×1

r ×1

regex ×1

string ×1