Tensorflow to Solve Simple Math Equation

Tensorflow is a open source deep learning library that developed by Google Inc. It is really a interesting machine learning library that will definitely get hook and impressed from it after you know what it can be achieve.
I am given some time to spend on the reading about Tensorflow and was thinking should start a simple task in order for me to really know how to use Tensorflow. So, I came up with a idea on how to solve simple math equation by using Tensorflow!
Here is the simple equation Y = X + Z while Z is a unknown value, X = (1, 2) and Y = (12, 4). Obviously we all can tell Z can be calculate as simple as taking Y-X but that not the purpose of what I doing here. The purpose is to use Tensorflow library to find the possibility value of Z by using optimizer and deviation. Sound amazing right? :)
The first step to do is make sure your machine have installed Tensorflow library and python3. Otherwise, you can execute the below code in your terminal for installing it
brew install python3
pip3 install tensorflowAfter that create a file and name it tensorflow_solve_math.py .Open it and insert the first line on below to the file.
import tensorflow as tfNext, we need to define x and y as a constant below and Z as unknown value because we don’t know what is the Z value. We declared Z as tf.Variable because it is changeable value by Tensorflow during execution time on session run (Don’t worry what is about it because we will discuss it later). What we define here is a 2x1 dimension array and it will be use to compute the equation that we define above.
x = tf.constant([[1., 2.]])
y = tf.constant([[12., 4.]])Z = tf.Variable(tf.zeros([1, 2]))
Now we calculate the total sum by add up X and Z while Z will be a variable that control by Tensorflow during the session run. At this point we don’t know what the right value of Z so we need deviation help us to find it. After that, find the deviation value by using Tensorflow square root.
yy = tf.add(x, Z)
deviation = tf.square(y - yy)Then, we find the train_step by using the GradientDescentOptimizer and minimize the deviation so we can find the closest number for the Z. We also have to define learning_rate here as 0.01.
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(deviation)After that, it is time for create Tensorflow session and execute the run step.
init = tf.global_variables_initializer()
sess = tf.Session();
sess.run(init)for i in range(5000):
sess.run(train_step)print(sess.run(Z))
Now we have complete the code and please save it. Run the command below and make sure you have navigated to the correct file directory.
python3 tensorflow_solve_math.pyBelow is the output for the result. Boom!
[[ 11. 1.99999988]]Z variable was calculated by Tensorflow which is the (11, 1.99999988) and we replace the whole formula as below
Y = X + Z
Hence, (12, 4) = (1, 2) + (11, 1.99999988)Tensorflow is really a powerful deep learning framework that it can help us reduce automation works and save us from daily hassle. Apart from that, it is also can be train to do smart thing such as solving puzzle.
My next plan is to develop a Tensorflow classifier and use it for solving the game puzzle that able to calculate the least step to solve it.
If you have any queries please reach me at licco.wee@gmail.com. I would like to help you with any Tensorflow question that I am capable of it. :)
