我有非常大的训练(30Gb)文件。
由于我的可用 RAM 无法容纳所有数据,因此我想批量读取数据。
我看到有 Tensorflow-io 包实施了一种方式 https://www.tensorflow.org/io/api_docs/python/tfio/IODataset#from_hdf5借助该函数,可以通过这种方式将 HDF5 读入 Tensorflowtfio.IODataset.from_hdf5()
然后,自从tf.keras.model.fit()
需要一个tf.data.Dataset
作为包含样本和目标的输入,我需要将 X 和 Y 压缩在一起,然后使用.batch and .prefetch
仅将必要的数据加载到内存中。为了进行测试,我尝试将此方法应用于较小的样本:训练(9Gb)、验证(2.5Gb)和测试(1.2Gb),我知道它们效果很好,因为它们可以放入内存中,并且我得到了很好的结果(70%的准确度和训练文件存储在 HDF5 文件中,分为样本 (X) 和标签 (Y) 文件,如下所示:
X_learn.hdf5
X_val.hdf5
X_test.hdf5
Y_test.hdf5
Y_learn.hdf5
Y_val.hdf5
这是我的代码:
BATCH_SIZE = 2048
EPOCHS = 100
# Create an IODataset from a hdf5 file's dataset object
x_val = tfio.IODataset.from_hdf5(path_hdf5_x_val, dataset='/X_val')
y_val = tfio.IODataset.from_hdf5(path_hdf5_y_val, dataset='/Y_val')
x_test = tfio.IODataset.from_hdf5(path_hdf5_x_test, dataset='/X_test')
y_test = tfio.IODataset.from_hdf5(path_hdf5_y_test, dataset='/Y_test')
x_train = tfio.IODataset.from_hdf5(path_hdf5_x_train, dataset='/X_learn')
y_train = tfio.IODataset.from_hdf5(path_hdf5_y_train, dataset='/Y_learn')
# Zip together samples and corresponding labels
train = tf.data.Dataset.zip((x_train,y_train)).batch(BATCH_SIZE, drop_remainder=True).prefetch(tf.data.experimental.AUTOTUNE)
test = tf.data.Dataset.zip((x_test,y_test)).batch(BATCH_SIZE, drop_remainder=True).prefetch(tf.data.experimental.AUTOTUNE)
val = tf.data.Dataset.zip((x_val,y_val)).batch(BATCH_SIZE, drop_remainder=True).prefetch(tf.data.experimental.AUTOTUNE)
# Build the model
model = build_model()
# Compile the model with custom learing rate function for Adam optimizer
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=lr_schedule(0)),
metrics=['accuracy'])
# Fit model with class_weights calculated before
model.fit(train,
epochs=EPOCHS,
class_weight=class_weights_train,
validation_data=val,
shuffle=True,
callbacks=callbacks)
这段代码可以运行,但损失非常高(300+),并且准确度从一开始就下降到 0(0.30 -> 4*e^-5)...我不明白我做错了什么,我错过了吗某物 ?