How to do it...

We proceed with Keras as follows:

  1. As the first step, we define the type of our model. Keras offers two types of models: sequential and Model class API. Keras offers various types of neural network layers:
# Import the model and layers needed  
from keras.model import Sequential 
from keras.layers import Dense 
 
model = Sequential() 
  1. Add the layers to the model with the help of model.add(). Keras offers the option of a dense layer--for a densely connected neural network, layer 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). According to Keras documentation:

Dense implements the operation: output = activation(dot(input, kernel) + bias) where activation is the element-wise activation function passed as the activation argument, kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only applicable if use_bias is True).
  1. We can use it to add as many layers as we want, with each hidden layer being fed by the previous layer. We need to specify the input dimension only for the first layer:
#This will add a fully connected neural network layer with 32 neurons, each taking 13 inputs, and with activation function ReLU 
mode.add(Dense(32, input_dim=13, activation='relu')) )) 
model.add(10, activation='sigmoid')
  1. Once the model is defined, we need to choose a loss function and optimizers. Keras offers a variety of loss_functions: mean_squared_error, mean_absolute_error, mean_absolute_percentage_error, categorical_crossentropy; and optimizers: sgd, RMSprop, Adagrad, Adadelta, Adam, and so on. With these two decided, we can configure the learning process using compile(self, optimizer, loss, metrics=None, sample_weight_mode=None):
model.compile(optimizer='rmsprop', 
          loss='categorical_crossentropy', 
          metrics=['accuracy']) 
  1. Next, the model is trained using the fit method:
model.fit(data, labels, epochs=10, batch_size=32) 
  1. Lastly, prediction can be performed with the help of the predict method predict(self, x, batch_size=32, verbose=0):
model.predict(test_data, batch_size=10) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset