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
1 2 3 4 5 |
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
1 2 3 4 5 |
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
1 2 3 4 5 6 7 8 |
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) |