WTF is Tensor Flow — Part 2

Venkatesh Gupta
2 min readJun 6, 2018

If you haven’t read Part 1 of this series, I recommend you to read it. In this blog, we are going to discuss about constants, variables, placeholders along with tensor board.

Tensor Flow is easy!

Constants

A constant takes no external input. It outputs a value, that it stores internally.

import tensorflow as tf
a = tf.constant(3.0, tf.float32)
b = tf.constant(4.0)
sess= tf.Session()
output = sess.run(tf.add(a,b))
print(output)
sess.close()
Output :-
7.0

Scroll up and read the line. A constant takes no external input. What if the graph wants to accept external input?

And here comes placeholder in rescue.

Placeholders

A placeholder is a promise to the graph that values will be provided to it, later. (Promises are meant to be broken, Sigh!)

import tensorflow as tf
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
add = tf.add(a,b)
sess = tf.Session()
output = sess.run(add, {a:[1,2,3], b:[-1,4,5]})
print(output)
sess.close()
Output :-
[0. 6. 8.]

Here, the graph is parameterised to accept external inputs via placeholders.

Variables

What if you want to try different values to arrive at a perfect shot!

import tensorflow as tf
W = tf.Variable([.3],tf.float32)
b = tf.Variable([-.3],tf.float32)
x = tf.placeholder(tf.float32)
model = W * x + b
init = tf.global_variables_initializer()
sess= tf.Session()
sess.run(init)
output = sess.run(model, {x:[1,3,2,4]})
print(output)
sess.close()
Output:-
[0. 0.6 0.3 0.90000004]

Note :- Variables needs to be explicitly declared unlike placeholders and constants. Here, we have use the line init = tf.global_variables_initializer() to initialise the variable.

Tensor Board

A tensor board is a web application suite to visualise the computational graph.

To open up tensor board, you need to do two steps :-

  1. You need to create object of a class to write summary. One such class is FileWriter class.
filewriter = tf.summary.FileWriter('directory name', sess.graph)

2. Go to the terminal and type :-

tensorboard --logdir = "directory name"

À la carte:-

  1. Calculating Loss in Tensor Flow
import tensorflow as tf#Model Parameters
W = tf.Variable([-0.18], tf.float32)
b = tf.Variable([1.0], tf.float32)
# Input and Output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_mean(squared_deltas)
init = tf.global_variables_initializer()with tf.Session() as sess:
sess.run(init)
output = sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]})
print(output)
Output:-
5.043

2. Using Optimiser to find the optimum value of the Variables.

import tensorflow as tf#Variables
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
#Input
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
init = tf.global_variables_initializer()
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_mean(squared_deltas)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = (optimizer.minimize(loss))
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
print(sess.run([W,b]))
Output:-
[array([-0.99999964], dtype=float32), array([0.9999989], dtype=float32)]

--

--

Venkatesh Gupta

Product @ Internshala | Career-tech (Ed-tech + Recruitment-tech) & creator economy enthusiast | Writes on Xplainerr