How to create Models in Keras?


In this article, we are going to learn about how to create Models in Keras

Introduction

Keras is an open-source library in Python which provides APIs for building Artificial Neural Network Models with great flexibility. Modelling in Keras can be done either using the Functional API or the Keras Sequential Model. Keras module is also available under the popular Tensorflow Library.

Latest version and installation

The latest version of keras as of writing this article is 2.1.0.

Keras can be installed from PyPI repository using pip.

Advantages of Keras for Modelling

  • Keras is used for fast implementation due to the simple API it exposes

  • Keras is Flexible and robust and provides both simple and advanced workflows for building models

  • Provides industry strength API for building robust models

  • Can also be used for quick prototyping and research work

There are two types of Keras Models:

1. Keras Sequential Model

As the name suggest Keras Sequential model lets us build models layer by layer in a sequential manner.

This helps to create models that are limited to single input and output. It is used for building simple layers.

Example snippet to show use of Keras Sequential Model.

from keras.models import Sequential from keras.layers import Dense model=Sequential() model.add(Dense(128,input_shape=(8,))) model.add(Dense(64)) model.add(Dense(32))

2.Keras Functional API

Keras Functional API provides more flexibility in terms of creating models. It can help us to create models with multiple inputs and outputs. Layers can be added and shared across as in graphs.

Keras Functional API can be used as blueprint and the graph and nodes can be used wherever required.

Example of Keras Function API –

from keras.layers import Input, Dense,Concatenate from keras.models import Model import numpy as np # This returns a tensor inputs = Input(shape=(784,)) # a layer instance is callable on a tensor, and returns a tensor x = Dense(64, activation='relu')(inputs) x = Dense(64, activation='relu')(x) predictions = Dense(10, activation='softmax')(x) # This creates a model that includes # the Input layer and three Dense layers model = Model(inputs=inputs, outputs=predictions) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) print(model.summary()) # Keras with muliple inputs input1 = Input(shape=(32,)) input2 = Input(shape=(64,)) x = Concatenate(axis=1)([input1, input2]) output = Dense(10, activation='softmax')(x) model10 = Model([input1, input2],output) print(model10.summary())

Keras using Tensorflow API [ Functional API ]

Keras is available under Tensorflow as tf.keras API. The functional API is basically centered around building directed acyclic graph (DAG) of layers. It is a graph of layers.

Example using tensorflow keras with mnist model:

import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers inputs = keras.Input(shape=(784,)) image_inputs = keras.Input(shape=(32, 32, 3)) dense_layer = layers.Dense(64, activation="relu") x = dense_layer(inputs) x = layers.Dense(64, activation="relu")(x) outputs = layers.Dense(10)(x) model = keras.Model(inputs=inputs, outputs=outputs, name="mnist") #TRAINING AND INFERENCE (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train = x_train.reshape(60000, 784).astype("float32") / 255 x_test = x_test.reshape(10000, 784).astype("float32") / 255 model.compile( loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.RMSprop(), metrics=["accuracy"], ) history = model.fit(x_train, y_train, batch_size=32, epochs=10, validation_split=0.3) test_scores = model.evaluate(x_test, y_test, verbose=2) print("Test loss:", test_scores[0]) print("Test accuracy:", test_scores[1])

Advantages of Functional API

  • In Functional API the input shape and dtype are created in advance using Input. There will be an internal check by the API for the specifications passed for the input and dtype and it also raises helpful suggestions.

  • The graph can be plotted and we can access the intermediate nodes in a graph. Such as

features_list = [layer.output for layer in vgg19.layers] feat_extraction_model = keras.Model(inputs=vgg19.input, outputs=features_list)
  • A functional API is serializable and can be stored as a single file since it is a data structure.

Disadvantages of Functional API

  • Dynamic architectures are not supported since the Functional API treats a network as a directed acyclic graph.

Conclusion

Keras is a powerful tool/ library for deep learning model implementation both for industry and as well as for prototyping owing to its simplicity and robustness. While the Sequential API helps to build quick models, where more control is needed the Functional API comes to the rescue.

Updated on: 01-Dec-2022

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements