Как передать веса выборки в многовыходную модель KerasPython

Программы на Python
Ответить
Anonymous
 Как передать веса выборки в многовыходную модель Keras

Сообщение Anonymous »

У меня есть модель 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..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..resolve_path(path, object)
783 def resolve_path(path, object):
784 for _path in path:
--> 785 object = object[_path]
786 return object

KeyError: 0


Подробнее здесь: https://stackoverflow.com/questions/793 ... tput-model
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»