18 Chapter 16, Deep Learning
=================================================================
conv2d_1 (Conv2D) (None, 26, 26, 64) 640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 13, 13, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 11, 11, 128) 73856
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 3200) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 409728
_________________________________________________________________
dense_2 (Dense) (None, 10) 1290
=================================================================
Total params: 485,514
Trainable params: 485,514
Non-trainable params: 0
_________________________________________________________________
Which of the following statements is false?
a. A model’s summary method shows you the model’s layers.
b. The parameters are the weights that the network learns during training. Our
relatively small convnet, needs to learn nearly 500,000 parameters.
c. In the Output Shape column, None simply means that the model does not know
in advance how many training samples you’re going to provide—this is known
only when you start the training.
d. By default, Keras trains only the parameters that most affect prediction accu-
racy.
16.6 Q30: Which of the following statements a), b) or c) is false?
a. You can visualize the model summary using the plot_model function from the
module tensorflow.keras.utils, as in:
from tensorflow.keras.utils import plot_model
from IPython.display import Image
plot_model(cnn, to_file=‘convnet.png’, show_shapes=True,
show_layer_names=True)
b. Module IPython.display’s Image class can be used to load an image into a
Jupyter Notebook and display the image in the notebook.
c. Keras assigns the layer names in the image.
Chapter 16, Deep Learning 19
d. All of the above statements are true.
16.6 Q31: Once you’ve added all the layers to a Keras neural network, you com-
plete the Keras model by calling its compile method, as in:
cnn.compile(optimizer=‘adam’,
loss=‘categorical_crossentropy’,
metrics=[‘accuracy’])
Which of the following statements about the arguments is false?
a. optimizer=’adam’ specifies the optimizer this model will use to adjust the
weights throughout the neural network as it learns.
b. There are many optimizers —‘adam’ performs well across a wide variety of
models.
c. loss=’categorical_crossentropy’ specifies the loss function used by the
optimizer in multi-classification networks like our convnet, which predicts 10
classes. As the neural network learns, the optimizer attempts to maximize the
values returned by the loss function. The greater the loss, the better the neural
network is at predicting what each image is.
d. metrics=[‘accuracy’]—This is a list of the metrics that the network will
produce to help you evaluate the model. We use the accuracy metric to check
the percentage of correct predictions.
16.6.5 Training and Evaluating the Model
16.6 Q32: You train a Keras model by calling its fit method. Which of the fol-
lowing statements about the fit method is false?
a. As in Scikit-learn, the first two arguments are the training data and the categor-
ical target labels.
b. The iterations argument specifies the number of times the model should
process the entire set of training data.
c. batch_size specifies the number of samples to process at a time during each
epoch. Most models specify a power of 2 from 32 to 512. Larger batch sizes can
decrease model accuracy.
d. In general, some samples should be used to validate the model. If you specify
validation data, after each epoch, the model will use it to make predictions and
display the validation loss and accuracy. You can study these values to tune your
20 Chapter 16, Deep Learning
layers and the fit method’s hyperparameters, or possibly change the layer com-
position of your model.
16.6 Q33: Which of the following statements a), b) or c) is false?
a. TensorBoard is a TensorFlow tool for visualizing data from your deep-learning
models as they execute.
b. You can view TensorFlow charts showing how the training and validation ac-
curacy and loss values change through the epochs.
c. Andrej Karpathy’s ConvnetJS tool, trains convnets in your web browser and dy-
namically visualizes the layers’ outputs, including what each convolutional layer
“sees” as it learns.
d. All of the above statements are true.
16.6 Q34: Consider the following code, which evaluates our convnet model using
the MNIST test data:
[38]: loss, accuracy = cnn.evaluate(X_test, y_test)
10000/10000 [==============================] – 4s 366us/step
[39]: loss
[39]: 0.026809450998473768
[40]: accuracy
[40]: 0.9917
Which of the following statements a), b) or c) is false?
a. You can check the accuracy of a model on data the model has not yet seen. To
do so, call the model’s evaluate method, which displays as its output how long
it took to process the test samples.
b. According to the output of the preceding snippet, our convnet model is 99.17%
accurate when predicting the labels for unseen data.
c. With a little online research, you can find models that can predict MNIST with
nearly 100% accuracy.
d. Each of the above statements is true.
16.6 Q35: Which of the following statements a), b) or c) is false?
Chapter 16, Deep Learning 21
a. Calling our MNIST cnn model’s predict method as shown below predicts the
classes of the digit images in its argument array (X_test):
predictions = cnn.predict(X_test)
b. You can check what the first sample digit should be by looking at y_test[0]:
[42]: y_test[0]
[42]: array([0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], dtype=float32)
The one-hot encoding in the preceding output shows that the first sample is the
digit 7.
c. The following code outputs the probabilities returned by the predict method
for the first test sample:
[43]: for index, probability in enumerate(predictions[0]):
print(f‘{index}: {probability:.10%}‘)
0: 0.0000000201%
1: 0.0000001355%
2: 0.0000186951%
3: 0.0000015494%
4: 0.0000000003%
5: 0.0000000012%
6: 0.0000000000%
7: 99.9999761581%
8: 0.0000005577%
9: 0.0000011416%
According to the preceding output, predictions[0] indicates that our cnn
model believes this digit is a 7 with nearly 100% certainty. Not all predictions
have this level of certainty.
d. All of the above statements are true.
16.6.6 Saving and Loading a Model
16.6 Q36: Which of the following statements a), b) or c) is false?
a. Neural network models can require significant training time. Once you’ve de-
signed and tested a model that suits your needs, you can save its state. This allows
you to load it later to make more predictions. Sometimes models are loaded and
further trained for new problems. For example, layers in our model already know
how to recognize features such as lines and curves, which could be useful in
22 Chapter 16, Deep Learning
handwritten character recognition as well. This process is called transfer learn-
ing—you transfer an existing model’s knowledge into a new model.
b. A Keras model’s save method stores the model’s architecture and state infor-
mation in a format called Hierarchical Data Format (HDF5). Such files use the .h5
file extension.
c. You can load a saved model with the load_model function from the tensor-
flow.keras.models module, as in:
from tensorflow.keras.models import load_model
cnn = load_model(‘mnist_cnn.h5′)
You can then invoke the loaded model’s methods. For example, if you’ve acquired
more data, you could call the model’s predict method to make additional pre-
dictions on new data, or you could call the model’s fit method to start training
with the additional data.
d. All of the above statements are true.
16.7 Visualizing Neural Network Training with
TensorBoard
16.7 Q1: Which of the following statements a), b) or c) is false?
a. With deep learning networks, there’s so much complexity and so much going
on internally that’s hidden from you that it’s difficult to know and fully under-
stand all the details. This creates challenges in testing, debugging and updating
models and algorithms.
b. Deep learning learns the features but there may be enormous numbers of them,
and they may not be apparent to you.
c. Google provides the TensorBoard tool for visualizing neural networks imple-
mented in TensorFlow and Keras. A TensorBoard dashboard visualizes data from
a deep learning model that can give you insights into how well your model is
learning and potentially help you tune its hyperparameters.
d. All of the above statements are true.
16.7 Q2: Which of the following statements a), b) or c) is false?
a. TensorBoard monitors a folder you specify looking for files output by models
during training.
b. TensorBoard loads the data from that folder into a browser-based dashboard.
c. TensorBoard can load data from multiple models at once and you can choose
which to visualize. This makes it easy to compare several different models or mul-
tiple runs of the same model.
Chapter 16, Deep Learning 23
d. All of the above statements are true.
16.7 Q3: To use TensorBoard, before you fit your model, you need to configure
a TensorBoard object, which the model will use to write data into a specified
folder that TensorBoard monitors. This TensorBoard object is known as a
________ in Keras.
a. callforward
b. entry point
c. callback
d. None of the above.
16.7 Q4: The following code creates a TensorBoard object:
from tensorflow.keras.callbacks import TensorBoard
import time
tensorboard_callback = TensorBoard(
log_dir=f‘./logs/mnist{time.time()}‘,
histogram_freq=1, write_graph=True)
Which of the following statements a), b) or c) about the above code is false?
a. The log_dir argument is the name of the folder in which this model’s log files
will be written.
b. The notation ‘./logs/’ indicates that we’re creating a new folder within the
logs folder you created previously. The preceding code follows that folder with
‘/mnist’ and the current time. Using the time ensures that each new execution
of the notebook will have its own log folder. That will enable you to compare mul-
tiple executions in TensorBoard.
b. The histogram_freq argument is the frequency in epochs that Keras will out-
put to the model’s log files. In this case, we’ll write data to the logs for every epoch.
c. When the write_graph argument is True, a graph of the model will be output.
You can view the graph in the GRAPHS tab in TensorBoard.
d. All of the above statements are true.
24 Chapter 16, Deep Learning
16.8 ConvnetJS: Browser-Based Deep-Learning Training
and Visualization
No questions.
16.9 Recurrent Neural Networks for Sequences;
Sentiment Analysis with the IMDb Dataset
16.9 Q1: Which of the following statements a), b) or c) is false?
a. Our convnet used stacked layers that were applied sequentially. Non-sequen-
tial models are possible with recurrent neural networks.
b. A recurrent neural network (RNN) processes sequences of data, such as time
series or text in sentences.
c. The term “recurrent” comes from the fact that the neural network contains
loops in which the output of a given layer becomes the input to that same layer in
the next time step.
d. All of the above statements are true.
16.9 Q2: Which of the following statements is false?
a. In a time series, a time step is the next point in time.
b. In a text sequence, a “time step” would be the next word in a sequence of words.
c. The looping in convolutional neural networks enables them to learn and re-
member relationships among the data in the sequence.
d. The word “good” on its own has positive sentiment. However, when preceded
by “not,” which appears earlier in the sequence, the sentiment becomes negative.
16.9 Q3: Which of the following statements a), b) or c) is false?
a. RNNs for text sequences take into account the relationships among the earlier
and later parts of a sequence.
b. When determining the meaning of text there can be many words to consider
and an arbitrary number of words in between them.
c. A Long Short-Term Memory (LSTM) layer makes a neural network convolu-
tional and is optimized to handle learning from sequences.
d. All of the above statements are true.
26 Chapter 16, Deep Learning
b. The arrays y_train and X_test are one-dimensional arrays containing 1s and
0s, indicating whether each review is positive or negative.
c. Based on the outputs from the snippets in Part (a), X_train and X_test appear
to be one-dimensional. However, their elements actually are lists of integers, each
representing one review’s contents, as shown in the code below:
[8]: %pprint
[8]: Pretty printing has been turned OFF
[9]: X_train[123]
[9]: [1, 307, 5, 1301, 20, 1026, 2511, 87, 2775, 52, 116,
5, 31, 7, 4, 91, 1220, 102, 13, 28, 110, 11, 6, 137, 13,
115, 219, 141, 35, 221, 956, 54, 13, 16, 11, 2714, 61, 322,
423, 12, 38, 76, 59, 1803, 72, 8, 2, 23, 5, 967, 12, 38,
85, 62, 358, 99]
d. All of the above statements are true:
16.9 Q6: Which of the following statements a), b) or c) is false?
a. Because IMDb movie reviews are numerically encoded in the dataset bundled
with Keras, to view their original text, you need to know the word to which each
number corresponds.
b. Keras’s IMDb dataset provides a dictionary that maps the words to their in-
dexes. Each word’s corresponding value is its frequency ranking among all the
words in the entire set of reviews.
c. In the dictionary mentioned in Part (b), the word with the ranking 1 is the most
frequently occurring word (calculated by the Keras team from the dataset), the
word with ranking 2 is the second most frequently occurring word, and so on.
Though the dictionary values begin with 1 as the most frequently occurring word,
in each encoded review, the ranking values are offset by 3. So any review contain–
ing the most frequently occurring word will have the value 4 wherever that word
appears in the review.
d. All of the above statements are true.
16.9 Q7: Which of the following statements a), b) or c) is false regarding decoding
IMDb movie reviews?
Chapter 16, Deep Learning 27
a. The following snippet gets the word-to-index dictionary by calling the function
get_word_index from the tensorflow.keras.datasets.imdb module:
[10]: word_to_index = imdb.get_word_index()
b. The word ‘great’ might appear in a positive movie review, so the following
code checks whether it’s in the dictionary:
[11]: word_to_index[‘great’]
[11]: 84
c. According to the Part (b) output, ‘great’ is the dataset’s 84th most frequent
word. If you use an expression like the one in Part (b) to look up a word that’s not
in the dictionary, you’ll get an exception.
d. All of the above statements are true.
16.9.3 Data Preparation
16.9 Q8: Which of the following statements about the IMDb movie reviews da-
taset a), b) or c) is false?
a. The number of words per review varies, but the Keras requires all samples to
have the same dimensions.
b. To use the IMDb dataset for deep learning, we need to restrict every review to
the same number of words.
c. When performing the data preparation in Part (b), some reviews will need to
be padded with additional data and others will need to be truncated.
d. All of the above statements are true.
16.9 Q9: Which of the following statements is false?
a. The pad_sequences utility function (module tensorflow.keras.prepro-
cessing.sequence) reshapes the rows in an array of to the number of features
specified by the maxlen argument (200) and returns a two-dimensional array:
[16]: words_per_review = 200
[17]: from tensorflow.keras.preprocessing.sequence import
pad_sequences
[18]: X_train = pad_sequences(X_train,
maxlen=words_per_review)
28 Chapter 16, Deep Learning
b. If a sample has more features, pad_sequences truncates it to the specified
length.
c. If a sample has fewer features, pad_sequences adds space characters to the
beginning of the sequence to pad it to the specified length.
d. You can confirm X_train’s new shape with the array’s shape attribute:
[19]: X_train.shape
[19]: (25000, 200)
16.9.4 Creating the Neural Network
16.9 Q10: Which of the following statements a), b) or c) is false?
a. We’ve used one-hot encoding to convert the MNIST dataset’s integer labels into
categorical data. The result for each label was a vector in which all but one ele-
ment was 0. We could also do that for the index values that represent the words
in the IMDb dataset.
b. For our IMDb example that processes 10,000 unique words, we’d need a
10,000-by–10,000 array to represent all the words. That’s 100,000,000 elements,
and almost all the array elements would be 0. This is not an efficient way to en-
code the data.
c. If we were to process all 88,000+ unique words in the IMDb dataset, we’d need
an array of nearly eight billion elements.
d. All of the above statements are true.
16.9 Q11: Which of the following statements a), b) or c) is false?
a. To reduce ambiguity, RNNs that process text sequences typically begin with an
embedding layer that encodes each word in a compact dense-vector representa-
tion.
b. The vectors produced by the embedding layer also capture the word’s con-
text—that is, how a given word relates to the words around it.
c. An embedding layer enables the RNN to learn word relationships among the
training data.
d. All of the above statements are true.
30 Chapter 16, Deep Learning
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: d.
16.9.5 Training and Evaluating the Model
16.9 Q16: A Keras model’s ________ method returns the loss and accuracy values
of a trained model.
a. assess
b. account
c. grade
d. evaluate
16.10 Tuning Deep Learning Models
16.10 Q1: Which of the following are variables that affect model performance?
a. having more or less data to train with, having more or less to test with
b. having more or less to validate with, having more or fewer layers
c. the types of layers you use and the order of the layers
d. All of the above
16.10 Q2: Which of the following statements is false?
a. The compute time required to train models multiple times is significant so, in
deep learning, you generally tune hyperparameters with techniques like k-fold
cross-validation and grid search.
b. There are various tuning techniques, but one particularly promising area is au-
tomated machine learning (AutoML).
c. Auto-Keras (https://autokeras.com/) is geared to automatically choosing the
best configurations for your Keras models.
d. Google’s Cloud AutoML and Baidu’s EZDL are among various other automated
machine learning efforts.
16.11 Convnet Models Pretrained on ImageNet
16.11 Q1: Moving the weights learned by a deep-learning model for a similar
problem into a new model is called ________ learning.
a. assignment
b. transfer
Chapter 16, Deep Learning 31
c. relegation
d. relocation
16.11 Q2: Which of the following statements a), b) or c) is false?
a. ImageNet is limited in size, so it can be trained efficiently on most computers.
b. You can reuse just the architecture of each model and train it with new data, or
you can reuse the pretrained weights.
c. ImageNet now has a continuously running challenge on the Kaggle competition
site called the ImageNet Object Localization Challenge. The goal is to identify “all
objects within an image, so those images can then be classified and annotated.”
There’s no obvious optimal solution for many machine learning and deep learn-
ing tasks.
d. All of the above statements are true.
16.11 Q3: Which of the following statements is false?
a. On Kaggle, companies and organizations fund competitions where they encour-
age people worldwide to develop better-performing solutions than they’ve been
able to do for something that’s important to their business or organization.
b. Sometimes companies offer prize money, which has been as high as $1,000,000
on the famous Netflix competition.
c. Netflix wanted to get a 100% or better improvement in their model for deter-
mining whether people will like a movie, based on how they rated previous ones.
They used the results to help make better recommendations to members.
d. Even if you do not win a Kaggle competition, it’s a great way to get experience
working on challenging problems of current interest.
16.12 Reinforcement Learning
16.12 Q1: Which of the following statements is false?
a. Reinforcement learning is a form of machine learning in which algorithms learn
from their environment, similar to how humans learn—for example, a video game
enthusiast learning a new game, or a baby learning to walk or recognize its par-
ents.
32 Chapter 16, Deep Learning
b. Reinforcement learning implements an agent that learns by trying to perform
a task, receiving feedback about success or failure, making adjustments then try-
ing again. The goal is to minimize the loss function.
c. The agent receives a positive reward for doing a right thing and a negative re-
ward (that is, a punishment) for doing a wrong thing.
d. The agent uses this information to determine the next action to perform and
must try to maximize the reward.
16.12 Q2: Which of the following statements is false?
a. Reinforcement learning was used in some key artificial-intelligence milestones
that captured people’s attention and imagination.
b. In 2011, IBM’s Watson beat the world’s two best human Jeopardy! players in a
$1 million match.
c. In the Jeopardy! competition, Watson simultaneously executed hundreds of lan-
guage-analysis algorithms to locate correct answers in 200 million pages of con-
tent (including all of Facebook) requiring four terabytes of storage.
d. Watson was trained with machine learning and used reinforcement learning
techniques to learn the game-playing strategies (such as when to answer, which
square to pick and how much money to risk on daily doubles).
16.12 Q3: Which of the following statements a), b) or c) is false?
a. Go—a board game created in China thousands of years ago—is widely consid-
ered to be one of the most complex games ever invented with 10170 possible
board configurations.
b. To give you a sense of how large Go’s possible number of board configurations
is, it’s believed that there are (only) between 1078 and 1087 atoms in the known
universe!
c. In 2015, AlphaGo—created by Google’s DeepMind group—used deep learning
with two neural networks to beat the European Go champion Fan Hui.
d. All of the above statements are true.
16.12 Q4: Which of the following statements a), b) or c) is false?
Chapter 16, Deep Learning 33
a. Google generalized its AlphaGo AI to create AlphaZero—a game-playing AI that
uses reinforcement learning to teach itself to play other games.
b. In December 2017, AlphaZero learned the rules of and taught itself to play
chess in less than four hours. It then beat the world champion chess program,
Stockfish 8, in a 100-game match—winning or drawing every game.
c. After training itself in Go for just eight hours, AlphaZero was able to play Go vs.
its AlphaGo predecessor, winning 60 of 100 games.
d. All of the above statements are true.
16.12.1 Deep Q-Learning
16.12 Q5: Which of the following statements a), b) or c) is false?
a. One of the most popular reinforcement learning techniques is Deep Q-Learning,
which was originally described in the Google DeepMind team’s paper “Playing
Atari with Deep Reinforcement Learning.”
b. Using Deep Q-Learning, the DeepMind team was able to develop an agent that
learned to play Atari video games by observing how the users manipulated the
controls on the game controllers.
c. In Q-Learning, a Q function determines the reward using a combination of the
environment’s current state and the action the agent performs. For example, if
the agent is trying to learn how to avoid obstacles, every move the agent makes
that does not hit an obstacle would get a positive reward and every move that
collides with an obstacle would get a negative reward (that is, a punishment).
d. All of the above statements are true.
16.12.2 OpenAI Gym
No questions.