from openmind import AutoTokenizer, AutoModel, is_torch_npu_available,pipeline
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F
# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="../",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
# 我们想要获取句子嵌入的句子
sentences = ['This is an example sentence', 'Each sentence is converted']
classifier = pipeline("text-classification", model="../")
result = classifier("The product didn't arrive on time and was damaged.")
print(result)
if __name__ == "__main__":
main()该模型在McAuley-Lab/Amazon-Reviews-2023数据集上进行训练。此数据集包含来自亚马逊的带标签客户评论,主要分为两类:正面(Positive)和负面(Negative)。
模型使用亚马逊评论数据集的一个子集进行评估,主要任务是将文本进行二元分类,即分为正面或负面。
准确率(Accuracy):0.98
精确率(Precision):0.98
召回率(Recall):0.99
F1分数(F1-Score):0.98
from transformers import pipeline
classifier = pipeline("text-classification", model="dnzblgn/Sentiment-Analysis-Customer-Reviews")
result = classifier("The product didn't arrive on time and was damaged.")
print(result)