TensorFlow

From Grundy
Jump to: navigation, search

TensorFlow is a machine learning framework created by Google Brain. Almost all Google products now run their machine learning component on TensorFlow. TensorFlow has a very well documented Python API, which makes it a paradise for Machine Learning engineers and enthusiasts.

Well even if you don't know Machine Learning, TensorFlow is the best place to begin! Unfortunately, it's workflow is slightly different from the standard Python code. It does take a while, but soon you will be flourishing!

What are the Pre-requisites?

  • Python - Needless to say, you have to know how to write decent Python code. Have a look at the Python tutorials in Programming 101 to begin exploring Python. Make sure you have written a few modular programs before coming to TensorFlow.
  • Numpy - Many TensorFlow functions are very similar to Numpy functions and they involve manipulating complicated multidimensional matrices called Tensors. This can be particularly hard for beginners and it's necessary that you write a few simpler numpy programs before coming to TensorFlow. Have a look at our Scientific Computation and Numpy guide for more details.
  • Machine Learning - Not essential, but knowing basic Machine Learning will help you rise up the steep learning curve very quickly. Have a look at our Machine Learning guide for more. The book by Nielson, Neural Networks and Deep Learning has more than enough for basic TensorFlow programming.

Installing TensorFlow

Google has written an excellent document on TensorFlow installation. Make sure you choose CPU vs GPU, your OS and 32 bit vs 64 bit carefully. Here it is - TensorFlow Setup.

TensorFlow Workflow

TensorFlow models follow a three stage procedure. The first part involves constructing a computational graph, a sort of design specification where you build your Neural Network. Here's what a simple feedforward Neural Network looks like -

import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

The next step is adding some functions to train your computational graph. Here's a sample -

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

Finally, you need to actually run the graph and feed in data for training. Here's a sample -

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

That's it! This is all it takes to make a simple feedforward neural network.

Learning TensorFlow

Learning TensorFlow can be challenging. We strongly recommend reading certain Google tutorials, and making a small project for yourself. You are always welcome to help us in our projects - Photometric Redshifts, Weather Prediction and Basic ML Experiments.

Here are the Google tutorials you must completely understand -

  • Basic MNIST - This tutorial is best if you are new to Machine Learning and TensorFlow. This is the first tutorial you do. Here is a short explanation to a difficult part of this tutorial by Kalpesh Krishna - StackOverflow - MNIST Matrices.
  • TensorFlow Mechanics 101 - A fast paced complete introduction to the different elements used to make Machine Learning possible in TensorFlow.
  • Sharing Variables - A really complicated and powerful feature of TensorFlow. A must do before you write actual code.

Other excellent tutorials we've used -

See Also