如何将样本权重传递给Keras多输出模型

问题描述 投票:0回答:1

我有混合类型的多输出(一种回归和一种分类)Keras 模型。我正在尝试为两个输出传递相同的样本权重,如下所示。

import numpy as np
import tensorflow as tf
from tensorflow import keras

# Generate some sample data
np.random.seed(42)
X = np.random.rand(1000, 10)  # 1000 samples, 10 features
y_regression = X.sum(axis=1) + np.random.normal(0, 0.1, 1000) # Regression target
y_classification = (X.sum(axis=1) > 5).astype(int) # Classification target (binary)

# Create sample weights 
sample_weights = np.random.rand(1000)

# Define the model with mixed outputs
def create_model():
    input_layer = keras.layers.Input(shape=(10,))
    dense1 = keras.layers.Dense(64, activation='relu')(input_layer)
    dense2 = keras.layers.Dense(32, activation='relu')(dense1)

    # Regression output
    regression_output = keras.layers.Dense(1, name='regression_output')(dense2)

    # Classification output
    classification_output = keras.layers.Dense(1, activation='sigmoid', name='classification_output')(dense2)

    model = keras.Model(inputs=input_layer, outputs=[regression_output, classification_output])

    return model


model = create_model()

# Compile the model with appropriate losses and metrics for each output
model.compile(
    optimizer='adam',
    loss={'regression_output': 'mse', 'classification_output': 'binary_crossentropy'},
    metrics={'regression_output': 'mae', 'classification_output': 'accuracy'},
)

# Train the model with sample weights
history = model.fit(
    X,
    {'regression_output': y_regression, 'classification_output': y_classification},
    epochs=10,
    batch_size=32,
    sample_weight=sample_weights,
)

还尝试过为每个输出指定权重

history = model.fit(
    X,
    {'regression_output': y_regression, 'classification_output': y_classification},
    epochs=10,
    batch_size=32,
    sample_weight={'regression_output': sample_weights, 'classification_output': sample_weights},  
)

但是,在这两种情况下,我都会收到以下错误,该错误通常表明

sample_weight
数组的形状与输入数据的形状之间不匹配,尽管这里不是这种情况。在多输出模型中传递
sample_weight
的正确方法是什么?

KeyError                                  Traceback (most recent call last)
Cell In[18], line 58
     49 model.compile(
     50     optimizer='adam',
     51     loss={'regression_output': 'mse', 'classification_output': 'binary_crossentropy'},
     52     metrics={'regression_output': 'mae', 'classification_output': 'accuracy'},
     53 )
     57 # Train the model with sample weights
---> 58 history = model.fit(
     59     X,
     60     {'regression_output': y_regression, 'classification_output': y_classification},
     61     epochs=20,
     62     batch_size=32,
     63     sample_weight=sample_weights,
     64 )

File /opt/jupyter/notebooks-generic/.venv/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File /opt/jupyter/notebooks-generic/.venv/lib/python3.9/site-packages/keras/src/trainers/compile_utils.py:785, in CompileLoss.call.<locals>.resolve_path(path, object)
    783 def resolve_path(path, object):
    784     for _path in path:
--> 785         object = object[_path]
    786     return object

KeyError: 0
python tensorflow machine-learning keras
1个回答
0
投票

将 y 从字典更改为 model.fit 中的列表解决了错误。

history = model.fit(
    X,
    [y_regression, y_classification],
    epochs=10,
    batch_size=32,
    sample_weight=sample_weights,
)
© www.soinside.com 2019 - 2024. All rights reserved.