What is the meaning of loss functions in keras?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What is the meaning of loss functions in keras?

What is the meaning of loss functions in keras?

This recipe explains what is the meaning of loss functions in keras

1

Recipe Objective

To understand the meaning of loss functions in keras.

The loss is calculated to get the gradients(please refer to gradient descent graph to understand) concerning model weights and update those weights accordingly via backpropagation. Loss is calculated then network weights are updated after every iteration until model updates don't get close or make any improvement in the desired evaluation metric.

Step 1- Importing Libraries.

from tensorflow import keras from tensorflow.keras import layers import numpy as np

Step 2- Loading the Sequential model.

We will define the layers, kernel initializer, and its input nodes shape in the model.

model = keras.Sequential() model.add(layers.Dense(64, kernel_initializer='uniform', input_shape=(10,)))

Step 3- Defining the activation function.

We will define the activation function as relu.

model.add(layers.Activation('relu'))

Step 4- Initialize the Loss function.

We will initialize the loss function as 'Binary_Cross_entropy' with reduction as 'sum_over_batch_size'.

BC = keras.losses.BinaryCrossentropy(reduction='sum_over_batch_size') model.compile(loss=loss_fn, optimizer='adamax')

Step 5- Taking a sample dataset

Let's take a sample dataset of predicted and true values then calculate the loss.

y_true = [[1, 2], [4, 6],[0.5, 0.7],[0.4, 0.6]] y_pred = [[1.5, 1.4], [5, 7],[0.6, 0.5],[0.7, 0.7]] BC(y_true, y_pred).numpy()
-16.878973

As we can see minimum loss with this model is -16.878973 for the sample dataset. We can improve it by choosing another type of loss function or optimizer.

Relevant Projects

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

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.

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.

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.

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.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.