Deep Neural Networks

Simple neural network

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(1024, activation='relu', input_shape=[11]),
    layers.Dropout(0.3),
    layers.BatchNormalization(),
    layers.Dense(1024, activation='relu'),
    layers.Dropout(0.3),
    layers.BatchNormalization(),
    layers.Dense(1024, activation='relu'),
    layers.Dropout(0.3),
    layers.BatchNormalization(),
    layers.Dense(1),])Copy the code

Add loss and optimizer

model.compile(
    optimizer="adam",
    loss="mae".)Copy the code

Define EarlyStopping

from tensorflow.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(
    min_delta=0.001.# minimium amount of change to count as an improvement
    patience=20.# how many epochs to wait before stopping
    restore_best_weights=True.)Copy the code

training

history = model.fit(
    X_train, y_train,
    validation_data=(X_valid, y_valid),
    batch_size=256,
    epochs=500,
    callbacks=[early_stopping], # put your callbacks in a list
    verbose=0.# turn off training log
)

history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss'.'val_loss']].plot();
print("Minimum validation loss: {}".format(history_df['val_loss'].min()))
Copy the code