Placeholders in Tensorflow Last Updated : 18 Mar, 2022 Comments Improve Suggest changes Like Article Like Report A placeholder is a variable in Tensorflow to which data will be assigned sometime later on. It enables us to create processes or operations without the requirement for data. Data is fed into the placeholder as the session starts, and the session is run. We can feed in data into tensorflow graphs using placeholders. Syntax: tf.compat.v1.placeholder(dtype, shape=None, name=None) Parameters: dtype: the datatype of the elements in the tensor that will be fed.shape : by default None. The tensor's shape that will be fed , it is an optional parameter. One can feed a tensor of any shape if the shape isn't specified.name: by default None. The operation's name , optional parameter. Returns: A Tensor that can be used to feed a value but cannot be evaluated directly. Example 1: Python3 # importing packages import tensorflow.compat.v1 as tf # disabling eager mode tf.compat.v1.disable_eager_execution() # creating a placeholder a = tf.placeholder(tf.float32, None) # creating an operation b = a + 10 # creating a session with tf.Session() as session: # feeding data in the placeholder operation_res = session.run(b, feed_dict={a: [10, 20, 30, 40]}) print("after executing the operation: " + str(operation_res)) Output: after executing the operation: [20. 30. 40. 50.] Explanation: Eager mode is disabled in case there are any errors. A placeholder is created using tf.placeholder() method which has a dtype 'tf.float32', None says we didn't specify any size. Operation is created before feeding in data. The operation adds 10 to the tensor. A session is created and started using tf.Session(). Session.run takes the operation we created and data to be fed as parameters and it returns the result.Example 2: Python3 # importing packages import tensorflow.compat.v1 as tf # disabling eager mode tf.compat.v1.disable_eager_execution() # creating a tensorflow graph graph = tf.Graph() with graph.as_default(): # creating a placeholder a = tf.placeholder(tf.float64, shape=(3, 3), name='tensor1') # creating an operation b = a ** 2 # array1 will be fed into 'a' array1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Creating a session, and running the graph with tf.Session(graph=graph) as session: # run the session until it reaches node b, # then input an array of values into a operation_res = session.run(b, feed_dict={a: array1}) print("after executing the operation: ") print(operation_res) Output: after executing the operation: [[ 1. 4. 9.] [16. 25. 36.] [49. 64. 81.]] Comment More infoAdvertise with us Next Article Placeholders in Tensorflow S sarahjane3102 Follow Improve Article Tags : Python Geeks Premier League Geeks-Premier-League-2022 Tensorflow Practice Tags : python Similar Reads Install Tensorflow on MacOS TensorFlow is an open-source software library developed by the Google brain team. It widely used to implement deep learning models which helps in solving real world problems. In this article, we learn how to install TensorFlow on macOS using Homebrew. Requirements: Python 3.6â3.8macOS 10.12.6 (Sierr 2 min read TensorArray in TensorFlow In TensorFlow, a tensor is a multi-dimensional array or data structure representing data. It's the fundamental building block of TensorFlow computations. A tensor can be a scalar (0-D tensor), a vector (1-D tensor), a matrix (2-D tensor), or it can have higher dimensions. In this article, we are goi 6 min read Sparse tensors in Tensorflow Imagine you are working with a massive dataset which is represented by multi-dimensional arrays called tensors. In simple terms, tensors are the building blocks of mathematical operations on the data. However, sometimes, tensors can have majority of values as zero. Such a tensor with a lot of zero v 10 min read Tensorflow.js tf.gather() Function Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .gather() function is used to collect the fragments from the stated tensor x's axis as per the stated indices. Synt 2 min read Neural Network Layers in TensorFlow TensorFlow provides powerful tools for building and training neural networks. Neural network layers process data and learn features to make accurate predictions. A neural network consists of multiple layers, each serving a specific purpose. These layers include:Input Layer: The entry point for data. 2 min read Like