This article mainly introduces the meaning of nn.Sequential(*net[3: 5]) in pytorch. The article introduces it in detail through sample codes. It has a certain reference learning value for everyone's deep learning or work. Need Friends, follow the editor to learn together
see this in the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
You can see that the sixth line in the code (remove the serial number by yourself, I typed it in) self.layer0 = nn.Sequential(net[:3])
and
the seventh line self.layer1 = nn.Sequential(*net[3: 5])
have a nn.Sequential(net[:3])
and nn.Sequential(*net[3: 5])
Today I won’t talk about nn.Sequential()
usage, meaning, and function because I don’t really understand it. Shockingly, *net[3: 5]
why should this thing be included? When “ * ”
the code is not included , the following problems will occur when running*
It means that the list is not a subclass, which means that the parameters are wrong
1 |
|
This line of code is to take out each layer of the model to build a list, just try to print it yourself. The approximate output is [conv(),BatchNorm2d(), ReLU,MaxPool2d]
to wait
A total of one element, and the general list is not the same.
When we fetch net[:3]
, the parameter passed in is a list, but *net[:3]
when we use it, it passes in a single element
1 2 3 4 |
|
The result without ✳ is a list, and the one with ✳ is an element, so nn.Sequential(*net[3: 5])
in *net[3: 5]
is to nn.Sequential()
pass multiple layers into this container.
So far, this article about the meaning of nn.Sequential(*net[3: 5]) in pytorch is introduced here. For more related pytorch nn.Sequential(*net[3: 5]) content, please search Read the previous articles of developpaer or continue to browse the related articles below. I hope you will support developpaer in the future!