Must use forward function in nn.Module?

007: <p>Sometimes I need to use different net in one model. For example, somtimes I need the value from a network call ‘critic’, sometimes I need the value from a network called ‘actor’, and sometimes I need both. My question is, can I define some function to get the critic value without using the forward function? Will this method influcence the backward propogation?</p>
<p>(an example)</p>
<p>class Model(nn.Module):<br>
def <strong>init</strong>(self,obs_dim, action_num, device):<br>
super(Model, self).<strong>init</strong>()<br>
self.actor = nn.Sequential(<br>
nn.Linear(obs_dim, action_num)<br>
)<br>
self.critic = nn.Sequential(<br>
nn.Linear(obs_dim, 1)<br>
)</p>
<pre><code>def get_value(self, inputs):
    return self.critic(inputs)

def get_action(self, inputs):
    return self.actor(inputs)

def get_both(self, inputs):
      return self.actor(inputs), self.critic(inputs)</code></pre>

JuanFMontesinos: <p>I don’t understand the question at all. You want to use subnetworks to get some values without affecting backprop?</p>
<p>Whatever you run helps to build a computational graph which will be further backpropagated.<br>
If you need to use subnetworks to obtain values not involved in backprop you can use the context manager <code>with torch.no_grad()</code></p>

tom: <p>The only thing special about the forward method is that you can invoke it by calling the model (i.e. m(*inputs) will call m.forward(*inputs)) <em>and</em> that you get hook processing etc.</p>
<p>As such, there is no reason why you would not be able to provide other entry points - I often have a separate <code>predict</code> method for sequence-generating models or <code>sample</code> for density estimation modules.</p>
<p>Whether it is a good idea or not likely depends on your view how closely they are coupled. I’m not entirely sure that it is the case here. On the other hand frameworks like fastai like to stick anything and everything (models, loss functions, learning rate schedule, optimizer, callbacks…) related into one class - but not a nn.Module one.</p>
<p>Best regards</p>
<p>Thomas</p>
<p>Best regards</p>
<p>Thomas</p>

007: <p>Thank u for your patient reply, Thomas. I think I got it.</p>
<p>Best wishes</p>
<p>Linda</p>

007: <aside class="quote no-group" data-username="JuanFMontesinos" data-post="2" data-topic="50943">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/48/76115_2.png" class="avatar"> JuanFMontesinos:</div>
<blockquote>
<p>o use subnetworks to get some values without affecting backprop?</p>
<p>Whatever you run helps to build a computational graph which will be further backpropagated.<br>
If you need to use subnetworks to obtain values</p>
</blockquote>
</aside>
<p>Thank u for your reply. Well, my question is if I get value from subnetworks with self-defined function instead of  forward function in nn.module, will it affect backprop process, for example, the subnetwork won’t backprop correctly. But it turns out that it’s no necessary to use the forward function.</p>

JuanFMontesinos: <p><a class="mention" href="/u/tom">@tom</a> Regarding your reply, even if you entry a module not using forward function, at some point you will be calling a nn.Module at least for built-in nn.Modules, right?</p>
<p>As far as I understand (autograd) to perform inference it would be necessary to disable autograd module, don’t you think so?</p>

tom: <p>autograd seems completely orthogonal to the headline issue (whether forward is special), but yes, for inference it probably makes sense to use <code>with torch.no_grad():</code> or <code>for p in model.parameters(): p.requires_grad_(False)</code>.</p>
<p>Best regards</p>
<p>Thomas</p>