76
3.5.1
Cuapter 3 Introduction to Keras and TensorFlow
= Backpropagation, a way to compute the gradient of mathematical expressions
(handled in TensorFlow via the Gradient Tape object)
= Second, high-level deep learning concepts. This translates to Keras APIs
= Layers, which are combined into a model
= A loss function, which defines the feedback signal used for learning
= An optimizer, which determines how learning proceeds
= Metrics to evaluate model performance, such as accuracy
— A training loop that performs mini-batch stochastic gradient descent
In the previous chapter, you already had a first light contact with some of the corre-
sponding TensorFlow and Keras APIs: you've briefly used TensorFlow's Variable class,
the matmul operation, and the Gradient Tape. You've instantiated Keras Dense layers,
packed them into a Sequential model, and trained that model with the £it ()
method.
Now let's take a deeper dive into how all of these different concepts can be
approached in practice using TensorFlow and Keras.
Constant tensors and variables
To do anything in TensorFlow, we're going to need some tensors. Tensors need to be
created with some initial value. For instance, you could create all-ones or all-zeros ten-
sors (sce listing 3.1), or tensors of values drawn from a random distribution (see liste
ing 3.2)
a
Cr koe
bop import tensorfiow as cf
pop x = tf.ones (ehape=(2, 1))
7 Equivalent to
So> print (x) a
nnp.enes(shape=(2, 1))
tf Tensor(
(24
[2.1), shapes (2, 1), dtypestioars2}
po> x 6 tf. zeros (shapes(2, 2) Equivalent to
bo> print (x) npzeros(shape=(2, 1))
tf. Tensor (
[0.7
[0.1], shape=(2, 1), dtype=float32)
re
por x = tf random.normal (shapes (2, 1), mean=0., stadevet.) — ¢———
ze print) Tester of randons vain dan to eral tution
with mean 0 and standard deviation 1. Equivalent to
U1-0, 14208166) np.random.normal(size=(3, 1), loe=0., scale=1.).
[-0, 95319825] oe *
[hidosesa2 1), shage=(2, 2 oat32)
pee print (x) Tensor of random values drawn from a uniform distrit on between 0
tf-Tensor( and 1. Equivalent to np.randem.uniform(size=(3, 1), low=0., high=1First steps with TensorFlow 7
[[0.33779848)
[o,06682922)
[0.7749384 }), shape
3, 1), dtype=tloat32)
A significant difference between NumPy arrays and TensorFlow tensors is that Tensor-
Flow tensors aren't assignable: they're constant. For instance, in NumPy, you can do
the following
[SITE Munn ee a
import nunpy as 2p
x = np.ones(shape=(2, 2))
x19, 0] = 0
Try to do the same thing in TensorFlow, and you will get an error: “EagerTensor object
does not support item assignment.”
(ELM kee ed
x = tf.ones(shapes(2, 2)) | This will fall, asa
x10, 0] = 0 tensor isn’t assign
To tain a model, we'll need to update its state, which is a set of tensors. If tensors
aren't assignable, how do we do it? That's where variables come in, tf Variable is the
class meant to manage modifiable state in TensorFlow. You've already briefly seen it in
action in the training loop implementation at the end of chapter 2.
To create a variable, you need to provide some initial value, stich as a random tensor
Listing 3.5 Creating a TensorFlow variable
p>> v = Uf.Variable(initial_value-tf.random.normal (shape= (3, 1)))
oo> print (v)
array ([[-0, 751339731,
[-0.as72as3 1,
[ 1.6626a85 1], atype=floas32) >
‘The state of a variable can be modified via its assign method, as follows.
Listing 3.6 Assigning a value to a TensorFlow variable
boo v.aeeign(e£.ones((3, 2)))
array(((1.1,
aa,
(2.11, atypestloat32) >
It also works for a subset of the coefficients.78
3.5.2
3.5.3
Cuapter 3 Introduction to Keras and TensorFlow
Listing 3.7 Assigning a value to a subset of a TensorFlow variable
>>> v0, 0] assign (3.)
array(([3.1,
aa,
[1.1], dtypesfioat3a) >
Similarly, assign_add() and assign_sub() are efficient equivalents of += and -=, as
shown next
Eee
pos v.assign_add(tf.ones((3, 1)))
array(((2.1,
ul,
[2.1], avype=float3a) >
Tensor operations: Doing math in TensorFlow
Just like NumPy, TensorFlow offers a large collection of tensor operations to express
mathematical formulas. Here are a few examples.
Listing 3.9 _A few basic math operations
Sy Pomme ((2, 23) | Tae the square
b = tf square(a)
© = tf.sart (a)