How to create a custom cost function to evaluate keras model?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

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

0

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
>

Relevant Projects

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.