Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-13T02:03:59.593Z
Answers.Text: 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).
Answers.UserDisplayName: Community
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-13T00:42:33.933Z
Answers.LastActivityDate: 2022-07-13T00:42:33.933Z
Answers.UserDisplayName: Salvia rosmarinus
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-14T12:08:16.723Z
Answers.Text: 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?
Answers.UserDisplayName: Edee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-15T04:46:21.083Z
Answers.Text: 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
Answers.UserDisplayName: CuCaRot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-13T07:00:20.637Z
Answers.LastActivityDate: 2022-07-13T07:00:20.637Z
Answers.UserDisplayName: CuCaRot
Body: <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>
  
Tags: <neural-networks><training><pytorch><hyperparameter-optimization>
Title: How to manually adjust output from model?
UserDisplayName: Edee
ThreadText: # 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  
  
  ---
  
  