-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
sidra_chain_integration/src/space_exploration/project/Lynx/utils/model_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import tensorflow as tf | ||
from tensorflow import keras | ||
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix | ||
|
||
def create_model(input_shape, num_classes): | ||
# Define the machine learning model architecture | ||
model = keras.Sequential([ | ||
keras.layers.Dense(64, activation='relu', input_shape=input_shape), | ||
keras.layers.Dense(32, activation='relu'), | ||
keras.layers.Dense(num_classes, activation='softmax') | ||
]) | ||
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) | ||
return model | ||
|
||
def train_model(model, train_data, test_data, epochs=10): | ||
# Train the machine learning model | ||
model.fit(train_data, epochs=epochs, validation_data=test_data) | ||
return model | ||
|
||
def evaluate_model(model, test_data): | ||
# Evaluate the machine learning model | ||
y_pred = model.predict(test_data) | ||
y_pred_class = tf.argmax(y_pred, axis=1) | ||
y_true = test_data['label'] | ||
accuracy = accuracy_score(y_true, y_pred_class) | ||
report = classification_report(y_true, y_pred_class) | ||
matrix = confusion_matrix(y_true, y_pred_class) | ||
return accuracy, report, matrix |