AI & Machine Learning Topic

CNNs, RNNs and LSTMs: Learn Images and Sequences Step by Step

Different tasks need different ways to use structure. Nearby pixels matter in an image. Order matters in a sentence or sensor stream. CNNs and recurrent networks are ways to use those patterns, not rules that every image or text system must follow.

What a CNN does

A convolutional neural network applies learned filters over local regions. A filter can respond to a pattern such as an edge. Reusing it across positions reduces the number of separate weights compared with a fully connected layer over all pixels.

A feature map records responses at different positions. Stride controls how far the filter moves. Padding changes how boundaries are handled. Pooling can reduce spatial size. These choices affect what the network can preserve and notice.

A small filter you can calculate

The first function below slides a two-number filter over a list. It is a one-dimensional teaching version of the cross-correlation operation commonly called convolution in neural-network libraries. It has fixed weights and does not train a CNN.

from math import tanh

def sliding_filter(values, weights):
    if not weights or len(values) < len(weights):
        raise ValueError("The filter must fit inside the input")
    return [sum(v*w for v,w in zip(values[i:i+len(weights)],weights))
            for i in range(len(values)-len(weights)+1)]

def recurrent_state(values):
    state = 0.0
    for value in values:
        state = tanh(0.5 * state + 0.2 * value)
    return state

if __name__ == "__main__":
    print("Local differences:",sliding_filter([1,1,4,4],[1,-1]))
    print("Forward state:",round(recurrent_state([1,2,3]),3))
    print("Reverse state:",round(recurrent_state([3,2,1]),3))

Save it as sequence_demo.py and run python sequence_demo.py. The local differences are 0, -3 and 0. The filter responds where the values change. An image filter does related work over a two-dimensional patch, usually with several channels.

What an RNN does

A recurrent neural network processes steps in order while carrying a hidden state. In the second function, each value changes a small state. Reversing the order produces a different result. The numbers and weights are invented to show the process.

A hidden state is not a perfect memory of everything. During training, gradients through many steps can shrink or grow. This can make long dependencies difficult to learn. Gradient clipping can help with large gradients, but does not solve every learning problem.

Why use an LSTM?

A long short-term memory network adds a cell state and gates. The gates control what information is kept, added and read. This can help learning across longer sequences compared with a basic RNN. It does not guarantee unlimited memory or solve all sequence tasks.

A bidirectional model reads both directions. It can be useful when the whole sequence is available. For a live prediction that must not see the future, a backward-looking pass may be inappropriate.

Understand the shapes

Many PyTorch image layers use batch, channels, height, width. TensorFlow image workflows commonly use batch, height, width, channels. Defaults and options differ. Read the exact layer's documentation rather than moving an array between tools without checking.

A sequence often has batch, time steps, features. PyTorch LSTM has a batch_first option; its default order differs. A shape mismatch can crash a program, while a wrongly ordered but accepted shape can produce a harder-to-find mistake.

Where transformers fit

Transformers use attention to combine information across positions. They are widely used for language and also appear in vision and time-series tasks. CNNs and LSTMs remain useful options. Choose with the data, latency, memory and evaluation results in mind.

Practice

Change the filter to [1, 1] and explain why it measures something different. Try two sequences with the same values in a different order. Then describe why a random row split can leak future information when overlapping time windows come from one sensor stream.

Official references

Reviewed on 25 October 2026. Use documentation for your installed versions.

All AI & Machine Learning tutorials

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?