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!

输出:

相关推荐
vibag1 分钟前
LangSmith监控
人工智能·python·语言模型·langchain·大模型
严文文-Chris5 分钟前
向量内积是什么?解决什么问题?
人工智能
亦复何言??9 分钟前
过拟合/灾难性遗忘:SFT vs on-policy RL
人工智能
飞哥数智坊10 分钟前
别让 AI 成了你的“后门”:一个被忽视的安全盲区
人工智能
六边形战士DONK21 分钟前
[强化学习基础05-2] 压缩映射定理
人工智能
weisian15127 分钟前
入门篇--知名企业-18-阿里巴巴-6--DashScope(灵积):从新手入门到企业落地,推开AI普惠之门,让智能触手可及
人工智能·阿里云·dashscope·灵积平台
2401_8414956432 分钟前
【DeepSeek系列】论文《mHC: Manifold-Constrained Hyper-Connections》全流程复现详解(附Python代码)
人工智能·pytorch·python·深度学习·论文复现·deepseek·mhc模型
万俟淋曦36 分钟前
【论文速递】2025年第47周(Nov-16-22)(Robotics/Embodied AI/LLM)
人工智能·机器学习·机器人·大模型·论文·robotics·具身智能
风途知识百科38 分钟前
太阳能杀虫灯——风吸式物联网杀虫灯
大数据·人工智能·物联网
helloworld也报错?42 分钟前
深度强化学习(1)——基础知识(名词解释,概率论基础,蒙特卡洛采样,马尔可夫决策过程)
人工智能·深度学习·机器学习·概率论