Is it necessary to overwrite the forward method for a class that inherits from nn.Module?

Raphikowski: <p>Hi all,</p>
<p>I have a class that represents a shallow neural network (just one layer), which is why I have implemented that layer by myself via matrix multiplications (i.e., without using any of the nn.linear or other preimplemented layers). I have implemented this directly in a loss function and have therefore no forward method. The network trains as expected, but are there any disadvantages or pitfalls when not overwriting that forward method (my class inherits from nn.Module)?</p>

ptrblck: <p>How did you implement your class?<br>
If you like the manual approach, you don’t need to derive a class from <code>nn.Module</code> and can just write the operations as you wish. An <code>nn.Module</code> class helps in managing the internal parameters and makes some workflows easier, e.g. pushing all parameters to the GPU by calling <code>.to('cuda')</code>.</p>

Raphikowski: <p>Thank you for your answer.<br>
So the class basically consists of an init where I initializie the matrices as nn.Parameters to enable training and then I have a loss function where I calculate the loss using these matrices and some external input (the data). Do you think there will be any unexpected behaviour without the forward class when I inherit from nn.Module? Because I actually would like to make use of cuda since this class is only one part of a bigger model. So basically I would like to let it remain inheriting from nn.Module. Do you think this is a bad idea?</p>

ptrblck: <p>In that case, I would just implement the <code>forward</code> method, since I assume your current <code>loss</code> function is probably doing the same the <code>forward</code> would do.<br>
If you implement the <code>forward</code> method, you can call the module directly (<code>my_module(data)</code>), it will work inside an <code>nn.Sequential</code> module, and all hooks will be registered properly.</p>
<p>I’m not sure, if there are any significant differenced between your custom <code>loss</code> function and what <code>forward</code> would do.</p>

Raphikowski: <p>Thank you for your reply.<br>
Do you think I could also just call the forward function from within my loss function? Because I would rather call that loss function by its name and not call it ‘forward,’ since this is more understandable for other ppl working on my code.<br>
PS: What exactly is a hook? I googled it but didnt find a comprehensive answer? Can you provide a short explanation or a useful link?</p>

ptrblck: <p>Would calling your custom function inside the <code>forward</code> work?<br>
E.g.</p>
<pre><code class="lang-python">...
def forward(self, x):
    l = self.loss(x)
    return l
</code></pre>
<p>It’s a bit of nitpicking and if you are sure you won’t use hooks and always call the custom method, you could of course just go for it.<br>
However, if other users would like to use your module in an <code>nn.Sequential</code> or just use it as a standard <code>nn.Module</code>, they will face a <code>NotImplementedError</code>.</p>
<p>Hooks are used to e.g. get intermediate activations as explained <a href="https://discuss.pytorch.org/t/how-can-l-load-my-best-model-as-a-feature-extractor-evaluator/17254/6">here</a>.</p>

Raphikowski: <p>Ok so just to be sure: Is the worst thing that could happen if I dont implement the forward method, that ppl could get a NotImplementedError, if they try to call the forward method? Or could there be some unexpected behaviour in the code execution or in the training of the model parameters, due to the hooks or sth?</p>

ptrblck: <p>I think the worst thing would be the exception, when trying to use your module in the “normal” way, i.e. calling it directly via <code>module(data)</code>.<br>
Also, if users are using hooks for whatever reason, they would need to implement <code>forward</code> manually (which is not the standard use case, but could be annoying).</p>
<p>The first issue is why I would rewrite your module, if I would like to use it <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Raphikowski: <p>Hi so to bring that topic up another time:<br>
When trying to display the computational graph using tensorboard this is not possible if I have  a  class inheriting from nn.Module without a forward method. But if i impelment the forward method the graph can be displayed. Do you know why this is the case?<br>
Does it not build up the computational graph correctly without a forward method? Or do you just need these hooks from the forward method to properly display the graph?</p>

ptrblck: <p>As explained before, the <code>forward</code> method will be called if you call the module directly. Also hooks etc. will be properly registered.<br>
Since the computation graph is created dynamically during the forward pass, each module will be most likely called at some point (haven’t looked in detail into tensorboard backend).</p>

Raphikowski: <p>Thank you for your answer. I think my question is rather: Is it possible that if I dont implement the forward method and do everything by means of functions in my nn.module class that have different names (e.g., ‘likelihood’), that these functions are not added to the computational graph (even though I call them during training)?<br>
Because tensorflow is only able to draw the computational graph as an image if I rename my custom made function ‘likelihood’ to ‘forward’. Or is that only due to the fact that this tensorboard script relies somehow on the forward function?<br>
Baseline quesion: If I dont name my function ‘forward’ but instead ‘likelihood’ (for example) is the computational graph then still properly build up? I.e., would the results/computations change somehow if I renamed the function to ‘forward’?</p>

ptrblck: <p>The computation graph will be properly built even without any function definitions.<br>
You could write your complete model using the functional API and Autograd will track each operation as long as you are using parameters, which require gradients.</p>
<p>However, if you are writing a custom <code>nn.Module</code>, I would still recommend to implement the <code>forward</code> method, as e.g. other users or libraries might rely on it.<br>
Internally the <code>__call__</code> method will call into <code>forward</code>, so if some users or libs use your model as:</p>
<pre><code class="lang-python">output = model(input)
</code></pre>
<p>your custom function will not be called.<br>
I assume this might be the cast for the tensorboard issue, as at some point the model might be called directly with some dummy inputs.</p>

Raphikowski: <p>Great, thanks a lot for your answer!</p>