I was trying to run some epochs to train my sentiment analysis model, at the very last passage, the epochs stopped with the error in the title. I attach the codes here:
Sentiment classifier:
# Build the Sentiment Classifier class
class SentimentClassifier(nn.Module):
# Constructor class
def __init__(self, n_classes):
super(SentimentClassifier, self).__init__()
self.bert = BertModel.from_pretrained(MODEL_NAME)
self.drop = nn.Dropout(p=0.3)
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
# Forward propagaion class
def forward(self, input_ids, attention_mask):
_, pooled_output = self.bert(
input_ids=input_ids,
attention_mask=attention_mask
)
# Add a dropout layer
output = self.drop(bertOutput['pooler_output'])
return self.out(output)
Iterations:
# Number of iterations
EPOCHS = 10
# Optimizer Adam
optimizer = AdamW(model.parameters(), lr=2e-5, correct_bias=False)
total_steps = len(train_data_loader) * EPOCHS
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=0,
num_training_steps=total_steps
)
# Set the loss function
loss_fn = nn.CrossEntropyLoss().to(device)
evaluation (where the issue raises):
def eval_model(model, data_loader, loss_fn, device, n_examples):
model = model.eval()
losses = []
correct_predictions = 0
with torch.no_grad():
for d in data_loader:
input_ids = d["input_ids"].to(device)
attention_mask = d["attention_mask"].to(device)
targets = d["targets"].to(device)
# Get model ouptuts
outputs = model(
input_ids=input_ids,
attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
loss = loss_fn(outputs, targets)
correct_predictions += torch.sum(preds == targets)
losses.append(loss.item())
return correct_predictions.double() / n_examples, np.mean(losses)
and finally, the error itself:
%%time
history = defaultdict(list)
best_accuracy = 0
for epoch in range(EPOCHS):
# Show details
print(f"Epoch {epoch + 1}/{EPOCHS}")
print("-" * 10)
train_acc, train_loss = train_epoch(
model,
train_data_loader,
loss_fn,
optimizer,
device,
scheduler,
len(df_train)
)
print(f"Train loss {train_loss} accuracy {train_acc}")
# Get model performance (accuracy and loss)
val_acc, val_loss = eval_model(
model,
val_data_loader,
loss_fn,
device,
len(df_val)
)
print(f"Val loss {val_loss} accuracy {val_acc}")
print()
history['train_acc'].append(train_acc)
history['train_loss'].append(train_loss)
history['val_acc'].append(val_acc)
history['val_loss'].append(val_loss)
# If we beat prev performance
if val_acc > best_accuracy:
torch.save(model.state_dict(), 'best_model_state.bin')
best_accuracy = val_acc
Epoch 1/10
----------
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<timed exec> in <module>
<ipython-input-95-6ade5aeecbf2> in train_epoch(model, data_loader, loss_fn, optimizer, device, scheduler, n_examples)
10 targets = d["targets"].to(device)
11
---> 12 outputs = model(
13 input_ids=input_ids,
14 attention_mask=attention_mask
6 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in dropout(input, p, training, inplace)
1423 raise ValueError(f"dropout probability has to be between 0 and 1, but got {p}")
1424 return (
-> 1425 _VF.dropout_(input, p, training) if inplace else _VF.dropout(input, p, training)
1426 )
1427
TypeError: dropout(): argument 'input' (position 1) must be Tensor, not str
I treid referting transformers with
pip install transformers==3
but nothing.
Any help is appreciated!
New contributor
Laura Valentini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.