Understanding the fundamental building blocks of deep learning
import tensorflow as tf
from tensorflow.keras import layers, models
# Create a simple neural network
model = models.Sequential([
layers.Dense(64, activation='relu', input_shape=(input_dim,)),
layers.Dropout(0.2),
layers.Dense(32, activation='relu'),
layers.Dense(output_dim, activation='softmax')
])
# Compile the model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Train the model
history = model.fit(
X_train, y_train,
epochs=50,
batch_size=32,
validation_split=0.2,
callbacks=[
tf.keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=5
)
]
)