Professional Documents
Culture Documents
This Code Fragment Defines A Single Layer With Artificial Neurons, and It Expects Input Variables
This Code Fragment Defines A Single Layer With Artificial Neurons, and It Expects Input Variables
Keras
The initial building block of Keras is a model, and the simplest model is
called sequential
A sequential Keras model is a linear pipeline (a stack) of neural networks
layers.
This code fragment defines a single layer with 12 artificial
neurons, and it expects 8 input variables
a neuron with sigmoid activation has a behavior similar to the perceptron, but the
changes are gradual and output values
Activation function — ReLU
nonlinear function is represented in the following graph. As you can see in the
following graph, the function is zero for negative values, and it grows linearly for
positive values
sigmoid and ReLU functions, are the basic building blocks to developing a
learning algorithm which adapts little by little, by progressively reducing the
mistakes made by our nets
In this case, we can use training examples for tuning up our net
Each MNIST image is in gray scale, and it consists of 28 x 28 pixels
one-hot encoding (OHE)
convenient to transform categorical (non-numerical) features into numerical
variables
value d in [0-9] can be encoded into a binary vector with 10 positions, which always
has 0 value for all values, except the d position where a 1 is present.
Keras provides suitable libraries
X_train, used for fine-tuning our net, and tests set X_test, used for assessing the
performance.
NB_EPOCH = 200
BATCH_SIZE = 128
VERBOSE = 1
The output is 10 classes, one for each digit.
#the values associated with each pixel are normalized in the range [0,
1] (which means that the intensity of each pixel is divided by 255, the
maximum intensity value)
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
10 outputs
# final stage is softmax
model = Sequential()
model.add(Dense(NB_CLASSES, input_shape=(RESHAPED,)))
model.add(Activation('softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer=OPTIMIZER,
metrics=['accuracy'])
Once the model is compiled, it can be then trained with the fit() function
history = model.fit(X_train, Y_train,
batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT)
Once the model is trained, we can evaluate it on the test set that contains new
unseen examples. In this way, we can get the minimal value reached by
the objective function and best value reached by the evaluation metric.
We can see in the following graph that by increasing the complexity of the model,
the run time increases significantly because there are more and more parameters
to optimize. However, the gains that we are getting by increasing the size of the
network decrease more and more as the network grows:
What is a tensor
A tensor is nothing but a multidimensional array or matrix. Both the backends are
capable of efficient symbolic computations on tensors, which are the fundamental
building blocks for creating neural networks.
There are two ways of composing models in Keras. They are as follows:
Sequential composition
Functional composition
sequential composition, where different predefined models are
stacked together in a linear pipeline of layers similar to a stack
or a queue.
predefined neural network layers
A dense model is a fully connected neural network layer
keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform',
bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,
kernel_constraint=None, bias_constraint=None)
we discussed dense nets, in which each layer is fully connected to the adjacent
layers. We applied those dense networks to classify the MNIST handwritten
characters dataset. In that context, each pixel in the input image is assigned to a
neuron for a total of 784 (28 x 28 pixels) input neurons. However, this strategy
does not leverage the spatial structure and relations of each image