pytorch+sklearn实现数据加载的流程

 更新时间:2022年11月18日 16:08:16   作者:梁小憨憨  
这篇文章主要介绍了pytorch+sklearn实现数据加载,之前在训练网络的时候加载数据都是稀里糊涂的放进去的,也没有理清楚里面的流程,今天整理一下,加深理解,也方便以后查阅,需要的朋友可以参考下

之前在训练网络的时候加载数据都是稀里糊涂的放进去的,也没有理清楚里面的流程,今天整理一下,加深理解,也方便以后查阅。

pytorch+sklearn实现数据加载

epoch & batch_size & iteration

  • epoch:1个epoch等于使用训练集中的全部样本训练一次,通俗的讲epoch的值就是整个数据集被轮几次。
  • batch_size:批大小。在深度学习中,一般采用SGD训练,即每次训练在训练集中取batchsize个样本训练;
  • iteration:1个iteration等于使用batch_size个样本训练一次;

优化算法——梯度下降

深度学习的优化算法,说白了就是梯度下降。每次的参数更新有两种方式。

Batch gradient descent

第一种,遍历全部数据集算一次损失函数,然后算函数对各个参数的梯度,更新梯度,这称为批梯度下降(Batch gradient descent)

这样做至少有 2 个好处:其一,由全数据集确定的方向能够更好地代表样本总体,从而更准确地朝向极值所在的方向。其二,由于不同权重的梯度值差别巨大,因此选取一个全局的学习率很困难。 Full Batch Learning 可以使用 Rprop 只基于梯度符号并且针对性单独更新各权值。

对于更大的数据集,以上 2 个好处又变成了 2 个坏处:其一,随着数据集的海量增长和内存限制,一次性载入所有的数据进来变得越来越不可行。其二,以 Rprop 的方式迭代,会由于各个 Batch 之间的采样差异性,各次梯度修正值相互抵消,无法修正。这才有了后来 RMSProp 的妥协方案。

Stochastic gradient descent

另一种,每看一个数据就算一下损失函数,然后求梯度更新参数,这个称为随机梯度下降(Stochastic gradient descent)。这个方法速度比较快,但是收敛性能不太好,可能在最优点附近晃来晃去,达不到最优点。两次参数的更新也有可能互相抵消掉,造成目标函数震荡的比较剧烈。

Mini-batch gradient decent

为了克服两种方法的缺点,现在一般采用的是一种折中手段,mini-batch gradient decent,小批的梯度下降,这种方法把数据分为若干个批,按批来更新参数,这样,一个批中的一组数据共同决定了本次梯度的方向,下降起来就不容易跑偏,减少了随机性。另一方面因为批的样本数与整个数据集相比小了很多,计算量也不是很大。

现在用的优化器SGD是stochastic gradient descent的缩写,但不代表是一个样本就更新一回,还是基于mini-batch的。

  • 批量梯度下降:批量大小=训练集的大小
  • 随机梯度下降:批量大小= 1
  • 小批量梯度下降:1 <批量大小<训练集的大小

在小批量梯度下降的情况下,流行的批量大小包括32,64和128个样本。

再谈Batch_Size

在合理范围内,增大 Batch_Size 有何好处?

  • 内存利用率提高了,大矩阵乘法的并行化效率提高。
  • 跑完一次 epoch(全数据集)所需的迭代次数减少,对于相同数据量的处理速度进一步加快。
  • 在一定范围内,一般来说 Batch_Size 越大,其确定的下降方向越准,引起训练震荡越小。

盲目增大 Batch_Size 有何坏处?

  • 内存利用率提高了,但是内存容量可能撑不住了。
  • 跑完一次 epoch(全数据集)所需的迭代次数减少,要想达到相同的精度,其所花费的时间大大增加了,从而对参数的修正也就显得更加缓慢。
  • Batch_Size 增大到一定程度,其确定的下降方向已经基本不再变化。

深度学习的第一项任务——数据加载

数据加载流程——重要

以BCICIV_2a数据为例

import mne
import numpy as np
import torch
import torch.nn as nn
class LoadData:
    def __init__(self,eeg_file_path: str):
        self.eeg_file_path = eeg_file_path

    def load_raw_data_gdf(self,file_to_load):
        self.raw_eeg_subject = mne.io.read_raw_gdf(self.eeg_file_path + '/' + file_to_load)
        return self

    def load_raw_data_mat(self,file_to_load):
        import scipy.io as sio
        self.raw_eeg_subject = sio.loadmat(self.eeg_file_path + '/' + file_to_load)

    def get_all_files(self,file_path_extension: str =''):
        if file_path_extension:
            return glob.glob(self.eeg_file_path+'/'+file_path_extension)
        return os.listdir(self.eeg_file_path)
class LoadBCIC(LoadData):
    '''Subclass of LoadData for loading BCI Competition IV Dataset 2a'''
    def __init__(self, file_to_load, *args):
        self.stimcodes=('769','770','771','772')
        # self.epoched_data={}
        self.file_to_load = file_to_load
        self.channels_to_remove = ['EOG-left', 'EOG-central', 'EOG-right']
        super(LoadBCIC,self).__init__(*args)

    def get_epochs(self, tmin=0,tmax=1,baseline=None):
        self.load_raw_data_gdf(self.file_to_load)
        raw_data = self.raw_eeg_subject
        # raw_downsampled = raw_data.copy().resample(sfreq=128)
        self.fs = raw_data.info.get('sfreq')
        events, event_ids = mne.events_from_annotations(raw_data)
        stims =[value for key, value in event_ids.items() if key in self.stimcodes]
        epochs = mne.Epochs(raw_data, events, event_id=stims, tmin=tmin, tmax=tmax, event_repeated='drop',
                            baseline=baseline, preload=True, proj=False, reject_by_annotation=False)
        epochs = epochs.drop_channels(self.channels_to_remove)
        self.y_labels = epochs.events[:, -1] - min(epochs.events[:, -1])
        self.x_data = epochs.get_data()*1e6
        eeg_data={'x_data':self.x_data,
                  'y_labels':self.y_labels,
                  'fs':self.fs}
        return eeg_data
data_path = "/home/pytorch/LiangXiaohan/MI_Dataverse/BCICIV_2a_gdf"
file_to_load = 'A01T.gdf'
'''for BCIC Dataset'''
bcic_data = LoadBCIC(file_to_load, data_path)
eeg_data = bcic_data.get_epochs() # {'x_data':, 'y_labels':, 'fs':}

X = eeg_data.get('x_data')
Y = eeg_data.get('y_labels')
Y.shape

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
X_train.shape

from sklearn.model_selection import StratifiedKFold
train_idx = {}
eval_idx = {}
skf = StratifiedKFold(n_splits=4, shuffle=True)
i = 0
for train_indices, eval_indices in skf.split(X_train, y_train):
    train_idx.update({i: train_indices})
    eval_idx.update({i: eval_indices})
    i += 1
train_idx.get(1).shape

def split_xdata(eeg_data, train_idx, eval_idx):
    x_train=np.copy(eeg_data[train_idx,:,:])
    x_eval=np.copy(eeg_data[eval_idx,:,:])
    x_train = torch.from_numpy(x_train).to(torch.float32)
    x_eval = torch.from_numpy(x_eval).to(torch.float32)
    return x_train, x_eval
def split_ydata(y_true, train_idx, eval_idx):
    y_train = np.copy(y_true[train_idx])
    y_eval = np.copy(y_true[eval_idx])
    y_train = torch.from_numpy(y_train)
    y_eval = torch.from_numpy(y_eval)
    return y_train, y_eval
x_train, x_eval = split_xdata(X_train, train_idx.get(1), eval_idx.get(1))
y_train, y_eval = split_ydata(Y_train, train_idx.get(1), eval_idx.get(1))
y_train.shape

from torch.utils.data import Dataset, DataLoader, TensorDataset
from tqdm import tqdm
def BCICDataLoader(x_train, y_train, batch_size=64, num_workers=2, shuffle=True):
    
    data = TensorDataset(x_train, y_train)

    train_data = DataLoader(dataset=data, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)

    return train_data
train_data = BCICDataLoader(x_train, y_train, batch_size=32)
for inputs, target in tqdm(train_data):
    print(target)

到此数据就读出来了!!!

相关API解释

sklearn.model_selection.train_test_split

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html?highlight=train_test_split

sklearn.model_selection.StratifiedKFold

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html?highlight=stratifiedkfold#sklearn.model_selection.StratifiedKFold

torch.utils.data.TensorDataset

https://pytorch.org/docs/stable/data.html?highlight=tensordataset#torch.utils.data.TensorDataset

torch.utils.data.DataLoader

https://pytorch.org/docs/stable/data.html?highlight=dataloader#torch.utils.data.DataLoader

参考资料

深度学习中的batch、epoch、iteration的含义

神经网络中Batch和Epoch之间的区别是什么?

谈谈深度学习中的 Batch_Size

到此这篇关于pytorch+sklearn实现数据加载的文章就介绍到这了,更多相关pytorch数据加载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解

    Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解

    这篇文章主要介绍了Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法,结合实例形式详细分析了Python GUI编程中tkinter控件的原理、用法及相关操作注意事项,需要的朋友可以参考下
    2020-03-03
  • python实现简单爬虫功能的示例

    python实现简单爬虫功能的示例

    本文主要是介绍python实现简单爬虫功能的示例,主要实现了把我们想要的图片爬虫到本地的一个示例,有需要的朋友可以了解一下。
    2016-10-10
  • 基于Python写一个番茄钟小工具

    基于Python写一个番茄钟小工具

    最近听到朋友说在用番茄钟,有点兴趣也想下载一个来用用,后面仔细一想这玩意做起来也不难,索性自己顺手写一个算了,在这里也分享给大家了
    2022-12-12
  • 关于Python turtle库使用时坐标的确定方法

    关于Python turtle库使用时坐标的确定方法

    这篇文章主要介绍了关于Python turtle库使用时坐标的确定方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • OpenCV4.1.0+VS2017环境配置的方法步骤

    OpenCV4.1.0+VS2017环境配置的方法步骤

    这篇文章主要介绍了OpenCV4.1.0+VS2017环境配置的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • python计算最小优先级队列代码分享

    python计算最小优先级队列代码分享

    python计算最小优先级队列代码分享,大家参考使用吧
    2013-12-12
  • Python报mongod: error while loading shared libraries: libcrypto.so.1.1解决

    Python报mongod: error while loading shared libraries: l

    这篇文章主要介绍的是Python报mongod: error while loading shared libraries: libcrypto.so.1.1的解决方法,下面文章解决过程,需要的小伙伴可以参考一下
    2022-02-02
  • Visual Studio code 配置Python开发环境

    Visual Studio code 配置Python开发环境

    这篇文章主要介绍了Visual Studio code 配置Python开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Python如何实现SSH远程连接与文件传输

    Python如何实现SSH远程连接与文件传输

    这篇文章主要介绍了Python如何实现SSH远程连接与文件传输问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Python实现自动批量修改文件名称

    Python实现自动批量修改文件名称

    这篇文章主要为大家详细介绍了如何基于Python语言,实现按照一定命名规则批量修改多个文件的文件名的效果,文中的示例代讲解详细,感兴趣的可以了解一下
    2023-01-01

最新评论