Python 3 PyTorch Library Example to Generate Random Tensor Number Values and Specific Shape and Range Full Tutorial For Beginners

You are currently viewing Python 3 PyTorch Library Example to Generate Random Tensor Number Values and Specific Shape and Range Full Tutorial For Beginners

 

Welcome folks today in this post we will be generating random tensor number values using pytorch library in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install pytorch

 

After installing this library make an app.py file and copy paste the following code

 

app.py

 

generating random values

 

import torch
#create tensor with random data
rand_tensor = torch.rand((2, 5))
#print tensor
print(rand_tensor)

 

 

generating random values less than 8

 

app.py

 

import torch
#create tensor with random data, and multiply with a scalar
rand_tensor = 8*torch.rand((2, 5))
#print tensor
print(rand_tensor)

 

 

generating random values in a given range of 4 to 8

 

app.py

 

import torch
#range
max = 8
min = 4
#create tensor with random values in range (min, max)
rand_tensor = (max-min)*torch.rand((2, 5)) + min
#print tensor
print(rand_tensor)

 

Leave a Reply