What does tf where function do

This recipe explains what does tf where function do

Recipe Objective

What does tf.where function do?

Where condition in tensorflow, it will return the elements where the condition is being True after multiplexing the x and y variables. There are several conditions depending on which "Where" is being decided.

If the x and y variables are not provided means both are none then:

The "where" will return the indices of the condition that are True, where the output will be in the form of 2D tensor having shape (n,d) in which "n" is the number of matching indices and "d" is the number of dimensions in the condition.

If the x and y variables are provided means both are non-none then:

Then the "where" will choose the output shape from shapes of the condition, x and y where all the three shapes are broadcastable.

Step 1 - Import library

import tensorflow as tf

Step 2 - Create first condition

first_Sample = tf.where([False, True, False, True])

Step 3 - Create Second condition

Second_Sample = tf.where([True, False, True,False], [5,6,7,8], [200])

Step 4 - Print Results

print("This is result where x and y are none:","\n",first_Sample,"\n") print("This is result where x and y are not-none:","\n",Second_Sample)

This is result where x and y are none: 
 tf.Tensor(
[[1]
 [3]], shape=(2, 1), dtype=int64) 

This is result where x and y are not-none: 
 tf.Tensor([  5 200   7 200], shape=(4,), dtype=int32)

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
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.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.