Issue
I have my model (a VGG16, but it is not important). I want to check only some parameters of my network, for example the first ones.
To do this I do list(model.parameters())
and it prints all the parameters.
Now, considering that a VGG has this shape:
VGG16(
(block_1): Sequential(
(0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU()
(3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU()
(6): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
)
...
If I want only the weights of the convolutions I do this: list(model.block_1[0].parameters())
and it prints this:
[Parameter containing:
tensor([[[[-0.3215, -0.0771, 0.4429],
[-0.6455, -0.0827, -0.4266],
[-0.2029, -0.2288, 0.1696]]],
[[[ 0.5323, -0.2418, -0.1031],
[ 0.5917, 0.2669, -0.5630],
[ 0.3064, -0.4984, -0.1288]]],
[[[ 0.3804, 0.0906, -0.2116],
[ 0.2659, -0.3325, -0.1873],
[-0.5044, 0.0900, 0.1386]]],
Now, these lists are always enormous. How can I print only the first values, for example, the first matrix?
[[[[-0.3215, -0.0771, 0.4429],
[-0.6455, -0.0827, -0.4266],
[-0.2029, -0.2288, 0.1696]]]
Solution
You can treat it as a NumPy array when it's processed correctly. In your example, this should work:
from torchvision import models
model = models.vgg16()
first_param = list(model.features[0].parameters())[0].data
The first_param
will hold the tensor as:
tensor([[[[-0.3215, -0.0771, 0.4429],
[-0.6455, -0.0827, -0.4266],
[-0.2029, -0.2288, 0.1696]]],
[[[ 0.5323, -0.2418, -0.1031],
[ 0.5917, 0.2669, -0.5630],
[ 0.3064, -0.4984, -0.1288]]],
[[[ 0.3804, 0.0906, -0.2116],
[ 0.2659, -0.3325, -0.1873],
[-0.5044, 0.0900, 0.1386]]]
Then just continue as NumPy array:
print(first_param[0])
>> tensor([[[[-0.3215, -0.0771, 0.4429],
[-0.6455, -0.0827, -0.4266],
[-0.2029, -0.2288, 0.1696]]])
Answered By - CuCaRot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.