hi community
I am training a simple LSTM 3 layers network on a small dataset size of around 6K records, with 6 imbalanced classess.
My network is overfitting , I tried early stopping, different batch size, and learning rate scheduler . I am now exploring class_weight option but I am getting error when compiling. Here is my code:
Identifiying my network
from keras.layers import Embedding, Bidirectional, LSTM, GlobalMaxPooling1D, Dense
def get_model_text_lstm_embedding(input_shape=False):
model = Sequential()
model.add(
Embedding(
input_dim=len(vocab_list),
output_dim=embedding_matrix.shape[1],
input_length=train_features_indexed_padding.shape[1],
)
)
model.add(Bidirectional(LSTM(units=128, return_sequences=True)))
model.add(GlobalMaxPooling1D())
model.add(Dense(units=HP.n_classes, activation="softmax"))
return model
Class Weights
from sklearn.utils.class_weight import compute_class_weight
train_classes = np.argmax(y_train_text, axis=1)
class_weights = compute_class_weight(class_weight="balanced", classes=np.unique(train_classes), y=train_classes)
Compling and Training
text_model_lstm_embedding.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss="categorical_crossentropy", metrics=["accuracy"], class_weight=class_weights)
history_text_model_lstm_embedding = text_model_lstm_embedding.fit(
x_train_text, y_train_text,validation_data=(x_test_text, y_test_text),
callbacks=callback_f(nameof(text_model_lstm_embedding)),
epochs=HP.epochs,verbose=3,shuffle=True,batch_size=32)
`
The error message
TypeError: Invalid keyword argument(s) in
compile()
: ({âclass_weightâ},). Valid keyword arguments include âcloningâ, âexperimental_run_tf_functionâ, âdistributeâ, âtarget_tensorsâ, or âsample_weight_modeâ.