Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying class_weight for Models with multiple "output" #20701

Open
wosimidwa opened this issue Dec 29, 2024 · 1 comment
Open

Applying class_weight for Models with multiple "output" #20701

wosimidwa opened this issue Dec 29, 2024 · 1 comment
Assignees
Labels
stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@wosimidwa
Copy link

I am trying to train a Model using Keras and I have to apply class_weights to set the data ratio correctly. However, I do have 3 types of labels and when I try to train the model it gives the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File f:\code\small\class_weight.py:2
      1 #%% Modeli Eğit
----> 2 history = model.fit(
      3     train_dataset,
      4     epochs=20,
      5     validation_data=val_dataset,
      6     class_weight=class_weights
      7 )

File ~\AppData\Roaming\Python\Python311\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 ~\AppData\Roaming\Python\Python311\site-packages\keras\src\trainers\data_adapters\tf_dataset_adapter.py:128, in make_class_weight_map_fn.<locals>.class_weights_map_fn(*data)
    122 if tree.is_nested(y):
    123     raise ValueError(
    124         "`class_weight` is only supported for Models with a single "
    125         "output."
    126     )
--> 128 if y.shape.rank >= 2:
    129     y_classes = tf.__internal__.smart_cond.smart_cond(
    130         tf.shape(y)[-1] > 1,
    131         lambda: tf.argmax(y, axis=-1),
    132         lambda: tf.cast(tf.round(tf.squeeze(y, axis=-1)), tf.int32),
    133     )
    134 else:
    135     # Special casing for rank 1, where we can guarantee sparse encoding.

TypeError: '>=' not supported between instances of 'NoneType' and 'int'

here are some essential parts from my code:

#%% CLass Weight Calculation
total_samples = sum(class_counts.values())
current_ratios = {cls: count / total_samples for cls, count in class_counts.items()}
target_ratios = {0: 0.45, 1: 0.35, 2: 0.2}
class_weights = {cls: target_ratios[cls] / current_ratios[cls] for cls in class_counts}
print("Class Weights:", class_weights)
num_classes = 3
n_features = 24

model = Sequential()
model.add(LSTM(128, input_shape=(time_steps, n_features), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(64, return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) #categorical_crossentropy -> one hot encoding
model.summary()

What should I do? I have researched the web but I couldn't find a way to apply class_weight properly for a model with multiple outputs.

@sonali-kumari1 sonali-kumari1 added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Jan 2, 2025
@sonali-kumari1
Copy link

Hi @wosimidwa,

Thanks for reporting this issue. The code you provided seems to be a single output model with multi-class because you have only specified one loss and one metrics. To create a model with multiple outputs you would need to specify different losses and metrics for each output. However, I have tested your code with random data and it is working fine. Attaching gist for your reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

3 participants