pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 23. Placeholders: Placeholders are used to feed values into a TensorFlow graph.They are used along with feed_dict to feed data. They are normally usedto feed new training examples while training a neural network. We assignvalues to a placeholder while running the graph in session. They allow us tocreate our operations and build the computational graph without requiringany data. An important detail to note is that placeholders do not contain anydata and thus there is no need to initialize them.Examples of operationsLet's see some examples of different operations available in TensorFlow 1.x.ConstantsFirst, 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(). Thisstatement 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 NumPyarray or tensor constant using:tf.zeros_like(t_2) # Create a zero matrix of same shape as t_2tf.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 onesmatrix 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 ]

TensorFlow 1.x and 2.x• We can broadcast in a similar way to how it's done with NumPy:Example: t = tf.Variable([[0., 1., 2.], [3., 4., 5.], [6., 7.,8]])print (t*2) ==>tf.Tensor([[ 0. 2. 4.][ 6. 8. 10.][12. 14. 16.]], shape=(3, 3), dtype=float32)Sequences• We can generate a sequence of evenly spaced vectors, starting from start toend, with a total num values:tf.linspace(start, stop, num) // The corresponding values differby (stop-start)/(num-1)Example: range_t = tf.linspace(2.0,5.0,5) ==> [ 2. 2.75 3.54.25 5. ]• Generate a sequence of numbers starting from start (default=0),incremented by delta (default=1) up to but not including the limit:tf.range(start,limit,delta)Example: range_t = tf.range(10) ==> [0 1 2 3 4 5 6 7 8 9]Random tensorsTensorFlow allows random tensors with different distributions to be created:• To create random values from a normal distribution of shape [M,N], withmean (default =0.0), standard deviation stddev (default=1.0), and using seed,we can use:t_random = tf.random_normal([2,3], mean=2.0, stddev=4, seed=12)==> [[ 0.25347459 5.37990952 1.95276058], [-1.537603141.2588985 2.84780669]]• To create random values from a truncated normal distribution of shape[M,N] with mean (default =0.0), standard deviation stddev (default=1.0),and using seed, we can use:t_random = tf.truncated_normal([1,5], stddev=2, seed=12) ==> [[-0.8732627 1.68995488 -0.02361972 -1.76880157 -3.87749004]][ 56 ]

TensorFlow 1.x and 2.x

• We can broadcast in a similar way to how it's done with NumPy:

Example: t = tf.Variable([[0., 1., 2.], [3., 4., 5.], [6., 7.,

8]])

print (t*2) ==>

tf.Tensor(

[[ 0. 2. 4.]

[ 6. 8. 10.]

[12. 14. 16.]], shape=(3, 3), dtype=float32)

Sequences

• We can generate a sequence of evenly spaced vectors, starting from start to

end, with a total num values:

tf.linspace(start, stop, num) // The corresponding values differ

by (stop-start)/(num-1)

Example: range_t = tf.linspace(2.0,5.0,5) ==> [ 2. 2.75 3.5

4.25 5. ]

• Generate a sequence of numbers starting from start (default=0),

incremented by delta (default=1) up to but not including the limit:

tf.range(start,limit,delta)

Example: range_t = tf.range(10) ==> [0 1 2 3 4 5 6 7 8 9]

Random tensors

TensorFlow allows random tensors with different distributions to be created:

• To create random values from a normal distribution of shape [M,N], with

mean (default =0.0), standard deviation stddev (default=1.0), and using seed,

we can use:

t_random = tf.random_normal([2,3], mean=2.0, stddev=4, seed=12)

==> [[ 0.25347459 5.37990952 1.95276058], [-1.53760314

1.2588985 2.84780669]]

• To create random values from a truncated normal distribution of shape

[M,N] with mean (default =0.0), standard deviation stddev (default=1.0),

and using seed, we can use:

t_random = tf.truncated_normal([1,5], stddev=2, seed=12) ==> [[-

0.8732627 1.68995488 -0.02361972 -1.76880157 -3.87749004]]

[ 56 ]

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

Saved successfully!

Ooh no, something went wrong!