I built a sequential bi-lstm RNN for a binary classification problem. I have two different classes/labels (0s and 1s) and my input is a sentence (Sequence) with each word of the sequence being classified as either 0 or 1. The model is working properly and I get an 84% val accuracy.
The problem comes when I want to fit the model using the parameter of class_weight. When I use this parameter my model fails and gives me the following error message.
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[2] = 3 is not in [0, 2)
[[{{node GatherV2}}]]
[[IteratorGetNext]] [Op:__inference_train_function_115529]
The reason why I want to use the class weight is because my data is imbalanced (5211 0s for 1789 1s).
This is how my model looks:
vocab_size = n_words + 1
word_embedding_size = 30
sequence_length = max_len
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=word_embedding_size, mask_zero=False, input_length=max_len))
model.add(Bidirectional(LSTM(units=100, return_sequences=True)))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.build
print(model.summary())
history = model.fit(X_train, y_train, batch_size=32, epochs=15, validation_split=0.2, verbose=1, class_weight=class_weight)
Class weight Dictionary:
unique, counts = np.unique(y, return_counts=True)
weight_for_0 = (1/counts[0])*(counts[0]+counts[1])/2.0
weight_for_1 = (1/counts[1])*(counts[0]+counts[1])/2.0
class_weight = {0: weight_for_0, 1: weight_for_1}
print(class_weight)
{0: 0.6716561120706198, 1: 1.956400223588597}
question from:
https://stackoverflow.com/questions/65863413/using-class-weight-in-keras-model-gives-me-an-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…