What is Categorical Crossentropy in TF learn explain with example

This recipe explains what is Categorical Crossentropy in TF learn with example

Recipe Objective

This recipe explains what is Categorical Crossentropy.

Step 1: Importing Libraries

We'll import tflearn, tensorflow as tf and tflearn.datasets.mnist as mnist.

import tflearn
import tensorflow as tf
import tflearn.datasets.mnist as mnist
from __future__ import division, print_function, absolute_import

Step 2: Building Model

We have combined TFLearn built-in ops with Tensorflow graph. We have built this using MNIST Dataset.
To create a multilayer perceptron we have used TFLearn PReLU activations ops.

with tf.Graph().as_default():

    x = tf.placeholder("float", [None, 392])
    y = tf.placeholder("float", [None, 5])

    u = tf.Variable(tf.random_normal([392, 128]))
    v = tf.Variable(tf.random_normal([128, 128]))
    w = tf.Variable(tf.random_normal([128, 5]))
    a = tf.Variable(tf.random_normal([128]))
    b = tf.Variable(tf.random_normal([128]))
    c = tf.Variable(tf.random_normal([5]))

    def net(X):
       X = tflearn.prelu(tf.add(tf.matmul(X, u), a))
       tflearn.summaries.monitor_activation(x)
       X = tflearn.prelu(tf.add(tf.matmul(X, v), b))
       tflearn.summaries.monitor_activation(x)
       X = tf.nn.softmax(tf.add(tf.matmul(X, w), c))
       return X

    my_net = net(x)

Step 3: Categorical Crossentropy

It's syntax is : tflearn.objectives.categorical_crossentropy (y_pred, y_true). Its argument are y_pred which is predicted values and y_true which is a probability distribution.
It computes categorical cross entropy between my_net (logits) and y (labels) and the probability error in indivisual classification tasks in which each entry is in exactly one class.

Loss = tflearn.categorical_crossentropy(my_net, y)
accuracy = tflearn.metrics.accuracy_op(my_net, y)

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

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.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.