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

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.