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 分钟前
数据结构排序系列指南:从O(n²)到O(n),计数排序如何实现线性时间复杂度
运维·数据结构·c++·人工智能·后端·深度学习·排序算法
2301_8129148724 分钟前
简单神经网络
人工智能·深度学习·神经网络
koo3641 小时前
pytorch环境配置
人工智能·pytorch·python
模型启动机5 小时前
黄仁勋GTC开场:「AI-XR Scientist」来了!
人工智能·ai·大模型
k***1955 小时前
自动驾驶---E2E架构演进
人工智能·架构·自动驾驶
Techblog of HaoWANG6 小时前
目标检测与跟踪 (4)- 基于YOLOv8的工业仪器仪表智能读数与状态检测算法实
人工智能·视觉检测·智能制造·yolov8·工业检测·指针式仪表·仪器仪表检测
1***Q7846 小时前
深度学习技术
人工智能·深度学习
KKKlucifer6 小时前
2025 国产化数据分类分级工具实测:国产化适配、多模态识别与动态分级能力深度解析
人工智能·分类·数据挖掘
虹科网络安全7 小时前
从AI模型到云生态:构建系统化的企业AI安全管理体系【系列文章(3)】
人工智能·安全
互联网江湖7 小时前
这个Q3,百度开始AI
人工智能·百度