Convolutional Neural Networks for images (e.g. MNIST) and a short intro to time series with deep learning.
A Convolutional Neural Network (CNN) is built for grid-like data (images, or 1D sequences). It uses convolution layers that slide small filters over the input to detect edges, textures, and patterns. Then pooling reduces size; after several such blocks, the result is flattened and passed to dense layers for classification. So: convolution → pooling → … → flatten → dense → output.
MNIST is a classic dataset: 28×28 grayscale images of digits 0–9. We build a small CNN in Keras to classify them.
import tensorflow as tf from tensorflow.keras import layers, models # Load MNIST (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0 x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0 # Simple CNN: Conv -> Pool -> Conv -> Pool -> Flatten -> Dense -> Output model = models.Sequential([ layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Flatten(), layers.Dense(64, activation='relu'), layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
Time series = data ordered in time (e.g. daily sales, temperature). We can use:
Typical steps: load sequence data, create windows (e.g. last 30 days → next day), normalize, build a model (e.g. LSTM or 1D CNN), train and predict. Libraries like TensorFlow/Keras and PyTorch support RNN/LSTM and 1D Conv layers for time series.
In one sentence: why do we use Conv2D and pooling for images but LSTM or 1D CNN for time series?