psst... I heard you might be interested in bitcoin, blockchains and cryptocurrencies.

If so, you might be interested in our new tutorial page, LearningBlockchains.com! It has great lessons on using crypto, coding for it, and more lessons are coming all the time.

Check it out



Linear Equations

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

Stuck? Looking for more content?

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.

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: