From 1f73f3c0ef34fd919bcfc8496f99cbf5ce2bf111 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Fri, 17 May 2024 13:04:58 +0200 Subject: [PATCH] Remove verbosity of training and prediction (no logging) Import and use metrics from Keras when compiling the model Use model.export instead of save_model --- training/train.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/training/train.py b/training/train.py index 86d8fb9..5b546ff 100644 --- a/training/train.py +++ b/training/train.py @@ -44,7 +44,7 @@ from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, LSTM, Dense -from tensorflow.keras.models import save_model +from tensorflow.keras.metrics import Accuracy, Recall, Precision, F1Score from sklearn.model_selection import train_test_split from sklearn.metrics import ( accuracy_score, @@ -86,13 +86,22 @@ model.add(Embedding(MAX_WORDS, 128, input_length=MAX_LEN)) model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2)) model.add(Dense(1, activation="sigmoid")) -model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) +model.compile( + loss="binary_crossentropy", + optimizer="adam", + metrics=[ + Accuracy(), + Recall(), + Precision(), + F1Score(), + ], +) # Train model -model.fit(X_train, y_train, epochs=11, batch_size=32) +model.fit(X_train, y_train, epochs=11, batch_size=32, verbose=0) # Predict test set -y_pred = model.predict(X_test) +y_pred = model.predict(X_test, verbose=0) y_pred_classes = np.argmax(y_pred, axis=1) # Calculate model performance indicators @@ -111,4 +120,4 @@ print("ROC: {:.2f}%".format(tp / (tp + fn) * 100)) # Save model as SavedModel format -save_model(model, sys.argv[2]) +model.export(sys.argv[2])