Linear() in PyTorch

Linear() can get the 1D or more D tensor of the zero or more values computed by Affine transformation from the 1D or more D tensor of zero or more elements as shown below:

import torch
from torch import nn tensor1 = torch.tensor([8., -3., 0., 1., 5., -2.]) tensor1.requires_grad
# False

torch.manual_seed(42) linear1 = nn.Linear(in_features=6, out_features=4)
tensor2 = linear1(input=tensor1)
tensor2
# tensor([0.9212, -1.1756, 3.5270, -3.5009], grad_fn=<ViewBackward0>)

tensor2.requires_grad
# True

linear1
# Linear(in_features=6, out_features=4, bias=True)

linear1.in_features
# 6

linear1.out_features
# 4

linear1.bias
# Parameter containing:
# tensor([-0.3223, -0.1882, -0.1153, -0.2455], requires_grad=True)

linear1.weight
# Parameter containing:
# tensor([[0.3121, 0.3388, -0.0956, 0.3750, -0.0894, 0.0824],
# [-0.1988, 0.2398, 0.3599, -0.2995, 0.3548, 0.0764],
# [ 0.3016, 0.0553, 0.1969, -0.0576, 0.3147, 0.0603],
# [-0.1906, 0.1041, -0.1881, -0.0479, -0.1658, 0.2708]],
# requires_grad=True)

linear2 = nn.Linear(in_features=4, out_features=3)
linear2(input=tensor2)
# tensor([3.8380, -1.5321, 0.5521], grad_fn=<ViewBackward0>)

torch.manual_seed(42) linear = nn.Linear(in_features=6, out_features=4, bias=True)
linear(input=my_tensor)
# tensor([0.9212, -1.1756, 3.5270, -3.5009], grad_fn=<ViewBackward0>)

torch.manual_seed(42) linear = nn.Linear(in_features=6, out_features=4, bias=False)
linear(input=my_tensor)
# tensor([1.2434, -0.9874, 3.6423, -3.2555], grad_fn=<SqueezeBackward4>)

my_tensor = torch.tensor([[8., -3., 0.], [1., 5., -2.]])
torch.manual_seed(42) linear = nn.Linear(in_features=3, out_features=4)
linear(input=my_tensor)
# tensor([[2.5202, 4.7006, -2.9875, -4.9755],
# [3.5345, -0.2570, 0.6747, 1.7880]], grad_fn=<AddmmBackward0>)

my_tensor = torch.tensor([[[8.], [-3.], [0.]], [[1.], [5.], [-2.]]])
torch.manual_seed(42) linear = nn.Linear(in_features=1, out_features=4)
linear(input=my_tensor)
# tensor([[[5.8972, 6.8419, -2.3610, 7.9362],
# [-2.5127, -2.2882, 0.2160, -2.1686],
# [-0.2191, 0.2018, -0.4869, 0.5873]],
# [[0.5454, 1.0318, -0.7211, 1.5059],
# [3.6036, 4.3518, -1.6582, 5.1803],
# [-1.7482, -1.4582, -0.0183, -1.2499]]], grad_fn=<ViewBackward0>)
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Copyright © 2024 solid future. Here are the top skills needed for a successful career in software development. Cloudystorage offers affordable online storage solutions tailored to meet the needs of both individual users and businesses.