Confusion regarding use of forward function while developing a CNN model

bolt25: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/323761c6d12af4b336de94b62b92fb59319f800d.png" data-download-href="https://discuss.pytorch.org/uploads/default/323761c6d12af4b336de94b62b92fb59319f800d" title="Screenshot_20190730-191321"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/3/323761c6d12af4b336de94b62b92fb59319f800d_2_281x500.png" alt="Screenshot_20190730-191321" data-base62-sha1="7aewMDsi3CufuJLRf1W4Eaga3Dv" width="281" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/3/323761c6d12af4b336de94b62b92fb59319f800d_2_281x500.png, https://discuss.pytorch.org/uploads/default/optimized/2X/3/323761c6d12af4b336de94b62b92fb59319f800d_2_421x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/3/323761c6d12af4b336de94b62b92fb59319f800d_2_562x1000.png 2x" data-dominant-color="EEEEEE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot_20190730-191321</span><span class="informations">1080×1920 236 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/669e2ff9f32eba2b3640c960932ea47e0934dcc9.png" data-download-href="https://discuss.pytorch.org/uploads/default/669e2ff9f32eba2b3640c960932ea47e0934dcc9" title="Screenshot_20190730-191312"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/669e2ff9f32eba2b3640c960932ea47e0934dcc9_2_281x500.png" alt="Screenshot_20190730-191312" data-base62-sha1="eDNCfwXuowpTv8q1U3CbWP14UCB" width="281" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/669e2ff9f32eba2b3640c960932ea47e0934dcc9_2_281x500.png, https://discuss.pytorch.org/uploads/default/optimized/2X/6/669e2ff9f32eba2b3640c960932ea47e0934dcc9_2_421x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/6/669e2ff9f32eba2b3640c960932ea47e0934dcc9_2_562x1000.png 2x" data-dominant-color="EDEEEE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot_20190730-191312</span><span class="informations">1080×1920 302 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>when we define a class for a NN or CNN, in the init function we define the model and in the forward function we pass the images.<br>
But when we need the outputs while training why do we write model(images) instead of model.forward(images)?</p>
<p>model here is the object of CNN/NN class.</p>

Nikronic: <p>Hi,</p>
<p>Actually, it is about python programming language. <code>forward()</code> function overrides <code>__call__()</code> function so when you call your class like <code>CNNClass()</code> it calls <code>__call__()</code> function which in pytorch case it is <code>forward()</code> function. So</p>
<pre><code class="lang-python">
model = CNNClass()
out = model(image)
</code></pre>
<p>Is same as:</p>
<pre><code class="lang-python">model = CNNClass()
out = model.forward(image)
</code></pre>
<p>But the first one is the conventional method.</p>
<p>By the way, it would be great if you paste codes as text not image. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Good luck</p>

bolt25: <p>Thank you so much! And yeah, I’ll paste the code next time! <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

sparseinference: <p>This section of the docs talks about it:<br>
<a href="https://pytorch.org/docs/stable/nn.html?highlight=forward#torch.nn.Module.forward" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html?highlight=forward#torch.nn.Module.forward</a></p>