What is Weighted Crossentropy in TF learn explain with example

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

Recipe Objective

This recipe explains what is Weighted 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: Weighted Crossentropy

It's syntax is : tflearn.objectives.weighted_crossentropy (y_pred, y_true, weight). Its argument are y_pred which is predicted values, y_true which is labeled targets and weights are a coefficient to use on the positive examples.
It computes weighted sigmoid cross entropy between my_net (logits) and y (labels) and weighted cross entropy.

Loss = tflearn.weighted_crossentropy(my_net, y, weights=0.8)
accuracy = tflearn.metrics.accuracy_op(my_net, y)

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling 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.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling