У меня есть модель 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
Как передать веса выборки в многовыходную модель Keras ⇐ Python
Программы на Python
-
Anonymous
1737213851
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
Подробнее здесь: [url]https://stackoverflow.com/questions/79367409/how-to-pass-sample-weights-to-keras-multi-output-model[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия