首页 > 其他分享 >数据不均衡处理五:深度学习

数据不均衡处理五:深度学习

时间:2024-03-20 19:59:00浏览次数:16  
标签:模型 学习 train 均衡 深度 test import model class

在处理数据不均衡问题时,迁移学习、自编码器和半监督学习是一些有效的方法。这些技术可以帮助模型更好地学习数据的分布特征,提高对少数类样本的识别能力,并改善整体的分类性能。

目录

一:迁移学习

二:自编码器

三:半监督学习


一:迁移学习

迁移学习通过利用源领域的知识来帮助目标领域的学习任务。在数据不均衡问题中,可以使用迁移学习来从源领域学习到的知识和特征,帮助改善目标领域的分类性能。可以通过迁移学习将在大量数据上训练的模型迁移到数据不均衡的任务上,从而提高模型对少数类样本的识别能力。

下面的代码加载了一个预训练的ResNet-50模型,并修改了最后一层以适应我们的分类任务。为了处理数据不均衡问题,使用了两种方法:
1、类别权重:使用sklearn.utils.class_weight.compute_class_weight计算每个类别的权重,并在损失函数中使用这些权重。
2、加权随机采样器:使用torch.utils.data.WeightedRandomSampler根据每个样本的类别权重进行采样。这样,少数类的样本在训练过程中被选中的概率会增加。

import torch  
import torch.nn as nn  
import torchvision.models as models  
import torchvision.transforms as transforms  
from torch.utils.data import Dataset, DataLoader, RandomSampler, WeightedRandomSampler  
from sklearn.utils import class_weight  

class Dataset(Dataset):  
    def __init__(self, X, y):  

        self.data = X
        self.label = y

    def __len__(self):  
        # 返回数据集的长度  
        return len(self.label)  
  
    def __getitem__(self, idx):  
        # 获取单个数据
        data = self.data[idx] 
        # data = data.reshape(data.shape[0],1,1) 
        label = self.label[idx]
        return data, label  # 返回数据和标签 

# 加载数据集  
X, y = torch.rand(1000, 3, 256, 256), torch.randint(0, 2, (1000,))
  
# 划分训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 

# 加载预训练模型  
model = models.resnet50(pretrained=True)  
  
# 修改模型的最后一层以适应你的分类任务(假设你有num_classes个类别) 
num_classes = len(np.unique(y_train))
num_ftrs = model.fc.in_features  
model.fc = nn.Linear(num_ftrs, num_classes)  
  
# 如果有GPU,将模型移到GPU上  
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")  
model = model.to(device)  
  
# 定义损失函数和优化器  
criterion = nn.CrossEntropyLoss()  
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)  

# 数据加载器 
train_dataset = Dataset(X_train, y_train)    
test_dataset = Dataset(X_test, y_test)    
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=False)  
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)  
  
# 计算类别权重(针对数据不均衡)  
class_weights = class_weight.compute_class_weight(class_weight = 'balanced',classes = np.unique(y_train), y = y_train.numpy())
class_weights = torch.tensor(class_weights, dtype=torch.float)  
  
# 采样器(可选,用于进一步处理数据不均衡)  
# 例如,使用加权随机采样器  
weights = [class_weights[t] for t in y_train]  
sampler = WeightedRandomSampler(weights, len(weights))  
train_loader = DataLoader(train_dataset, batch_size=32, sampler=sampler)  
  
# 训练模型  
num_epochs = 10  # 设置训练轮数  
for epoch in range(num_epochs):  
    model.train()  # 设置模型为训练模式  
    running_loss = 0.0  
    for inputs, labels in train_loader:  
        inputs, labels = inputs.to(device), labels.to(device)  
          
        # 梯度清零  
        optimizer.zero_grad()  
          
        # 前向传播  
        outputs = model(inputs)  
          
        # 计算损失  
        loss = criterion(outputs, labels)  
          
        # 反向传播和优化  
        loss.backward()  
        optimizer.step()  
          
        # 累计损失  
        running_loss += loss.item()  
      
    print(f'Epoch {epoch+1}/{num_epochs}, Loss: {running_loss/len(train_loader)}') 
二:自编码器

自编码器是一种无监督学习方法,通过学习数据的压缩表示来重构输入数据。自编码器(Autoencoder)本身并不是直接用于解决类别不均衡问题的工具。然而,我们可以结合自编码器进行特征学习,并将学到的特征用于后续的分类任务,以改善在不均衡数据集上的性能。通过自编码器来降维数据、去除噪声、提取关键特征,从而改善模型对数据不均衡问题的处理能力。

import numpy as np  
from sklearn.datasets import make_classification  
from sklearn.model_selection import train_test_split  
from sklearn.preprocessing import StandardScaler  
from sklearn.metrics import classification_report  
import numpy as np  
from keras.layers import Input, Dense  
from keras.models import Model  
from keras import regularizers  
from sklearn.metrics import roc_auc_score, auc, roc_curve
from sklearn.linear_model import LogisticRegression  
  
# 生成不均衡数据集  
X, y = make_classification(n_classes=2, class_sep=2,  
                             weights=[0.1, 0.9], n_informative=3,  
                             n_redundant=1, flip_y=0,  
                             n_features=20, n_clusters_per_class=1,  
                             n_samples=1000, random_state=10)  
  
# 划分训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  

# 定义编码器  
input_img = Input(shape=(X_train.shape[1],))  
encoded = Dense(64, activation='relu')(input_img)  
encoded = Dense(32, activation='relu')(encoded)  
encoded = Dense(16, activation='relu')(encoded)  
  
# 定义解码器  
decoded = Dense(32, activation='relu')(encoded)  
decoded = Dense(64, activation='relu')(decoded)  
decoded = Dense(20, activation='linear')(decoded)  
  
# 构建自编码器模型  
autoencoder = Model(input_img, decoded)  
  
# 构建编码器模型(用于提取特征)  
encoder = Model(input_img, encoded)  
  
# 编译自编码器  
autoencoder.compile(optimizer='adam', loss='mean_squared_error')  
  
# 训练自编码器  
autoencoder.fit(X_train, X_train,  
                epochs=50,  
                batch_size=256,  
                shuffle=True,  
                validation_data=(X_test, X_test)) 

# 使用编码器部分提取特征
X_train_encoded = encoder.predict(X_train)  
X_test_encoded = encoder.predict(X_test)

# 使用逻辑回归分类器进行训练  
clf = LogisticRegression(max_iter=10000)  # 增加最大迭代次数以避免警告  
clf.fit(X_train_encoded, y_train)  
  
# 在测试集上进行预测并评估模型性能  
y_pred = clf.predict(X_test_encoded)  
print(classification_report(y_test, y_pred))
三:半监督学习

半监督学习利用少量标记数据和大量未标记数据进行模型训练。在数据不均衡问题中,可以利用半监督学习来提高模型的泛化能力和对少数类样本的识别能力。可以通过半监督学习来利用未标记数据进行特征学习和模型训练,从而提高模型在数据不均衡问题上的性能。

import numpy as np  
from sklearn.linear_model import LogisticRegression  
from sklearn.metrics import classification_report  
from sklearn.datasets import make_classification  
  
# 生成不均衡数据集  
X, y = make_classification(n_classes=2, class_sep=2,  
                             weights=[0.1, 0.9], n_informative=3,  
                             n_redundant=1, flip_y=0,  
                             n_features=20, n_clusters_per_class=1,  
                             n_samples=1000, random_state=10)  
  
# 划分训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  
  
# 初始训练一个有监督模型,使用一部分少数类样本和多数类样本  
minority_class_indices = np.where(y_train == 1)[0]  
minority_samples_to_use = 50  # 假设我们使用50个少数类样本来初始化模型  
initial_majority_class_indices = np.where(y_train == 0)[0][:minority_samples_to_use]  # 取相同数量的多数类样本  
X_initial_train = np.vstack((X_train[minority_class_indices[:minority_samples_to_use]], X_train[initial_majority_class_indices]))  
y_initial_train = np.hstack((y_train[minority_class_indices[:minority_samples_to_use]], y_train[initial_majority_class_indices]))  
  
initial_model = LogisticRegression(max_iter=10000)  # 增加最大迭代次数以避免警告  
initial_model.fit(X_initial_train, y_initial_train)  
  
#自训练过程  
def self_training(model, X_labeled, y_labeled, X_unlabeled, max_iter=10, threshold=0.9):  
    for _ in range(max_iter):  
        # 使用当前模型预测未标记数据  
        y_pred_unlabeled = model.predict_proba(X_unlabeled)[:, 1]  
          
        # 将最自信的预测添加到训练集中  
        confident_indices = np.where(y_pred_unlabeled > threshold)[0]  
        X_labeled = np.vstack((X_labeled, X_unlabeled[confident_indices]))  
        y_labeled = np.hstack((y_labeled, np.ones(len(confident_indices))))  # 假设我们只对少数类感兴趣  
          
        # 重新训练模型  
        model = LogisticRegression(max_iter=10000)  
        model.fit(X_labeled, y_labeled)  
    return model  
  
# 执行自训练  
X_labeled = X_initial_train  
y_labeled = y_initial_train  
X_unlabeled = X_train[y_train == 1]  # 假设未标记数据是少数类样本  
final_model = self_training(initial_model, X_labeled, y_labeled, X_unlabeled, max_iter=5, threshold=0.9)  
  
# 在测试集上评估模型  
y_pred_test = final_model.predict(X_test)  
print(classification_report(y_test, y_pred_test))

这些方法在处理数据不均衡问题时具有一定的优势,可以帮助提高模型的泛化能力、对少数类样本的识别能力和整体的分类性能。在实际应用中,可以根据具体问题和数据特点选择合适的方法或结合多种方法来处理数据不均衡问题,从而提高模型的性能和稳定性。

标签:模型,学习,train,均衡,深度,test,import,model,class
From: https://blog.csdn.net/a2313179618/article/details/136886808

相关文章

  • 决策树的学习及应用
    文章目录一、决策树是什么二、决策树的发展历史三、决策树的相关概念详解四、决策树的构建过程五、决策树的代码示例一、决策树是什么决策树是一种基本的分类与回归方法,它呈树形结构,用于表示基于特征对实例进行分类的过程。决策树可以被看作是if-then规则的集合,也......
  • 学习selenium
    importrequestsimportbs4fromselenium.webdriver.common.byimportByimportpandasaspddriverPath="C:\ProgramFiles(x86)\Microsoft\Edge\Application\msedgedriver.exe"url='https://so.gushiwen.cn/'url2='gushi/tangsh......
  • 芒果YOLOv5改进86:上采样Dysample:顶会ICCV2023,轻量级图像增采样器,通过学习采样来学习上
    ......
  • (day 13)JavaScript学习笔记(对象1)
    概述        这是我的学习笔记,记录了JavaScript的学习过程。在写博客的时候我会尽量详尽的记录每个知识点。如果你完全没接触过JavaScript,那么这一系列的学习笔记可能会对你有所帮助。    今天学习对象,主要是创建对象、对象属性、省略key、遍历对象属性、删......
  • Blazor学习记录三
    11.自定义组件与消费端变量之间实现双向绑定这也实现了从子组件到父组件的状态传递。1.定义一个数据类型为T的参数。2.再定义一个参数名+[Changed]为名称,EventCallback结构类型的参数。注意T类型要和第一步中的参数数据类型T相同。3.定义一个被用于元素中的C#事件触发的事件......
  • CCS5.5帮助文档Help学习
    技巧:纯英文文档,用微信扫一扫翻译。主要看以下三个,按下面顺序:C/C++DevelopmentUserGuide:新建一个项目并运行的demo界面流程,依据操作一遍。【建个小demo,从头到尾操作一遍。3h。】CodeComposerStudioHelp:界面图标认识,用到再查也行。【5分钟大概瞄一眼】C6000CompilerVers......
  • 【机器学习】科学库使用第2篇:机器学习概述,学习目标【附代码文档】
    机器学习(科学计算库)完整教程(附代码资料)主要内容讲述:机器学习(常用科学计算库的使用)基础定位、目标,机器学习概述定位,目标,学习目标,学习目标。机器学习概述,1.3人工智能主要分支学习目标,学习目标,1主要分支介绍,2小结。机器学习概述,1.5机器学习算法分类学习目标,学习目标,1监......
  • Vue学习笔记54--动画效果+过度效果
    动画效果示例一:Test.vue——主要代码文件<template><div><button@click="isShow=!isShow">显示.隐藏</button><!--方式一class=comeorgo--><!--<h2v-show="isShow"class="come">你......
  • 从深度伪造到恶意软件:网络安全迎来AI新挑战
    如今,有越来越多的恶意行为者开始利用AI大语言模型开发能够绕过YARA规则的自我增强型恶意软件。根据近日RecordedFuture发布的一份新报告:AI可以通过增强小型恶意软件变种的源代码来规避基于字符串的YARA规则,从而有效降低检测率。目前,已经有威胁行为者在尝试使用AI技术......
  • 深入了解鸿鹄工程项目管理系统源码:功能清单与项目模块的深度解析
     工程项目管理软件是现代项目管理中不可或缺的工具,它能够帮助项目团队更高效地组织和协调工作。本文将介绍一款功能强大的工程项目管理软件,该软件采用先进的Vue、Uniapp、Layui等技术框架,涵盖了项目策划决策、规划设计、施工建设到竣工交付、总结评估、运维运营等全过程。通过......