我有一个大数据集(300.000 个示例 x 33.000 个特征),这当然不适合内存。数据以 HDF5 格式保存。这些值大多为零(稀疏数据)。它们看起来像这样:
Attr1 52 52 52 52 52 52 52 52 ...
Attr2 umb umb umb umb umb umb umb umb ...
CellID TGC-1 TGG-1 CAG-1 TTC-1 GTG-1 GTA-1 CAA-1 CAC-1 ...
Acc Gene ...
243485 RP11-.3 0 0 0 0 0 0 0 0 ...
237613 FAM138A 0 0 0 0 0 0 0 0 ...
186092 OR4F5 0 0 0 0 0 0 0 0 ...
238009 RP11-.7 0 0 0 0 0 0 0 0 ...
239945 RP11-.8 0 0 0 0 0 0 0 0 ...
279457 FO538.2 0 0 0 0 0 0 0 0 ...
228463 AP006.2 0 0 0 0 0 0 0 0 ...
... ... ... ... ... ... ... ... ... ...
我已经完成了以下工作,将整个数据集加载到 TensorFlow 中(loompy
只是一个在后台使用 hdf5 的包):
import tensorflow as tf
import numpy as np
import loompy as lp
batch_size = 1000
with loompy.connect(filename, 'r') as ds:
ds_shape = (batch_size, ds.shape[0])
ds_dtype = ds[0:1, 0:1].dtype
labels = np.asarray([ds.ca.CellID, ds.ca.Attr1]).T
labels_shape = (batch_size, 1)
data_placeholder = tf.placeholder(ds_dtype, ds_shape)
labels_placeholder = tf.placeholder(labels[:,1].dtype, labels_shape)
dataset = tf.data.Dataset.from_tensor_slices((data_placeholder, labels_placeholder))
dataset = dataset.prefetch(batch_size)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
with tf.Session() as sess:
with loompy.connect(filename, 'r') as ds:
for i in range(0, ds.shape[1], batch_size):
batch = ds[0 : ds_shape[1], i : i + batch_size].T
batch_labels = np.asarray([ds.ca.CellID[i : i + batch_size],
ds.ca.Attr1[i : i + batch_size]]).T[:,1]
sess.run(iterator.initializer, feed_dict = {data_placeholder: batch,
labels_placeholder: batch_labels.reshape(batch_size, 1)})
for _ in range(batch_size):
print(sess.run(next_element))
输出:
(array([0, 0, 0, ..., 0, 0, 0], dtype=int32), array([b'52'], dtype=object))
(array([0, 0, 0, ..., 0, 0, 0], dtype=int32), array([b'52'], dtype=object))
...
但是,通过这种方式,我无法将数据拆分为训练集、测试集和评估集。此外,我只能在每个批处理内将它们打乱,这是无效的,因为大多数时候批处理上的数据属于同一类。
我如何操作此类数据才能将它们加载为训练集、测试集、评估集并执行改组等(最好尽可能利用我的 TitanX GPU)?
最佳答案
你绝对应该试试 Dask ,它允许您处理不适合内存的数据,并使计算瘫痪,以便您可以使用 cpu 的所有核心。我还建议将您的数据从 hdf 移动到 parquet ,它允许并发读取和写入,从而加快速度。请查看 Wes McKinney(pandas 创作者)深入研究并将其与其他格式进行比较的链接。
您可以在 Dask 中准备片段以准备训练集、测试集和验证集,并在不超过可用内存的情况下读取它们。
关于python - 在 TensorFlow 中导入巨大的非图像数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50814066/