09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

TensorFlow for Mobile and IoT and TensorFlow.js

The next step is to load the data. Fortunately, Google provides a JavaScript script

that we have called directly from our index.html file. It downloads the images and

labels from GCP storage and returns shuffled and normalized batches of image and

label pairs for training and testing. We can download this to the same folder as the

index.html file using the following command:

wget https://raw.githubusercontent.com/tensorflow/tfjs-examples/master/

mnist-core/data.js

Model definition, training, and evaluation code is all specified inside the script.js

file. The function to define and build the network is shown in the following code block.

As you can see, it is very similar to the way you would build a sequential model with

tf.keras. The only difference is the way you specify the arguments, as a dictionary of

name-value pairs instead of a list of parameters. The model is a sequential model, that

is, a list of layers. Finally, the model is compiled with the Adam optimizer:

function getModel() {

const IMAGE_WIDTH = 28;

const IMAGE_HEIGHT = 28;

const IMAGE_CHANNELS = 1;

const NUM_OUTPUT_CLASSES = 10;

const model = tf.sequential();

model.add(tf.layers.conv2d({

inputShape: [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS],

kernelSize: 5,

filters: 8,

strides: 1,

activation: 'relu',

kernelInitializer: 'varianceScaling'

}));

model.add(tf.layers.maxPooling2d({

poolSize: [2, 2], strides: [2, 2]

}));

model.add(tf.layers.conv2d({

kernelSize: 5,

filters: 16,

strides: 1,

activation: 'relu',

kernelInitializer: 'varianceScaling'

}));

model.add(tf.layers.maxPooling2d({

poolSize: [2, 2], strides: [2, 2]

}));

model.add(tf.layers.flatten());

[ 480 ]

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

Saved successfully!

Ooh no, something went wrong!