tf.solve function, TensorFlow can solve series of Linear Equations. You might recognised these as being sets of linked equations like this:
These types of linear equations are used for a number of problems in mathematics, from optimising factory output to geometry.
You can solve these equations using a number of methods, but in this lesson, we will see how to use
tf.solve to do this for us.
I’ll focus on the geometry. Here are two points, p1 and p2 that lie on a 2-Dimensional (x, y) space:
Here is where they sit on a plot:
To do this in TensorFlow, we first setup a series of linear equations with our points at the center.
First, we create our matrix of points. The first row corresponds to the first point, and the second row to the second point.
Likewise, the first column is the
x values, while the second column is the
The equation for a line is:
Rearranging equation to get
y on the same side, we get the following:
Our job is to find the values for a and b in the above equation, given our observed points. We can do this quite easily by taking the inverse of the points array and multiplying it by a matrix with ones.
Using matrices (because we are using TensorFlow), if X is our matrix of observed points and A is the parameters we need to learn, we setup a system:
The parameters to learn is then simply:
B is simple the number one - broadcasted appropriately - it originates from the right hand side of the equation above.
A is our parameters in equation 3 above.
There is a final step to find our
b values from equation above, which is to convert from these parameters (that fit equation ).
This solution is nicely wrapped up in the
tf.solve function. To see this in action, let’s look at another example.
Here is a circle:
Here are three observed points that lie on this circle:
The canonical equation for a circle is:
To solve for the parameters d, e, and f, we create another points array, and pad it with ones to create a square matrix. We are looking for three parameters, and therefore our A matrix must have a shape (3, 3).
As there are no parameters for the squared parts of this equation, our equation becomes a little different when we have observed values of x and y:
Therefore, our A matrix is composed of the x and y values (along with another column of ones), and our B matrix is the negation of the sum of the squares of x and y.
We then use
tf.matrix_solve to find our X array, which is the parameters to our equation.
Running this in a session, we get three values, which are D, E and F.
1) Solve for the circle that contains the following three points: P(2,1), Q(0,5), R(-1,2)
2) The general form of an ellipse is given below. Solve for the following points (five points are needed to solve this equation):
General form of an ellipse: