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!

输出:

相关推荐
学究天人1 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(补充卷9)
人工智能·线性代数·算法·数学建模·动态规划·原型模式·抽象代数
ZeekerLin1 小时前
AI 原生开发落地路线图:从个人提效到团队体系化
大数据·人工智能
代码青铜1 小时前
从黑盒到“上帝视角”:深度拆解【数据鸟瞰图】功能
人工智能
俊哥V1 小时前
每日 AI 研究简报 · 2026-07-11
人工智能·ai
武子康1 小时前
GPT-Live 全双工语音 Agent 深度拆解:连续交互 + 后台委托 + Voice Runtime 三层架构
人工智能·ai·chatgpt·架构·llm·交互
阿黎梨梨1 小时前
听说大模型总“胡言乱语”?用 RAG 堵住它的嘴!
人工智能
刘小八1 小时前
LangGraph 人机交互实战:Interrupt、人工审批与工作流恢复
人工智能·python·人机交互
cocoafei1 小时前
GPT-5.6 后,别再混淆 ChatGPT 和 Codex 额度了
前端·人工智能·chatgpt
范闲1 小时前
第二章:Octo v0.1.1:从「能用」到「好用」——终端 AI 聊天体验的全面升级
人工智能·后端
念雨思1 小时前
每日穿搭助手:鸿蒙AI应用开发实战——AI衣橱,每日穿搭不再愁
人工智能·华为·powerpoint·harmonyos·鸿蒙