Multi Dimensional Inputs in Pytorch Linear Method in Python
Last Updated :
24 Apr, 2025
In PyTorch, the torch.nn.Linear class is a linear layer that applies a linear transformation to the input data. It is called linear transformation because it applies the linear equation. i.e
y = xA^{T}+b
Here
- x : input data of one or more dimensions
- A : weight
- b : bias
syntax: torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)
Here:
- in_features : Input features will be an integer value. It denotes the number of variables or columns for the rectangular data frame.
- out_features : Output features will also be an integer value. It denotes the number of output value. We can get1 or more output values.
- bias : It take the boolean value True or False, For default it is True.
- device (Optional) : For default it is None. Here we can change of GPU.
- dtype (Optional) : For default it is None. We can change to any float value.
The torch.nn.Linear class expects the input data to be in the form of a 1-D,2-D, or 3-D tensor, with the last dimension being the input size and the other dimensions representing the batch size and other dimensions.
When working with multi-dimensional input data, the torch.nn.Linear class will apply the linear transformation to the last dimension of the input tensor. It means that the last dimension of the input tensor is considered as input size
In this way, we can use the torch.nn.Linear class to apply a linear transformation to multi-dimensional input data like images, videos, etc. This can be useful in various applications like image classification, object detection, etc.
For example, if you're working with a 3-D tensor representing a set of images, each image having a number of channels, the last dimension of the tensor will be the number of pixels, and the other dimensions will represent the batch size, and the number of channels. The torch.nn.Linear layer will apply a linear transformation to the number of pixels, and the other dimensions will be preserved.
Example 1:
Step 1: Import the necessary packages
Python3
# Import the library
import torch
import torch.nn as nn
Step 2: Define the Input Tensor
Python3
# Define the input data as a 2-D tensor
x = torch.tensor([[0.,1.],[2.,3.],[4.,5.]])
#Shape
print('input_data:', x.shape)
Output:
input_data: torch.Size([3, 2])
Step 3: Define the output size and Create the Linear transformation layer
Python3
# Create an instance of the nn.Linear class
output_size = 1
linear_layer = nn.Linear(in_features=2,
out_features=output_size,
bias = True,
dtype= torch.float)
Step 4: Apply the linear transformation to the input data. and print the output.
Python3
# Apply the linear transformation to the input data
y = linear_layer(x)
#print the outputs
print('output:',y)
#print the outputs shape
print('output Shape:', y.shape)
Output:
output:
tensor([[-0.5798],
[-2.1357],
[-3.6916]], grad_fn=<AddmmBackward0>)
output Shape: torch.Size([3, 1])
Check the Result:
Python3
# Linear layer weight
A = linear_layer.weight
#Bias
b = linear_layer.bias
#Output
y = x @ A.transpose(0,1) + b
#print the outputs
print(y)
Output:
tensor([[-0.5798],
[-2.1357],
[-3.6916]], grad_fn=<AddBackward0>)
Example 2
Python3
# Import the library
import torch
import torch.nn as nn
# Define the input data as a 3-D tensor
x = torch.tensor([[[0.,1.,2.],
[3.,4.,5.],
[6.,7.,8.]]])
print('input_data:', x.shape)
# Create an instance of the nn.Linear class
output_size = 5
linear_layer = nn.Linear(in_features=x.size()[-1],
out_features=output_size,
bias = True,
dtype= torch.float)
# Apply the linear transformation to the input data
y = linear_layer(x)
#print the outputs
print('\noutput:\n',y)
#print the outputs shape
print('\noutput Shape:', y.shape)
Output:
input_data: torch.Size([1, 3, 3])
output:
tensor([[[ 0.8020, 0.7344, -0.1366, 0.7514, -0.0804],
[ 3.7831, 1.5299, -0.1147, -0.1602, 0.8117],
[ 6.7643, 2.3254, -0.0927, -1.0718, 1.7038]]],
grad_fn=<ViewBackward0>)
output Shape: torch.Size([1, 3, 5])
Check the Results:
Python3
# Linear layer weight
A = linear_layer.weight
#Bias
b = linear_layer.bias
#Output y= xA^T + b
##Matrix product of two tensors
y = x.matmul(A.transpose(0,1)) + b
#print the outputs
print(y)
Output:
tensor([[[ 0.8020, 0.7344, -0.1366, 0.7514, -0.0804],
[ 3.7831, 1.5299, -0.1147, -0.1602, 0.8117],
[ 6.7643, 2.3254, -0.0927, -1.0718, 1.7038]]],
grad_fn=<AddBackward0>)
Example 3:
Python3
# Import the library
import torch
import torch.nn as nn
# Define the input data as a 3-D tensor
batch_size = 2
num_channels = 3
input_size = 4
input_data = torch.randn(size =(batch_size, num_channels, input_size))
print('input Shape :', input_data.shape)
print('Input\n',input_data)
# Create an instance of the nn.Linear class
output_size = 2
linear_layer = nn.Linear(in_features=input_data.size()[-1],
out_features=output_size,
bias = True)
# Apply the linear transformation to the input data
output = linear_layer(input_data)
print('\noutput shape:', output.shape)
print('output:\n',output)
Output
input Shape : torch.Size([2, 3, 4])
Input
tensor([[[ 0.2777, -0.1791, -0.0452, 1.4240],
[-0.3565, -1.0471, 0.2848, 0.4885],
[ 0.4119, 0.4591, -0.9893, 0.7301]],
[[-0.8063, -0.7165, 1.6583, 1.2563],
[ 0.0972, -0.3562, 1.1938, 0.4868],
[ 2.1877, 0.3338, 0.7358, -1.3842]]])
output shape: torch.Size([2, 3, 2])
output:
tensor([[[ 0.5222, 0.1193],
[ 0.0817, -0.4504],
[ 0.2597, 0.4163]],
[[ 0.7127, -0.3072],
[ 0.4723, -0.3006],
[-0.0074, -0.4597]]], grad_fn=<ViewBackward0>)
Check the Results:
Python3
# Linear layer weight
A = linear_layer.weight
#Bias
b = linear_layer.bias
#Output y= xA^T + b
##Matrix product of two tensors
y = input_data.matmul(A.transpose(0,1)) + b
#print the outputs
print(y)
Output:
tensor([[[ 0.5222, 0.1193],
[ 0.0817, -0.4504],
[ 0.2597, 0.4163]],
[[ 0.7127, -0.3072],
[ 0.4723, -0.3006],
[-0.0074, -0.4597]]], grad_fn=<AddBackward0>)
Explanation:
Here, input_data is 3D tensor having batch_size, num_channels and input_size as dimensions. torch.nn.Linear(in_features=input_data.size()[-1], out_features=output_size) creates a Linear layer with input size being the last dimension of input_data (i.e. input_size) and output size being output_size.
The output of this operation will be a 3D tensor having dimensions batch_size, num_channels and output_size. The output size is defined by the out_features parameter when creating an instance of the nn.Linear class.
It's also important to note that this script is only for linear layers and for model building, we need to perform forward and backward pass, And there a non-linear activation function like ReLU, tanh, etc. will be added based on the use case, which to decide the weights and biases of the model.
You can also change the shape of the input_data as per your requirement, for example, you can use a 4D tensor for 3D convolution, and so on.
Similar Reads
Python Pytorch linspace() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.linspace() returns a one-dimensional tensor of steps equally spaced points between start and end. The output tensor is 1-D of size
2 min read
Python - PyTorch ceil() method
PyTorch torch.ceil() method returns a new tensor having the ceil value of the elements of input, Which is the smallest integer larger than or equal to each element. Syntax: torch.ceil(inp, out=None) Arguments inp: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t
1 min read
Function with Multiple Inputs in a Script File in MATLAB
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. In this article, we will see a function with multiple inputs in a script file in MATLAB. Steps:The steps below can be used to build a function with multiple inputs in a MATLAB script file: L
2 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
Python - PyTorch div() method
PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor. Syntax: torch.div(inp, other, out=None) Arguments inp: This is input tensor. other: This is a number to be divided to each element of input inp. out: The output tensor. Return: It returns
1 min read
Python | PyTorch cos() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.The function torch.cos() provides support for the cosine function in PyTorch. It expects the input in radian form and the output is in the range [-1,
2 min read
Python - PyTorch exp() method
PyTorch torch.exp() method returns a new tensor after getting the exponent of the elements of the input tensor. Syntax: torch.exp(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example
1 min read
Python PyTorch linalg.svd() method
PyTorch linalg.svd() method computes the singular value decomposition (SVD) of a matrix. 2D tensors are matrices in PyTorch. This method supports both real and complex-valued matrices (float, double, cfloat, and cdouble dtypes). Â It takes input a matrix or a batch of matrices and returns decompositi
4 min read
Multiprocessing in Python and PyTorch
Multiprocessing is a technique in computer science by which a computer can perform multiple tasks or processes simultaneously using a multi-core CPU or multiple GPUs. It is a type of parallel processing in which a program is divided into smaller jobs that can be carried out simultaneously. The progr
12 min read
Python - PyTorch clamp() method
PyTorch torch.clamp() method clamps all the input elements into the range [ min, max ] and return a resulting tensor. Syntax: torch.clamp(inp, min, max, out=None) Arguments inp: This is input tensor. min: This is a number and specifies the lower-bound of the range to which input to be clamped. max:
2 min read