2. Implementing a Neural Network

2.1 Hyperparameters

In [7]:
# Hyperparameters
training_epochs = 100 # Total number of training epochs
learning_rate = 0.001 # The learning rate

2.2 Creating a model

In [8]:
# create a model
def create_model():
    model = tf.keras.Sequential()
    # Input layer
    model.add(tf.keras.layers.Dense(8, input_dim=2, kernel_initializer='uniform', activation='relu'))
    # Output layer
    model.add(tf.keras.layers.Dense(y_train.T.shape[1], activation='sigmoid'))

    # Compile a model
    model.compile(loss='binary_crossentropy', 
                optimizer=tf.keras.optimizers.Adam(learning_rate), 
                metrics=['accuracy'])
    return model

model = create_model()
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 8)                 24        
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 9         
=================================================================
Total params: 33
Trainable params: 33
Non-trainable params: 0
_________________________________________________________________

2.3 Train the model

Let's trains the model for a given number of epochs.

In [9]:
results = model.fit(
    X_train, y_train.T,
    epochs= training_epochs,
    validation_data = (X_test, y_test.T),
    verbose = 0
)