What are Neural Networks?
Neural networks are computing systems inspired by biological neural networks. They consist of interconnected nodes (neurons) organized in layers that process information to learn patterns from data.
Network Architecture
import tensorflow as tf
from tensorflow import keras
# Sequential model
model = keras.Sequential([
keras.layers.Dense(128, activation='relu', input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# Compile model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Activation Functions
# ReLU: most common for hidden layers
# Sigmoid: binary classification output
# Softmax: multi-class classification output
# Tanh: alternative to sigmoid
# Custom activation
def custom_activation(x):
return tf.nn.relu(x) * tf.math.tanh(x)
Training Process
# Train the model
history = model.fit(
X_train, y_train,
epochs=10,
batch_size=32,
validation_split=0.2,
callbacks=[
keras.callbacks.EarlyStopping(patience=3),
keras.callbacks.ModelCheckpoint('best_model.h5')
]
)
# Evaluate
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f'Test accuracy: {test_acc}')



