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!

Linear Equations

Recommended reading:

Through the 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 y values.

import tensorflow as tf

# Point 1
x1 = tf.constant(2, dtype=tf.float32)
y1 = tf.constant(9, dtype=tf.float32)
point1 = tf.stack([x1, y1])

# Point 2
x2 = tf.constant(-1, dtype=tf.float32)
y2 = tf.constant(3, dtype=tf.float32)
point2 = tf.stack([x2, y2])

# Combine points into an array
X = tf.transpose(tf.stack([point1, point2]))

The equation for a line is:

Rearranging equation to get x and 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:

The matrix B is simple the number one - broadcasted appropriately - it originates from the right hand side of the equation above.

The matrix A is our parameters in equation 3 above.

B = tf.ones((1, 2), dtype=tf.float32)

parameters = tf.matmul(B, tf.matrix_inverse(X))

with tf.Session() as session:
    A =

There is a final step to find our a and b values from equation above, which is to convert from these parameters (that fit equation ).

b = 1 / A[0][1]
a = -b * A[0][0]
print("Equation: y = {a}x + {b}".format(a=a, b=b))

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.

import tensorflow as tf

points = tf.constant([[2, 1],
                 [0, 5],
                 [-1, 2]], dtype=tf.float64)

A = tf.constant([
    [2, 1, 1],
    [0, 5, 1],
    [-1, 2, 1]
], dtype='float64')

B = -tf.constant([[5], [25], [5]])

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.

X = tf.matrix_solve(A, B)

with tf.Session() as session:
    result =
    D, E, F = result.flatten()

    print("Equation: x**2 + y**2 + {D}x + {E}y + {F} = 0".format(**locals()))


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:

Observed points:

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.

Get updates

Sign up here to receive infrequent emails from us about updates to the site and when new lessons are released.

* indicates required

You can also support 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!

Coming soon!

Note: despite the title, this book has no relationship to

Learn More!