09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2

3. Placeholders: Placeholders are used to feed values into a TensorFlow graph.

They are used along with feed_dict to feed data. They are normally used

to feed new training examples while training a neural network. We assign

values to a placeholder while running the graph in session. They allow us to

create our operations and build the computational graph without requiring

any data. An important detail to note is that placeholders do not contain any

data and thus there is no need to initialize them.

Examples of operations

Let's see some examples of different operations available in TensorFlow 1.x.

Constants

First, let's look at some constants that we will encounter:

• We can declare a scalar constant:

t_1 = tf.constant(4)

Example: A constant vector of shape [1,3]:

t_2 = tf.constant([4, 3, 2])

• To create a tensor with all elements as zero, we use tf.zeros(). This

statement creates a matrix of zeros of shape [M,N] with dtype (int32,

float32, and so on):

tf.zeros([M,N],tf.dtype)

Example: zero_t = tf.zeros([2,3],tf.int32) ==>[[0 0 0], [0 0 0]]

• Get the shape of a tensor:

Example: print(tf.zeros([2,3],tf.int32).shape) ==> (2, 3)

• We can also create tensor variables of the same shape as an existing NumPy

array or tensor constant using:

tf.zeros_like(t_2) # Create a zero matrix of same shape as t_2

tf.ones_like(t_2) # Creates a ones matrix of same shape as t_2

• We can create a tensor with all elements set to one; next, we create a ones

matrix of shape [M,N]:

tf.ones([M,N],tf.dtype)

Example: ones_t = tf.ones([2,3],tf.int32) ==>[[0 0 0], [0 0 0]]

[ 55 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!