<p>It doable of course! Let say you have a Net class with <code>__init__</code> and <code>__call__</code>, you can do something like this:</p>
<pre><code>class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.architect = nn.Sequential(...)


    def __call__(self, x):
        out = self.architect(x)
        # Do anythin you want with the prediction in here
        return out
</code></pre>

CC BY-SA 4.0
2022-07-13T02:03:59.593Z
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Community
CC BY-SA 4.0
2022-07-13T00:42:33.933Z
2022-07-13T00:42:33.933Z
Salvia rosmarinus
<p>To answer your question, yes, the computation graph in PyTorch is guaranteed as long as the operation in there is used by the torch package.</p>
<pre><code># Codesnip example for your case
import torch

# with input x
net = model()
output = net(x) # a binary vector
index = [0, 6, 8] # inverse the value at position (0, 6, 8)
output[index] = 1 - out[index]
</code></pre>
<p>Further, I don't know why you need to switch it, but switching, clipping, or forcing the output to be some values will harm the gradient, it makes your function non-differential so be careful when you use it.</p>
<p>Lastly, I think this question is more suitable for StackOverflow since you ask for the code editing and the implementation of your idea.</p>

CC BY-SA 4.0
2022-07-14T12:08:16.723Z
Yes, what you said is what I am currently trying to deal with. The gradient issue. After I hand-craft the output value from the model, my goal is to guide the gradient so it can converge to my modified output. Do you have any idea on this?
Edee
CC BY-SA 4.0
2022-07-15T04:46:21.083Z
I think with the target vector, the [cross entropy loss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html) will help achieve the goal without hand-crafting
CuCaRot
CC BY-SA 4.0
2022-07-13T07:00:20.637Z
2022-07-13T07:00:20.637Z
CuCaRot
<p>I wonder if it is possible to add manual inference to the output of a model?
For example, I have a model called 'net', and the output value of 'net' is a vector called v = [v1, ... vn]. v is a binary vector. For some reason, I need to manually adjust this output, which means I need to manually flip some 0s to 1s and vice versa.</p>
<p>My question is, is it possible to do so. The reason I have this question are</p>
<ol>
<li>I am new to torch</li>
<li>If I manually change the variable.data, even it is doable, I did not do any due adjustments on gradient.</li>
</ol>
<p>Please enlighten me under the context of PyTorch</p>

<neural-networks><training><pytorch><hyperparameter-optimization>
How to manually adjust output from model?
Edee
# How to manually adjust output from model?

(Asked by: username_0 on 2022-07-12)

I wonder if it is possible to add manual inference to the output of a model?
For example, I have a model called 'net', and the output value of 'net' is a vector called v = [v1, ... vn]. v is a binary vector. For some reason, I need to manually adjust this output, which means I need to manually flip some 0s to 1s and vice versa.

My question is, is it possible to do so. The reason I have this question are

1. I am new to torch
2. If I manually change the variable.data, even it is doable, I did not do any due adjustments on gradient.

Please enlighten me under the context of PyTorch

**Tags:** neural-networks, training, pytorch, hyperparameter-optimization


---

## 2 answers

### Answer by username_1 on 2022-07-13. Score: 0

It doable of course! Let say you have a Net class with __init__ and __call__, you can do something like this:
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.architect = nn.Sequential(...)


    def __call__(self, x):
        out = self.architect(x)
        # Do anythin you want with the prediction in here
        return out

> **Comments:**
> **username_2**: As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).  

---

### Answer by username_3 on 2022-07-13. Score: 0

To answer your question, yes, the computation graph in PyTorch is guaranteed as long as the operation in there is used by the torch package.
# Codesnip example for your case
import torch

# with input x
net = model()
output = net(x) # a binary vector
index = [0, 6, 8] # inverse the value at position (0, 6, 8)
output[index] = 1 - out[index]

Further, I don't know why you need to switch it, but switching, clipping, or forcing the output to be some values will harm the gradient, it makes your function non-differential so be careful when you use it.
Lastly, I think this question is more suitable for StackOverflow since you ask for the code editing and the implementation of your idea.

> **Comments:**
> **username_0**: Yes, what you said is what I am currently trying to deal with. The gradient issue. After I hand-craft the output value from the model, my goal is to guide the gradient so it can converge to my modified output. Do you have any idea on this?  
> **username_3**: I think with the target vector, the [cross entropy loss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html) will help achieve the goal without hand-crafting  

---

