What is Binary Crossentropy in TF learn explain with example

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

Recipe Objective

This recipe explains what is Binary Crossentropy.

Access Face Recognition Project Code using Facenet in Python

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: Binary Crossentropy

It's syntax is : tflearn.objectives.binary_crossentropy (y_pred, y_true). Its argument are y_pred which is predicted values and y_true which is labeled targets.
It computes sigmoid 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.
let a = logits, b = targets
then logistic loss is a - a * b + log(1 + exp(-b))
To ensure stability and avoid overflow, the implementation uses
max(a, 0) - a * b + log(1 + exp(-abs(a)))

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

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

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.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

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.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.