Keras

Keras Model Compilation

Keras Model Compilation

In this chapter, we are going to discuss various concepts that are required to understand the compilation process. These concepts include:

Optimizer: Optimizer is used to optimize the input weights by checking the prediction results and loss function. There are various optimizers that can be used as a module shown below:

To use SGD (Stochastic Gradient Descent) optimizer:

keras.optimizers. SGD(learning_rate = 0,1, momentum = 0.1, nesterov = False)

You can also use Adam optimizer with the following code:

keras.optimizer.Adam(
	learning_rate = 0.01, beta_1 = 0.7, beta_2 = 0.9, amsgrad = False
)

This way you can implement various kinds of optimizers that include: Adadelta, Nadam, Adamax, RMSprop, and Adadelta. 

  • Compilation: In Keras, you can use compile() method to implement the compilation process. The default value of the compile method is shown below
compile(
   optimizer, 
   los = None, 
   mtris = None, 
   los_wghts = None, 
   smpl_wght_mode = None, 
   wghtd_mtris = None, 
   trgt_tnsrs = None
)

Some arguments that are implemented using the Compilation method are as follows: 

  • Optimizer
  • Loss Function
  • Metrics

The sample code for compilation is shown below:

from keras import losses 
from keras import optimizers 
from keras import metrics 

model.compile(los = 'mean_sqard_err',  
   optimzr = 'sgd', metrics = [metrics.categorical_accuracy])

Here, the loss function is set as mean_sqard_err, the optimizer is set as sgd, and metrics are set with the name metrics.categorical_accuracy. 

  • Multi-layer Perceptron ANN: At this point, I assume you have learned to create and compile Keras models. Now you can apply what you’ve learned so far and create a simple Model based on ANN. 

The steps required to create a multi-layer perceptron ANN are shown below:

Step – 1: Import the required modules
Step – 2: Load the dataset
Step – 3: Process the dataset
Step – 4: Create the model and compile it
Step – 5: Train the model and test it

So, these are the steps that you are required to follow to create a Multi-layer perceptron ANN model and the final code snippet will look like this:

import keras 
from keras.datasets import mnist 
from keras.models import Sequential 
from keras.layers import Dense, Dropout 
from keras.optimizers import RMSprop 
import numpy as npy 

(model_train, model2_train), (model_test, model2_test) = mnist.load_data() 

import keras 
from keras.datasets import mnist 
from keras.models import Sequential 
from keras.layers import Dense, Dropout 
from keras.optimizers import RMSprop 
import numpy as npy 

(model_train, model2_train), (model_test, model2_test) = mnist.load_data() 
model_train = model_train.reshape(80000, 1245) 
model_test = model_test.reshape(10000, 1245) 
model_train = model_train.astype('float32') 
model_test = model_test.astype('float32') 
model_train /= 255 
model_test /= 255 
model2_train = keras.utils.to_categorical(model2_train, 10) 
model2_test = keras.utils.to_categorical(model2_test, 10) 
model = Sequential() 
model.add(Dense(512, activation='relu', input_shape = (1245,))) 
model.add(Dropout(0.2)) 
model.add(Dense(512, activation = 'relu')) model.add(Dropout(0.2)) 
model.add(Dense(10, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', 
   optimizer = RMSprop(), 
   metrics = ['speed']) 

history_of_models = model.fit(model_train, model2_train, 
   batch_size = 128, epochs = 20, verbose = 1, validation_data = (model_test, model2_test))

You can test this code yourself and find the results that will help you to better understand these concepts. 

  • Loss: This function is used to find the errors that occurred in the learning process. In Keras, a loss function is required at the time of compilation. 

A few loss functions in the loss module include:

  • hinge
  • mean_squared_error
  • mean_squared_logarithmic_error
  • poission
  • is_categorical_crossentropy
  • mean_absolute_error
  • logcosh
  • binary_crossentropy
  • cosine_proximity
  • sparse_categorical_crossentropy
  • squared_hinge

Keras - Model Evaluation and Prediction

In this chapter, you will understand Model Evalutaion and Model Prediction in Keras.

 Model Evaluation: 

It is a process that can be used for creating a model and checking if the model is best for the given problem. You can evaluate a function provided by the Keras model. To evaluate a Keras model, you can use the test data as follows:

score = model.evaluate(model_test, model2_test, verbose = 0) 
print('The test loss score is:', score[0]) 
print('The test accuracy score is:', score[1])

After the execution of the code, you will find accurate results. This accurate result will help you to improve the working of your model. 

Model Prediction:

As the name suggests prediction is a process where some decisions are made based on some analysis. In our case, Keras provides a method that can be used to train our Prediction model. To implement the prediction method, you can follow the code snippet below:

predict(
   x, 
   batch_size = None, 
   verbose = 0, 
   total_steps = None, 
   callback_methods = None, 
   max_queue_size = 10, 
   total_workers = 1, 
   use_multiprocessors = False
)

Now, let us predict our MPL model by using the code below:

predict_mpl = model.predict(x_test) 
predict_mpl = np.argmax(pred, axis = 1) [:5] 
text = np.argmax(model2_test,axis = 1) [:5] 

print(predict_mpl) 
print(text)

You can try these coding snippets that will help you predict correctly your Keras model. 

Top course recommendations for you

    Circular Queue
    1 hrs
    Beginner
    3.2K+ Learners
    4.53  (224)
    Python Stack
    2 hrs
    Beginner
    6.2K+ Learners
    4.41  (400)
    AI and Big Data in IOT
    1 hrs
    Intermediate
    10.7K+ Learners
    4.48  (1308)
    Selenium Basics
    1 hrs
    Beginner
    25.9K+ Learners
    4.43  (2066)
    Selenium with Python
    1 hrs
    Beginner
    13.5K+ Learners
    4.45  (903)
    Selenium Projects with Python
    2 hrs
    Intermediate
    9K+ Learners
    4.52  (281)
    Java Projects
    1 hrs
    Beginner
    25.8K+ Learners
    4.19  (247)
    JDBC in Java
    1 hrs
    Beginner
    9.7K+ Learners
    4.42  (1008)
    Flask Python
    1 hrs
    Beginner
    8.7K+ Learners
    4.38  (634)
    Linked List in Python
    3 hrs
    Beginner
    3K+ Learners
    4.56  (70)