16.03.2021 Views

Advanced Deep Learning with Keras

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

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

Introducing Advanced Deep Learning with Keras

Listing 1.3.2, mlp-mnist-1.3.2.py shows the Keras implementation of the MNIST

digit classifier model using MLP:

import numpy as np

from keras.models import Sequential

from keras.layers import Dense, Activation, Dropout

from keras.utils import to_categorical, plot_model

from keras.datasets import mnist

# load mnist dataset

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# compute the number of labels

num_labels = len(np.unique(y_train))

# convert to one-hot vector

y_train = to_categorical(y_train)

y_test = to_categorical(y_test)

# image dimensions (assumed square)

image_size = x_train.shape[1]

input_size = image_size * image_size

# resize and normalize

x_train = np.reshape(x_train, [-1, input_size])

x_train = x_train.astype('float32') / 255

x_test = np.reshape(x_test, [-1, input_size])

x_test = x_test.astype('float32') / 255

# network parameters

batch_size = 128

hidden_units = 256

dropout = 0.45

# model is a 3-layer MLP with ReLU and dropout after each layer

model = Sequential()

model.add(Dense(hidden_units, input_dim=input_size))

model.add(Activation('relu'))

model.add(Dropout(dropout))

model.add(Dense(hidden_units))

model.add(Activation('relu'))

model.add(Dropout(dropout))

model.add(Dense(num_labels))

# this is the output for one-hot vector

[ 10 ]

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

Saved successfully!

Ooh no, something went wrong!