Introduction
Many of the common problems in machine learning are the classification of independent data points. For example, given an image, predict whether it contains a cat or a dog, or an image that gives the character a shot, predicting which number is 0 to 9. However, it turns out that many of the problems are not appropriate for the framework. For example, given a sentence, "I like machine learning," which marks each word with its parts of speech (nouns, pronouns, verbs, adjectives, etc.), this task cannot be solved by independently processing each word, since "learning" can be a noun or a verb. This task is more important for more complex text processing, such as translating from one language to another, text translation, and so on.
How to use the standard classification model to address these issues is a challenge. A powerful framework for this type of problem is the probabilistic graphical model (probabilistic graphical models, PGM). In this article, I'll cover some of the basic elements of the framework.
Before we discuss how to apply probability graph models to machine learning problems, we need to understand the PGM framework. The probability graph model (or short-name graph model) consists of a graphical structure, each node of the graph is associated with a random variable, and the edges in the graph are used to explain the relationship between random variables.
Depending on whether the graph is forward or not, we can divide the graphics pattern into two categories:
- Bayesian networks
- Markov networks
Bayesian networks: Directional graphic models
A typical example of a Bayesian network is the so-called "Student Network", which looks like this:
This chart describes the settings for students enrolled in a university course. There are 5 random variables in the diagram:
difficulty (difficulty of the course): value 0 (Low difficulty) and 1 (
difficult)
Intelligence (Intelligence level): value 0 (Low) and 1 (high)
Grade (Student's class score): value 1 (good result), 2 (average score) and 3 (poor grades)
Sat (Sat test score): 0 (Low score) and 1 (high score)
Letter (The quality of the recommendation received from the Professor after completing the course): Take 0 (not good faith) and 1 (good faith)
the edges in the diagram contain the dependencies of the variables. the student's "achievement" depends on the "difficulty" of the course and the student's "intelligence". in turn, "score" determines whether a student has received a good recommendation from the professor. In addition, students ' "intelligence" also affects their "SAT" scores, in addition to influencing "results". It is worth noting that the direction of the arrows tells us causality: "Intelligence" affects "sat" scores, but "sat" scores do not affect "intelligence".
Finally, we can see the table associated with each node, these are called conditional probability distributions (conditional probability distributions, CPD).
Conditional probability distribution (CPD)
The CPD for "difficulty" and "Intelligence" is fairly straightforward, because these variables do not depend on any other variables. These tables give the probability of a variable, with a value of 0 or 1. You can notice that the value in each table must be a sum of 1.
For the "SAT" CPD, each row corresponds to the possible values of its parent node ("Intelligence"), and each column corresponds to the value that the "sat" can take. Each cell has a conditional probability $p (SAT = S | Intelligence = i) $, that is, assuming that the value of "Intelligence" is $i$, the value of "SAT" is the probability of $s$. For example, we can see $p (SAT =s^1| Intelligence =i^1) $ is 0.8, that is to say, if the student's intelligence is high, then the probability of high SAT score is also 0.8. On the other hand, $p (SAT =s^0| Intelligence =i^1) $, if the student has high intelligence, the probability of a low SAT score is 0.2.
The sum of the values in each row is 1. This makes sense, since the intelligence =i1,sat score can be s or s1, so two probabilities must be added up to 1. Similarly, the CPD conditional probability of "letter" $p (letter = L | Grade = g) $. Because "rank" can take three values, we have three rows in this table.
According to the above knowledge, the CPD of "Grade" is easy to understand. Because it has two parent nodes, the conditional probability will be the form of P $p (grade=g | Difficulty=d, Sat=s) $, in other words, "Grade" is the probability of G if the value of "difficulty" is "s" for "G" and "SAT".
The basic requirement of Bayesian networks is that the graph must be a direction-free graph (directed acyclic graph,dag).
Markov networks: A non-direction graphic model
For the sake of brevity, we look directly at abstract graphs, which represent the interactions between variables. We see that although a and b directly interact with each other, a and C do not.
Potential functions
Just as we have the CPD for Bayesian networks, we also use tables to integrate the relationships between nodes in a Markov network. However, there are two important differences between these tables and the CPD.
First, these values do not need a sum of 1, that is, the table does not define a probability distribution, it only tells us the possibility of having certain values. Second, there is no condition. It is proportional to the union distribution of the associated variable, not the conditional distribution in the CPD.
The result table is called a "factor" or "potential function" and is represented by the Greek symbol φ. For example, we can use the following function to capture the relationship between three variables, a and C, if A and B are different, C can be 1, and if A and B are the same, C can be 0:
In general, we define a potential function for each of the largest groups in the graph.
The graph Structure and table express the joint probability distribution on the random variable.
One of the problems we may encounter is, why do we need to have a map and a graph? The reason for this is that, for some problems, it is more natural to represent them as a graph, such as the student network above, where it is easy to describe the causal relationship between variables: Students ' intelligence affects SAT scores, but SAT scores do not affect students ' intelligence.
For other types of problems, the comparison, we want to represent each pixel as a node, and we know that neighboring pixels interact with each other, but there is no causal relationship between pixels; instead, they may be symmetric. Therefore, in this case, we use the non-direction graphical model.
Problem
So far, we've been talking about graphs and random variables and tables, and you might think about what this is all about? What do we want to do? Where is machine learning-data, training, forecasting, etc.? This section resolves these issues.
Let's go back to the student network. Assuming we have a graphical structure, we can create a graphical structure based on our understanding of the world (called "Domain Knowledge" in machine learning). But we don't have CPD tables, only their size. We do have some data: for the 10 different classes in the university, we can measure their difficulty.
In addition, we have data about each student in each course-their intelligence, what their SAT scores are, what grades they get, and whether they have received a good letter from the professor. Based on this data, we can estimate the parameters of the CPD. For example, data may indicate that students with higher intelligence usually get good SAT scores, and we may be able to learn from $p (Sat = s^1 | Intelligence = i^1) $ very high. This is the learning stage. We will soon see how we can estimate this parameter in Bayesian networks and Markov networks.
Now, for the new data points, you will observe some variables that are not complete. For example, in, you will understand the difficulty of the course and the student's SAT score, and you want to estimate the probability of a student getting good grades.
Although we do not provide the CPD directly to us, we can see that the student's high SAT scores indicate that the student may be smart, so if the difficulty is higher, the probability of good results is high. The process is very low, as shown in the Red Arrows in. We may also want to estimate the probabilities of multiple variables at the same time, such as the probability of a student getting good grades and what is the letter?
A variable with a known value is called a "watch variable", and a variable that does not observe a value is called a "hidden variable" or "latent variable." Traditionally, the observed variable is represented by a gray node, while a potential variable is represented by a white node, such as the image above. We may be interested in finding values for some or all of the potential variables.
Answering these questions is similar to predicting other areas of machine learning-in a graphical model, this process is called "reasoning."
Although we use Bayesian networks to describe the above terminology, it also applies to Markov networks. Before we go into learning and reasoning algorithms, let's formalize the idea we just saw-given the value of some nodes, can we get information on other nodes?
Conditional independence
So far, the graphical structure we've been talking about actually captures important information about variables. Specifically, they define a set of conditional independence between variables, the form of a statement-"If A is observed, B is independent of C". Let's take a look at some examples.
In the student network, it is assumed that students have high SAT scores. What do you think of her achievements? As we've seen before, high SAT scores indicate that students are smart, so you expect students to get a good grade. What if students have low SAT scores? In this case, you don't expect students to get a good grade.
Now, in addition to her SAT scores, you know that the students are smart. If the SAT score is high, you will expect a high score. What if the SAT scores are low? You still expect a good result because you know that the student is smart and you think she's just not doing well on the SAT. So, if we see students ' intelligence, we know that SAT scores don't tell us anything. As a conditional declaration of independence, we would say-"If you observe intelligence, then the SAT and the results are independent." ”
The way we connect these nodes obtains this conditional independent information. If they are connected in different ways, we will get different conditions for independent information.
Let's look at another example.
If you know that the student is very clever. What is your opinion on the difficulty of this course? It's simple, right? Now, what would you think if I told you that the students didn't do well in the course? This shows that the course is difficult because the smart students ' grades are not good. Therefore, we can give our terms independently stating the following-"If the rank is not observed, then the intellect and difficulty are independent." ”
Because this illustrates the independence of the two nodes that are constrained by the condition, they are called conditional independence. Note that the two examples have the opposite semantics:
- In the first example, if a connection node is observed, independence is established;
- In the second example, if a connection node is not observed, independence is established.
This difference is due to the way the nodes are connected, that is, the direction of the arrows.
In a Markov network, we can use a similar method, but because there is no direction edge (arrow), the conditional independent statement is relatively simple-if there is no path between nodes A and B, then all nodes on the path are not observed, then A and b are independent. In other words, if there is at least one path from a to B where all the intermediate nodes are not observed, A and B are not independent.
Application of Bayesian Networks: Monty Hall problem
The master showed you three closed doors, one behind which was a car, and the other was sheep behind the other doors. You can choose a door. The host then opens the remaining door and shows that it is not a car. Now you can choose to switch the door from the gate you originally selected to the door that the host does not open. Did you change it?
The moderator does not seem to have disclosed any information, and you may think that the odds of winning a car in exchange for a change are $1/2$, but it turns out that this intuition is not entirely true. Let's use the graph model to understand this.
Let's start by defining some variables:
- D: Door.
- F: Your first choice.
- H: The host opens the door.
- I:f = D?
D,f and H values of 1, 2 or 3,i take 0 or 1.D and I are not observed, F is observable. H is not observed until the host opens one of the doors. Therefore, we obtain the following Bayesian networks for our problems:
Note the direction of the arrows: D and F are independent, and the host chooses the door h also depends on D and F. So far, you know nothing about D. (This is similar to the structure in the student network, where knowing the student's wisdom does not tell you any information about the difficulty of the course.) )
Now the master picked up a door H and opened it. So, now we're looking at H.
Observing H did not tell us anything about me, or whether we chose the right door. But it does tell us something about d! (Again, with the student network analogy, if you know that the student is smart and the grades are low, it will tell you some information about the difficulty of the course.) )
Let's take a look at the numbers. The CPD table for the variable is as follows (this is the case where the variable is not observed).
The tables D and F are simple: We select one of the doors at the same probability.
I table: When D and F are the same, I = 1, and when D and f are different i = 0.
H table: If D and F are equal, the host chooses a door from the other two gates at the same probability, and if D and F are different, the host chooses the third door.
Now, let's assume that we have chosen a door, that is, now we see F, say F = 1. What are the conditional probabilities for a given f,i and d?
We can get:
So far, the possibility of our choosing the right door is $1/3$.
Now, the owner opens one of the doors except F, so we look at H. Suppose H = 2. Let's calculate the new conditional probabilities for the I and d of the given F and H.
Probability distribution:
So when we know nothing about it: our first choice is still the right probability $1/3$, and that's what our intuition tells us. However, we now know that the probability of a car behind Gate 3 is 2/3, not 1/3.
So if we switch, we get the probability of the car to be 2/3; If we do not do this, we will get the car with a probability of 1/3.
We can get the same answer without using a graphical model, but the graphical model gives us a framework that scales well to larger problems.
Summary
At this point, we study some basic terms in the graph model, including Bayesian networks, Markov networks, conditional probability distributions, potential functions and conditional independence. We also study the application of graph model of Monty Hall problem.
Below, I will introduce parameter estimation and inference, and give another application case.
Parameter estimation
Bayesian networks
Estimating the number of CPD tables in a Bayesian network is only equivalent to calculating the number of occurrences of the event in our training data. In other words, in order to estimate $p (SAT = s^1 | Intelligence = i^1) $, we only calculate the probability of $sat = s^1$ and $intelligence = i^1$ data points, these points are discrete and easy to calculate.
Markov networks
Unfortunately, for Markov networks, the above-mentioned counting methods are not statistically proven (and thus result in suboptimal parameters). Therefore, we need to use more sophisticated techniques. The basic idea behind most technologies is gradient descent: We define parameters that describe probability distributions, and then use gradient drops to find the values of these parameters to maximize the likelihood of observing the data.
Finally, since we have the parameters of the model, we want to use them on the new data for reasoning and validation!
Reasoning
Most of the literature in the probability graph model is focused on reasoning. There are two reasons for this:
- Be able to make predictions based on what we already know.
- Reasoning is hard to calculate! In some specific types of diagrams, we can perform reasoning fairly efficiently, but it is difficult to handle on a general diagram. Therefore, we need to use approximate algorithms to sacrifice accuracy to improve efficiency.
We can solve several problems by reasoning:
- Marginal inference (marginal inference): Finds the probability distribution of a specific variable. For example, given a graph with variables a,b,c and D, where A takes a value of 1, 2, and 3, $p (A = 1) $, $p (A = 2) $ and $p (A = 3) $.
- Posteriori inference (posterior inference): Given some observed variables $v_e$ (e stands for Evidence) value E, find some hidden variables v_h the posterior distribution $p (V_h | V_e = E) $.
- Maximum posteriori inference (Maximum-a-posteriori (MAP) inference): Given some observed variable $v_e$, its value E, looks for the highest probability $v_h$ value of other variables.
In the following, I will describe some popular algorithms for answering these questions, including exact algorithms and approximate algorithms. All of these algorithms are suitable for Bayesian networks and Markov networks.
Variable elimination
Using the definition of conditional probabilities, we can write a post-test distribution as:
Let's see how to use a simple example to calculate the numerator and denominator above. Considering a network with three variables, the federated distribution is defined as follows:
Suppose we want to calculate $p (A | B = 1) $, which means we want to calculate $p (A = 0 | B = 1) $ and $p (A = 1 | B = 1) The value of $, they should sum to 1. Using the equation above, we can write:
The numerator is the probability of $ A = 0$ and $b = 1$. We don't care about the value of C. So we add all the values of C, because $p (a = 0,b) = 1,c = 0) $ and $p (a = 0,b = 1,c = 1) $ are mutually exclusive events, so their set $p (a = 0,b = 1) $ is just the sum of each probability.
We add lines 3rd and 4th to get $p (A = 0,b = 1) = 0.15$. Similarly, add lines 7 and 8 give $p (A = 1,b = 1) = 0.40$. In addition, we can calculate the denominator by summing all rows that contain $b = 14$ (that is, rows 3,4,7 and 8) to get $p (B = 1) = 0.55$. We can calculate:
$ $p (A = 0 | B = 1) = 0.15/0.55 = 0.27$$
$ $p (A = 1 | B = 1) = 0.40/0.55 = 0.73$$
If you look at the calculations above, you will notice that we have done some repetitive calculations-adding lines 3rd and 4th, and 7th and 8th lines two times. The more efficient way to calculate $p (B = 1) $ is to simply calculate $p (A = 0,b = 1) $ and $p (a = 1,b = 1) $. This is the basic idea of variable elimination.
Usually, when you have a lot of variables, you can not only use the value of the numerator to calculate the denominator, but if you look closely, the molecule itself will contain repeated computations. You can use dynamic programming to efficiently use pre-computed values.
We sum up one variable at a time to eliminate it, so the process of finding multiple variables is equivalent to eliminating them one at a time. Therefore, the name "variable elimination".
It is also easy to extend the above process to solve marginal inference or map inference problems. Similarly, it is easy to generalize this idea to apply it to a Markov network.
The time complexity of variable elimination depends on the graphics structure and the order in which variables are eliminated. In the worst case, it is exponentially increasing in time complexity.
Faith Communication
We just saw that the VE algorithm only gives a final distribution. Suppose we want to find the marginal distribution of all the variables. We can do smarter things instead of running variable elimination multiple times.
Suppose you have a graphical structure. To calculate the margin, you need to add the federated distribution to all other variables, which is equivalent to summarizing the information in the entire drawing. This is another way to aggregate information from the entire diagram-each node looks at its neighbors and distributes the local approximate variables.
Each pair of neighboring nodes then sends a message to each other, where the message contains a local distribution. Each node now looks at the messages it receives and aggregates them to update the probability distribution of their variables.
In, C aggregates information from its neighbors A and B, and sends a message to D. D then aggregates this message with the information from E and F.
The advantage of this approach is that if you save messages sent on each node, a forward pass and a reverse pass of the message will provide information about all the other nodes for all nodes.
If the graphic does not contain loops, the process converges after the forward and back passes. If the graphic contains loops, this process may or may not converge, but it is often used to obtain approximate answers.
Approximate inference
Because accurate inference can be time-consuming for large graphical models, many approximate inference algorithms have been developed for the graphical model, most of which fall into one of the following two classes:
Sampling method
These algorithms use sampling estimation probabilities. To give a simple example, consider the following situation-given a coin, how do you determine the probability of getting a head when tossing a coin? The simplest thing to do is flip the coin 100 times and find out how much you got the head thrown.
This is a sampling-based algorithm to estimate the probability of a head. For more complex problems in the probabilistic graphics model, you can use a similar process. Sampling-based algorithms can be further divided into two categories. In the first, the samples are independent of each other, as shown in the coin throw example above. These algorithms are called Monte Carlo methods.
For many variable problems, it is difficult to produce high-quality independent samples, so we generate the relevant samples, i.e. each new sample is random, but close to the last one. This algorithm is called the Markov chain Monte Carlo (MCMC) method, because the sample forms a "Markov chain". Once we have samples, we can use them to answer a variety of reasoning questions.
Variational method
The Variational method does not use sampling, but instead attempts to approximate the desired distribution in the analysis. Suppose you write an expression that calculates the distribution of interest-marginal probability distribution or posterior probability distribution.
In general, the integrals in these expressions are computationally difficult to evaluate accurately. A good way to approximate these expressions is to solve alternative expressions and to make sure that this substitution expression approaches the original expression in some way. This is the basic idea behind the variational approach.
When we try to estimate the complex probability distribution P_complex, we define a separate set of probability distributions p_simple, which are easier to use, and then find the probability distribution P_approx closest to P_complex from P_simple.
Application: Image denoising
Suppose you have a slice:
Now let's say it's broken by random noise, so your noisy image looks like this:
The goal is to remove the image noise. Let's take a look at how we use the probability map model to do this.
The first step is to consider what variables we observe and what are not observed, and how we connect them to form a graph. Let's define each pixel in the noise image as an observed random variable and define each pixel in the live image as an observable variable. Therefore, if the image is MXN, there are MN observation variables and MN non-observable variables. Let's represent the observed variable as a $X _{ij}$, and the non-observable variable as $y_{ij}$. Each variable is evaluated with +1 and 1 (corresponding to black and white pixels respectively). Given the observed variable, we want to find the most probable value of the non-observable variable. This corresponds to map inference.
Now let's use some domain knowledge to build the graph structure. Obviously, the noise image in the position $ (i,j) $ in the observation variable depends on the actual image in the position $ (i,j) $ in the unobserved variable. This is because most of the time they are the same.
For the original image, adjacent pixels usually have the same value-not at the boundary where the color changes, but within the monochrome region, this property is true. So, if they are adjacent pixels, we connect $y_{ij}$ and $y_{kl}$.
Therefore, our graphical structure is as follows:
The white node represents the unobserved variable $y_{ij}$, and the gray node represents the observed variable $x_{ij}$. Each $x_{ij}$ is connected to the corresponding $y_{ij}$, each $y_{ij}$ connected to its neighboring node.
This is a Markov network because there is no causal relationship between the pixels of the image, so it is inappropriate to define the direction of the arrows in the Bayesian network here.
Our map inference problem can be mathematically based on the following:
We have used some standard simplification techniques that are common in maximum logarithmic likelihood calculations. We will use X and Y (without subscript) to represent a collection of all $x_{ij}$ and $y_{ij}$ values, respectively.
Now we need to define the federated distribution $p (x, y) $ according to the graphical structure. Suppose $p (x, y) $ consists of two factors: $\phi (X_{ij},y_{ij}) $ and $\phi (Y_{IJ},Y_{KL}) $, corresponding to the two edges in the diagram. Next, we define the following factors:
$\phi (X_{ij},y_{ij}) = exp ({w_e* X_{ij}, Y_{ij}) $, where $w_e$ is a parameter greater than 0.
When $x_{ij}$ and $y_{ij}$ are the same, the factor takes a large value, and when $x_{ij}$ and $y_{ij}$ are different, the small value is taken.
$\phi (y_{ij},y_{kl}) = exp (W_s*y_{ij}, Y_{KL}) $, where $w_s$ is a parameter greater than 0, as described earlier. This factor facilitates the same values for $y_{ij}$ and $y_{kl}$.
Therefore, our joint distribution is given by the following formula:
Where the Second product in $ (I,J) $ and $ (k,l) $ is adjacent pixels, z is the normalized constant.
Substituting it into our map inference equation gives:
Use parameter estimation techniques to obtain w_e$ and $w_s$ values from ground and noise image pairs. The process is mathematically quite complex, so we won't delve into it here. We assume that we have obtained the following values for these parameters: $ w_e = 8$ and $w_s = 10$.
The main focus of this example is reasoning. Given these parameters, we want to solve the map inference problem above. We can do this with variations of the confidence propagation , but it turns out that for graphs with this particular structure, there is a simpler algorithm called iterative conditional mode (ICM).
The basic idea is to select a node $y_{ij}$ in each step, view the values of the map inference expression for $y_{ij} = -1$ and $y_{ij} = 1$, and select a node with a higher value. Repeat this process for a certain number of iterations or until the convergence usually works reasonably well.
After removing the image:
Of course, you can use more fancy techniques in graphical models and outside to generate better things, but as you can see from this example, simple Markov networks with simple inference algorithms have provided you with quite good results. Python code
Summarize:
We have studied some of the core ideas in the probabilistic graph model. The graph model provides an explanatory way to simulate many real-world tasks, where dependencies exist. Using the graphical model gives us a way to handle such tasks in a principled manner.
Introduction to probability graph model