How to Create a Tensor with Random Values in Python using PyTorch



Python


In this article, we show how to create a tensor with random values assigned to it in Python using the PyTorch library.

A tensor is one of the most basic building blocks of PyTorch. It is basically the equivalent of a numpy array.

When you create a tensor, you can assign random values to the tensor using the rand() function in PyTorch.

The rand() function has a global data type of

We simply go over how to create empty tensors.

This can be done with the empty() function.

This is shown in the code below.



So you can see that the torch.rand() function produces outputs that are of type float.

In the variable, a, the tensor is one-dimensional, composed of 4 random values.

In the variable, x, the tensor is two-dimensional, composed of 2 rows, with each row consisting of 3 random values.

In the variable, y, the tensor is one-dimensional, composed of 10 random values.

In the variable, z, the tensor is two-dimensional, composed of 2 rows, with each row consisting of 10 random values.

In each case, the random number output is always a float in between 0 and 1.

You can then do mathematical operations on the tensors, such as multiplying them by a certain number to give a number greater than 10.

However, we are not just limited to float random outputs. We can produce random integer values with the torch.randint() function.

With the torch.randint() function, we are able to are able to get random integer output values.

The torch.randint() function takes in 2 parameters, but can also take in an optional third value.

The 2 mandatory parameters are the high parameter as the first parameter. The high parameter is the highest integer that you want the randint() function to return. If you specify 1000, the randint() function will return a value up until one minus the value specified; so, in this case, it would be 999.

The second parameter which must be specified is the size of the tensor, specified as a tuple. If it is a one-dimensional tensor, then a comma must be after the value, such as, (10,), for example.

The optional third parameter is the low value. If not specified, the low value is equal to 0, meaning the random integer generated can be as low as 0. However, you can specify your own low value. So if, for example, you want the random generator to generate a number from 100 to 200, then you would specify 100 as the low value.

Below is code that generates random integer values.



So we have a variable, x, which has a high value of 100 (meaning it outputs a random value up to 99). The size of the tuple is 10, making it a one-dimensional tensor composed of 10 random values.

The second variable, y, has a high value of 1000 (meaning it outputs a random value up to 999). The size of the tuple is 4, making it a one-dimensional tensor composed of 4 random values.

The third variable, z, has a low value of 100, a high value of 1000, and a tuple size of 10. This means that the random output value can range from 100 to 999. It is a one-dimensional tensor composed of 10 random values.

The fourth variable, zz, has a low value of 100, a high value of 201, and a tuple size of (2,10). This means that the random output value can range from 100 to 200. It is a two-dimensional tensor with 2 rows, with each row consisting of 10 random values.

And this is how to create a tensor with random values in Python using PyTorch.


Related Resources



HTML Comment Box is loading comments...