title: Do all variables in the loss function have to be tensor with grads in pytorch?
body: <p>I have the following function</p>
  
  <pre class="lang-py prettyprint-override"><code>
  def msfe(ys, ts):
      ys=ys.detach().numpy() #output from the network
      ts=ts.detach().numpy() #Target (true labels)
      pred_class = (ys&gt;=0.5) 
      n_0 = sum(ts==0) #Number of true negatives
      n_1 = sum(ts==1) #Number of true positives
      FPE = sum((ts==0)[[bool(p) for p in (pred_class==1)]])/n_0 #False positive error
      FNE = sum((ts==1)[[bool(p) for p in (pred_class==0)]])/n_1 #False negative error
      loss= FPE**2+FNE**2
  
      loss=torch.tensor(loss,dtype=torch.float64,requires_grad=True)
  
  
      return loss
  </code></pre>
  
  <p>and I wonder, if the autograd in Pytorch works properly, since <code>ys</code> and <code>ts</code> does not have the <code>grad</code> flag.</p>
  
  <p>So my question is: do all the variables (<code>FPE,FNE,ys,ts,n_1,n_0</code>) have to be tensors, before <code>optimizer.step()</code> works, or is it okay that it is only the final function (<code>loss</code>) which is ?</p>