Tensorflow Placeholders in Python

Tensorflow placeholder() as the name suggests creates a placeholder for a tensor that will be fed later.
In simple words, it allocates a block of memory for future use which allows us to build our operation graphs without needing the data which is the scenario in most of the machine learning tasks.
We can later use feed_dict to feed the data into the tensor.

Structure of TensorFlow placeholders:

x = tf.placeholder(dtype, shape = None, name = None)
  • dtype – The type of elements to be fed in the tensor.
  • shape – The shape of the tensor to be fed (optional). By default, the placeholder() has a shape with no constraints, which allows us to feed tensors of any shapes.
  • name – The name of the tensor which is optional but as a good practice we can provide names for each tensor.

Let us see examples of some simple tensors with a placeholder().

TensorFlow program to round the elements of lists

import tensorflow as  tf

x = tf.placeholder(dtype="float", shape=(2, 5), name='placeholder1')
y = tf.round(x)

with tf.Session() as session:

Let’s break down the above code which rounds the elements to the nearest integer value.

Here we first import the tensorflow as tf then create a placeholder x of dtype float,  shape (2, 5), as we later want to pass two lists of 5 float elements each.
Also, we have given the name to this placeholder() as ph1.

Then we have an operation y to run it in a session that rounds the elements of the placeholder x.
Note we have not assigned any values to x yet.

We create a session object and run the operation y which requires the values of x and we provide these values through feed_dict argument.

The following output is received showing the elements rounded to the nearest integer as our output.


[[ 2. 24.  7.  8.  1.]
 [ 8.  6. 75. 14. 48.]]

Some more examples.

TensorFlow program to perform Matrix Multiplication.

import tensorflow as tf
import random

matA = tf.placeholder(dtype = 'int32', shape = (3, 3), name = 'MatrixA')
matB = tf.placeholder(dtype = 'int32', shape = (3, 3), name = 'MatrixB')

mat_mul = tf.matmul(matA, matB)

with tf.Session() as session:

Here we perform matrix multiplication of two matrices A and B using two placeholders matA and matB.

To do this, we have used random to create two matrices of size 3X3 with random integer values and hence have mentioned the shape as (3, 3) for both the placeholders.


[[14 18 14]
 [30 34 30]
 [23 25 24]]

TensorFlow program to concat two strings.

import tensorflow as tf

str1 = tf.placeholder(dtype = 'string', shape = None, name='String1')
str2 = tf.placeholder(dtype = 'string', shape = None, name='String2')

str_concat =  str1 +" - "+str2

with tf.Session() as session:
    output = session.run(str_concat, {str1: str(input()), 
                                      str2: str(input())})

This code concatenates two strings taken from the user.


Coding Solution & Software Development
b'CodeSpeedy - Coding Solution & Software Development'

Note here we have mentioned the shape of both the placeholders as None.
This becomes useful in most of the machine learning tasks as most of the time we are unaware of the number of rows but let’s assume we know the number of features.
In such cases, we can use None.

x = tf.placeholder(dtype = 'float', shape = (None,5), name = 'xyz')

By doing this, we can feed a matrix with 5 columns and any number of rows.

Also, you can read the below-related blogs,

Python: How to create tensors with known values

Basics of TensorFlow with examples

Leave a Reply

Your email address will not be published. Required fields are marked *