In [1]:
import torch

$y = w \times x + b$

$y = 3x + 1$

In [2]:
w = torch.tensor(3.0, requires_grad=True)
b = torch.tensor(1.0, requires_grad=True)
print(w)
print(b)
tensor(3., requires_grad=True)
tensor(1., requires_grad=True)
In [3]:
def forward(x):
    y = w*x + b
    return y
In [4]:
x = torch.tensor(2)
forward(x)
Out[4]:
tensor(7., grad_fn=<AddBackward0>)
In [5]:
x = torch.tensor([[4],[7]])
x
Out[5]:
tensor([[4],
        [7]])
In [6]:
forward(x)
Out[6]:
tensor([[13.],
        [22.]], grad_fn=<AddBackward0>)