我用正面、负面和中性的类别来预测推文的情绪分析。我用拥抱脸训练了一个BERT模型。现在,我想要对未标记的Twitter文本的数据帧进行预测,但我遇到了困难。
我遵循了以下教程(https://curiousily.com/posts/sentiment-analysis-with-bert-and-hugging-face-using-pytorch-and-python/),并能够使用Hugging训练BERT模型。
这是一个对原始文本进行预测的示例,但它只有一句话,我想使用一列Tweets。https://curiousily.com/posts/sentiment-analysis-with-bert-and-hugging-face-using-pytorch-and-python/#predicting-on-raw-text
review_text = "I love completing my todos! Best app ever!!!"
encoded_review = tokenizer.encode_plus(
review_text,
max_length=MAX_LEN,
add_special_tokens=True,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
input_ids = encoded_review['input_ids'].to(device)
attention_mask = encoded_review['attention_mask'].to(device)
output = model(input_ids, attention_mask)
_, prediction = torch.max(output, dim=1)
print(f'Review text: {review_text}')
print(f'Sentiment : {class_names[prediction]}')
Review text: I love completing my todos! Best app ever!!!
Sentiment : positive比尔的回应奏效了。这就是解决方案。
def predictionPipeline(text):
encoded_review = tokenizer.encode_plus(
text,
max_length=MAX_LEN,
add_special_tokens=True,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
input_ids = encoded_review['input_ids'].to(device)
attention_mask = encoded_review['attention_mask'].to(device)
output = model(input_ids, attention_mask)
_, prediction = torch.max(output, dim=1)
return(class_names[prediction])
df2['prediction']=df2['cleaned_tweet'].apply(predictionPipeline)发布于 2021-11-18 17:08:03
您可以使用相同的代码来预测来自dataframe列的文本。
model = ...
tokenizer = ...
def predict(review_text):
encoded_review = tokenizer.encode_plus(
review_text,
max_length=MAX_LEN,
add_special_tokens=True,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
input_ids = encoded_review['input_ids'].to(device)
attention_mask = encoded_review['attention_mask'].to(device)
output = model(input_ids, attention_mask)
_, prediction = torch.max(output, dim=1)
print(f'Review text: {review_text}')
print(f'Sentiment : {class_names[prediction]}')
return class_names[prediction]
df = pd.DataFrame({
'texts': ["text1", "text2", "...."]
})
df_dataset["sentiments"] = df.apply(lambda l: predict(l.texts), axis=1)https://stackoverflow.com/questions/69820318
复制相似问题