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 = session.run(parameters)

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 = session.run(X)
    D, E, F = result.flatten()

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

Exercises

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:

Keep going!

We have an increasing set of lessons that we hope guides you through learning this powerful library. Follow these links to keep going to our next lesson.

You can also use the nav menu at the top of the page to go directly to a specific lesson.

Coming soon (although not written by us):

Get updates

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



* indicates required

If you have any feedback, please see our page here. If you spot any errors with our lessons, please direct them to our Github page with the name of the lesson in which the error resides, so that we can resolve them and close them off there.

If you have larger questions that may involve consultancy, please contact us here