Keras Modules – Types and Examples

Free Keras course with real-time projects Start Now!!

Hope you are enjoying DataFlair keras tutorials. Let us now move to the next topic which is Keras Modules.

Keras modules provide various predefined classes and functions for deep learning algorithms. In this Keras tutorial, we will learn various modules in Keras. We will study the features and few of the applications of these modules.

Keras Modules

Keras Modules

Various Modules available in keras are:

  • Backend
  • Utils
  • Image Processing
  • Sequence Processing
  • Text Processing
  • Callback

1. Backend module of Keras

Keras is a high-level API, it does not focus on backend computations. Keras allows users to study its backend and make changes to some level in its backend. For this task, Keras provides a backend module.

Its default configuration is stored at $Home/keras/keras.json file.

It looks like:

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

You can also write some code compatible with its backend.

from Keras import backend as K

b=K.random_uniform_variable(shape=(3,4),low=0,high=1)
c=K.random_uniform_variable(shape=(3,4),mean=0,scale=1)
d=K.random_uniform_variable(shape=(3,4),mean=0,scale=1)

a=b + c * K.abs(d)
c=K.dot(a,K.transpose(b))

a=K.sum(b,axis=1)
a=K.softmax(b)
a=K.concatenate([b,c],axis=1)

2. Utils Keras Module

This module provides utilities for deep learning operations. We would have a look at a few of them.

  • HDF5 Matrix

To convert input data in HDF5 format.

from.utils import HDF5Matrix data=HDF5Matrix(‘data.hdf5’,’data’)
  • to_categorical

For one hot encoding of class vectors.

from keras.utils import to_categorical
labels = [0,1,2,3,4,5]
to_categorical(labels)
  • print_summary

To print the model summary.

from keras.utils import print_summary
print_summary( model )

3. Image Processing Module of keras

It provides methods to convert images to NumPy arrays. It also provides functions for data presentation.

  • ImageDataGenerator class

We use it for real-time data augmentation.

keras.preprocessing.image.ImageDataGenerator(featurewise_center, samplewise_center, featurewise_std_normalization, samplewise_std_normalization, zca_whitening, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0, height_shift_range=0.0, brightness_range, shear_range=0.0, zoom_range=0.0, channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip, vertical_flip)
  • ImageDataGenerator methods

apply_transform:

To apply some transformation to the image.

apply_transform(x, transform_parameters)

flow:

To generate batches of augmented data.

flow(x, y, batch_size=32, shuffle, sample_weight, seed, save_to_dir, save_prefix='', save_format='png', subset)

standardize:

For normalization of input batch.

standardize(x)

4. Sequence Processing keras Module

It provides methods for generating time-based data from the given input. It also provides functions for data presentation.

  • TimeseriesGenerator:

To generate temporal data.

keras.preprocessing.sequence.TimeseriesGenerator(data, targets, length, sampling_rate, stride, start_index, end_index)
  • skipgrams:

It converts a sequence of words into tuples of words.

keras.preprocessing.sequence.skipgrams(sequence, vocabulary_size, window_size=4, negative_samples=1.0, shuffle, categorical, sampling_table, seed)

5. Keras Text Preprocessing Module

It provides methods to convert text into NumPy arrays for computation.  It also provides methods for data preparation.

  • Tokenizer:

We use it to convert a text corpus into vectors.

keras.preprocessing.text.Tokenizer(num_words, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower, split=' ', char_level, oov_token=, document_count=0)
  • one_hot:

To encode a text into a list of words.

keras.preprocessing.text.one_hot(text, n, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower, split=' ')
  • text_to_word_sequence:

To convert a text to a sequence of words.

keras.preprocessing.text.text_to_word_sequence(text, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower, split=' ')

6. Callback Module of Keras

It provides various callback functions. We can use it to study intermediate results.

  • Callback:

To build new callbacks

keras.callbacks.callbacks.Callback()
  • BaseLogger:

To calculate the epoch average of metrics.

keras.callbacks.callbacks.BaseLogger(stateful_metrics)
  • History:

To record events.

keras.callbacks.callbacks.History()
  • ModelCheckpoint:

To save the model after every epoch.

keras.callbacks.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only, save_weights_only, mode='auto', period=1)

Summary

Finally, this tutorial introduces you to various modules available in Keras. We have seen Backend Keras Modules, Util module, Image Processing module, Text Processing module, Sequence Processing module, and Callback modules. This tutorial also explains the various applications and methods available in these modules.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *