Keras LSTM时间序列二分类中class_weights引发图执行错误排查
问题描述
- 任务:使用Keras训练时间序列LSTM模型解决二分类问题,标签分布极不平衡(0类占比约75%),目标是校正不平衡问题,重点减少假阳性。
- 输入输出尺寸:
注:每个样本对应100个时间点的预测任务X_train.shape --> (8000, 100, 4) X_test.shape --> (2000, 100, 4) y_train.shape --> (8000, 100) y_test.shape --> (2000, 100)
当前模型代码
model = Sequential() model.add(LSTM(64, input_shape=(X_train.shape[1], X_train.shape[2]), use_bias=True, unroll=True, kernel_initializer='glorot_normal', return_sequences=True)) model.add(BatchNormalization()) model.add(Dropout(.25)) model.add(LSTM(32, return_sequences=False, use_bias=True, unroll=True)) model.add(Dense(num_points_per_inp, activation='sigmoid')) model.compile(optimizer=Adam(beta_1=.8, beta_2=.9), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), metrics=tf.keras.metrics.BinaryAccuracy()) # loss=weighted_binary_crossentropy, metrics='accuracy') model.summary()
已尝试调整BatchNormalization(BN)和Dropout(DO)的不同组合,若模型本身存在问题请指出。
遇到的错误
不使用class_weights时模型可正常编译训练,但添加后触发以下错误:
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-113-2ebc95d5f9f5> in <module>() 7 validation_data=(X_test, y_test), 8 class_weight=weights, ----> 9 verbose=1) 1 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 53 ctx.ensure_initialized() 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 55 inputs, attrs, num_outputs) 56 except core._NotOkStatusException as e: 57 if name is not None: InvalidArgumentError: Graph execution error: 2 root error(s) found. (0) INVALID_ARGUMENT: indices[2] = 52 is not in [0, 2) [[{{node GatherV2}}]] [[IteratorGetNext]] [[IteratorGetNext/_2]] (1) INVALID_ARGUMENT: indices[2] = 52 is not in [0, 2) [[{{node GatherV2}}]] [[IteratorGetNext]] 0 successful operations. 0 derived errors ignored. [Op:__inference_train_function_769866]
尝试过的无效解决方案
方案1:用sklearn生成class_weights
from sklearn.utils import class_weight w = class_weight.compute_class_weight( class_weight = 'balanced', classes = np.unique(y_train), y = y_train.flatten()) weights = dict(enumerate(np.round(w,2)))
方案2:手动计算class_weights
counts = np.bincount(y_train.flatten().astype(int)) w0 = 1 / counts[0] w1 = 1 / counts[1] weights = {0: w0, 1: w1}
方案3:自定义加权损失函数(效果不符合预期)
import keras.backend as K def weighted_binary_crossentropy(y_true, y_pred): weights =(tf.math.abs(y_true) * 59.) + 1. bce = K.binary_crossentropy(y_true, y_pred) weighted_bce = K.mean(bce * weights) return weighted_bce
猜测问题可能与每个样本包含100个时间点的预测任务有关,但不确定;尝试使用CategoricalCrossentropy也无济于事。
请问该如何解决这个问题?
内容的提问来源于stack exchange,提问作者ArieAI
相关产品推荐
相关产品推荐

