pdfcoffee
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 ]
- Page 39 and 40: Neural Network Foundations with Ten
- Page 41 and 42: Neural Network Foundations with Ten
- Page 43 and 44: Neural Network Foundations with Ten
- Page 45 and 46: Neural Network Foundations with Ten
- Page 47 and 48: Neural Network Foundations with Ten
- Page 49 and 50: Neural Network Foundations with Ten
- Page 51 and 52: Neural Network Foundations with Ten
- Page 53 and 54: Neural Network Foundations with Ten
- Page 55 and 56: Neural Network Foundations with Ten
- Page 57 and 58: Neural Network Foundations with Ten
- Page 59 and 60: Neural Network Foundations with Ten
- Page 61 and 62: Neural Network Foundations with Ten
- Page 63 and 64: Neural Network Foundations with Ten
- Page 65 and 66: Neural Network Foundations with Ten
- Page 67 and 68: Neural Network Foundations with Ten
- Page 69 and 70: Neural Network Foundations with Ten
- Page 71 and 72: Neural Network Foundations with Ten
- Page 73 and 74: Neural Network Foundations with Ten
- Page 75 and 76: Neural Network Foundations with Ten
- Page 77 and 78: Neural Network Foundations with Ten
- Page 79 and 80: Neural Network Foundations with Ten
- Page 81 and 82: Neural Network Foundations with Ten
- Page 83 and 84: Neural Network Foundations with Ten
- Page 86 and 87: TensorFlow 1.x and 2.xThe intent of
- Page 88 and 89: An example to start withWe'll consi
- Page 92 and 93: • To create random values from a
- Page 94 and 95: To know the value, we need to creat
- Page 96 and 97: Chapter 2Both PyTorch and TensorFlo
- Page 98 and 99: Chapter 2state = [tf.zeros([100, 10
- Page 100 and 101: Chapter 2For now, there's no need t
- Page 102 and 103: Chapter 2Let's see an example of a
- Page 104 and 105: Chapter 2If you want to save a mode
- Page 106 and 107: Chapter 2supervised=True)train_data
- Page 108 and 109: Chapter 2There, tf.feature_column.n
- Page 110 and 111: Chapter 2print (dz_dx)print (dy_dx)
- Page 112 and 113: Chapter 2In our toy example we use
- Page 114 and 115: Chapter 2For multi-machine training
- Page 116 and 117: Chapter 25. Use tf.layers modules t
- Page 118 and 119: Chapter 2Keras or tf.keras?Another
- Page 120: • tf.data can be used to load mod
- Page 123 and 124: RegressionLet us imagine a simpler
- Page 125 and 126: RegressionTake a look at the last t
- Page 127 and 128: Regression3. Now, we calculate the
- Page 129 and 130: RegressionIn the next section we wi
- Page 131 and 132: Regression2. Now, we define the fea
- Page 133 and 134: Regression2. Download the dataset:(
- Page 135 and 136: RegressionThe following is the Tens
- Page 137 and 138: RegressionIn regression the aim is
- Page 139 and 140: RegressionThe Estimator outputs the
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 ]