⚡0 XP🔥0

Neural Networks and Deep Learning Basics

Lesson 10 of 11 ⏱ 8 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

From a single neuron (perceptron) to multi-layer networks - weights, bias, activation functions, forward pass, loss, backpropagation and gradient descent - then train an MLP on handwritten digits with scikit-learn.

Neural networks power today's most impressive AI: image recognition, speech-to-text, translation and large language models. They are built from a very simple unit - the artificial neuron.

A single neuron (perceptron)#

A neuron takes inputs x₁, x₂, ..., multiplies each by a weight, adds a bias, and passes the result through an activation function:

z = w₁x₁ + w₂x₂ + ... + b
output = activation(z)

The weights say how important each input is; the bias shifts the decision point. Learning means finding good weights and biases.

InteractiveA neuron that learns logic gates

Move the sliders and see how the weights and bias decide the output. Choose a target gate and press Train to apply the perceptron learning rule. AND and OR are learned quickly - now try XOR.

हिंदी में समझें

एक neuron हर input को उसके weight (महत्त्व) से गुणा करता है, सबको जोड़ता है, bias जोड़ता है और फिर activation function से फैसला करता है। Training का मतलब है ऐसे weights और bias खोजना जिनसे सही output आए। एक अकेला neuron सिर्फ़ एक सीधी रेखा से data बाँट सकता है, इसलिए XOR जैसे सवालों के लिए कई neurons की परतें (layers) चाहिए।

Activation functions#

▶ Try it YourselfPython
Ctrl + Enter to run
Output
z       : [-3. -1.  0.  1.  3.]
step    : [0 0 0 1 1]
sigmoid : [0.047 0.269 0.5   0.731 0.953]
tanh    : [-0.995 -0.762  0.     0.762  0.995]
ReLU    : [0. 0. 0. 1. 3.]
Activation Output range Typical use
sigmoid 0 to 1 output layer for binary classification (a probability)
softmax probabilities that add to 1 output layer for multi-class classification
tanh -1 to 1 hidden layers (older networks, RNNs)
ReLU max(0, z) 0 to ∞ hidden layers of most modern networks

Without non-linear activations, any number of layers would still compute just one linear function - the network could only draw straight lines.

Multi-layer networks#

Neurons are arranged in layers:

input layer        hidden layer(s)          output layer
 x₁ ──┐          ┌─ h₁ ─┐
 x₂ ──┼──(W1,b1)─┼─ h₂ ─┼──(W2,b2)──  ŷ
 x₃ ──┘          └─ h₃ ─┘

Each layer computes activation(previous_layer @ W + b). This is the forward pass. A network with several hidden layers is "deep" - hence deep learning.

XOR solved with one hidden layer#

▶ Try it YourselfPython
Ctrl + Enter to run
Output
input [0 0] -> hidden [0 0] -> XOR 0
input [0 1] -> hidden [1 0] -> XOR 1
input [1 0] -> hidden [1 0] -> XOR 1
input [1 1] -> hidden [1 1] -> XOR 0

The hidden layer creates new features (OR, AND) in which XOR is linearly separable: XOR = OR and not AND.

How networks learn: loss, backpropagation, gradient descent#

  1. Forward pass - compute predictions with the current weights.
  2. Loss - measure how wrong they are (MSE for regression, cross-entropy for classification).
  3. Backpropagation - use the chain rule of calculus to compute how much each weight contributed to the loss (its gradient), from the output layer backwards.
  4. Update - move each weight a small step against its gradient (gradient descent).
  5. Repeat for many epochs (passes over the training data), usually in mini-batches.

Here is a complete two-layer network trained from scratch with NumPy to learn XOR:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
epoch    1  loss 1.1836
epoch  100  loss 0.0385
epoch 1000  loss 0.0025
epoch 5000  loss 0.0005
predictions: [0.    0.999 0.999 0.001]

The loss falls and the predictions approach 0, 1, 1, 0. Libraries like PyTorch and TensorFlow/Keras compute these gradients automatically ("autograd"), so you only write the forward pass.

A real task: recognising handwritten digits#

scikit-learn's MLPClassifier (multi-layer perceptron) trains a neural network with backpropagation for you. The digits dataset has 1,797 images of 8×8 pixels:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
layers: [64, 64, 32, 10]  trained for 113 epochs
weights to learn: 6570
test accuracy: 0.964
mistakes (true, predicted, count): [(1, 8, 1), (2, 1, 1), (2, 3, 1), (3, 2, 1), (3, 8, 1), (3, 9, 1), (4, 8, 1), (6, 1, 2), (8, 1, 2), (8, 3, 1), (8, 9, 1), (9, 3, 2), (9, 5, 1)]
▶ Try it YourselfPython
Ctrl + Enter to run

Where to go next: deep learning frameworks#

Network type Specialised for Examples
MLP (fully connected) tabular data, small problems what you trained above
CNN (convolutional) images, video face unlock, medical scans, crop disease detection
RNN / LSTM sequences (older approach) time series, early speech models
Transformer text, code, images, audio GPT, Claude, Gemini, translation

For bigger networks use PyTorch or TensorFlow/Keras with a GPU - for example in Google Colab or Kaggle Notebooks, both free. A classic first deep-learning project is the MNIST (28×28 digits) or Fashion-MNIST dataset.

Tip

Neural networks need more data, more computing power and more tuning than classic models. On ordinary tabular business data, gradient boosting and random forests often beat them. Reach for deep learning with images, audio, text and very large datasets.

Summary#

  • A neuron computes activation(w·x + b); layers of neurons form a network.
  • Non-linear activations (ReLU, sigmoid, tanh) let networks learn complex patterns; one neuron cannot learn XOR, a hidden layer can.
  • Training = forward pass → loss → backpropagation → gradient-descent update, repeated for many epochs.
  • MLPClassifier trains small networks; PyTorch/TensorFlow handle deep learning (CNNs, Transformers).

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Forward pass by hand

A tiny network has 2 inputs, a hidden layer of 2 ReLU neurons and 1 sigmoid output. Using the weights in the starter, compute the output probability for input x = [1.0, 2.0] and store it in out (a float). Print it rounded to 4 decimals.

2

Train an MLP on digits

Train MLPClassifier(hidden_layer_sizes=(64,), max_iter=400, random_state=0) inside a pipeline with StandardScaler on the digits data (test_size=0.25, random_state=0, stratify=y). Store the test accuracy in acc and print it rounded to 3 decimals.

Quick Quiz

Q1.

What does an activation function add to a neural network?

Q2.

Why can't a single perceptron learn XOR?

Q3.

Backpropagation computes...

Q4.

Which activation is most common in hidden layers of modern networks?

Q5.

An epoch is...