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!

输出:

相关推荐
学好statistics和DS9 分钟前
【CV】神经网络中哪些参数需要被学习?
人工智能·神经网络·学习
大千AI助手10 分钟前
机器学习特征筛选中的IV值详解:原理、应用与实现
人工智能·机器学习·kl散度·roc·iv·信息值·woe
姜—姜12 分钟前
通过构建神经网络实现项目预测
人工智能·pytorch·深度学习·神经网络
Ro Jace15 分钟前
模式识别与机器学习课程笔记(4):线性判决函数
人工智能·笔记·机器学习
科研小白_1 小时前
基于遗传算法优化BP神经网络(GA-BP)的数据时序预测
人工智能·算法·回归
互联网江湖2 小时前
蓝桥杯出局,少儿编程的价值祛魅时刻?
人工智能·生活
Elastic 中国社区官方博客2 小时前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
paid槮3 小时前
OpenCV图像形态学详解
人工智能·opencv·计算机视觉
点控云4 小时前
点控云智能短信:重构企业与用户的连接,让品牌沟通更高效
大数据·人工智能·科技·重构·外呼系统·呼叫中心
救救孩子把7 小时前
14-机器学习与大模型开发数学教程-第1章 1-6 费马定理与极值判定
人工智能·数学·机器学习