TensorFlow: Learning how to make a lineair algebraic network

TensorFlow is an open-source software library for Machine Intelligence. It is so popular even some graphic cards have so-called TensorCores in them.

But what can you really do with TensorFlow? The easiest example is solving lineair algebraic equations. And that is what I am going to show today.

If you want to follow along, you must have TensorFlow installed as described here.

Solving lineaur algebraics

First import TensorFlow into your environment

import tensorflow as tf

If you are familiar with Python, you can now access all of TensorFlow's classes, method & symbols. The docs are here.

Since we want to solve a simple y = ax + b, we need to make variables for y, a, x and b.

a = tf.Variable([0.0], dtype=tf.float32)
b = tf.Variable([0.0], dtype=tf.float32)
x = tf.placeholder(tf.float32)
y = a*x + b
realAnswer = tf.placeholder(tf.float32)

Noticed we used tf.variable and not constants, since we want a good value for these parameters. The network will be told to change a and b according to preset x and y. We also need place holders to hold our predetermined x and y. Now we need to feed this train data.

x_train = [1,2,3,4,5]
y_train = [6,11,16,21,26]

Next we need some kind of indication whether we are close to good values or not, this is done with a loss function, with the sum of squares.

loss = tf.reduce_sum(tf.square(y - realAnswer)) # sum of the squares

Next we need to define the "learning rate" or change rate of the variables for a and b. We do this by

optimizer= tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)

Pick a value for the Gradient Descent between 1 and 0.000001. You should change this value during training.
Next we initialise the network and run it 1000 times.

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000) :
   sess.run(train, {x : x_train, realAnswer: y_train})
curr_a, curr_b = sess.run([a,b], {x: x_train, realAnswer: y_train})
print("a: %s, b: %s"%(curr_a, curr_b))

Now your output will show something like 4.9xxx for a and 1.0xxxx for b. And that is close to correct, since I used y = 5x+1 to determine the test values.

Congrats! You just made your own AI!

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 96483.87
ETH 3356.14
USDT 1.00
SBD 3.20