What is use of clone_model functions in keras why is it used?

This recipe explains what is use of clone_model functions in keras why is it used

Recipe Objective

What is the 'clone_model' function in Keras? Why is it used?

A clone mode is a model that reproduces the behavior of the original model, where we can input new tensors, with new weights to the layers. The cloned models behave differently if we customize the cloned model or we use the custom clone_function.

Hands-On Guide to the Art of Tuning Locality Sensitive Hashing in Python

Step 1- Importing Libraries

import numpy as np import keras from keras.models import Sequential from keras.layers import UpSampling2D

Step 2- Cloning.

We will make a model to clone.

# define input data X = np.array([10, 6, 3, 20]) # show input data for context print(X) # reshape input data into one sample a sample with a channel X = X.reshape((1, 2, 2, 1)) # define model model = Sequential() model.add(UpSampling2D(input_shape=(2, 2, 1))) model.summary()

[10  6  3 20]
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
up_sampling2d (UpSampling2D) (None, 4, 4, 1)           0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

Step-3 we will clone the model.

#cloning of model model_copy= keras.models.clone_model(model) model_copy.build((None, 5)) # replace 10 with number of variables in input layer model_copy.compile(optimizer='adam', loss='categorical_crossentropy') model_copy.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
up_sampling2d (UpSampling2D) (None, 4, 4, 1)           0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .