代码在一周前运行良好,但从昨天开始不断出错:通过PyTorch在Colab上微调Bert模型训练



我是伯特的新手。两周前,我成功地在nlp分类任务中运行了一个微调Bert模型,尽管结果并不理想。然而,昨天,当我尝试运行相同的代码和数据时,AttributeError总是存在,它说:"str"对象没有属性"dim"。请知道一切都在Colab和PyTorch变形金刚上。我该怎么修?

以下是我安装变压器时尝试过的一件事,但结果不起作用:而不是!pip安装变压器,我尝试使用以前的变形金刚版本:!pip-install--target-lib-升级转换器==3.5.0

如有任何反馈,我们将不胜感激!

请查看以下代码和错误消息:

代码:

  1. 列车定义
# function to train the model
def train():

model.train()
total_loss, total_accuracy = 0, 0

# empty list to save model predictions
total_preds=[]

# iterate over batches
for step,batch in enumerate(train_dataloader):

# progress update after every 50 batches.
if step % 200 == 0 and not step == 0:
print('  Batch {:>5,}  of  {:>5,}.'.format(step, len(train_dataloader)))
# push the batch to gpu
batch = [r.to(device) for r in batch]

sent_id, mask, labels = batch
# clear previously calculated gradients 
model.zero_grad()        
# get model predictions for the current batch
preds = model(sent_id, mask)
# compute the loss between actual and predicted values
loss = cross_entropy(preds, labels)
# add on to the total loss
total_loss = total_loss + loss.item()
# backward pass to calculate the gradients
loss.backward()
# clip the the gradients to 1.0. It helps in preventing the exploding gradient problem
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
# update parameters
optimizer.step()
# update learning rate schedule
# scheduler.step()  
# model predictions are stored on GPU. So, push it to CPU
preds=preds.detach().cpu().numpy()
# append the model predictions
total_preds.append(preds)
# compute the training loss of the epoch
avg_loss = total_loss / len(train_dataloader)

# predictions are in the form of (no. of batches, size of batch, no. of classes).
# reshape the predictions in form of (number of samples, no. of classes)
total_preds  = np.concatenate(total_preds, axis=0)
#returns the loss and predictions
return avg_loss, total_preds
  1. 培训过程
# set initial loss to infinite
best_valid_loss = float('inf')
# empty lists to store training and validation loss of each epoch
train_losses=[]
valid_losses=[]
#for each epoch
for epoch in range(epochs):

print('n Epoch {:} / {:}'.format(epoch + 1, epochs))

#train model
train_loss, _ = train()

#evaluate model
valid_loss, _ = evaluate()

#save the best model
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), 'saved_weights.pt')

# append training and validation loss
train_losses.append(train_loss)
valid_losses.append(valid_loss)

print(f'nTraining Loss: {train_loss:.3f}')
print(f'Validation Loss: {valid_loss:.3f}')
  1. 错误消息:
Epoch 1 / 10
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-c5138ddf6b25> in <module>()
12 
13     #train model
---> 14     train_loss, _ = train()
15 
16     #evaluate model
5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1686         if any([type(t) is not Tensor for t in tens_ops]) and has_torch_function(tens_ops):
1687             return handle_torch_function(linear, tens_ops, input, weight, bias=bias)
-> 1688     if input.dim() == 2 and bias is not None:
1689         # fused op is marginally faster
1690         ret = torch.addmm(bias, input, weight.t())
AttributeError: 'str' object has no attribute 'dim'

据我记忆所及,colab中有一个旧的transformer版本。大概是2.11.0。尝试:

!pip install transformers~=2.11.0

更改版本号,直到它起作用。

最新更新