Deep Learning Notes - 5: Convolutional Networks and Probabilistic Graphical Models
Deep Learning Notes - 5, covering Convolutional Neural Networks (convolution, pooling, object detection, image segmentation, style transfer) and Probabilistic Graphical Models (Bayesian networks, conditional independence, d-separation). Corresponds to Chapters 10-11 of "Deep Learning: Fundamentals and Concepts".
Part 5/8 of the series ← Previous | Next | Glossary
It is recommended to read Parts 1-4 first, especially the neural network basics in Part 3 and backpropagation in Part 4. This part explains how CNNs process images, and how probabilistic graphical models represent relationships between variables.
Chapter 10 Convolutional Networks
Computer Vision
Traditional machine learning models usually assume the input data is “unstructured,” meaning the elements of the input vector are mutually independent. But much of real-world data has structural features, for example:
- In natural language, words appear in sequence, with dependencies between preceding and following words.
- In images, pixels are arranged in a two-dimensional grid, and neighboring pixels are highly correlated.
If you randomly permute the pixels of an image, it no longer looks like a natural image; likewise, independently and randomly generating each pixel can almost never produce a “decent” image. This shows that images have strong local correlation and spatial structural priors.
Encode this structural prior into the model, rather than letting the model learn it from scratch. (Recall: the fully connected networks in Part 3 do not exploit this structure.)
The Convolutional Neural Network (CNN) is an architectural solution that explicitly models the spatial structure of images through sparse connectivity and parameter sharing.
Image Data
- An image is a rectangular array made up of pixels.
- Each pixel has:
- Grayscale image: single channel (intensity)
- Color image: three channels (RGB, red, green, blue)
- Pixel values are non-negative, usually represented by 8-bit integers (range 0-255).
- Video can be viewed as a three-dimensional structure (time + height + width); medical images (such as MRI) are three-dimensional voxels.
Challenge: using a fully connected network directly on images leads to an explosion of parameters.
For example: a color image has 3 million dimensions (100010003=3000000) as input.
If the first layer has 1000 neurons, then just the first layer has weights, which is untrainable.
Convolutional Filters
Fully connected networks have far too many parameters to process images and simply cannot be trained. The idea of CNNs is straightforward: since images have spatial structure, “hard-code” this structure into the network architecture — use sparse connectivity (each neuron only looks at a small patch) and parameter sharing (the same filter scans the entire image) to drastically reduce parameters.
CNNs exploit four structural properties of images:
- Hierarchy: from edges → textures → parts → objects, abstracted layer by layer
- Locality: each neuron only looks at a local region (no need to see the whole image)
- Translation equivariance: a cat in the top-left corner and in the bottom-right corner should be detected by the same feature
- Invariance: the final classification should not change just because the cat moved
Feature Detectors
Consider a grayscale image (single channel).
- Each hidden-layer neuron only receives a small region of the image (called the receptive field) as input.
- For example, a receptive field corresponds to 9 inputs.
- The neuron’s output can be written as: where:
- : the pixel vector within the receptive field.
- : the weight vector, which can be viewed as a matrix (called a convolution kernel or filter).
- : the bias term.
- : the activation function, defined as .
The neuron is detecting image patches similar to the pattern of w. The more similar the input x is to w (the larger the dot product), the stronger the output response.

a is the receptive field, which receives a 3*3 pixel input, forming the receptive field of a hidden unit. b is the weight values associated with the hidden unit, called the convolution kernel.
Translation Equivariance
The same feature (e.g., an eye) may appear anywhere in the image. We want the same filter to detect that feature at all positions.
Solution: share weights across the entire image, i.e., slide the same convolution kernel over the image. This operation is called convolution. In practice, the “convolution” in deep learning is usually cross-correlation (computing the dot product of the filter with the input at each position).
Intuition: the convolution operation is like holding a magnifying glass and scanning the image row by row. The magnifying glass (the convolution kernel) only looks at a small region (the receptive field) at a time, then computes a single value. When the magnifying glass moves to the edge of the image, it “sees” the edge pattern and the output becomes large; when it moves to a flat region, the output is small. The same magnifying glass scans from the top-left to the bottom-right corner, finding all similar patterns across the whole image — this is the power of parameter sharing.
2D convolution formula (ignoring the activation function) where:
- : the pixel value of the input image at position .
- : the weight of the convolution kernel at relative position .
- : the value at position of the output feature map (the output of the convolution operation).

Left: 1D convolution (connections of the same color share the same weights). Right: 2D convolution.
Padding
After convolution, the feature map size shrinks. For example: a J×K image convolved with an M×M kernel produces an output of (J−M+1)×(K−M+1).
Add “padding” pixels at the edges of the image.
- Valid convolution: no padding (P=0)
- Same convolution: padding so that the output size equals the input size. When M is odd, P = (M-1)/2 The padding value is usually set to 0 (but it is recommended to mean-normalize the image first, so that 0 is close to the average pixel value).

Padding
Strided Convolution
Use a stride , i.e., the filter moves pixels each time.
The output feature map size is controlled to be:
- J×K: the size of the input image (height × width)
- P: the padding size
- M: the convolution kernel size (assumed to be a square M×M)
- S: the stride
- : the floor function
Example:
- Input: 7×7 image, kernel: 3×3, stride: 2, padding: 1
- Output size: , so the output is 4×4
Multi-channel Convolution
The input image has multiple channels (e.g., RGB three channels), so the convolution kernel must also be three-dimensional: , where is the number of input channels.
Use an filter for each channel, then sum the results.
Input image: red, green, blue
Convolution kernel: red weights, green weights, blue weights all 3×3 = a three-dimensional kernel of 3×3×3
Extension: multiple output channels (i.e., multiple filters) Use a filter tensor of . Each output channel corresponds to an independent filter. Total number of parameters: (+1 is the bias, one per output channel)

Receives input from the three RGB channels; the kernel has 27 weights (+1 bias parameter not shown).
1×1 convolution: the filter size is 1×1×C.
Effect: Change the number of channels (up-sampling or down-sampling dimensions) Introduce non-linearity (together with an activation function) Small computation cost, often used in network bottleneck layers
Reduce the number of channels to lower the computational burden, or fuse cross-channel information.
Pooling
Pooling is a down-sampling operation (reducing data resolution) in convolutional neural networks.
Intuition: pooling is like shrinking a large photo into a thumbnail. The thumbnail loses some detail, but the main features (e.g., “there is a cat here”) are still discernible. Max pooling is equivalent to “keeping only the most prominent feature,” and average pooling is equivalent to “taking the average impression of the region.” The shrunk image takes up less space and is processed faster.
Purpose: introduce translation invariance (small shifts do not affect classification). Further down-sample and reduce the feature map size. Reduce parameters and prevent overfitting (the model memorizing the training data too well).
It can be divided into max pooling (taking the maximum within the receptive field) and average pooling (taking the average).
- Pooling has no parameters; it is a fixed function.
- Usually a 2×2 window with a stride of 2 is used.
- Each channel is pooled independently.

Max pooling
Multi-layer Convolution
A CNN is usually composed of multiple stacked “convolution + activation + pooling” layers.
Structure flow:
Input Image→ Conv Layer (convolutional layer: uses multiple filters to perform convolution on the input and extract features)→ Activation (activation function: usually applies a ReLU activation to non-linearly transform the convolution result)→ Pooling (pooling layer: down-samples via max pooling or average pooling to reduce data dimensions)→ Conv Layer (repeating structure: multiple convolutional layers, activation functions, and pooling layers stacked alternately)→ Activation→ Pooling→ ...→ Fully Connected Layers (fully connected layer: uses fully connected layers at the end of the network to integrate features)→ Output- Each layer extracts more abstract features (edges → textures → parts → objects)
- Parameter sharing and sparse connectivity drastically reduce the number of parameters
- The hierarchical structure enables the model to learn complex patterns
Advantages:
- Few parameters, easy to train
- Possesses translation equivariance and local invariance
- Can handle inputs of arbitrary size (in principle)
Through the combination of multiple layers of convolution, activation, and pooling, a CNN builds a hierarchical feature extraction system. This design not only drastically reduces the number of parameters, but also enables the network to progressively extract from low-level features to high-level semantic features.
Visualizing a Trained CNN
Understanding “what a CNN has learned” is an important question. We can explore the feature representations of different layers through visualization techniques.
Filters
An intuitive method: find the image patches in the validation set that maximize a neuron’s activation value.
- Layer 1: usually responds to edges, corners, color blobs (similar to Gabor filters). Corresponds to features detected by active neurons in the mammalian visual cortex
- Layer 2: responds to textures, simple geometric shapes.
- Layer 3: object parts start to appear (e.g., wheels, eyes, bird beaks).
- Layer 4: combinations of complex parts or local object features
- Layer 5 (high level): responds to complete objects (e.g., faces, dogs, cars).

Trained filters
A more powerful method is to directly optimize the input image so as to maximize the activation of a certain neuron.
- Optimization objective: maximize the pre-activation value of some hidden unit or output unit.
- Why use pre-activation? To avoid cross-class interference introduced by softmax normalization. - Related to adversarial training (later chapters)
For example, maximizing the pre-activation value of the “dog” class output can generate an image that is “most dog-like.”

Maximize class probability to generate image
Saliency Maps
A saliency map is a visualization technique used to identify the regions in the input image that have the greatest influence on the network’s final classification decision:
- Determine the importance of each pixel by computing the gradient of the loss function with respect to the input pixels.
- A pixel with a large absolute gradient indicates that a tiny change will have a large impact on the classification result, and is therefore “salient.”
Gradient-weighted Class Activation Mapping (Grad-CAM):
- Select the target class : for example, we want to see why the network thinks this image is a “dog.”
- Forward pass: compute the network output, obtaining the pre-activation value of the target class (dog).
- Backpropagate the gradient: compute the gradient of with respect to the pre-activation value of each neuron in the last convolutional layer.
- Compute the weights : for each channel , compute the global average of its gradients. where:
- is the total number of neurons in channel .
- can be understood as the “importance” of channel for the target class .
- Generate the heatmap: weight and sum the feature maps of the last convolutional layer by . is the saliency heatmap, with the same size as the output of the last convolutional layer (e.g., 14×14).
- Upsample and overlay: upsample (resize) to the size of the original image, and overlay it on the original image, using color intensity to represent saliency.

Saliency map
Adversarial Attacks
Adversarial attacks reveal a surprising weakness of deep neural networks: carefully designed, imperceptible perturbations that the human eye cannot detect can cause the network to make completely wrong classifications.
The Fast Gradient Sign Method is a simple yet effective way to generate adversarial examples:
Principle:
- Compute the gradient: compute the gradient of the loss function with respect to the input image , (the gradient computation relies on the backpropagation algorithm).
- : the original image
- : the true label
- : e.g., negative log-likelihood
- Generate the perturbation: the direction of the perturbation is the same as the gradient direction, with the goal of maximizing the loss.
- : a very small scalar (e.g., 0.01) controlling the perturbation magnitude
- : takes the sign of the gradient (+1 or -1)
- Construct the adversarial example:
must be small enough that the difference between and is “imperceptible” to the human eye.
Why does it work?
The gradient direction is the direction of fastest increase of the loss function. Even though the network has strong generalization ability, its decision boundary can be very complex and “fragile” in high-dimensional space. A tiny, correctly-directed perturbation can push the input sample to the other side of the decision boundary.
This fragility does not stem from overfitting. Adversarial examples generated by one network can often also fool other networks with different structures.

Top: a panda is recognized as a gibbon. Bottom: a stop sign is recognized as a speed limit.
Synthetic Images
The DeepDream technique: DeepDream is a technique for generating artistic images that produces surreal, dream-like images by amplifying the patterns detected by the network at specific layers.
How it works:
- Select the target layer: choose some hidden layers of the network (usually middle layers).
- Forward pass: feed the input image into the network and compute the activation values of all neurons in that layer, .
- Set the backward gradient: during backpropagation, set the error signal of that layer to the activation values of that layer themselves.
- Backpropagate the gradient: backpropagate the gradient into the input pixel space, obtaining .
- Update the image: move the original image a small step along the gradient direction.
- Repeat: repeat steps 2-5 multiple times, and the effect becomes increasingly strong.
The whole process can be viewed as maximizing a function:
i.e., maximizing the sum of squares of the activation values of all neurons in the selected layer.

DeepDream example
Object Detection
The object detection task not only classifies objects in an image, but also localizes them (usually with bounding boxes).
Bounding Boxes
- Representation: use a rectangular box to locate an object, usually defined by four parameters: center coordinates
(bx, by)and width/height(bW, bH). These values are usually normalized to the [0, 1] interval. - Network output: on top of a standard classification network, add 4 output nodes to regress the four coordinates of the bounding box.
- Loss function:
- Classification loss: cross-entropy is usually used.
- Localization loss: sum-of-squares error is used to measure the discrepancy between the predicted box and the ground-truth box.
Intersection over Union (IoU)
IoU is the core metric for evaluating the localization accuracy of an object detection model.
- Definition: the area of intersection of the predicted bounding box and the ground-truth bounding box divided by the area of their union.
- Range: [0, 1]. The larger the value, the higher the overlap between the two boxes, and the more accurate the localization.
- Criterion: a prediction with IoU ≥ 0.5 is usually considered a “correct detection.”

Green on the left is larger than green on the right.
Sliding Window
The naive sliding window method is extremely inefficient, because a complete network forward pass must be performed independently for each window position, and the inputs of adjacent windows are highly overlapping, causing a lot of redundant computation in the convolutional layers.
Fully convolutional idea: feed the entire large image into a Fully Convolutional Network (FCN) at once.
- Principle: a convolution operation is essentially the weight-sharing version of a sliding window. When the input image becomes larger, the convolutional layer naturally applies the filter at all possible positions, outputting a feature map where each position corresponds to the response of a receptive field in the original image.
- Result: a single forward pass yields the classification results for all window positions, greatly improving efficiency.

Top vs. bottom comparison: the only extra computation is the blue part.
Image Segmentation
Image segmentation is a finer task than object detection: assign a category label to every pixel in the image.
Convolutional Segmentation
Naive Method
- Take a local window centered on each pixel as input.
- Feed it into a CNN to classify that pixel.
- Repeat this for all pixels.
A lot of redundant computation, extremely inefficient.
Improved Method: Fully Convolutional Network (FCN)
- Replace the fully connected layers of a traditional CNN with convolutional layers.
- The entire network consists of convolution, pooling, and activation.
- Input an arbitrary-size image → output a segmentation map of the same spatial size.
A single forward pass yields predictions for all pixels, which is efficient.
Upsampling
Pooling reduces the resolution of the feature map, losing spatial information.
Use up-sampling or transposed convolution to restore the low-resolution feature map to the original image size.

Left: corresponds to average pooling. Right: corresponds to max pooling (the max position can also be recorded and restored).
Transposed Convolution
Can “enlarge” a small feature map into a large feature map, and can be viewed as the “reverse” process of convolution.

Transposed convolution (overlapping parts can be summed or averaged).
Fully Convolutional Network (FCN)
- The FCN architecture does not use pooling; that is, FCN replaces fully connected layers with 1×1 convolutions, and all up- and down-sampling is done by convolutions.
- Advantages:
- Can handle arbitrary-size inputs
- Output is a segmentation map of the same size
- Parameter sharing, efficient
Role of 1×1 convolution: change the number of channels without changing the spatial size (often used to reduce the number of channels to the number of classes C).
U-Net Architecture
U-Net is a classic architecture for semantic segmentation.
- Encoder (downsampling path): a series of convolutions + pooling that extracts high-level semantic features, but reduces resolution.
- Decoder (upsampling path): a series of upsampling + convolution that progressively restores spatial resolution.
- Skip connections: directly “skip” the feature map of a certain encoder layer and concatenate it to the input of the corresponding decoder layer.
This “passes” the high-resolution detail information of low layers to high layers, compensating for the spatial information loss caused by pooling.

U-Net has a symmetric arrangement of downsampling and upsampling layers; the output of each downsampling layer is concatenated to the corresponding upsampling layer.
Style Transfer
Neural style transfer is an artistic image generation technique that combines the “content” of one image with the “style” of another.
Basic idea:
- Content image (C): provides the scene and objects (e.g., a photo).
- Style image (S): provides the artistic style (e.g., a Van Gogh painting).
- Generated image (G): a synthesized image whose content looks like C and style looks like S.
The total loss function (one term similar to the original content, one term similar to the original style):
Treat the generated image as a learnable parameter, initialized from the content image or from random noise, and minimize the total loss via gradient descent, so that the generated image looks like in high-level semantics, and like in texture, brushstrokes, and color distribution.
Select an intermediate convolutional layer (usually one that captures object contours), and compute the squared error of the feature activations of and at that layer (content loss):
where is the pre-activation value of the input image at position and channel .
- Content image C activations at layer 10: [0.8, 0.2, 0.9, …]
- Generated image G activations at layer 10: [0.7, 0.3, 0.8, …]
- Content loss = (0.8-0.7)² + (0.2-0.3)² + (0.9-0.8)² + …
To ensure that the generated image is similar in style to the style image , the correlation between different feature channels must be maintained, using the style matrix:
For the selected convolutional layer, compute its style matrix :
measures the spatial co-occurrence (correlation) degree of features of channels and .
The style loss is the squared error between the style matrices of and :
Usually a weighted sum of the style losses of multiple layers is used, to capture style features at different scales:
Exercise 1
A color image, convolved with a kernel (stride=1, no padding), what is the output size? What if the stride is changed to 2?
Remember the formula: output = (input - kernel) / stride + 1
stride=1: , output . Each convolution “shrinks” the image by one ring.
stride=2: , floored to . A larger stride shrinks more aggressively.
If we instead use a kernel, stride=1, padding=1 (pad a ring of zeros around), what is the output size? What is padding for?
, the same size as the input. Padding is “not letting you shrink” — when you stack many layers in a deep network, if every layer shrinks, the map disappears quickly. Padding keeps the feature map size, making stacking convenient.
Exercise 2
Adversarial attacks (FGSM) add a tiny bit of noise to an image that the human eye cannot see, yet the CNN is fooled. What does this show?
It shows that CNNs and the human eye “see” in completely different ways. The human eye looks at semantics (shape, texture, object parts), while the CNN looks at pixel-level statistical features (gradients, frequencies). Those tiny perturbations that the human eye ignores happen to make the CNN’s feature representation fail — moving a little along the gradient direction crosses the decision boundary.
Simply put, the “features” learned by a CNN are not the same thing as the “semantics” understood by humans.
Chapter 10 Summary
One-sentence version: A CNN scans an image with convolution kernels (sparse connectivity + parameter sharing) to extract hierarchical features from edges to objects, uses pooling to reduce dimensions, then uses fully connected layers for classification; it can also be visualized, be fooled (adversarial attacks), and can perform detection, segmentation, and style transfer.
Knowledge map:
- Why CNNs are needed: images have spatial structure; fully connected networks have an explosion of parameters
- Core operations: convolution (feature extraction) -> activation (ReLU) -> pooling (dimension reduction) -> multi-layer stacking
- Key concepts: receptive field, convolution kernel/filter, padding, stride, parameter sharing, translation equivariance
- What CNNs can do: classification, object detection (bounding box + IoU), image segmentation (FCN + U-Net), style transfer
- Understanding CNNs: filter visualization, saliency maps, Grad-CAM
- CNN’s weakness: adversarial attacks (tiny perturbations cause misclassification)
Want to know how gradients in a CNN are computed? Please review backpropagation in Part 4. Want to see how CNNs are replaced by Transformers in modern architectures? Please continue to Part 6.
Chapter 11 Structured Distributions
Probabilistic Graphical Models
The core idea of Probabilistic Graphical Models (PGM) is intuitive: use a connection graph to represent the causal and dependency relationships between variables. Just as a circuit diagram in engineering uses boxes and connections to represent the wiring between components, a probabilistic graphical model uses nodes (boxes) to represent random variables and arrows (connections) to represent “who influences whom.” With a glance at the graph, you can tell which variables are dependent and which are independent — without any mathematical derivation.
All probability operations are essentially built on two basic rules:
- Sum rule: (marginalize to get the marginal distribution)
- Product rule: (get the joint distribution)
Although we could handle complex probabilistic models using algebra alone, using a graphical representation has huge advantages:
- Visualize structure: intuitively show the dependencies of a probabilistic model, helping to design and understand new models.
- Reveal properties: by observing the structure of the graph, one can infer the conditional independence between variables.
- Simplify computation: complex inference and learning algorithms (such as message passing) can be expressed as operations on the graph, making the underlying mathematics clearer.
Note: After this point, neural network graphs are shown in blue, while probabilistic graphical models are shown in red, to avoid confusion.
Directed Graphs
A directed graphical model (also called a Bayesian network or Bayes net) is a type of probabilistic graphical model in which the edges (links) carry arrows, indicating dependencies or causal relationships between variables. Its basic elements are:
- Nodes (Vertices): represent random variables (e.g., , , ).
- Directed edges (Links): an arrow points from one variable to another, indicating that the former is the “cause” or “parent” of the latter.
- Parent and child nodes: if there is an arrow from to , then is the parent of , and is the child of .
It should be noted that although the edges in a Bayesian network have directions, this does not always mean causality. The direction of an edge more represents the dependency relationship between variables and the order of factorization.
Factorization
Consider the joint distribution of three variables , , .
By the product rule, it can be factorized:
Graphical Representation
We can draw the above factorization as a directed graph:
- Create a node for each variable , , .
- Add a directed edge for each conditional probability factor:
- : draw arrows from and pointing to .
- : draw an arrow from pointing to .
- : no condition, so no edge points to .

Factorization graph
Fully connected graph: for K variables, the most general factorization is: The corresponding graph is a fully connected graph: each node receives edges from all nodes with smaller indices.
- This factorization applies to any joint distribution.
- But such a graph provides no “interesting” information, because it assumes all variables may depend on each other.
The truly valuable information comes from the missing edges. Because the presence of an edge represents a direct dependency (given the parents). And therefore the absence of an edge represents a conditional independence.
For a directed graph with K nodes, the general factorization rule for its joint probability distribution is:
where:
- denotes the parent set of node (parents of k).
- The product is taken over all nodes in the graph.
- As long as each conditional probability is normalized (i.e., integrates to 1 over ), then the whole product is automatically normalized.
Discrete Variables
When the nodes in a graphical model represent discrete variables, each variable has possible states (e.g., categories).
The distribution of a -state discrete variable is defined by a parameter vector , satisfying . Its probability mass function is:
where is the one-hot encoding of , i.e., only one and the rest are 0. Due to the normalization constraint, only parameters need to be specified.
Consider two discrete variables and , each with states.
- Full joint distribution: is defined by values . Since , the number of independent parameters is .
- Chain structure: using the product rule, .
- needs parameters.
- needs parameters (because for each of the values of , is a -state distribution needing parameters).
- Total parameters: , the same as the full joint distribution.
A fully connected directed graph (e.g., an arrow from to ) can represent any joint distribution, because it imposes no restriction.
Relationship Between Number of Parameters and Graph Structure
- Fully connected graph (all possible edges exist): can represent the most general joint distribution, but the number of parameters is , growing exponentially with the number of variables .
- Unconnected graph (independent variables): the joint distribution is . Each needs parameters, so the total number of parameters is , growing linearly with .
- Partially connected graph: strikes a balance between fully general and fully independent. For example, the chain structure:
- : parameters.
- Each : parameters.
- Total parameters: , growing linearly with the chain length (rather than exponentially), but able to represent more complex dependencies than the independent model.

Partially linked graph (chain structure) reduces parameters
A further way to reduce parameters is parameter sharing.
In a chain structure, we can assume that all the conditional distributions () are controlled by the same set of parameters.
- Effect: the total number of parameters becomes , independent of .
- Limitation: this means all transitions (from to ) follow the same “rule.”
- Application: this is common in Hidden Markov Models (HMM) and Recurrent Neural Networks (RNN).
Similar to the figure above, but the topmost parameter can be shared by multiple nodes in the graphical model.
Gaussian Variables
Conditional distributions:
Consider an arbitrary directed acyclic graph with variables, where node represents a single continuous random variable with a Gaussian distribution. The mean of this distribution is taken as a linear combination of the states of the parent nodes :
where:
- and are the parameters controlling the mean,
- is the variance of the conditional distribution .
Joint distribution:
The log of the joint distribution is the product of the logs of all node conditional distributions:
Substituting the above gives:
where , and ‘const’ denotes terms independent of .
This expression is a quadratic function of the components of , so the joint distribution is a multivariate Gaussian distribution.
The mean of each variable satisfies the following recurrence relation:
Assuming the node indices are such that each node has a higher index than its parents, one can start from the smallest-indexed node and recursively compute each component of .
The covariance matrix elements of the joint distribution satisfy the recurrence relation:
where is an indicator function (1 when , 0 otherwise). The covariance can also be computed recursively starting from the smallest-indexed node.
Consider a joint Gaussian distribution whose covariance matrix is partially constrained:
Mean:
Covariance matrix:
Similarly, the linear-Gaussian graphical model can be extended to the case where nodes represent multivariate Gaussian variables. The conditional distribution of node is:
where is a matrix (non-square if and have different dimensions). It is likewise easy to verify that the joint distribution of all variables is still Gaussian.
Model Representation
In practical applications, when we use graphical models for machine learning problems, we usually set some random variables to specific observed values. For example, in a linear regression model, the random variable would be set to the concrete values in the training set. In a graphical model, we represent these observed variables by shading the corresponding nodes.
In addition, model parameters (such as the weight ) are usually treated as deterministic parameters, represented by floating variables. Unobserved random variables (such as latent or hidden variables) are represented by open red circles.
Ultimately, we have three kinds of variables:
- Unobserved random variables (latent variables), represented by open red circles.
- Observed random variables, represented by blue-shaded red circles.
- Non-random parameters, represented by floating variables.
This representation helps clearly describe the structure of the model and the relationships between variables, providing a basis for subsequent inference and learning.

Example graph
- Red circle : represents the model’s parameter (weight), i.e., a non-random variable. In the graph it is represented by a “floating variable,” indicating it is a learnable parameter rather than a random variable.
- Blue rectangle : indicates there are identical nodes.
- Red circle : shaded blue, indicating this is an observed variable (i.e., the true label in the training data). In probabilistic graphical models, observed variables are represented by shaded nodes.
- : floating variables, deterministic parameters.
Conditional Independence
Consider three random variables , , and . If, conditioned on , the conditional distribution of does not depend on the value of , i.e.:
then we say is conditionally independent of given (conditional independence).
This can also be expressed through the joint distribution. Using the product rule of probability:
Substituting conditional independence into the above gives:
This shows that, conditioned on , the joint distribution of and can be factorized into the product of their respective conditional distributions, i.e., and are statistically independent given .
For concise notation, we use the following symbol:
to indicate that is conditionally independent of given .
Note: conditional independence must hold for all possible values of , not just some specific values.
Three Examples
Directly verifying conditional independence through probability calculations is very time-consuming. The advantage of graphical models is that we can directly judge from the graph structure which variables are conditionally independent. This method is called d-separation (d stands for “directed”).
Understand it through three simple three-node graph examples:
- Graph structure: a → c ← b The corresponding joint distribution is:
- When no variable is observed, marginalizing over : This generally cannot be factorized as , so and are not independent, written as .
- When is observed (i.e., conditioning on ), the joint distribution becomes: At this point and are independent given , i.e., .
- Node in the path is tail-to-tail (arrow tails connected). When is not observed, the path is “open” (information can pass along this path, so there is a dependency between the variables), and and are dependent; when is observed (conditioned on), the path is “blocked,” and and become conditionally independent.
When c is not observed: the path is “open,” and a and b are associated through the common “effect” c.
- Example: a=rain, b=sprinkler, c=ground wet: although rain and sprinkler are unrelated, they can both cause the ground to be wet, so through the common result “ground wet,” a and b become correlated.
After observing c: the path is “blocked”; once the state of c is known, a and b become independent.
- Example: if we already know the ground is wet, then rain and sprinkler are independent.
- Graph structure: a → c → b The corresponding joint distribution:
- When no variable is observed, and are not independent (there is a path from to ).
- When is observed, using Bayes’ theorem: Therefore .
- Node in the path is head-to-tail (one arrow head, one arrow tail). When not observed the path is “open,” and when observed the path is “blocked.”
- Graph structure: a → c ← b The corresponding joint distribution:
- When no variable is observed, marginalizing over : So and are independent, i.e., .
- When is observed: This expression generally cannot be factorized as , so .
- Node in the path is head-to-head (arrow heads connected), also called a collider . When not observed, the path is “blocked,” and and are independent; once is observed, the path is “opened,” and and become dependent.
Not only itself, but if any descendant node of is observed, the path is also “opened.”
Take medical diagnosis as an example, considering the variables: Symptom (S), Disease (D), and Test (T).
- For the structure S→D←T: without knowing the disease, the symptom and the test result are correlated; but once the disease diagnosis is known, the symptom and the test result become independent.
- For the structure S→D→T: without knowing the disease, the symptom and the test result are correlated; once the disease is known, the two become independent.
- For the structure S→D←T: without knowing any information, the symptom and the test result are independent; but once the disease is known, the two become correlated.
d-separation
Judge whether variables are conditionally independent by checking whether the path is “open.”
Given a Directed Acyclic Graph (DAG), determine whether set is d-separated from set given set :
- Consider all paths from any node in to any node in .
- A path is “blocked” if and only if there is a node on the path that satisfies any of the following conditions:
- The node is tail-to-tail or head-to-tail, and this node is in set (i.e., observed).
- The node is head-to-head, and this node together with all its descendant nodes are not in set .
- If all paths are blocked, then is d-separated from by , i.e., holds.

d-separation
- (a) The path from a to b is neither blocked by f (tail-to-tail and unobserved) nor by e (head-to-head but with only one observed descendant node), so cannot be concluded.
- (b) The path from a to b is blocked by f (tail-to-tail and observed), so , and also blocked by e (head-to-head and neither it nor its descendants are observed).
Explaining Away
The head-to-head structure leads to an interesting phenomenon called “explaining away.”
Example: consider a car fuel system containing three binary variables:
- : battery state (1=charged, 0=dead)
- : fuel tank state (1=full, 0=empty)
- : fuel gauge reading (1=full, 0=empty)

Fuel tank example
Assume and are independent, and depends on and (the gauge may read empty because the battery is dead or the tank is empty).
- Prior:
- After observing (gauge empty), compute the posterior:
The probability that the tank is empty rises (from 0.1 to 0.257).
- After further observing (battery dead):
The probability that the tank is empty drops.
Explanation: observing that the battery is dead () provides an “explanation” for the gauge being empty (), thereby “eliminating” the need for the empty tank () as an explanation. This shows that and , which were originally independent, become dependent after observing their common “effect” . This is the typical behavior of a head-to-head structure.
Naive Bayes
Naive Bayes is a classification model based on the conditional independence assumption.
Assume the input vector , and the class is . Its core assumption is: conditioned on the class , the features are mutually independent, i.e.:
The class node points to all feature nodes . Since is a tail-to-tail node, once conditioned on , all paths between any and are blocked, so they are conditionally independent.
For classification, use Bayes’ theorem:
where .
Note: although the conditional independence assumption is strong (in reality features are often correlated), Naive Bayes performs well on tasks such as text classification, because the decision boundary is insensitive to the details of the class-conditional densities.
Generative Models
Generative models attempt to learn the data generation process. An example of image generation:
- Class, position, and scale are independently sampled from a prior distribution.
- The image is generated from a conditional distribution that depends on class, position, and scale.
- When the image is unobserved, class, position, and scale are mutually independent (the path is head-to-head and the image is unobserved, so the path is blocked).
- After the image is observed, these variables become dependent (the path is opened). For example, knowing the object’s class helps infer its position.
Generative models can generate new samples, whereas discriminative models (such as directly trained classifiers) usually cannot.
Markov Blanket
For a node in a graph, its Markov blanket is the smallest set of nodes such that is conditionally independent of all the remaining nodes in the graph given that set.
Specifically, the Markov blanket of contains:
- Parent nodes
- Child nodes
- Other parent nodes of the child nodes
In a head-to-head structure, observing the child node opens the path between its parent nodes (explaining away). Therefore, to “isolate” , one must observe both its child nodes and the other parent nodes of those children.
The Markov blanket gives the set of variables that are actually depended upon when computing , greatly simplifying inference.

Markov blanket
Graphs as Filters
A graphical model can be understood from two equivalent perspectives:
- Factorization perspective: the graph defines that the joint distribution must factorize into a product of conditional probabilities of a specific form.
- Independence perspective: the graph defines, through d-separation, a set of conditional independencies that must be satisfied.
The d-separation theorem guarantees that these two perspectives are equivalent: a distribution passes the “factorization filter” if and only if it passes the “independence filter.”
We can view the graphical model (here a directed graphical model) as a kind of filter, where a probability distribution p(x) can only pass through the filter if it satisfies the directed factorization property. Let DF denote the set of all possible probability distributions p(x) that pass through the filter. We can also use the graph (as a second filter) to filter distributions based on whether they satisfy all the conditional independencies implied by the graph’s d-separation property. The d-separation theorem states that the same set of distributions DF will be allowed through the second filter.
Sequence Models
In many machine learning applications, data appears in the form of sequences. For example:
- Text is a sequence of words
- Proteins are sequences of amino acids
- Audio signals are sampled sequences over time
- Daily rainfall is a series of daily measurements
Although some sequences are not strictly time series, the terms “time,” “past,” and “future” are usually borrowed to describe the ordering relationships.
We use to denote a sequence of length , where each is a vector (which can be a scalar or a multi-dimensional vector).
Sometimes we independently draw multiple such sequences from the same distribution. In that case, the joint distribution of all sequences factorizes into the product of the individual sequence distributions. In this section we mainly focus on modeling a single sequence.
General Autoregressive Models
The core idea of an autoregressive model is: use the variable’s own past values to predict the current value. By the product rule of probability, the joint distribution of any variables can be written as a product of a series of conditional distributions. If we order the variables according to the natural order of the sequence, then:

General autoregressive model, each node receives a connection from the preceding node in the sequence.
This representation is completely general, so it brings no advantage in terms of modeling, because it introduces no assumption or simplification.
To simplify the model, we can introduce conditional independence assumptions, by removing edges from the graph, or equivalently, by deleting some variables from the conditional variables on the right-hand side of formula (11.42).
The most extreme simplification is to remove all conditional variables, yielding:
This means all variables are mutually independent, completely ignoring the order information.
A more reasonable assumption is: each variable depends only on the one preceding it (first-order Markov model). In this case the joint distribution is:
![]()
First-order Markov chain
Using the d-separation criterion, it can be verified that:
This means that to predict the next observation, only the previous observation needs to be known, independent of the earlier history.
We can extend the model so that each variable depends on the previous two variables, yielding the second-order Markov model:

Second-order Markov
In general, the conditional distribution of an M-th order Markov chain depends on the previous M variables.
Number of Parameters Issue
Assume the observed variables are discrete, with states.
- First-order Markov model: the conditional distribution has parameters.
- M-th order Markov model: the conditional distribution has parameters.
The number of parameters grows exponentially with , so higher-order models become impractical for large .
- First-order model needs 10×(10-1) = 90 parameters
- Second-order model needs 10×10×(10-1) = 900 parameters
- Third-order model needs 10³×(10-1) = 9000 parameters
Latent Variable Models
To build a sequence model that is not limited by the Markov order but still has a bounded number of parameters, one can introduce latent variables (variables that cannot be directly observed but influence the observations).
For each observation , introduce a corresponding latent variable (whose type or dimension may differ from ). Assuming these latent variables form a Markov chain yields a state-space model.

Latent variable model
- The latent variables form a Markov chain, meaning that given the current state , the future state is independent of the past state.
- Each observation depends only on its corresponding latent variable , not directly on other observations or latent variables.
The key conditional independence is:
The joint distribution is:
Using d-separation, one can find that any two observation variables and are connected through the latent variable path, and this path is never blocked. Therefore, the predictive distribution:
has no conditional independence, i.e., the future prediction depends on all past observations.
In other words, the observation variables themselves do not satisfy any order of the Markov property.
Exercise 3
A Bayesian network: weather → road condition → whether it slips . If you already know the road is wet (i.e., is observed), is there still any relationship between weather and slipping ?
No relationship — they are conditionally independent. This is a head-to-tail structure; once the middle node is observed, the path is “broken.”
To put it another way: you already know the road is wet, so “whether it rained” provides no extra information about “whether you will slip” — the road being wet already explains everything.
If we change the graph to (two causes pointing to the same effect), what happens after observing ?
This is a head-to-head (collider) structure, and it is interesting: when is not observed, and are independent, but once is observed they become dependent instead.
This is “explaining away”: given that the road is slippery, if you find the road is wet (), then you think it is less likely to have rained () — because “road wet” is already enough to explain the slipping, and there is no need to look for another cause.
Chapter 11 Summary
One-sentence version: Probabilistic graphical models draw a dependency graph between variables using nodes and arrows, and through the d-separation rule one can directly tell from the graph which variables are conditionally independent, without complex probability calculations.
Knowledge map:
- Core idea: use a graph to represent the structure of a probability distribution (two equivalent perspectives: factorization + independence)
- Directed graphical model (Bayesian network): joint distribution = product of each node’s conditional probability; absence of an edge = conditional independence
- Key concepts: parent/child nodes, conditional independence, d-separation (tail-to-tail / head-to-tail / head-to-head)
- head-to-head (collider): blocks the path when unobserved, opens the path when observed (explaining away)
- Application models: Naive Bayes (classification), generative models, sequence models
- Sequence modeling: autoregressive -> Markov model (exponential growth of parameters) -> latent variable model (bounded parameters + long-term dependencies)
The “factorization” idea in probabilistic graphical models has something in common with the hierarchical structure of neural networks in Part 3 — both use structured design to reduce parameters and improve efficiency. And the latent variable idea in sequence models will reappear in the form of “attention mechanisms” in the Transformer of Part 6.