class_weight in .fit fails with InvalidArgumentError: Graph execution error:
Opened this issue · 4 comments
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
@sachinprasadhs was able to replicate the issue reported here. Thank you!
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
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
Hello, any updates on this issue? Is there a workaround or fix available for using class weights with .fit()? Thank you.