分层交叉验证中使用datagen.flow出现维度错误的解决方案求助
解决表格型数据使用ImageDataGenerator数据增强的维度错误问题
错误原因
datagen.flow()是Keras为图像类4维数据(形状为(样本数, 高度, 宽度, 通道数))设计的迭代器,你的输入是2维表格型数据((550, 8),即550个样本,每个样本8个特征),不符合它要求的4维输入格式,因此抛出维度错误。而model.fit()本身支持处理2维表格数据,所以直接调用可以正常运行。
解决方法
因为你的数据是表格型(非图像),不适合用ImageDataGenerator,推荐以下两种方案:
1. 自定义表格数据增强生成器
针对表格数据的特性,手动实现数据增强逻辑,比如添加高斯噪声、随机缩放特征、随机扰动等,然后封装成生成器供模型训练使用。
示例代码:
import numpy as np def tabular_aug_generator(features, labels, batch_size=32, augment=True): num_samples = features.shape[0] while True: # 随机抽取batch样本索引 batch_idx = np.random.choice(num_samples, size=batch_size, replace=False) batch_x = features[batch_idx].copy() batch_y = labels[batch_idx].copy() if augment: # 示例增强操作:添加高斯噪声 noise = np.random.normal(loc=0, scale=0.02, size=batch_x.shape) batch_x += noise # 随机缩放特征(0.9-1.1倍) scale = np.random.uniform(low=0.9, high=1.1, size=batch_x.shape[1]) batch_x *= scale # 可根据需求添加其他增强:比如随机翻转某些特征、添加随机偏移等 yield batch_x, batch_y
训练时调用生成器:
batch_size = 32 # 计算每个epoch需要的步数 steps_per_epoch = len(y_train_fold) // batch_size # 如果样本数不能被batch_size整除,步数+1 if len(y_train_fold) % batch_size != 0: steps_per_epoch += 1 ensemble_model.fit( tabular_aug_generator(features, y_train_fold, batch_size=batch_size), steps_per_epoch=steps_per_epoch, epochs=20, verbose=1 )
2. 使用TensorFlow Dataset实现数据增强
如果用TensorFlow后端,也可以用tf.data.Dataset来构建带增强的数据集,灵活性更高:
示例代码:
import tensorflow as tf def augment_fn(x, y): # 添加高斯噪声 x = x + tf.random.normal(shape=tf.shape(x), mean=0, stddev=0.02) # 随机缩放 x = x * tf.random.uniform(shape=tf.shape(x)[1:], minval=0.9, maxval=1.1) return x, y batch_size = 32 steps_per_epoch = len(y_train_fold) // batch_size if len(y_train_fold) % batch_size != 0: steps_per_epoch += 1 # 构建数据集 dataset = tf.data.Dataset.from_tensor_slices((features, y_train_fold)) # 打乱、分批、添加增强 dataset = dataset.shuffle(buffer_size=len(features)) dataset = dataset.batch(batch_size) dataset = dataset.map(augment_fn, num_parallel_calls=tf.data.AUTOTUNE) dataset = dataset.repeat() # 重复供多epoch训练 ensemble_model.fit( dataset, steps_per_epoch=steps_per_epoch, epochs=20, verbose=1 )
不推荐的方案:强行reshape适配ImageDataGenerator
你可以把2维数据reshape成4维(比如(550, 2, 4, 1)),让它满足NumpyArrayIterator的维度要求,但这种方式只是强行适配格式,对表格数据没有实际的增强意义,还需要修改模型的输入层来匹配新的形状,因此不推荐。
内容的提问来源于stack exchange,提问作者Rezuana Haque
相关产品推荐
相关产品推荐

