作者:黄体测字_335 | 来源:互联网 | 2023-10-11 19:35
所以我一直在尝试研究我在github上发现的一些bert示例,这是我第一次尝试使用bert并查看它是如何工作的。使用的呼吸即时消息如下:https://github.com
所以我一直在尝试研究我在 github 上发现的一些 bert 示例,这是我第一次尝试使用 bert 并查看它是如何工作的。使用的呼吸即时消息如下:https : //github.com/prateekjoshi565/Fine-Tuning-BERT/blob/master/Fine_Tuning_BERT_for_Spam_Classification.ipynb
我使用了不同的数据集,但是我遇到了问题 TypeError: linear(): argument 'input' (position 1) must be Tensor, not str" 老实说,我不知道我做错了什么。有没有人可以帮助我?
我一直在使用的代码如下:
# convert class weights to tensor
weights= torch.tensor(class_wts,dtype=torch.float)
weights = weights.to(device)
# loss function
cross_entropy = nn.NLLLoss(weight=weights)
# number of training epochs
epochs = 10
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 % 50 == 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()
# 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
def evaluate():
print("nEvaluating...")
# deactivate dropout layers
model.eval()
total_loss, total_accuracy = 0, 0
# empty list to save the model predictions
total_preds = []
# iterate over batches
for step,batch in enumerate(val_dataloader): # Progress update every 50 batches.
if step % 50 == 0 and not step == 0:
# Calculate elapsed time in minutes.
elapsed = format_time(time.time() - t0)
# Report progress.
print(' Batch {:>5,} of {:>5,}.'.format(step, len(val_dataloader)))
# push the batch to gpu
batch = [t.to(device) for t in batch]
sent_id, mask, labels = batch
# deactivate autograd
with torch.no_grad():
# model predictions
preds = model(sent_id, mask)
# compute the validation loss between actual and predicted values
loss = cross_entropy(preds,labels)
total_loss = total_loss + loss.item()
preds = preds.detach().cpu().numpy()
total_preds.append(preds)
# compute the validation loss of the epoch
avg_loss = total_loss / len(val_dataloader)
# reshape the predictions in form of (number of samples, no. of classes)
total_preds = np.concatenate(total_preds, axis=0)
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 = 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
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
12
13 #train model
---> 14 train_loss, _ = train()
15
16 #evaluate model
5 frames
in train()
24
25 # get model predictions for the current batch
---> 26 preds = model(sent_id, mask)
27
28 # compute the loss between actual and predicted values
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
in forward(self, sent_id, mask)
28 _, cls_hs = self.bert(sent_id, attention_mask=mask)
29
---> 30 x = self.fc1(cls_hs)
31
32 x = self.relu(x)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)
92
93 def forward(self, input: Tensor) -> Tensor:
---> 94 return F.linear(input, self.weight, self.bias)
95
96 def extra_repr(self) -> str:
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1751 if has_torch_function_variadic(input, weight):
1752 return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1753 return torch._C._nn.linear(input, weight, bias)
1754
1755
TypeError: linear(): argument 'input' (position 1) must be Tensor, not str
回答
我也一直在研究这个 repo。受到此链接上提供的答案的启发。有一个可能名为 Bert_Arch 的类继承了 nn.Module,这个类有一个名为 forward 的重写方法。在 forward 方法中,只需将参数 'return_dict=False' 添加到 self.bert() 方法调用中。像这样:
_, cls_hs = self.bert(sent_id, attention_mask=mask, return_dict=False)
这对我有用。