How to access the weights of one particular convolutional kernel?

1315933876: <p>I am using Pytorch to train ResNet18 on CIFAR10 dataset.<br>
My question is , How to access the weights of one particular  convolutional kernel of layer 1, for example, the weights corresponding to layer 1 ,(0)basic block ,  conv1?<br>
The following are printed, after this command “net = ResNet18()” and “print(net.)”.  Thank you very much.</p>
<p>DataParallel(<br>
(module): ResNet(<br>
(conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(layer1): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential()<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential()<br>
)<br>
)<br>
(layer2): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential(<br>
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential()<br>
)<br>
)<br>
(layer3): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential(<br>
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential()<br>
)<br>
)<br>
(layer4): Sequential(<br>
(0): BasicBlock(<br>
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential(<br>
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)<br>
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(1): BasicBlock(<br>
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(shortcut): Sequential()<br>
)<br>
)<br>
(linear): Linear(in_features=512, out_features=10, bias=True)<br>
)<br>
)</p>

1chimaruGin: <p><code>print(net.module.layer1[0].conv1.weight)</code></p>

1315933876: <p>Thank you very much.</p>

1315933876: <aside class="quote no-group" data-username="1315933876" data-post="1" data-topic="110100">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/71e660/48.png" class="avatar"> 1315933876:</div>
<blockquote>
<p>DataParallel(<br>
(module): ResNet(<br>
(conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)</p>
</blockquote>
</aside>
<p>And how to access the weights corresponding to this convolutional kernel? Thank you very much.<br>
ResNet(<br>
(conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)</p>

1315933876: <p>Thank you very much. And I have another question.</p>
<aside class="quote no-group" data-username="1chimaruGin" data-post="2" data-topic="110100">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/1chimarugin/48/27435_2.png" class="avatar"> 1chimaruGin:</div>
<blockquote>
<p>print(net.module.layer1[0].conv1.weight)</p>
</blockquote>
</aside>
<p>It seems that “net.module.layer1[0].conv1.weight” is a struct, actually I want to get the tensor corresponding to this struct. I want to access the four dimensional array, whose entry is double or float.<br>
Which command should I use? Thank you very much.<br>
The following are printed after this command “print(net.module.layer1[0].conv1.weight)”.</p>
<aside class="quote no-group">
<blockquote>
<p>Parameter containing:<br>
tensor([[[[ 0.0239, -0.0199, -0.0045],<br>
[ 0.0490, 0.0366, 0.0082],<br>
[ 0.0374, 0.0352, 0.0106]],<br>
… …</p>
<pre><code> [[ 0.0250,  0.0002,  0.0127],
  [-0.0045,  0.0197, -0.0044],
  [ 0.0214,  0.0195,  0.0011]]]], device='cuda:0', requires_grad=True)
</code></pre>
</blockquote>
</aside>

1chimaruGin: <p>Add <code>.data</code></p>
<pre><code class="lang-auto">weight_data = net.module.layer1[0].conv1.weight.data

print(weight_data.shape)
print(weight_data.dtype)
</code></pre>