In deep learning code by pytorch

AishaMohamed2022: <pre><code class="lang-auto"># In[55]:
class ResBlock(nn.Module):
def __init__(self, n_chans):
super(ResBlock, self).__init__()
self.conv = nn.Conv2d(n_chans, n_chans, kernel_size=3,
padding=1, bias=False)
self.batch_norm = nn.BatchNorm2d(num_features=n_chans)
torch.nn.init.kaiming_normal_(self.conv.weight,
nonlinearity='relu')
torch.nn.init.constant_(self.batch_norm.weight, 0.5)
torch.nn.init.zeros_(self.batch_norm.bias)
def forward(self, x):
out = self.conv(x)
out = self.batch_norm(out)
out = torch.relu(out)
return out + x
</code></pre>
<p>can anyone tell me waht is code doing by steps ,please?<br>
What are the sources that you can follow to understand the codes with myself,please ?</p>

J_Johnson: <p>PyTorch docs provide the mathematical equations taking place behind the scenes for every operation where it is relevant.</p>
<p>For instance, <code>nn.Conv2d</code>:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/6/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d" title="IMG_20231208_113310"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d_2_690x309.jpeg" alt="IMG_20231208_113310" data-base62-sha1="wPa3rcUwnv2tEchThM0IYzHOeD3" width="690" height="309" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d_2_690x309.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d_2_1035x463.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/6/e60e1cf6b5d05a52263ba833a4481d4c0b62c76d_2_1380x618.jpeg 2x" data-dominant-color="F6F6F6"><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">IMG_20231208_113310</span><span class="informations">2431×1089 209 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>For <code>nn.Batchnorm2d</code>:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/f/cf6d3dd5500fa57ad1b9f95e579214807b000ab9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cf6d3dd5500fa57ad1b9f95e579214807b000ab9" title="IMG_20231208_113551"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/f/cf6d3dd5500fa57ad1b9f95e579214807b000ab9_2_690x263.jpeg" alt="IMG_20231208_113551" data-base62-sha1="tAYST4wfA0UirTLTxRza4jihiWJ" width="690" height="263" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/f/cf6d3dd5500fa57ad1b9f95e579214807b000ab9_2_690x263.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/f/cf6d3dd5500fa57ad1b9f95e579214807b000ab9_2_1035x394.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/f/cf6d3dd5500fa57ad1b9f95e579214807b000ab9_2_1380x526.jpeg 2x" data-dominant-color="F2F0F0"><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">IMG_20231208_113551</span><span class="informations">2453×935 244 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>The other 3 lines in the init are filling the weights and biases with certain values before training begins.</p>
<p>The forward pass is then taking any tensors sent into the model and passing them through those two layers. But the <code>torch.relu(out)</code> will probably give an error because I don’t believe that exists. Should be <code>F.relu(out)</code> or <code>torch.nn.functional.relu(out)</code>.</p>

vdw: <p>If you really want to understand what’s going on, you have to ignore PyTorch code and get familiar with the basic, in this case:</p>
<ul>
<li>Convolutional Layers</li>
<li>Batch Normalization (vs. Layer Normalization)</li>
<li>Activation functions (here: ReLU)</li>
<li>Initialization of weights (here: Kaiming initialization)</li>
<li>Residual layers (core idea behind ResNet)</li>
</ul>
<p>The whole purpose of PyTorch, Tensorflow, etc. is essentially to abstract from these nitty-gritty details. Of course, it still make sense to have at least some understanding, simply for understanding the input parameters of the different layers.</p>