How do i build a model using weights and biases?

Hi, Everybody :slight_smile:
I have a question. The title asks it all!

I have built a model using another framework (RAY), it is problem that calculates the loss (using loss.backward). So I make a Copy model using just PyTorch. How do I build a model using the below weights and biases?

Could anyone help me?

You could use the functional API to apply the parameters to an input.
The weight and bias arrays seem to be used in linear layers, so something like this should work:

x = torch.randn(10, 10) # your input
weight = nn.Parameter(torch.from_numpy(your_weight_array))
bias = nn.Parameter(torch.from_numpy(your_bias_array))

out = F.linear(x, weight, bias)

Additionally, in case you want to train these parameters, pass them to an optimizer and apply the standard training loop.

Hi, thank you every time @ptrblck !
if I wanna update weight and bias, Can I do this?
weight = torch.from_numpy(your_weight_array) / bias =torch.from_numpy(your_bias_array)

And i want to apply relu or tanh function, how can i do it?
out = F.linear(x, weight, bias)
out = F.relu (out)
Do this?

Pass both parameters to the optimizer and it should work:

x = torch.randn(10, 10) # your input
weight = nn.Parameter(torch.from_numpy(np.random.randn(10, 10)).float())
bias = nn.Parameter(torch.from_numpy(np.random.randn(10)).float())

optimizer = torch.optim.SGD([weight] + [bias], lr=1e-3)
target = torch.randn(10, 10)
criterion = nn.MSELoss()

for epoch in range(10):
    optimizer.zero_grad()
    out = F.relu(F.linear(x, weight, bias))
    loss = criterion(out, target)
    loss.backward()
    optimizer.step()
    print('epoch {}, loss {}'.format(epoch, loss.item()))

Oh, sorry, and thanks for the quick reply.
I have taken the mistake
What I meant to say is “I don’t wanna update weights and biases.”
really sorry to bother you

@ptrblck
Thank you really, i got solved.

Good to hear it’s working!
Just to make sure: yes, if you don’t want to update the weight and bias, you can just use tensors (not nn.Parameters).