For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

TensorFlow Placeholder

Simple Python Tensorflow Program

import numpy as np  
import tensorflow as tf

In the above code, we are importing numpy and TensorFlow and renaming them numpy as np and tensorflow as tf.

X_1 = tf.placeholder(tf.float32, name = "X_1")  
X_2 = tf.placeholder(tf.float32, name = "X_2")  

In the above code, we are determininig the two variables X_1 and X_2. When we create a placeholder node, we have to pass in the data type, here we are using a floating-point data type, let's use tf.float32. We also need to provide a name to this node.

multiply = tf.multiply(X_1, X_2, name = "multiply")

In the above code, we define the node that does the multiplication operation. In Tensorflow we can do that by creating a tf.multiply node. This node will result in the product of X_1 and X_2

with tf.Session() as session:    
    result = session.run(multiply, feed_dict={X_1:[1,2,3], X_2:[4,5,6]})    
    print(result) 

To run operations in the graph, we have to initialize a session. In Tensorflow, it is done by using tf.Session() method. Now that we have a session we can request the session to run operations on our computational graph by calling session. To execute the computation, we need to use run.

When the addition operation executes, it is going to look that it needs to take the values of the X_1 and X_2 nodes, so we also need to set values for X_1 and X_2. We can do that by providing a argument called feed_dict. We take the value 1,2,3 for X_1 and 4,5,6 for X_2.

import numpy as np  
import tensorflow as tf  
  
X_1 = tf.placeholder(tf.float32, name = "X_1")  
X_2 = tf.placeholder(tf.float32, name = "X_2")  
  
multiply = tf.multiply(X_1, X_2, name = "multiply")  
  
with tf.Session() as session:  
    result = session.run(multiply, feed_dict={X_1:[1,2,3], X_2:[4,5,6]})  
    print(result)

The Above Consolidated Program will give the following result: [ 4. 10. 18.]