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:

(1)3x+2y=15(2)4x−y=10″ role=”presentation”>3x+2y4x−y=15=10(1)(2)(1)3x+2y=15(2)4x−y=10

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:

(3)p1=(1,2)(4)p2=(0,−1)” role=”presentation”>p1p2=(1,2)=(0,−1)(3)(4)(3)p1=(1,2)(4)p2=(0,−1)

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:

(5)y=ax+b” role=”presentation”>y=ax+b(5)(5)y=ax+b

Rearranging equation (5)” role=”presentation”>(5)(5) to get x and y on the same side, we get the following:

(6)y−ax=b(7)1by−abx=1″ role=”presentation”>y−ax1by−abx=b=1(6)(7)(6)y−ax=b(7)1by−abx=1

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:

(8)AX=B” role=”presentation”>AX=B(8)(8)AX=B

The parameters to learn is then simply:

(9)A=BX−1″ role=”presentation”>A=BX−1(9)(9)A=BX−1

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 (5)” role=”presentation”>(5)(5) above, which is to convert from these parameters (that fit equation (7)” role=”presentation”>(7)(7) ).

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:

(10)p1=(2,1)(11)p2=(0,5)(12)p3=(−1,2)” role=”presentation”>p1p2p3=(2,1)=(0,5)=(−1,2)(10)(11)(12)(10)p1=(2,1)(11)p2=(0,5)(12)p3=(−1,2)

The canonical equation for a circle is:

(13)x2+y2+dx+ey+f=0″ role=”presentation”>x2+y2+dx+ey+f=0(13)(13)x2+y2+dx+ey+f=0

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:

(14)x2+y2+dx+ey+f=0(15)dx+ey+f=−(x2+y2)” role=”presentation”>x2+y2+dx+ey+fdx+ey+f=0=−(x2+y2)(14)(15)(14)x2+y2+dx+ey+f=0(15)dx+ey+f=−(x2+y2)

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:

(16)Ax2+By2+Cxy+Dx+Ey+F=0″ role=”presentation”>Ax2+By2+Cxy+Dx+Ey+F=0(16)(16)Ax2+By2+Cxy+Dx+Ey+F=0

Observed points:

(17)p1=(8,0)(18)p2=(4,−26)(19)p3=(−214,2)(20)p4=(−46,3)(21)p5=(14,5)” role=”presentation”>p1p2p3p4p5=(8,0)=(4,−26‾√)=(−214‾‾‾√,2)=(−46‾‾‾√,3)=(14‾‾‾√,5)(17)(18)(19)(20)(21)