Python 为什么列车测试需要很长时间才能运行?

Python 为什么列车测试需要很长时间才能运行?,python,deep-learning,conv-neural-network,google-colaboratory,Python,Deep Learning,Conv Neural Network,Google Colaboratory,我正在使用Google colab,我正在尝试训练一个卷积神经网络。用于分割约11500张图像的数据集,每个数据的形状为63x63x63。我使用了sklearn中的train\u test\u split test_split = 0.1 random_state = 42 X_train, X_test, y_train, y_test = train_test_split(triplets, df.label, test_size = test_split, random_state = r

我正在使用Google colab,我正在尝试训练一个卷积神经网络。用于分割约11500张图像的数据集,每个数据的形状为63x63x63。我使用了
sklearn
中的
train\u test\u split

test_split = 0.1
random_state = 42
X_train, X_test, y_train, y_test = train_test_split(triplets, df.label, test_size = test_split, random_state = random_state)
每次我的运行时断开连接时,我都需要运行这个来继续。但是,仅此命令就需要近10分钟(或更长时间)才能运行。笔记本中的其他所有命令都运行得非常快(可能在几秒钟或更短的时间内)。我真的不确定问题是什么;我尝试将运行时更改为GPU,我的internet连接似乎相当稳定。问题可能是什么?

为什么要花这么多时间? 您的数据形状是11500x63x663x63。通常需要这么长的时间,因为数据形状非常庞大

说明: 由于数据形状为11500x63x63x63,因此数据中大约有3x10^9个内存位置(实际值为2875540500)。通常,一台机器每秒可以执行10^7~10^8条指令。由于Python相对较慢,我认为谷歌COLAB能够每秒执行10 ^ 7指令,那么,

列车试验所需的最短时间\u分割=3x10^9/10^7=300秒=5分钟

然而,函数的实际时间复杂度几乎接近于
O(n)
,但由于巨大的数据操作,该函数基于巨大的数据传递和检索操作而存在瓶颈。这导致脚本的时间消耗几乎是原来的两倍

如何解决? 一个简单的解决方案是传递要素数据集的索引,而不是直接传递要素数据集(在这种情况下,要素数据集是
triplets
)。这将减少在train_test_split功能中复制返回的培训和测试功能所需的额外时间。根据当前使用的数据类型,这可能会提高性能

为了进一步解释我所说的,我添加了一个简短的代码

# Building a index array of the input feature
X_index = np.arange(0, 11500)

# Passing index array instead of the big feature matrix
X_train, X_test, y_train, y_test = train_test_split(X_index, df.label, test_size=0.1, random_state=42)

# Extracting the feature matrix using splitted index matrix
X_train = triplets[X_train]
X_test = triplets[X_test]
在上面的代码中,我传递了输入特征的索引,并根据train_test_split函数将其拆分。此外,我正在手动提取训练和测试数据集,以减少返回大矩阵的时间复杂性

估计的时间改进取决于您当前使用的数据类型。为了进一步强化我的答案,我添加了一个基准测试,它使用了在GoogleColab上测试的NumPy矩阵和数据类型。下面给出了基准代码和输出。然而,在某些情况下,它并没有像基准中那样有太多的改进

代码: 输出: 数据类型的基准测试 ---------------------------------------- 所用时间:0.473 索引所用时间:0.304 数据类型基准 ---------------------------------------- 所用时间:0.895 索引所用时间:0.604 数据类型基准 ---------------------------------------- 时间:1.792 索引所用时间:1.182 数据类型基准 ---------------------------------------- 时间:2.493 索引所用时间:2.398 数据类型基准 ---------------------------------------- 时间:0.730 索引所用时间:0.738 数据类型基准 ---------------------------------------- 时间:1.904 索引所用时间:1.400 数据类型基准 ---------------------------------------- 时间:5.166 索引所用时间:3.076 为什么要花这么多时间? 您的数据形状是11500x63x663x63。通常需要这么长的时间,因为数据形状非常庞大

说明: 由于数据形状为11500x63x63x63,因此数据中大约有3x10^9个内存位置(实际值为2875540500)。通常,一台机器每秒可以执行10^7~10^8条指令。由于Python相对较慢,我认为谷歌COLAB能够每秒执行10 ^ 7指令,那么,

列车试验所需的最短时间\u分割=3x10^9/10^7=300秒=5分钟

然而,函数的实际时间复杂度几乎接近于
O(n)
,但由于巨大的数据操作,该函数基于巨大的数据传递和检索操作而存在瓶颈。这导致脚本的时间消耗几乎是原来的两倍

如何解决? 一个简单的解决方案是传递要素数据集的索引,而不是直接传递要素数据集(在这种情况下,要素数据集是
triplets
)。这将减少在train_test_split功能中复制返回的培训和测试功能所需的额外时间。根据当前使用的数据类型,这可能会提高性能

为了进一步解释我所说的,我添加了一个简短的代码

# Building a index array of the input feature
X_index = np.arange(0, 11500)

# Passing index array instead of the big feature matrix
X_train, X_test, y_train, y_test = train_test_split(X_index, df.label, test_size=0.1, random_state=42)

# Extracting the feature matrix using splitted index matrix
X_train = triplets[X_train]
X_test = triplets[X_test]
在上面的代码中,我传递了输入特征的索引,并根据train_test_split函数将其拆分。此外,我正在手动提取训练和测试数据集,以减少返回大矩阵的时间复杂性

估计的时间改进取决于您当前使用的数据类型。为了进一步强化我的答案,我添加了一个基准测试,它使用了在GoogleColab上测试的NumPy矩阵和数据类型。下面给出了基准代码和输出。然而,在某些情况下,它并没有像基准中那样有太多的改进

代码: 输出: 数据类型的基准测试 ---------------------------------------- 所用时间:0.473 索引所用时间:0.304 数据类型基准 ---------------------------------------- 所用时间:0.895 索引所用时间:0.604 数据类型基准 ---------------------------------------- 时间:1.792 索引所用时间:1.182 数据类型基准 ---------------------------------------- 时间:2.493 索引所用时间:2.398 数据类型基准 ---------------------------------------- 时间
Benchmark for dtype <class 'numpy.int8'>
----------------------------------------
Time elapsed: 0.473
Time elapsed with indexing: 0.304

Benchmark for dtype <class 'numpy.int16'>
----------------------------------------
Time elapsed: 0.895
Time elapsed with indexing: 0.604

Benchmark for dtype <class 'numpy.int32'>
----------------------------------------
Time elapsed: 1.792
Time elapsed with indexing: 1.182

Benchmark for dtype <class 'numpy.int64'>
----------------------------------------
Time elapsed: 2.493
Time elapsed with indexing: 2.398

Benchmark for dtype <class 'numpy.float16'>
----------------------------------------
Time elapsed: 0.730
Time elapsed with indexing: 0.738

Benchmark for dtype <class 'numpy.float32'>
----------------------------------------
Time elapsed: 1.904
Time elapsed with indexing: 1.400
    
Benchmark for dtype <class 'numpy.float64'>
----------------------------------------
Time elapsed: 5.166
Time elapsed with indexing: 3.076