Introduction to Neural Networks
Neural networks are computing systems inspired by biological neural networks. Let us build one using Python and TensorFlow.
Installation
pip install tensorflow numpy pandas scikit-learn
Basic Neural Network
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Load dataset
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess
X_train = X_train.reshape(-1, 784) / 255.0
X_test = X_test.reshape(-1, 784) / 255.0
# Build model
model = keras.Sequential([
keras.layers.Dense(128, activation="relu"),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation="softmax")
])
# Compile
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
# Train
model.fit(X_train, y_train, epochs=5, batch_size=32)
# Evaluate
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")
Understanding the Code
- Data Loading: We use MNIST dataset (handwritten digits)
- Preprocessing: Normalize pixel values to 0-1
- Model Architecture: Input → Hidden → Output layers
- Training: 5 epochs with batch size 32