Updated April 4, 2023
Introduction to PyTorch Random
PyTorch random is the functionality available in PyTorch that enables us to get a tensor with random values that belong to the range of 0 to 1. The values are filled using a uniform distribution. In this article, we will try to dive deep into the topic of PyTorch random and understand What PyTorch random is, how to create PyTorch random, alternative PyTorch, PyTorch random examples, and finally provide our conclusion on the same.
What is PyTorch random?
PyTorch random functionality provides a tensor object containing the random values between the specified interval or, by default, between 0 to 1, [0,1] interval. The functional definition with its fully qualified name looks as shown below –
Torch.rand (* size, *, out = None, stype = None, layout = torch. strided, device = None, requires_grad = False
Output -The return value of the above function is a tensor object containing random values. Here, the argument named size helps specify the tensor size we want as an output.
Let’s understand various arguments or parameters that we need to pass to the rand function to get a tensor of random values –
- Size – This is the integer value that helps specify the shape of the tensor that we get as a resultant and is a sequence of integers. It can be a tuple, list, or any variable number of parameters.
- Out – We get the optional parameter and a tensor value as the output.
- Device – It is an optional argument and is of the type torch. device. It helps in the specification of the required devices of the output tensor. When not specified, the default value of this argument corresponds to None, which means that the same current device is used for that type of tensor. For example, in the case of CUDA tensor types, the device of CUDA that is currently used is the preferred device, while in the case of CPU tensor types, its corresponding CPU device is referred to by the device.
- Generator – It is an optional argument of type torch. Generator and us a pseudo-random number created or generated just for the sampling.
- Dtype – It is also an optional argument of type torch. dtype is used to specify the data type we want for the return output tensor. The default value corresponds to None and is a global value when not specified. For more information about this, you can read about the torch. set_default_tensor_type().
- Requires_grad – It is an optional argument of Boolean type and has its default value set to false. It specifies whether the auto grad should record all the operations carried on returned tensor.
- Layout – It is an optional argument of the torch. Layout type that has its default value set to torch. strode. It helps get the choice of device we want for the output tensor.
How to Create PyTorch random?
We can create the PyTorch random tensor containing random values in the range of 0 to 1 simply by importing the torch library in your program and then use the rand function to create your tensor by passing the required size of the output tensor in the parameter. Other optional arguments can also be passed as per your requirement and convenience.
Suppose you want to create a tensor containing random values of size 4, then you can write the statement in your program as a torch.rand(4) after you import the torch at the top. This will create a tensor object having uniformly distributed random values between the range of 0 to 1 of size 4, which means four columns in 1 row.
Alternative PyTorch random
None of the equivalent alternatives are present for implementing np.random.choice(). You can use the indexing with random integer values or shuffled indexing.
When you want to carry out this without doing any replacement, then you can follow the below steps –
- You can go for the generation of n indices that are created randomly.
- Further, you can use these indices to index the source tensor object, which is your original tensor.
- For example, when you have a tensor named images, you can use the following statement – images [torch.randint(len(images))]
When you want to perform the same task without the involvement of any replacement, then you can follow the below steps –
- The index should be shuffled.
- Retrieve the first n elements from the tensor.
- For example, we will refer to the same scenario above and use the following code.
sampleIndexes = torch.randperm(len(images))[:10]
images[smapleIndexes]
Suppose you want more information about the torch.randint and torch.randperm, refer to this article.
PyTorch random examples
Let us now consider some examples that will help us understand the implementation of PyTorch random, the rand function.
Example #1
We are creating one tensor containing random values and having the shape (2,3)
Code:
sampleEducbaTensor1 = torch.rand(2, 3)
print(sampleEducbaTensor1)
Output:
Example #2
We will take one example where we are passing the tuple to define the shape
Code:
sampleEducbaTensor2 = torch.rand((2, 3))
print(sampleEducbaTensor2)
Output:
Example #3
Let’s create a tensor having four size
Code:
sampleEducbaTensor3 = torch.rand(4)
print(sampleEducbaTensor3)
Output:
Example #4
Creating a tensor with the shape of (2,3)
Code:
sampleEducbaTensor4 = torch.rand(2, 3)
print(sampleEducbaTensor4)
Output:
Conclusion
PyTorch random functionality generates a tensor with a random value in nature and between intervals of [0,1]. For this, we will have to use the torch.rand() function and we can specify the desired size and shape of the output tensor we want as a resultant.
Recommended Articles
We hope that this EDUCBA information on “PyTorch Random” was beneficial to you. You can view EDUCBA’s recommended articles for more information.