How to create a tensor with uniform distribution using tf

This recipe helps you create a tensor with uniform distribution using tf

Recipe Objective

How to create a tensor with uniform distribution using tf?

Uniform distribution it is nothing but a probability distribution where all the outcomes are equally likely. It is also called as rectangle distribution which is a distribution having the constant probability. In tensorflow the uniform distribution is with low and high parameters. Lets understand with a mathematical expression.

pdf (x; a, b) = I[a < = x < b] / z

z = b - a

where,

low = a

high = b

z = it is an normalizing constant

I[predicate] = is nothing but the indicator function for predicate.

pdf = probability density function.

Lets undertand this with examples of without broadcasting and with broadcasting. Here the low and high parameters are shaped in a way which supports the broadcasting.

Step 1 - Import library

import tensorflow as tf

Step 2 - Initialize uniform

Uniform = tf.compat.v1.distributions.Uniform()

Step 3 - Without Broadcasting

uniform_1 = tf.compat.v1.distributions.Uniform(low= 4.0, high= 5.0) uniform_2 = tf.compat.v1.distributions.Uniform(low=[2.0, 3.0], high=[4.0, 5.0]) uniform_3 = tf.compat.v1.distributions.Uniform(low=[[3.0, 4.0], [5.0, 6.0]], high=[[7.0, 8.0], [9.0, 1.0]]) print("This is Single uniform ditribution:",uniform_1,"\n") print("This is 2 uniform ditribution:",uniform_2,"\n") print("This is 4 uniform ditribution:",uniform_3)

This is Single uniform ditribution: tfp.distributions.Uniform("None", batch_shape=(), event_shape=(), dtype=float32) 

This is 2 uniform ditribution: tfp.distributions.Uniform("None", batch_shape=(2,), event_shape=(), dtype=float32) 

This is 4 uniform ditribution: tfp.distributions.Uniform("None", batch_shape=(2, 2), event_shape=(), dtype=float32)

Step 4 - With Broadcasting

broad_uniform = tf.compat.v1.distributions.Uniform(low=5.0, high=[6.0, 7.0, 8.0]) print("This is 3 uniform distribution with broadcasting:", broad_uniform)

This is 3 uniform distribution with broadcasting: tfp.distributions.Uniform("None", batch_shape=(3,), event_shape=(), dtype=float32)

{"mode":"full","isActive":false}

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

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.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

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

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

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.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.