How to create a tensor with random distribution using tf

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

Recipe Objective

How to create a tensor with random distribution using tf?

Random Distribution is a distribution that is a set of random values or numbers which follow a certain probability density function. The probability density function describes the continuous probability. In TensorFlow, this can be achieved by using "tf.random.uniform" which outputs the random values from a uniform distribution. The values generated follow a uniform distribution range which is "minval" and "maxval", the lower bound is "minval" which is included in the range while the upper bound is excluded which is "maxval". For float values the default range is [0,1] and for integer values at least "maxval" must be specified explicitly.

Step 1 - Import library

import tensorflow as tf

Step 2 - Take Random values

random_1 = tf.random.uniform(shape=[3]) random_2 = tf.random.uniform(shape=[], minval=-2., maxval=1.) random_3 = tf.random.uniform(shape=[], minval=6, maxval=12, dtype=tf.int64)

Step 3 - Print the Results

print("This is Random distribution with shape of 2:",random_1, "\n") print("This is Random distribution with minval and maxval:",random_2, "\n") print("This is Random distribution with minval, maxval and dtype is integer:",random_3)

This is Random distribution with shape of 2: tf.Tensor([0.12759817 0.3839183  0.14330661], shape=(3,), dtype=float32) 

This is Random distribution with minval and maxval: tf.Tensor(-0.44896007, shape=(), dtype=float32) 

This is Random distribution with minval, maxval and dtype is integer: tf.Tensor(7, shape=(), dtype=int64)

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

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

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

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 .

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.