diff --git a/models/ai_models/neural_networks.py b/models/ai_models/neural_networks.py index ff533b5..e5e0f67 100644 --- a/models/ai_models/neural_networks.py +++ b/models/ai_models/neural_networks.py @@ -1,9 +1,11 @@ # Import necessary libraries import tensorflow as tf -from tensorflow import keras from sklearn.preprocessing import StandardScaler +from tensorflow import keras # Define a class for neural networks + + class NeuralNetwork: def __init__(self, input_shape, output_shape, hidden_layers): self.input_shape = input_shape @@ -16,14 +18,22 @@ def _create_model(self): model = keras.Sequential() model.add(keras.layers.InputLayer(input_shape=self.input_shape)) for layer in self.hidden_layers: - model.add(keras.layers.Dense(layer, activation='relu')) - model.add(keras.layers.Dense(self.output_shape, activation='softmax')) - model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) + model.add(keras.layers.Dense(layer, activation="relu")) + model.add(keras.layers.Dense(self.output_shape, activation="softmax")) + model.compile( + optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] + ) return model def train(self, X_train, y_train, X_test, y_test): # Train the neural network model - self.model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test)) + self.model.fit( + X_train, + y_train, + epochs=10, + batch_size=128, + validation_data=(X_test, y_test), + ) def predict(self, X): # Make predictions using the trained model @@ -34,7 +44,10 @@ def evaluate(self, X_test, y_test): loss, accuracy = self.model.evaluate(X_test, y_test) return accuracy + # Define a function to load a neural network model + + def load_neural_network(model_path): # Load the neural network model from a file model = tf.keras.models.load_model(model_path)