Linear Transformation to incoming data in Pytorch
Last Updated :
28 Feb, 2022
We could apply linear transformation to the incoming data using the torch.nn.Linear() module in PyTorch. This module is designed to create a Linear Layer in the neural networks. A linear layer computes the linear transformation as below-
Where
is the incoming data. It must be a tensor of dtype float32 and shape (*, in_features). Here * is any number of dimensions. in_features is number of features in the input data.
is the output data after the transformation with same dtype as
and with shape (*, out_features). Note that all dimensions except last are of the same shape as input data.
is the learnable weights of shape (out_features, in_features). out_features is the last dimension of the output data.
is the additional bias learned during the training.
Please note that weights
and biases
are initialized randomly.
Stepwise Implementation
Below are the steps to follow to apply a linear transformation to incoming data –
Step 1: Import PyTorch
The required library is PyTorch. The first step is to import PyTorch.
Step 2: Define the Input Data
Now define the input data. We are going to apply a linear transformation to this data. The input data must be a Tensor of dtype float32. We created a tensor of size [3, 4] using a random generator. We can interpret this tensor as an input of three samples each of size 4. Here for the input data the in_features = 4, see the next step. Note that in a Neural network, this data is incoming from the previous layer.
Step 3: Define the Number of Input and Output Features
The ‘in_features’ is the number of features in each input sample, and the ‘out_features’ is the number of features in each output sample. The in_features depend on the input tensor as in the second step the in_features = 4. The out_features are decided according to our need and the neural network architecture.
Python3
in_features = 4
out_features = 2
|
Step 4: Define a Linear Transformation
We define linear transformation ‘linear’ using the torch.nn.Linear() module. We pass the in-features and out_features as parameters. We could also pass the optional parameter bias = False if we don’t want the layer to learn the bias. Note that the module torch.nn.Linear() performs as a layer in the neural network.
Python3
linear = torch.nn.Linear(in_features, out_features)
|
Step 5: Apply the Linear Transformation to the Input Data:
Now we apply the defined linear transformation to the input data (incoming data). We could print the output data, shape and size of the output data after transformation.
Example 1:
Here the in_features=5 as the input data size is [5]. And we set out_features = 3, so the size of output data (data after transformation) is [3]. In the above example, the input data is in a one-dimensional tensor.
Python3
import torch
data = torch.tensor([ 23. , 12. , 33. , 4.01 , - 65. ])
print ( "Data before Transformation:\n" , data)
print ( "dtype of Data:" , data.dtype)
print ( "Size of Data:" , data.size())
in_features = 5
out_features = 3
linear = torch.nn.Linear(in_features, out_features)
data_out = linear(data)
print ( "Data after Transformation:\n" , data_out)
print ( "Size of Data after Transformation:" , data_out.size())
|
Output:
You may get the output tensor with different elements at each run as the weights and biases are initialized randomly.
Data before Transformation:
tensor([ 23.0000, 12.0000, 33.0000, 4.0100, -65.0000])
dtype of Data: torch.float32
Size of Data: torch.Size([5])
Data after Transformation:
tensor([-6.3559, -1.5512, 22.3267], grad_fn=<AddBackward0>)
Size of Data after Transformation: torch.Size([3])
Example 2:
Here the in_features=4 as the input tensor size is [3, 4]. And we set out_features = 2, so the size of the output tensor (data after transformation) is [3, 2]. The dimension except the last dimension is the same as input tensor. The last dimension is the same as out_features.
Python3
import torch
data = torch.randn( 3 , 4 )
print ( "Tensor before Transformation:\n" , data)
print ( "dtype of Tensor:" , data.dtype)
print ( "Size of Tensor:" , data.size())
in_features = 4
out_features = 2
linear = torch.nn.Linear(in_features, out_features)
data_out = linear(data)
print ( "Transformed Tensor:\n" , data_out)
print ( "Size of Transformed Tensor:" , data_out.size())
|
Output:
You may get the output tensor with different elements at each run as the weights and biases are initialized randomly.
Tensor before Transformation:
tensor([[ 0.0664, 1.8933, -0.4003, -0.2383],
[-0.7531, 1.8737, -2.0936, -0.8959],
[-1.6239, -1.1321, -0.5427, -0.4834]])
dtype of Tensor: torch.float32
Size of Tensor: torch.Size([3, 4])
Transformed Tensor:
tensor([[-0.5405, 0.4570],
[-0.5507, -0.3917],
[ 0.4763, -0.3399]], grad_fn=<AddmmBackward>)
Size of Transformed Tensor: torch.Size([3, 2])
Similar Reads
How to perform random affine transformation of an image in PyTorch
In this article, we will cover how to perform the random affine transformation of an image in PyTorch. RandomAffine() method RandomAffine() method accepts PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents the number of channels and  H, W repres
2 min read
How to implement transfer learning in PyTorch?
What is Transfer Learning?Transfer learning is a technique in deep learning where a pre-trained model on a large dataset is reused as a starting point for a new task. This approach significantly reduces training time and improves performance, especially when dealing with limited datasets. It is very
15+ min read
Two-Dimensional Tensors in Pytorch
PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations. Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns. Representat
3 min read
Classification using PyTorch linear function
In machine learning, prediction is a critical component. It is the process of using a trained model to make predictions on new data. PyTorch is an open-source machine learning library that allows developers to build and train neural networks. One common use case in PyTorch is using linear classifier
7 min read
Creating a Tensor in Pytorch
All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function. The tensor() Method: To create tensors with Pytorch we can simply use the tensor() method:
6 min read
One-Dimensional Tensor in Pytorch
In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts: Creation of One-Dimensional TensorsAccessing Elements of TensorSize of TensorData Types of Elements of TensorsView of TensorFloating Point TensorIntroduction The Pytorch is used to
5 min read
PyTorch Functional Transforms for Computer Vision
In this post, we will discuss ten PyTorch Functional Transforms most used in computer vision and image processing using PyTorch. PyTorch provides the torchvision library to perform different types of computer vision-related tasks. The functional transforms can be accessed from the torchvision.transf
6 min read
Way to Copy a Tensor in PyTorch
In deep learning, PyTorch has become a popular framework for building and training neural networks. At the heart of PyTorch is the tensorâa multi-dimensional array that serves as the fundamental building block for all operations in the framework. There are many scenarios where you might need to copy
5 min read
How to Slice a 3D Tensor in Pytorch?
In this article, we will discuss how to Slice a 3D Tensor in Pytorch. Let's create a 3D Tensor for demonstration. We can create a vector by using torch.tensor() function Syntax: torch.tensor([value1,value2,.value n]) Code: [GFGTABS] Python3 # import torch module import torch # create an 3 D tensor w
2 min read
Converting a List of Tensors to a Single Tensor in PyTorch
PyTorch, a popular deep learning framework, provides powerful tools for tensor manipulation. One common task in PyTorch is converting a list of tensors into a single tensor. This operation is crucial for various applications, including data preprocessing, model input preparation, and tensor operatio
4 min read