transformers bert-base-uncased情感分析

一、使用huggingface中的预训练模型,先要安装transformers、torch和SentencePiece

bash 复制代码
pip install transformers
pip install torch
pip install SentencePiece

手动下载:https://huggingface.co/google-bert/bert-base-uncased/tree/main

添加以目录:

二、运行代码

bash 复制代码
from transformers import BertTokenizer, BertForSequenceClassification, pipeline

# 加载预训练的模型和分词器
model_name = 'bert-base-uncased'
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)  # 假设是二分类问题

# 使用pipeline简化流程
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# 文本分类
text = "I hate this movie!"
result = classifier(text)
print(result)

输入:I hate unnecessary waste

输出结果:

输入:I love dance!

输出:

相关推荐
池央10 分钟前
调用蓝耘API打造AI 智能客服系统实践教程
人工智能
西猫雷婶15 分钟前
深度学习|pytorch基本运算-hadamard积、点积和矩阵乘法
pytorch·深度学习·矩阵
TomatoSCI18 分钟前
TomatoSCI数据分析实战:探索社交媒体成瘾
人工智能·机器学习
stay night482 小时前
DAY40 训练和测试
人工智能·深度学习
SSH_55233 小时前
【大模型】情绪对话模型项目研发
人工智能·python·语言模型
love530love3 小时前
【笔记】在 MSYS2(MINGW64)中安装 python-maturin 的记录
运维·开发语言·人工智能·windows·笔记·python
清醒的兰3 小时前
OpenCV 图像像素的算术操作
人工智能·opencv·计算机视觉
拾忆-eleven4 小时前
NLP学习路线图(十四):词袋模型(Bag of Words)
人工智能·学习·自然语言处理·nlp
sbc-study5 小时前
精英-探索双群协同优化(Elite-Exploration Dual Swarm Cooperative Optimization, EEDSCO)
人工智能
白熊1885 小时前
【机器学习基础】机器学习入门核心算法:XGBoost 和 LightGBM
人工智能·算法·机器学习