How to create a custom cost function to evaluate keras model?

This recipe helps you create a custom cost function to evaluate keras model

Recipe Objective

How to create a custom cost function to evaluate keras model

The formula for every loss function is predefined, but if we want to create a loss function(cost function) specifically for our model then we can create. So we can define our own loss function and call it a custom cost function.

Step 1- Importing Libraries

import keras as k from keras.models import Sequential from keras.layers import Dense import numpy as np

Step 2- Defining two sample arrays.

We will define two sample arrays as predicted and actual to calculate the loss. y_pred=np.array([2,3,5,7,9]) y_actual=np.array([4,2,8,5,2])

Step 3- Define your new custom loss function.

we are considering the formula for MSE here.

def custom_loss(y_true,y_pred): return K.mean(K.square(y_pred - y_actual) + K.square(layer), axis=-1)

Step 4- Creating the Neural Network Model.

We will pass our own custom cost function to the model.

# create model model = Sequential() model.add(Dense(12, input_dim=8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(1, activation='sigmoid')) # Compile model model.compile(loss='custom_loss', optimizer='adam', metrics=['accuracy'])

Step 5- Create the model summary.

model.summary

>

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.