I am trying to pass individual np.arrays of sample_weights for two outputs of my Keras model, one of which is a confidence measure of a binary value and one of which is a continuous output. However, I receive a KeyError: 0 as tf.keras tries to read it using object = object[_path] according to the traceback. Imports are provided in the code snippet for clarity. 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): class_weights 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 I expected the model to take the sample weights without issue, and also tried to pass them as a list, pass them as one array that is a mean of both, and tried to give y_train, x_train, and sample_weights all as arrays, all of which gave me a myriad of errors and still failed to give a positive result. The outputs of my model are defined as following: 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) Continue reading...