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!

输出:

相关推荐
文青的青23 分钟前
不写一行代码,用 WorkBuddy 搭建并上线个人静态博客
人工智能
IT_陈寒26 分钟前
Redis连接池泄漏那次,我差点把生产环境搞崩了
前端·人工智能·后端
Dawson Zhu26 分钟前
从空间 Scaling 到时间 Scaling
人工智能·语言模型·架构·aigc·agi
阿里云大数据AI技术1 小时前
本体如何落地?阿里云 Milvus 把企业文档变成知识图谱和 Wiki
人工智能
QXWZ_IA1 小时前
公安视频专网如何与定位数据融合?架构与实施要点
人工智能
小林ixn1 小时前
GraphRAG 实战:用 Neo4j 知识图谱,让 RAG 从“找货“进化到“梳理关系“
人工智能·知识图谱·neo4j
敖老师2 小时前
Dify 连局域网 Ollama 的两个坑:docker0 网段撞车,和请求里少掉的那个参数
人工智能
MocMoc2 小时前
从 0 到 1:把 Dify Chatflow 接进一个 qiankun 中后台
人工智能
民乐团扒谱机2 小时前
【微实验】NSGA-II(非支配排序遗传算法)——解决多目标优化问题的“自然选择模拟器”
linux·人工智能·算法