Bonus content for just $7!
Get our exercise solution bundle, with loads of extra code, solutions to all exercises and updates for life - we add a new lesson, you get more code at no cost!
Buy it now, support the site, and thanks for your support!
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:
Stuck? Get the exercise solutions here
If you are looking for solutions on the exercises, or just want to see how I solved them, then our solutions bundle is what you are after.
Buying the bundle gives you free updates for life - meaning when we add a new lesson, you get an updated bundle with the solutions.
It's just $7, and it also helps us to keep running the site with free lessons.
Grow your business with data analytics
Looking to improve your business through data analytics? Are you interested in implementing data mining, automation or artificial intelligence?This book is the ultimate guide to getting started with using data in your business, with a non-technical view and focusing on achieving good outcomes. We don't get bogged down by technical detail or complex algorithms.
For additional offers, including a premium package, see this page.
You can also support LearningTensorFlow.com by becoming a patron at Patreon. If we have saved you trawling through heavy documentation, or given you a pointer on where to go next, help us to create new lessons and keep the site running.
You'll also get access to extra content and updates not available on LearningTensorFlow.com!
Note: despite the title, this book has no relationship to LearningTensorFlow.com