Intro to TensorFlow
TensorFlow™ is an open source software library for numerical computation using data flow graphs
Lets learn by doing! Here’s our traditional ‘hello world’ program for tensorflow.
Line 1: __future__ is a pseudo-module which is used, to enable new language features which are not compatible with the current interpreter.
Line 2: Here’s where it starts. Lets import our tensorflow module.
Line 5: The
tf.constant() op takes a numpy array (or something implicitly convertible to a numpy array), and returns a
tf.Tensor whose value is the same as that array. Lets assign the tensor to a variable named 'hello'.
Line 6: A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated.
Line 7: Here, the session environment evaluates our tensor object, ‘hello’, and lets print it.
Line 8: closing the session.
Output: Hello, Tensorflow!
Lets look at some basic operations using tensorflow.
Line 1–5: same as in the previous example
Line 7: It is equivalent to calling the Session as we did earlier in our previous example.
Line 8: regular print statement
Line 9: we are adding the two tensors, a and b using sess.run(a + b)
Line 10: we are using multiplication operator using sess.run(a * b)
Line 12–13: Basic operations with variable as graph input.
Line 15–16: Lets define some operations.
Line 18–20: launching our default graph. run every operation with variable input.
Line 22: You can find more details on this example in Matrix Multiplication from tensorflow. Create a constant op that produces 1x2 matrix. The op is added as a node to the default graph. The value returned by the constructor represents the output of the constant op
Line 23: Create another constant that produces 2x1 matrix.
Line 25: Create a matmul op that takes ‘matrix1’ and ‘matrix2’ as inputs. The returned value, ‘product’, represents the result of the matrix multiplication.
Line 27–29: To run the matmul op we call the session ‘run()’ method, passing ‘product’, which represents the output of the matmul op. This indicates to the call that we want to get the output of the matmul op back. All inputs needed by the op are run automatically by the session. They typically run in parallel. The call ‘run(product)’ thus causes the execution of three ops in the graph, the two constants and matmul. The output of the op is returned in ‘result’ as a numpy ndarray object.
Alright that’s it for now! Thank you for spending your time. Cheers!