You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

tf.keras多输出模型传递样本权重字典触发KeyError:0的问题求助

tf.keras多输出模型传递样本权重字典触发KeyError:0的问题求助

嘿,各位大佬,我最近在搞一个Keras双输出模型的时候遇到了个棘手的问题,折腾了好几种方法都没解决,想请教下大家!

我的模型有两个输出:一个是二进制置信度(多维度,形状为(84,5)),另一个是连续值输出(形状同样为(84,5))。我想给这两个输出分别传递对应的样本权重数组,但调用model.fit()的时候却触发了KeyError: 0,回溯信息显示是tf.keras在执行object = object[_path]的时候出错的。

我已经尝试过这些方法,但都没成功:

  • 把样本权重用列表形式传递
  • 把两个权重数组按行取平均合并成一个数组传递
  • 把输入、输出和样本权重都改成纯数组形式传递

下面是我的训练函数代码:

def train_model(model, X_ts_train, X_item_train, y_train_conf, y_train_pct, epochs=50, batch_size=32):
    import numpy as np
    from sklearn.utils.class_weight import compute_class_weight

    # ---- Ensure y_train_conf is integer (0 or 1) ---- #
    y_train_conf = np.asarray(y_train_conf).astype(int)

    # ---- Compute per-class weights for binary classification ---- #
    unique_classes = np.unique(y_train_conf.ravel())
    class_weight_dict = {0: 1.0, 1: 1.0}  # Default weights
    if len(unique_classes) == 2:  # Ensure both 0 and 1 exist
        class_weights = compute_class_weight(class_weight='balanced', classes=unique_classes, y=y_train_conf.ravel())
        class_weight_dict = {int(unique_classes[i]): class_weights[i] for i in range(len(unique_classes))}

    # ---- Convert class weights into per-sample weights (matching y_train_conf shape) ---- #
    sample_weights_conf = np.array([class_weight_dict[label] for label in y_train_conf.ravel()])
    sample_weights_conf = sample_weights_conf.reshape(y_train_conf.shape)  # Now shape is (84, 5)

    # ---- Compute per-sample weights for continuous spike percentage ---- #
    y_train_pct = np.asarray(y_train_pct)
    sample_weights_pct = np.ones_like(y_train_pct)  # Default weight = 1

    nonzero_mask = y_train_pct > 0
    if np.any(nonzero_mask):
        scaling_factor = np.sum(nonzero_mask) / y_train_pct.size
        sample_weights_pct[nonzero_mask] = 1 / max(scaling_factor, 1e-6)
    print(sample_weights_conf.shape, sample_weights_pct.shape, y_train_conf.shape, y_train_pct.shape, flush=True)

    # sample_weights_binary = np.mean(sample_weights_conf, axis=1)
    # sample_weights_continuous = np.mean(sample_weights_pct, axis=1)

    # print(sample_weights_continuous.shape, sample_weights_binary.shape, flush=True)
    # ---- Train Model ---- #
    history = model.fit(
        {"ts_input": X_ts_train, "item_input": X_item_train},
        {"output_binary": y_train_conf, "output_continuous": y_train_pct},
        epochs=epochs,
        batch_size=batch_size,
        validation_split=0.1,
        verbose=2,
        sample_weight={'output_binary': sample_weights_conf, 'output_continuous': sample_weights_pct}  # Pass separately
    )

    return history

模型的输出层定义如下:

output_binary = Dense(num_binary_targets, activation='sigmoid', name="output_binary")(dense_out)     
output_continuous = Dense(num_continuous_targets, activation='linear', name="output_continuous")(dense_out)

我原本以为只要样本权重的键和输出层名称对应就能正常工作,但实际却各种报错,实在搞不清楚问题出在哪,希望大家能帮忙指点下!

备注:内容来源于stack exchange,提问作者Sean Cassidy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:24:29