pdfcoffee
Chapter 1Stochastic Gradient Descent (SGD) (see Chapter 15, The Math Behind Deep Learning)is a particular kind of optimization algorithm used to reduce the mistakes madeby neural networks after each training epoch. We will review SGD and otheroptimization algorithms in the next chapters. Once the model is compiled, itcan then be trained with the fit() method, which specifies a few parameters:• epochs is the number of times the model is exposed to the training set. Ateach iteration the optimizer tries to adjust the weights so that the objectivefunction is minimized.• batch_size is the number of training instances observed before theoptimizer performs a weight update; there are usually many batchesper epoch.Training a model in TensorFlow 2.0 is very simple:# Training the model.model.fit(X_train, Y_train,batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=VERBOSE, validation_split=VALIDATION_SPLIT)Note that we've reserved part of the training set for validation. The key idea isthat we reserve a part of the training data for measuring the performance on thevalidation while training. This is a good practice to follow for any machine learningtask, and one that we will adopt in all of our examples. Please note that we willreturn to validation later in this chapter when we talk about overfitting.Once the model is trained, we can evaluate it on the test set that contains newexamples never seen by the model during the training phase.Note that, of course, the training set and the test set are rigorously separated. Thereis no point evaluating a model on an example that was already used for training. InTensorFlow 2.0 we can use the method evaluate(X_test, Y_test) to compute thetest_loss and the test_acc:#evaluate the modeltest_loss, test_acc = model.evaluate(X_test, Y_test)print('Test accuracy:', test_acc)So, congratulations! You have just defined your first neural network in TensorFlow2.0. A few lines of code and your computer should be able to recognize handwrittennumbers. Let's run the code and see what the performance is.[ 19 ]
Neural Network Foundations with TensorFlow 2.0Running a simple TensorFlow 2.0 net andestablishing a baselineSo let's see what happens when we run the code:Figure 13: Code ran from our test neural networkFirst, the net architecture is dumped and we can see the different types of layersused, their output shape, how many parameters (that is, how many weights) theyneed to optimize, and how they are connected. Then, the network is trained on48,000 samples, and 12,000 are reserved for validation. Once the neural model isbuilt, it is then tested on 10,000 samples. For now, we won't go into the internals ofhow the training happens, but we can see that the program runs for 200 iterationsand each time accuracy improves. When the training ends, we test our model on thetest set and we achieve about 89.96% accuracy on training, 90.70% on validation, and90.71% on test:Figure 14: Results from testing model, accuracies displayedThis means that nearly 1 in 10 images are incorrectly classified. We can certainly dobetter than that.[ 20 ]
- Page 4 and 5: packt.comSubscribe to our online di
- Page 6 and 7: I want to thank my kids, Aurora, Le
- Page 8 and 9: Sujit Pal is a Technology Research
- Page 10 and 11: Table of ContentsPrefacexiChapter 1
- Page 12 and 13: [ iii ]Table of ContentsConverting
- Page 14 and 15: Table of ContentsSo what is the pro
- Page 16 and 17: [ vii ]Table of ContentsChapter 10:
- Page 18 and 19: Table of ContentsPretrained models
- Page 20 and 21: PrefaceDeep Learning with TensorFlo
- Page 22 and 23: • Supervised learning, in which t
- Page 24 and 25: PrefaceThe complexity of deep learn
- Page 26 and 27: PrefaceFigure 5: Adoption of deep l
- Page 28 and 29: Chapter 1, Neural Network Foundatio
- Page 30 and 31: PrefaceChapter 13, TensorFlow for M
- Page 32 and 33: ConventionsThere are a number of te
- Page 34: PrefaceReferences1. Deep Learning w
- Page 37 and 38: Neural Network Foundations with Ten
- 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: 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 90 and 91: Chapter 23. Placeholders: Placehold
- 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
Chapter 1
Stochastic Gradient Descent (SGD) (see Chapter 15, The Math Behind Deep Learning)
is a particular kind of optimization algorithm used to reduce the mistakes made
by neural networks after each training epoch. We will review SGD and other
optimization algorithms in the next chapters. Once the model is compiled, it
can then be trained with the fit() method, which specifies a few parameters:
• epochs is the number of times the model is exposed to the training set. At
each iteration the optimizer tries to adjust the weights so that the objective
function is minimized.
• batch_size is the number of training instances observed before the
optimizer performs a weight update; there are usually many batches
per epoch.
Training a model in TensorFlow 2.0 is very simple:
# Training the model.
model.fit(X_train, Y_train,
batch_size=BATCH_SIZE, epochs=EPOCHS,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT)
Note that we've reserved part of the training set for validation. The key idea is
that we reserve a part of the training data for measuring the performance on the
validation while training. This is a good practice to follow for any machine learning
task, and one that we will adopt in all of our examples. Please note that we will
return to validation later in this chapter when we talk about overfitting.
Once the model is trained, we can evaluate it on the test set that contains new
examples never seen by the model during the training phase.
Note that, of course, the training set and the test set are rigorously separated. There
is no point evaluating a model on an example that was already used for training. In
TensorFlow 2.0 we can use the method evaluate(X_test, Y_test) to compute the
test_loss and the test_acc:
#evaluate the model
test_loss, test_acc = model.evaluate(X_test, Y_test)
print('Test accuracy:', test_acc)
So, congratulations! You have just defined your first neural network in TensorFlow
2.0. A few lines of code and your computer should be able to recognize handwritten
numbers. Let's run the code and see what the performance is.
[ 19 ]