tensorflow - 一周前代码运行良好,但从昨天开始一直出错:通过 Colab 上的 PyTorch 微调 Bert 模型训练
问题描述
我是伯特的新手。两周前,我成功地在 nlp 分类任务上运行了一个微调的 Bert 模型,尽管结果并不出色。然而,昨天,当我尝试运行相同的代码和数据时,总是出现 AttributeError,它说:“str”对象没有属性“dim”。请知道一切都在 Colab 和 PyTorch Transformers 上。我应该怎么做才能修复它?
这是我在安装变压器时尝试过的一件事,但结果它不起作用:我尝试使用以前的变压器版本而不是 !pip install transformers : !pip install --target lib --upgrade transformers==3.5.0
任何反馈将不胜感激!
请参阅以下代码和错误消息:
代码:
- 火车定义
# 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
- 培训过程
# 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}')
- 错误信息:
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 中有一个旧的变压器版本。2.11.0 之类的东西。尝试:
!pip install transformers~=2.11.0
更改版本号,直到它工作。
推荐阅读
- windows - Powershell Get-HotFix 查找文本文件中提供的更新
- linux - 最大驻留集大小是什么意思?
- python - Selenium 与 Python 随机超时没有错误消息
- c++ - 绕道从内核挂钩用户模式功能
- c - 即使在不必要的情况下,是否有客观的理由反对使用大括号?
- java - 在Java中查找两个字符串之间的多个字符串
- regex - 匹配脚本标记之外的所有出现
- javascript - 如何从字符串中删除除特殊类之外的所有 HTML 元素?
- dictionary - Clojure 嵌套映射。从值访问父键
- arrays - 参数类型“Spot.Restriction.Type”不符合预期类型“_FormatSpecifiable”