Hands on TensorFlow for Beginners.
This story is a continuation of TensorFlow : No idea where to begin? .
Every TensorFlow program involves building the graph and executing it. To build the graph, we start of with writing a python program in Terminal within TensorFlow virtual environment. To activate the environment, run this command in terminal
We will start to learn TensorFlow and TensorBoard skills by writing simple math commands in TensorFlow. Use a python editor to write the program. The program is explained to you using the comments.
#we start off by TensorFlow libraries as tfimport tensorflow as tf
declare 4 constants a,b,c, and d which we will use as the input to our computation graph.
constants in tensorflow are immutable, so once you assigned a particular value to it, you cannot change it within the course of the program
'''a = tf.constant(6, name='constant_a')
b = tf.constant(3, name='constant_b')
c = tf.constant(10, name='constant_c')
d = tf.constant(5, name='constant_d')
tf.constant is used to specify constants
along with constants, name parameter allows us to identify these constants when we visualize it using TensorBoard.
'''#a,b,c and d are the tensors flowing through the graphmul = tf.multiply(a,b, name="mul")#mul is a node which specifies the multiply operation on 'a' and 'b'div = tf.div(c,d, name="div")#division operation which divides c by d and have the name "div"#tf.add_n sums up the element in an arrayaddn = tf.add_n([mul,div], name="addn")'''
here we specified two computational node into the array, so the output of multiplication and division will be addedif we simply try to print the node, it prints the name of the tensor, shape and datatype.This is because we haven't run the graph we built up. To obtain the value after all the computations, we need to execute the graph to get results. It requires a tensorflow session to execute any tensorflow program.
'''sess = tf.Session()sess.run(addn)'''
you can run sess.run(name of node) on any intermediate steps to get the result. It will perform all the computations till the output of the node.To visualize the graph using tensorflow browser tools, now instantiate a filewritter which is in the tf.summary namespace which allows you to write out events to a log file directly.
'''writer = tf.summary.FileWriter('./exampleTensorflow1', sess.graph)#close the writer and session writer.close()
run the above code in terminal within the python environment
exit from the python environment and run ls command to see the exampleTensorflow1 directory has been created. Now run the TensorBoard command on your commandline and specify the log folder
copy your link address and open it in the browser.
top bar shows different types of summary statistics that you can write out to your log file. We focus on graphs. Constants a and b are fed into the mul node and c and d are fed into div node and output of mul and div goes to addn. This is a very tiny graph and I will talk about more in the upcoming stories. If you liked this story, please show your support.