Skip to content

Instantly share code, notes, and snippets.

@simonespa
Created May 17, 2024 09:23
Show Gist options
  • Select an option

  • Save simonespa/42f4cd2c3d4fa9b71336f0ac3adcde44 to your computer and use it in GitHub Desktop.

Select an option

Save simonespa/42f4cd2c3d4fa9b71336f0ac3adcde44 to your computer and use it in GitHub Desktop.
Simple Feed-Forward Fully Connected Neural Network
import tensorflow as tf
from tensorflow import keras
nn = keras.Sequential(name='CovidClassification')
nn.add(keras.Input(shape=(X_train.shape[1],)))
nn.add(keras.layers.Dense(
units=4,
activation='leaky_relu',
kernel_regularizer=keras.regularizers.L2(),
bias_regularizer=keras.regularizers.L2(),
name='hidden_layer'
))
nn.add(keras.layers.Dense(units=2, activation='softmax', name='output_layer'))
nn.summary()
nn.compile(
optimizer=keras.optimizers.Adam(),
loss=keras.losses.CategoricalCrossentropy(),
metrics=[
keras.metrics.AUC(name='auc'),
keras.metrics.Precision(),
keras.metrics.Recall()
]
)
y_train_encoded = keras.utils.to_categorical(y_train, num_classes=2)
y_val_encoded = keras.utils.to_categorical(y_val, num_classes=2)
training = nn.fit(
X_train, y_train_encoded,
batch_size=32,
epochs=2,
verbose=2,
callbacks=[
keras.callbacks.EarlyStopping(
monitor='val_auc',
patience=5,
mode='max',
restore_best_weights=True
)
],
validation_data=(X_val, y_val_encoded)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment