Artificial Intelligence - Keras

Back to Course

Lesson Description

Lession - #920 Keras-Model Compilation

Previously, we concentrated on the fundamentals of how to make model utilizing keras Sequential and Functional API. This part makes sense of about how for accumulate the model. The accumulation is the last advance in making a model. When the compilation is done, we can continue on toward training phase.

Allow us to learn few concepts expected to more readily compilation the aggregation process.


In machine learning, keras Loss function is utilized to find error or deviation in the growing experience. Keras requires loss function during model arrangement process.

Keras gives many loss function in the losses module and they are as per the following −

  • mean_squared_error
  • mean_absolute_error
  • mean_absolute_percentage_error
  • mean_squared_logarithmic_error
  • squared_hinge
  • hinge
  • categorical_hinge
  • logcosh
  • huber_loss
  • categorical_crossentropy

    All above loss function acknowledges two contentions −

  • y_true − true names as tensors
  • y_pred − expectation with same shape as y_true

    Import the losses module prior to involving loss function as determined below −
    from keras import losses


    In machine learning, Optimization is a significant process which advance the input loads by looking at the forecast and the loss function. Keras gives many streamlining agent as a module, analyzers and they are as per the following:

  • SGD − Stochastic angle plunge descent optimizer.
    keras.optimizers.SGD(learning_rate = 0.01, momentum = 0.0, nesterov = False>
  • RMSprop - RMSProp optimizer.
    keras.optimizers.RMSprop(learning_rate = 0.001, rho = 0.9>
  • Adagrad − Adagrad optimizer.
    keras.optimizers.Adagrad(learning_rate = 0.01>
  • Adadelta − Adadelta optimizer.
    keras.optimizers.Adadelta(learning_rate = 1.0, rho = 0.95>
  • Adam - keras Adam optimizer.
       learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, amsgrad = False


    In machine learning, Metrics is utilized to assess the performance of your model. It is like loss function, yet not utilized in preparing process. Keras gives many measurements as a module, measurements and they are as per the following

  • accuracy
  • binary_accuracy
  • categorical_accuracy
  • sparse_categorical_accuracy
  • top_k_categorical_accuracy
  • sparse_top_k_categorical_accuracy
  • cosine_proximity
  • clone_metric

    Import the metrics module prior to involving metrics as indicated below −
    from keras import metrics

    Compile the model

    Keras model gives a method, compile(>
    to compile the model. The contention and default value of the compile(>
    strategy is as per the following
       loss = None, 
       metrics = None, 
       loss_weights = None, 
       sample_weight_mode = None, 
       weighted_metrics = None, 
       target_tensors = None
    The significant arguments are as per the following −
  • loss function
  • Optimizer
  • metrics An example code to compile the mode is as per the following −
    from keras import losses 
    from keras import optimizers 
    from keras import metrics 
    model.compile(loss = 'mean_squared_error',  
       optimizer = 'sgd', metrics = [metrics.categorical_accuracy]>

    Model training

    Models are prepared by NumPy arrays utilizing fit(>
    . The main reason for this fit function is utilized to assess your model on preparing. This can be likewise utilized for graphing model execution. It has the accompanying syntax −
    model.fit(X, y, epochs = , batch_size = >

  • X, y − It is a tuple to assess your information.
  • ages − no of times the model is required to have been assessed during training.
  • batch_size − training instances.

    Create data

    Allow us to make an random information utilizing numpy for x and y with the assistance of below referenced command −
    import numpy as np 
    x_train = np.random.random((100,4,8>
    y_train = np.random.random((100,10>
    Presently, create random validation data,
    x_val = np.random.random((100,4,8>
    y_val = np.random.random((100,10>

    Create model

    Below is the command for create simple sequential model-
    from keras.models import Sequential model = Sequential(>

    Add Layers

    from keras.layers import LSTM, Dense 
    # add a sequence of vectors of dimension 16 
    model.add(LSTM(16, return_sequences = True>
    model.add(Dense(10, activation = 'softmax'>

    Compile model

    Below command is used to compile model-
       loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy']

    Create a Multi-Layer Perceptron ANN

    Dataset Module

    Prior to creating a model, we really want to pick an issue, need to gather the necessary information and convert the information to NumPy array. Whenever information is gathered, we can set up the model and train it by utilizing the gathered information. Information collection is one of the most difficult period of AI. Keras gives an exceptional module, datasets to download the online machine learning information for the end goal of preparing. It brings the information from online server, process the information and return the information as training and test set. Allow us to check the information given by Keras dataset module. The information available in the module are as per the following,

  • CIFAR10 small image classification
  • CIFAR100 small image classification
  • IMDB Movie reviews sentiment classification
  • Reuters newswire topics classification
  • MNIST database of handwritten digits

    Below code can be utilized to load the dataset −
    from keras.datasets import mnist 
    (x_train, y_train>
    , (x_test, y_test>
    = mnist.load_data(>

    Create a model

    Allow us to pick a simple multi-layer perceptron (MLP>
    as addressed underneath and attempt to make the model utilizing Keras. The core features of the model are as per the following −
  • Input layer comprises of 784 qualities (28 x 28 = 784>
  • First hidden layer, Dense comprises of 512 neurons and 'relu' actuation work.
  • Second hidden layer, Dropout has 0.2 as its worth.
  • Third hidden layer, again Dense comprises of 512 neurons and 'relu' actuation work.
  • Fourth hidden layer, Dropout has 0.2 as its worth.
  • Fifth and last layer comprises of 10 neurons and 'softmax' actuation work.
  • Use categorical_crossentropy as loss function.
  • Use RMSprop(>
    as Optimizer.
  • Use precision as measurements.
  • Utilize 128 as bunch size.
  • Utilize 20 as ages.

    Step 1 - import the modules
    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 np
    Step 2 - load data
    (x_train, y_train>
    , (x_test, y_test>
    = mnist.load_data(>
    Step 3 - Process the data
    x_train = x_train.reshape(60000, 784>
    x_test = x_test.reshape(10000, 784>
    x_train = x_train.astype('float32'>
    x_test = x_test.astype('float32'>
    x_train /= 255 x_test /= 255 y_train = keras.utils.to_categorical(y_train, 10>
    y_test = keras.utils.to_categorical(y_test, 10>
    Step 4 - Create the model
    model = Sequential(>
    model.add(Dense(512, activation = 'relu', input_shape = (784,>
    model.add(Dense(512, activation = 'relu'>
    model.add(Dense(10, activation = 'softmax'>
    Step 5 - compile the model
    model.compile(loss = 'categorical_crossentropy',     
       optimizer = RMSprop(>
    , metrics = ['accuracy']>
    step 6 - Train the model
    history = model.fit(
       x_train, y_train, 
       batch_size = 128, 
       epochs = 20, 
       verbose = 1, 
       validation_data = (x_test, y_test>