Skip to content

Commit

Permalink
feat: better choice of hyperparameters
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpinet committed Nov 13, 2023
1 parent 658edfe commit 79361bd
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions examples/simple_cancer_binary.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from keras.datasets import imdb
from keras_preprocessing.sequence import pad_sequences

from neuralnetlib.activations import Sigmoid, ReLU
from neuralnetlib.layers import Activation, Dense
from neuralnetlib.losses import BinaryCrossentropy
from neuralnetlib.model import Model
from neuralnetlib.optimizers import SGD
from neuralnetlib.metrics import *
from neuralnetlib.optimizers import Adam
from neuralnetlib.metrics import accuracy_score, f1_score, recall_score, precision_score

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
Expand All @@ -19,17 +16,17 @@ def main():
X, y = data.data, data.target

# 2. Preprocessing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
x_train = scaler.fit_transform(X_train)
x_test = scaler.transform(X_test)
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)

# 3. Model definition
# 4. Model definition
input_neurons = 30 # Cancer dataset has 30 features
num_hidden_layers = 2 # Number of hidden layers
hidden_neurons = 30 # Number of neurons in each hidden layer
num_hidden_layers = 5 # Number of hidden layers
hidden_neurons = 100 # Number of neurons in each hidden layer
output_neurons = 1 # Binary classification

model = Model()
Expand All @@ -43,20 +40,20 @@ def main():
model.add(Dense(hidden_neurons, output_neurons, random_state=42))
model.add(Activation(Sigmoid()))

# 4. Model compilation
model.compile(loss_function=BinaryCrossentropy(), optimizer=SGD(learning_rate=0.000001))
# 5. Model compilation
model.compile(loss_function=BinaryCrossentropy(), optimizer=Adam(learning_rate=0.0001))

# 5. Model training
model.train(x_train, y_train, epochs=20, batch_size=128, metrics=[accuracy_score], random_state=42)
# 6. Model training
model.train(x_train, y_train, epochs=20, batch_size=48, metrics=[accuracy_score], random_state=42)

# 6. Model evaluation
# 7. Model evaluation
loss = model.evaluate(x_test, y_test)
print(f'Test loss: {loss}')

# 7. Model prediction
# 8. Model prediction
y_pred = model.predict(x_test)

# 8. Printing some metrics
# 9. Printing some metrics
accuracy = accuracy_score(y_pred, y_test)
precision = precision_score(y_pred, y_test)
recall = recall_score(y_pred, y_test)
Expand All @@ -66,7 +63,7 @@ def main():
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")


if __name__ == '__main__':
main()
main()

0 comments on commit 79361bd

Please sign in to comment.