Implementing Functions in NumPy Array and Tensors
- rand() function: This function generates arrays or tensors filled with random values sampled from a uniform distribution over a specified interval, typically [0, 1). The rand function is part of the random module and takes one or more arguments representing the dimensions of the output array. Its implementation using Tensors is shown in the below code snippet.
Python
import torch import numpy as np # Generate a 1D tensor of size 5 filled with random numbers random_tensor = torch.rand( 5 ) print ( 'PyTorch Tensor' ) print (random_tensor) # Generate a 1D array of 5 random numbers random_array = np.random.rand( 5 ) print ( 'Numpy Array' ) print (random_array) |
Output:
PyTorch Tensor
tensor([0.7202, 0.5758, 0.8367, 0.7984, 0.7678])
Numpy Array
[0.34504422 0.54502723 0.60215318 0.60033514 0.34551743]
- seed() function: This function initializes the random number generator with a specified seed value. When you set the seed, you can ensure that the same sequence of random numbers will be generated every time the code is run with the seed.
Python
import torch import numpy as np # Set the seed to 42 torch.manual_seed( 42 ) # Generate a random tensor random_tensor = torch.rand( 5 ) print ("Random Pytorch Tensor:",random_tensor) # Set the seed to 42 np.random.seed( 42 ) # Generate a random array random_array = np.random.rand( 5 ) print ("Random Numpy Array:",random_array) |
Output:
Random Pytorch Tensor: tensor([0.8823, 0.9150, 0.3829, 0.9593, 0.3904])
Random Numpy Array: [0.37454012 0.95071431 0.73199394 0.59865848 0.15601864]
- Reshaping function: In Reshaping the array or tensor, the dimensions are changed while preserving the total number of elements. You can use this operation to rearrange the data so that it can be fit for the different computational tasks or algorithms.
For Tensors, you can use the view() method to reshape them as illustrated in the below code.
Python
import torch import numpy as np # Create a 1D tensor tensor = torch.arange( 12 ) print ( 'Pytorch Tensor:' , tensor) # Reshape the tensor into 3x4 matrix reshaped_tensor = tensor.view( 3 , 4 ) print ("Reshaped Tensor") print (reshaped_tensor) # Create 1D array arr = np.arange( 12 ) print ( 'Numpy Array:' , arr) # Reshape the array into 3x4 matrix reshaped_arr = arr.reshape( 3 , 4 ) print ("Reshaped Array") print (reshaped_arr) |
Output:
Pytorch Tensor: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Reshaped Tensor
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Numpy Array: [ 0 1 2 3 4 5 6 7 8 9 10 11]
Reshaped Array
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
- Slicing Operation: Slicing means to extract the subset of elements from the original array based on specified indices or ranges. In the below code snippet, we are creating the Tensor and then slicing it by specifying the row and column index inside the square brackets([]).
Python
import torch import numpy as np # Create 2D tensor tensor = torch.tensor([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]) print ( 'Pytorch Tensor:' ,tensor) # Slice the tensor to extract the sub-tensor sub_tensor = tensor[ 1 :, : 2 ] print ("Tensor after the slicing") print (sub_tensor) # Create 2D array arr = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]) print ( 'NumPy Array:' ,arr) # Slice the array to extract a subarray sub_arr = arr[ 1 :, : 2 ] print ("array after slicing") print (sub_arr) |
Output:
Pytorch Tensor: tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Tensor after the slicing
tensor([[4, 5],
[7, 8]])
NumPy Array: [[1 2 3]
[4 5 6]
[7 8 9]]
array after slicing
[[4 5]
[7 8]]
PyTorch Tensor vs NumPy Array
PyTorch and NumPy can help you create and manipulate multidimensional arrays. This article covers a detailed explanation of how the tensors differ from the NumPy arrays.