Machine Learning

model.parameters

Naranjito 2023. 1. 17. 01:28
  • model.parameters()

It has stored W, b. It transferable to optimizer.

Let's create 1 dim of input and 1 dim of output linear model.

model = nn.Linear(1,1)
print(model)

>>>
Linear(in_features=1, out_features=1, bias=True)

 

And check it out the Weight and Bias.

First value is W, second value is b. Both are subject to learning(requires_grad=True).

print(list(model.parameters()))

>>>
[Parameter containing:
tensor([[0.5153]], requires_grad=True), Parameter containing:
tensor([-0.4414], requires_grad=True)]

 

Then, declare the optimizer.

optimizer = torch.optim.SGD(model.parameters(), lr=0.01)