09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1

For example, the digit 3 can be encoded as [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]. This type of

representation is called One-hot encoding, or sometimes simply one-hot, and is very

common in data mining when the learning algorithm is specialized in dealing with

numerical functions.

Defining a simple neural network

in TensorFlow 2.0

In this section, we use TensorFlow 2.0 to define a network that recognizes

MNIST handwritten digits. We start with a very simple neural network and then

progressively improve it.

Following Keras style, TensorFlow 2.0 provides suitable libraries (https://www.

tensorflow.org/api_docs/python/tf/keras/datasets) for loading the dataset

and splits it into training sets, X_train, used for fine-tuning our net, and test sets,

X_test, used for assessing the performance. Data is converted into float32 to use

32-bit precision when training a neural network and normalized to the range [0,1].

In addition, we load the true labels into Y_train and Y_test respectively, and

perform a one-hot encoding on them. Let's see the code.

For now, do not focus too much on understanding why certain parameters have

specific assigned values, as these choices will be discussed throughout the rest of the

book. Intuitively, EPOCH defines how long the training should last, BATCH_SIZE is

the number of samples you feed in to your network at a time, and VALIDATION is the

amount of data reserved for checking or proving the validity of the training process.

The reason why we picked EPOCHS = 200, BATCH_SIZE = 128, VALIDATION_

SPLIT=0.2, and N_HIDDEN = 128 will be clearer later in this chapter when we will

explore different values and discuss hyperparameter optimization. Let's look at our

first code fragment of a neural network in TensorFlow. Reading is intuitive but you

will find a detailed explanation in the following pages:

import tensorflow as tf

import numpy as np

from tensorflow import keras

# Network and training parameters.

EPOCHS = 200

BATCH_SIZE = 128

VERBOSE = 1

NB_CLASSES = 10 # number of outputs = number of digits

N_HIDDEN = 128

VALIDATION_SPLIT = 0.2 # how much TRAIN is reserved for VALIDATION

[ 15 ]

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

Saved successfully!

Ooh no, something went wrong!