'Difference between model.parameters and model.parameters(), pytorch
I have read through the documentation and I don't really understand the explanation. Here is the explanation I got from the documentation Returns an iterator over module parameters. Why does model.parameters() return the file location e.g <generator object Module.parameters at 0x7f1b90c29ad0>. model.parameters will give me an output of
<bound method Module.parameters of ResNet9(
(conv1): Sequential(
(0): Conv2d(3, 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(inplace=True)
)
(conv2): Sequential(
(0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
).....
Solution 1:[1]
model.parameters()
It's simply because it returns an iterator object, not a list or something like that. But it behaves quite similar to a list. You can iterate over it eg with
[x for x in model.parameters()]
Or you can convert it to a list
[list(model.parameters())]
Iterators have some advantages over lists. Eg they are not "calculated" when they are created, which can improve performance.
For more on iterators just google for "python iterators", you'll find plenty of information. Eg w3schools.com is a good source.
model.parameters
The output model.parameters consists of two parts.
The first part bound method Module.parameters of tells you that you are referencing the method Module.parameters.
The second part tells you more about the object containing the referenced method. It' s the "object description" of your model variable. It's the same as print(model)
more on python references
model.parameter is just a reference to the parameter function, it's not executing the function. model.parameter() instead is executing it.
Maybe it gets more clear with a simple example.
print("Hello world")
>>> Hello world
print
>>> <function print>
abc = print
abc("Hello world")
>>> Hello world
abc
>>> <function print>
As you see abc behave exactly the same as print because i assigned the reference of print to abc.
If I would have executed the function instead, eg abc = print("Hello world"), abc would contain the string Hello world and not the function reference to print.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
