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!

输出:

相关推荐
LDG_AGI1 小时前
【推荐系统】深度学习训练框架(八):PyTorch分布式采样器DistributedSampler原理详解
人工智能·pytorch·分布式·深度学习·算法·机器学习·推荐算法
智能化咨询1 小时前
(66页PPT)某著名企业XX集团数据分析平台建设项目方案设计(附下载方式)
大数据·人工智能·数据分析
serve the people3 小时前
TensorFlow 图执行(tf.function)的 “非严格执行(Non-strict Execution)” 特性
人工智能·python·tensorflow
泰迪智能科技3 小时前
图书推荐分享 | 堪称教材天花板,深度学习教材-TensorFlow 2 深度学习实战(第2版)(微课版)
人工智能·深度学习·tensorflow
吴佳浩5 小时前
LangChain 深入
人工智能·python·langchain
LplLpl118 小时前
AI 算法竞赛通关指南:基于深度学习的图像分类模型优化实战
大数据·人工智能·机器学习
依米s8 小时前
各年度人工智能大会WAIC核心议题(持续更新)
人工智能·人工智能+·waic·人工智能大会+
小徐xxx8 小时前
Mamba架构讲解
深度学习·mamba·学习记录
python机器学习建模8 小时前
22篇经典金融风控论文复现(2025年11月更新)
人工智能·机器学习·论文·期刊·金融风控
Codebee9 小时前
深度解析AI编程技术:从原理到实践,手把手教你落地
人工智能·设计模式·开源