How do i build a model using weights and biases?

username: Dongsu_Lee
cooked: <p>Hi, Everybody <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
I have a question. The title asks it all!</p>
<p>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?</p>
<p>Could anyone help me?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09.png" data-download-href="https://discuss.pytorch.org/uploads/default/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09_2_690x452.png" alt="image" data-base62-sha1="A0HMOCbRI92Rq6e36AWJDv9yzrj" width="690" height="452" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09_2_690x452.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/c/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09_2_1035x678.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc62b352cdb8e1f3eb9687b477702fca0c6bbf09.png 2x" data-dominant-color="F0F0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1088×713 100 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>You could use the functional API to apply the parameters to an input.<br>
The <code>weight</code> and <code>bias</code> arrays seem to be used in linear layers, so something like this should work:</p>
<pre><code class="lang-python">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)
</code></pre>
<p>Additionally, in case you want to train these parameters, pass them to an optimizer and apply the standard training loop.</p>

username: Dongsu_Lee
cooked: <p>Hi, thank you every time <a class="mention" href="/u/ptrblck">@ptrblck</a> !<br>
if I wanna update weight and bias, Can I do this?<br>
weight = torch.from_numpy(your_weight_array) / bias =torch.from_numpy(your_bias_array)</p>
<p>And i want to apply relu or tanh function, how can i do it?<br>
out = F.linear(x, weight, bias)<br>
out = F.relu (out)<br>
Do this?</p>

username: ptrblck
cooked: <p>Pass both parameters to the optimizer and it should work:</p>
<pre><code class="lang-python">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()))
</code></pre>

username: Dongsu_Lee
cooked: <p>Oh, sorry, and thanks for the quick reply.<br>
I have taken the mistake<br>
What I meant to say is “I <strong>don’t</strong> wanna update weights and biases.”<br>
really sorry to bother you</p>

username: Dongsu_Lee
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Thank you really, i got solved.</p>

username: ptrblck
cooked: <p>Good to hear it’s working!<br>
Just to make sure: yes, if you don’t want to update the <code>weight</code> and <code>bias</code>, you can just use tensors (not <code>nn.Parameter</code>s).</p>