Combine linear layers

I want to implement a linear function: y = W_1x_1 + W_2x_2 + b How can I create this linear layer with nn.Linear()? I am new to pytorch, thanks for any help.

Should be something like

x = nn.Linear(input_size, output_size1, bias=False)
x = nn.Linear(output_size1, output_size2, bias=True)

import torch
import torch.nn as nn

x = torch.tensor([2.0])

class Linear_Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.lm1 = nn.Linear(1,1,bias=False)
        self.lm2 = nn.Linear(1,1,bias=True)
    def forward(self, X):
        out = self.lm1(X) + self.lm2(X)
        
        return out

mdl = Linear_Model()

print(mdl(x))

1 Like

Thanks for the solution.