【文本分类】bert二分类
import os
import torch
from torch.utils.data import DataLoader, Dataset
from transformers import BertTokenizer, BertForSequenceClassification, AdamW
from sklearn.metrics import accuracy_score, classification_report
from tqdm import tqdm# 自定义数据集
class CustomDataset(Dataset):def __init__(self, texts, labels, tokenizer, max_length=128):self.texts = textsself.labels = labelsself.tokenizer = tokenizerself.max_length = max_lengthdef __len__(self):return len(self.texts)def __getitem__(self, idx):text = self.texts[idx]label = self.labels[idx]encoding = self.tokenizer(text,max_length=self.max_length,padding="max_length",truncation=True,return_tensors="pt")return {"input_ids": encoding["input_ids"].squeeze(0),"attention_mask": encoding["attention_mask"].squeeze(0),"label": torch.tensor(label, dtype=torch.long)}# 训练函数
def train_model(model, train_loader, optimizer, device, num_epochs=3):model.train()for epoch in range(num_epochs):total_loss = 0for batch in tqdm(train_loader, desc=f"Training Epoch {epoch + 1}/{num_epochs}"):input_ids = batch["input_ids"].to(device)attention_mask = batch["attention_mask"].to(device)labels = batch["label"].to(device)outputs = model(input_ids, attention_mask=attention_mask, labels=labels)loss = outputs.losstotal_loss += loss.item()optimizer.zero_grad()loss.backward()optimizer.step()print(f"Epoch {epoch + 1} Loss: {total_loss / len(train_loader)}")# 评估函数
def evaluate_model(model, val_loader, device):model.eval()predictions, true_labels = [], []with torch.no_grad():for batch in val_loader:input_ids = batch["input_ids"].to(device)attention_mask = batch["attention_mask"].to(device)labels = batch["label"].to(device)outputs = model(input_ids, attention_mask=attention_mask)logits = outputs.logitspreds = torch.argmax(logits, dim=1).cpu().numpy()predictions.extend(preds)true_labels.extend(labels.cpu().numpy())accuracy = accuracy_score(true_labels, predictions)report = classification_report(true_labels, predictions)print(f"Validation Accuracy: {accuracy}")print("Classification Report:")print(report)# 模型保存函数
def save_model(model, tokenizer, output_dir):os.makedirs(output_dir, exist_ok=True)model.save_pretrained(output_dir)tokenizer.save_pretrained(output_dir)print(f"Model saved to {output_dir}")# 模型加载函数
def load_model(output_dir, device):tokenizer = BertTokenizer.from_pretrained(output_dir)model = BertForSequenceClassification.from_pretrained(output_dir)model.to(device)print(f"Model loaded from {output_dir}")return model, tokenizer# 推理预测函数
def predict(texts, model, tokenizer, device, max_length=128):model.eval()encodings = tokenizer(texts,max_length=max_length,padding="max_length",truncation=True,return_tensors="pt")input_ids = encodings["input_ids"].to(device)attention_mask = encodings["attention_mask"].to(device)with torch.no_grad():outputs = model(input_ids, attention_mask=attention_mask)logits = outputs.logitsprobabilities = torch.softmax(logits, dim=1).cpu().numpy()predictions = torch.argmax(logits, dim=1).cpu().numpy()return predictions, probabilities# 主函数
def main():# 配置参数config = {"train_batch_size": 16,"val_batch_size": 16,"learning_rate": 5e-5,"num_epochs": 5,"max_length": 128,"device_id": 7, # 指定 GPU ID"model_dir": "model","local_model_path": "roberta_tiny_model", # 指定本地模型路径,如果为 None 则使用预训练模型"pretrained_model_name": "uer/chinese_roberta_L-12_H-128", # 预训练模型名称}# 设置设备device = torch.device(f"cuda:{config['device_id']}" if torch.cuda.is_available() else "cpu")print(f"Using device: {device}")# 加载分词器和模型tokenizer = BertTokenizer.from_pretrained(config["local_model_path"])model = BertForSequenceClassification.from_pretrained(config["local_model_path"], num_labels=2)model.to(device)# 示例数据train_texts = ["This is a great product!", "I hate this service."]train_labels = [1, 0]val_texts = ["Awesome experience.", "Terrible product."]val_labels = [1, 0]# 创建数据集和数据加载器train_dataset = CustomDataset(train_texts, train_labels, tokenizer, config["max_length"])val_dataset = CustomDataset(val_texts, val_labels, tokenizer, config["max_length"])train_loader = DataLoader(train_dataset, batch_size=config["train_batch_size"], shuffle=True)val_loader = DataLoader(val_dataset, batch_size=config["val_batch_size"])# 定义优化器optimizer = AdamW(model.parameters(), lr=config["learning_rate"])# 训练模型train_model(model, train_loader, optimizer, device, num_epochs=config["num_epochs"])# 评估模型evaluate_model(model, val_loader, device)# 保存模型save_model(model, tokenizer, config["model_dir"])# 加载模型loaded_model, loaded_tokenizer = load_model(config["model_dir"], "cpu")# 推理预测new_texts = ["I love this!", "It's the worst."]predictions, probabilities = predict(new_texts, loaded_model, loaded_tokenizer, "cpu")for text, pred, prob in zip(new_texts, predictions, probabilities):print(f"Text: {text}")print(f"Predicted Label: {pred} (Probability: {prob})")if __name__ == "__main__":main()
相关文章:
【文本分类】bert二分类
import os import torch from torch.utils.data import DataLoader, Dataset from transformers import BertTokenizer, BertForSequenceClassification, AdamW from sklearn.metrics import accuracy_score, classification_report from tqdm import tqdm# 自定义数据集 class…...
单例模式-如何保证全局唯一性?
以下是几种实现单例模式并保证全局唯一性的方法: 1. 饿汉式单例模式 class Singleton { private:// 私有构造函数,防止外部创建对象Singleton() {}// 静态成员变量,存储单例对象static Singleton instance; public:// 公有静态成员函数&…...
设计模式学习笔记——结构型模式
文章目录 适配器模式 Adapter适用场景UML 桥接模式 Bridge适用场景UML 组合模式 Composite装饰模式 Decorator外观模式 Facade享元模式 Flyweight代理模式 Proxy 适配器模式 Adapter 适用场景 希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用…...

WEB攻防-通用漏洞_文件上传_黑白盒审计流程
目录 前置知识点 Finecms-CMS文件上传 编辑 Cuppa-Cms文件上传 Metinfo-CMS 文件上传 前置知识点 思路: 黑盒就是寻找一切存在文件上传的功能应用 1 、个人用户中心是否存在文件上传功能 2 、后台管理系统是否存在文件上传功能 3 、字典目录扫描探针文件上传构…...

RabbitMQ基本介绍及简单上手
(一)什么是MQ MQ(message queue)本质上是队列,满足先入先出,只不过队列中存放的内容是消息而已,那什么是消息呢? 消息可以是字符串,json也可以是一些复杂对象 我们应用场…...
服务器证书不受信任是什么问题?
用户在访问某些网站时,可能会遇到“服务器证书不受信任”的警告。这一问题不仅影响用户的浏览体验,更可能对网站的信誉和安全性产生深远影响。那么服务器证书不受信任是什么问题呢? 服务器证书的基本概念 服务器证书是由证书颁发机构(CA)签…...
spring mvc源码学习笔记之十
前面的文章介绍了用 WebApplicationInitializer 或者 AbstractAnnotationConfigDispatcherServletInitializer 来代替 web.xml 。 我们学 java web 的时候就知道,servlet 容器会自动加载 web.xml。 那么,疑问就来了,WebApplicationInitialize…...

Ubuntu 下载安装 elasticsearch7.17.9
参考 https://blog.csdn.net/qq_26039331/article/details/115024218 https://blog.csdn.net/mengo1234/article/details/104989382 过程 来到 Es 的版本发布列表页面:https://www.elastic.co/downloads/past-releases#elasticsearch 根据自己的系统以及要安装的…...
Qt笔记:网络编程Tcp
一、铺垫 1.以下只是告诉诸位怎样去构建服务器与客户端;客户端这样构建肯定没问题;但是服务端不可能这样写,因为他是布置在Linux上的,纯数据类处理服务器,根本不可能用Qt写;这在Qt的http类中就表明了&…...
C++单例模式跨DLL调用问题梳理
问题案例: 假设有这样一个单例模式的代码 //test.h header class Test { public:static Test &instance() {static Test ins;return ins;}void foo(); };void testFoo();//test.cpp source #include "test.h"void Test::foo() {printf("%p\n&q…...

oracle闪回版本查询
闪回版本查询(Flashback Versions Query)是Oracle数据库提供的一种功能,允许用户查看某个表在特定时间范围内的所有版本。这对于审计和调试数据修改问题非常有用。通过闪回版本查询,你可以了解表中的数据在某个时间段内的变化历史…...

C#用winform窗口程序操作服务+不显示Form窗体,只显示右下角托盘图标+开机时自启动程序【附带项目地址】
服务的文章在:https://blog.csdn.net/weixin_43768573/article/details/144957941 一、用winform窗口程序操作服务 1、点击“创建新项目”,选择“Windows 服务(.NET Framework)” 2、给项目命名 3、右击项目->添加->新建项,选择“应用程序清单文件(仅限Windo…...

UOS系统和windows系统wps文档显示差异问题解决
最近在使用UOS系统的过程中,发现了一个很有意思的现象。就是在UOS系统上编辑的文档,发到windows系统上,会出现两个文档显示差异很大的情况,文档都是使用一样的wps软件打开的。到底是什么原因导致这种现象的呢?该如何解…...

JS中函数基础知识之查漏补缺(写给小白的学习笔记)
函数 函数是ECMAScript中 最有意思的部分之一, 主要是因为函数实际上是对象.-- 每个函数 都是Function类型的实例,Function也有属性和方法. 因为函数是对象,所以函数名就是指向函数对象的指针. 常用的定义函数的语法: ①函数声明 ②函数表达式 ③箭头函数 function sum (n…...
蓝桥杯训练
1对于一个字母矩阵,我们称矩阵中的一个递增序列是指在矩阵中找到两个字母,它们在同一行,同一列,或者在同一 45 度的斜线上,这两个字母从左向右看、或者从上向下看是递增的。 例如,如下矩阵中 LANN QIAO有…...

前端学习DAY33(外边距的折叠)
垂直外边距的重叠 在网页中相邻的垂直方向的外边距,会发生外边距的重叠 兄弟元素 兄弟元素之间的相邻外边距会取(绝对值)最大值,而不是取和,谁大取谁 特殊情况:如果相邻的外边距一正一负,则取两…...
asp.net core mvc的 ViewBag , ViewData , Module ,TempData
在 ASP.NET MVC 和 ASP.NET Core MVC 中,ViewBag 和 ViewData 是两种用于将数据从控制器传递到视图(View)的常用方法。它们都允许控制器将动态数据传递给视图,但它们的实现方式有所不同。关于 Module,它通常指的是某种…...

Linux驱动学习之第二个驱动程序(LED点亮关闭驱动程序-分层设计思想,使其能适应不同的板子-驱动程序模块为多个源文件怎么写Makefile)
目录 看这篇博文前请先掌握下面这些博文中的知识需要的PDF资料完整源代码board_fire_imx6ull-pro.c中的代码leddrv.c中的代码ledtest.c中的代码 程序设计思想和文件结构实现分层思想的具体方法概述具体实现分析定义结构体led_operations用来集合各个单板硬件层面操作LED的函数定…...
手写@EnableTransactionalManagement
定义一个注解,用于标注于方法上,标志着此方法是一个事务方法。 Target({ElementType.METHOD,ElementType.TYPE}) Retention(RetentionPolicy.RUNTIME) public interface MyTransaction {}定义一个开启事务功能的注解 Component Import(TransActionBean…...
【Vue】:解决动态更新 <video> 标签 src 属性后视频未刷新的问题
问题描述 在 Vue.js 项目,当尝试动态更新 <video> 标签的 <source> 元素 src 属性来切换视频时,遇到了一个问题:即使 src 属性已更改,浏览器仍显示旧视频。具体表现为用户选择新视频后,视频区域继续显示之…...

12-Oracle 23ai Vector 使用ONNX模型生成向量嵌入
一、Oracle 23ai Vector Embeddings 核心概念 向量嵌入(Vector Embeddings) -- 将非结构化数据(文本/图像)转换为数值向量 - - 捕获数据的语义含义而非原始内容 - 示例:"数据库" → [0.24, -0.78, 0.5…...

【LLM-Agent】智能体的记忆缓存设计
note 实践:https://modelscope-agent.readthedocs.io/zh-cn/latest/modules/memory.html 文章目录 note一、Agent的记忆实现二、相关综述三、记忆体的构建四、cursor的记忆设计1. 记忆生成提示词2. 记忆评估提示词 五、记忆相关的MCPReference 一、Agent的记忆实现…...
概述侧边导航的作用与价值
侧边导航的作用与价值:介绍侧边导航的核心优势和用户体验提升点。设计原则:使用表格对比说明侧边导航的三大设计准则。基础实现方法:分步骤讲解静态侧边导航的实现技术。高级交互实现:提供滑动式侧边栏的完整交互解决方案。优化技…...

【Linux操作系统】基础开发工具(yum、vim、gcc/g++)
文章目录 Linux软件包管理器 - yumLinux下的三种安装方式什么是软件包认识Yum与RPMyum常用指令更新软件安装与卸载查找与搜索清理缓存与重建元数据 yum源更新1. 备份现有的 yum 源配置2. 下载新的 repo 文件3. 清理并重建缓存 Linux编辑器 - vim启动vimVim 的三种主要模式常用操…...

vue生成二维码图片+文字说明
需求:点击下载图片,上方是二维码,下方显示该二维码的相关内容,并且居中显示,支持换行 解决方案步骤: 1. 使用qrcode生成二维码的DataURL。 2. 创建canvas,将二维码图片绘制到canvas的上半部分…...

hadoop集群datanode启动显示init failed,不能解析hostname
三个datanode集群,有一个总是起不起来。去查看log显示 Initialization failed for Block pool BP-1920852191-192.168.115.154-1749093939738 (Datanode Uuid 89d9df36-1c01-4f22-9905-517fee205a8e) service to node154/192.168.115.154:8020 Datanode denied com…...

GIC700概述
GIC-700是用于处理外设与处理器核之间,以及核与核之间中断的通用中断控制器。GIC-700支持分布式微体系结构,其中包含用于提供灵活GIC实现的几个独立块。 GIC700支持GICv3、GICv3.1、GICv4.1架构。 该微体系结构规模可从单核到互联多chip环境࿰…...

靶场(二十)---靶场体会小白心得 ---jacko
老样子开局先看端口,先看http端口 PORT STATE SERVICE VERSION 80/tcp open http Microsoft IIS httpd 10.0 |_http-title: H2 Database Engine (redirect) | http-methods: |_ Potentially risky methods: TRACE |_http-server-header:…...

如何把 Mac Finder 用得更顺手?——高效文件管理定制指南
系统梳理提升 Mac Finder 体验的实用设置与技巧,助你用更高效的方式管理文件。文末引出进阶选择 Path Finder。 阅读原文请转到:https://jimmysong.io/blog/customize-finder-for-efficiency/ 作为一个用 Mac 多年的用户,我始终觉得 Finder 虽…...
如何写高效的Prompt?
概述 提示词(Prompt)的质量将直接影响模型生成结果的质量,所以精心设计一个让大模型能够理解并有效回复的提示词是至关重要的。本文内容自论文中获取:https://arxiv.org/pdf/2312.16171 介绍了5类共计26条提示词书写原则。 书写原则 类别原则备注快速…...