tf-keras
tf-keras copied to clipboard
Keras/tensorflow failed when specifying class_weight in model.fit()
System information.
- OS Platform and Distribution: Linux.
- TensorFlow version: Keras/tensorflow version 2.8.0.
- Python version: Python 3.7
- GPU model and memory: Use CPU (no GPU).
Describe the problem.
My network model works well without specifying class_weight
in model.fit()
.
However, when I specify class_weight
in model.fit()
, no matter what weight values I give, keras/tensorflow failed with the following error:
File "/opt/local/lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
indices[9] = 16 is not in [0, 10)
[[{{node GatherV2}}]]
[[IteratorGetNext]] [Op:__inference_train_function_43205]
Keras/tensorflow failed with the above error even when I give all classes an equal weight 1.0 (which is equivalent to no class weights), as the following (I have 10 classes):
class_weights_dict = {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0, 5: 1.0, 6: 1.0, 7: 1.0, 8: 1.0, 9: 1.0}
history = model.fit(train_input,
train_true_labels,
class_weight=class_weights_dict,
validation_split=validation_split,
shuffle=True,
epochs=epochs,
batch_size=batch_size)
And I verified that my true labels array train_true_labels
contains only integers 0-9, as the following:
values = np.unique(train_true_labels)
print(values)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
However, when I do not specify class_weight
in model.fit()
, the training for my model works just fine.
So it looks like I just cannot use class_weight
in training. But my classes are highly imbalanced; not using class weights would train a useless model.
I would greatly appreciate any solution for this issue.
Thank you very much!