Placeholders
A graph can be parameterized to accept external inputs, known as placeholders. A placeholder is a promise to provide a value later. While evaluating the graph involving placeholder nodes, a feed_dict parameter is passed to the session’s run method to specify Tensors that provide concrete values to these placeholders. Consider the example given below:
Python
# importing tensorflow import tensorflow as tf # creating nodes in computation graph a = tf.placeholder(tf.int32, shape = ( 3 , 1 )) b = tf.placeholder(tf.int32, shape = ( 1 , 3 )) c = tf.matmul(a,b) # running computation graph with tf.Session() as sess: print (sess.run(c, feed_dict = {a:[[ 3 ],[ 2 ],[ 1 ]], b:[[ 1 , 2 , 3 ]]})) |
Output:
[[3 6 9]
[2 4 6]
[1 2 3]]
Let us try to understand above program:
- We define placeholder nodes a and b like this:
a = tf.placeholder(tf.int32, shape=(3,1))
b = tf.placeholder(tf.int32, shape=(1,3))
- The first argument is the data type of the tensor and one of the optional argument is shape of the tensor.
- We define another node c which does the operation of matrix multiplication (matmul). We pass the two placeholder nodes as argument.
c = tf.matmul(a,b)
- Finally, when we run the session, we pass the value of placeholder nodes in feed_dict argument of sess.run:
print(sess.run(c, feed_dict={a:[[3],[2],[1]], b:[[1,2,3]]}))
- Consider the diagrams shown below to clear the concept:
- Initially:
- After sess.run:
Introduction to TensorFlow
TensorFlow is an open-source machine learning library developed by Google. TensorFlow is used to build and train deep learning models as it facilitates the creation of computational graphs and efficient execution on various hardware platforms. The article provides an comprehensive overview of tensorflow.
Table of Content
- TensorFlow
- How to install TensorFlow?
- The Computational Graph
- Variables
- Placeholders
- Linear Regression model using TensorFlow
- tf.contrib.learn
- What are TensorFlow APIs?
Contact Us