Basics of Deep Learning and Foundation Models · Topic 1 of 3
Perceptron and Fully-connected Neural Networks
Learn the perceptron and XOR, activation and loss functions, forward and backpropagation, SGD and Adam, vanishing gradients, dropout and early stopping.
- 5 min
- A perceptron is weighted sum plus bias through an activation; alone it solves only linearly separable problems.
- Nonlinear activations are what let deep networks learn complex functions.
- Sigmoid outputs 0 to 1 and causes vanishing gradients; ReLU is the default for hidden layers.
What it is
A perceptron is the simplest artificial neuron. It multiplies each input by a weight, adds a bias, and passes the sum through an activation function to give an output.
A fully-connected neural network (also called a multilayer perceptron, MLP) stacks layers of neurons, where every neuron in one layer connects to every neuron in the next. It is trained by repeating a forward pass, a loss calculation, backpropagation and a weight update.
How it works
The perceptron
The output is y = f(w1x1 + w2x2 + ... + b). Rosenblatt's original perceptron used a step function: output 1 if the sum is above 0, else 0.
A single perceptron draws one straight line (a hyperplane) between classes, so it solves only linearly separable problems. It can do AND and OR, but not XOR, which Minsky and Papert pointed out in 1969.
Adding a hidden layer with nonlinear activations solves XOR. That is why multilayer networks replaced the single perceptron.
Network structure
| Layer | Role |
|---|---|
| Input layer | Receives the features; no computation |
| Hidden layers | Learn intermediate features; "deep" means several hidden layers |
| Output layer | Gives the prediction, such as class scores or a value |
A layer with n inputs and m outputs has n x m weights plus m biases. For 784 inputs and 128 neurons: 784 x 128 + 128 = 100,480 parameters.
Activation functions
Without a nonlinear activation, stacked layers collapse into one linear function, so the network cannot learn complex patterns.
| Function | Output range | Notes |
|---|---|---|
| Sigmoid | 0 to 1 | Smooth; saturates at both ends; largest derivative 0.25, so gradients vanish in deep networks |
| Tanh | -1 to 1 | Zero-centered, but still saturates |
| ReLU, max(0, x) | 0 to infinity | Cheap; no saturation for positive inputs; neurons can "die" when stuck below 0 |
| Leaky ReLU | Small slope below 0 | Avoids dead neurons |
| Softmax | 0 to 1, summing to 1 | Output layer for multi-class classification |
Softmax of logits [2, 1, 0]: e squared = 7.389, e = 2.718, 1, sum 11.107. The probabilities are 0.665, 0.245 and 0.090.
Loss functions
| Loss | Used for |
|---|---|
| Mean squared error (MSE) | Regression |
| Cross-entropy | Classification, usually after softmax or sigmoid |
Training process
Backpropagation applies the chain rule from the output layer back to the input, giving the gradient of the loss for every weight. The optimizer then updates the weights. One pass over the whole training set is an epoch.
Optimizers
| Optimizer | Idea |
|---|---|
| SGD | Plain gradient step with a fixed learning rate |
| Momentum | Adds a fraction of the previous update, speeding progress and damping oscillation |
| AdaGrad | Per-parameter learning rate that shrinks with the sum of past squared gradients |
| RMSprop | Like AdaGrad but uses a moving average, so the rate does not shrink to zero |
| Adam | Combines momentum with RMSprop-style adaptive rates; a common default |
Vanishing and exploding gradients
In deep networks, gradients are multiplied layer by layer. With sigmoid, factors of at most 0.25 shrink gradients toward zero, so early layers barely learn (vanishing gradient). Large weights can make gradients grow out of control (exploding gradient).
Remedies include ReLU, careful weight initialization, batch normalization, residual connections and gradient clipping.
Regularization in deep learning
| Method | How it reduces overfitting |
|---|---|
| L1 and L2 (weight decay) | Penalize large weights in the loss |
| Dropout | Randomly zeroes neurons during training, so the network cannot rely on any single one |
| Early stopping | Stops training when validation loss stops improving |
| Data augmentation | Creates new training samples, such as flipped or cropped images |
Dropout is active only in training. Calling model.eval() turns it off for inference.
Training in PyTorch
import torch
from torch import nn
model = nn.Sequential(nn.Linear(784, 128), nn.ReLU(), nn.Linear(128, 10))
loss_fn = nn.CrossEntropyLoss() # takes raw scores (logits)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for x, y in loader: # loader is a DataLoader
optimizer.zero_grad() # clear old gradients
loss = loss_fn(model(x), y) # forward pass and loss
loss.backward() # backpropagation
optimizer.step() # update weights
A worked example
Build AND with one perceptron: w1 = 1, w2 = 1, b = -1.5 and a step function. Input (1, 1) gives 1 + 1 - 1.5 = 0.5, above 0, so output 1.
Input (1, 0) gives -0.5 and input (0, 0) gives -1.5, both output 0. No single choice of w1, w2 and b can do the same for XOR, because its positive points (0, 1) and (1, 0) cannot be split from (0, 0) and (1, 1) by one line.
How the exam asks about this
- "Why can a single-layer perceptron not solve XOR?" → XOR is not linearly separable, and a perceptron draws only one linear boundary.
- "Which activation function is usually used in the output layer for multi-class classification?" → Softmax.
- "Which technique randomly deactivates neurons during training to reduce overfitting?" → Dropout.
What to remember for the exam
- A perceptron is weighted sum plus bias through an activation; alone it solves only linearly separable problems.
- Nonlinear activations are what let deep networks learn complex functions.
- Sigmoid outputs 0 to 1 and causes vanishing gradients; ReLU is the default for hidden layers.
- Training loop: forward pass, loss, backpropagation (chain rule), optimizer step.
- Regularization: L1 and L2, dropout, early stopping, data augmentation.
Backpropagation computes gradients; it does not update weights. The optimizer, such as SGD or Adam, performs the update.
Common mistakes
Thinking more layers always help. Without remedies such as ReLU, good initialization or residual connections, deeper networks can train worse because of vanishing gradients.
Leaving dropout on at test time. Dropout is for training only. Switch to evaluation mode before inference so all neurons are used.
Forgetting to clear gradients. PyTorch accumulates gradients, so call optimizer.zero_grad() before each loss.backward() unless you mean to accumulate gradients over several batches.
Check yourself
How many parameters does a fully-connected layer with 100 inputs and 10 outputs have?
100 x 10 weights plus 10 biases, which is 1,010 parameters.
Why does sigmoid cause vanishing gradients?
Its derivative is at most 0.25 and near zero when saturated. Multiplying many such small factors through deep layers shrinks the gradient toward zero.
What does early stopping watch to decide when to stop?
The validation loss. Training stops when it stops improving, before the model overfits the training set.
Topic 1 of 3 in Basics of Deep Learning and Foundation Models
Test yourself on Basics of Deep Learning and Foundation Models
The full Huawei HCIA-AI H13-311 question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Huawei. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.