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

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.

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

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])
```

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)
```

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'])
```

```
model.summary
```

>

Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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.

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

In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

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.

In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.