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

class_weight in .fit fails with InvalidArgumentError: Graph execution error: #28

Open
Satori1313 opened this issue Sep 10, 2023 · 4 comments
Assignees

Comments

@Satori1313
Copy link

A simple model throws an error "InvalidArgumentError: Graph execution error" when using the 'class_weight' parameter. Without this parameter, the model trains without any issues. Conducted multiple experiments; the error is reproducible both on a local PC and in Google Colab
keras version: '2.10.0'

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten

# X_train_original = np.random.rand(1000, 100, 4)
X_train = np.random.rand(1000, 400)
y_train = np.random.randint(0, 2, size=(1000, 100))

model_test = Sequential([
    # Flatten(input_shape=(4, 100)),  # Изменен input_shape
    Dense(10, activation='relu'),
    Dense(100, activation='sigmoid')  # Размер выходного слоя остается прежним
], name='model_test')

model_test.compile(loss='binary_crossentropy', metrics=['categorical_accuracy'])

model_test.fit(X_train, y_train, epochs=5, class_weight={0: 1., 1: 760.})  # - ERROR
# model_test.fit(X_train, y_train, epochs=5)  # OK

@sushreebarsa
Copy link
Contributor

@sachinprasadhs was able to replicate the issue reported here. Thank you!

@divyashreepathihalli
Copy link
Contributor

Hi @Satori1313, you have 3 classes in your training and you are setting class_weight for just two classes. The following code should fix your issue.

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Flatten

# X_train_original = np.random.rand(1000, 100, 4)
X_train = np.random.rand(1000, 400)
y_train = np.random.randint(0, 1, size=(1000, 100))

model_test = Sequential([
    # Flatten(input_shape=(4, 100)),  # Изменен input_shape
    Dense(10, activation='relu'),
    Dense(100, activation='sigmoid')  # Размер выходного слоя остается прежним
], name='model_test')

model_test.compile(loss='binary_crossentropy', metrics=['categorical_accuracy'])

model_test.fit(X_train, y_train, epochs=5, class_weight={0: 1., 1: 760.})  # - ERROR
# model_test.fit(X_train, y_train, epochs=5)  # OK

@Satori1313
Copy link
Author

Satori1313 commented Sep 14, 2023

Hi @divyashreepathihalli, I don't understand why you're talking about three classes.
y_train = np.random.randint(0, 2, size=(1000, 100)) creates an array with two classes: '0' and '1'. ('2' is not included in the randint range)
This can be easily verified:

y_train = np.random.randint(0, 2, size=(1000, 100))
print(y_train.min(), y_train.max())


Output: 0 1

In your example, y_train = np.random.randint(0, 1, size=(1000, 100)), the entire y_train array consists exclusively of '0'."

y_train.max()
0.0
y_train.min()
0.0

@sachinprasadhs sachinprasadhs transferred this issue from keras-team/keras Sep 22, 2023
@Satori1313
Copy link
Author

Hello, any updates on this issue? Is there a workaround or fix available for using class weights with .fit()? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants