Making parameters()
return an iterator gives ample opportunities to shoot yourself in the foot, e.g.:
# broken gradient clipping
a = model.parameters()
norm = 0
for p in a:
norm += p.norm()
for p in a:
p /= norm
Also, printing gradients by writing list(m.parameters())[0]
is annoying.
I don't think I agree. A lot of things like this are generators/iterators in Python3 (think map
, filter
, zip
, range
, ...) and you can shoot yourself in the same way. If you want to iterate over the thing multiple times just do list(model.parameters())
and if you just want the first one do next(model.parameters())
(which is also going to be much faster, because it won't iterate over all of your model at all).
Most helpful comment
I don't think I agree. A lot of things like this are generators/iterators in Python3 (think
map
,filter
,zip
,range
, ...) and you can shoot yourself in the same way. If you want to iterate over the thing multiple times just dolist(model.parameters())
and if you just want the first one donext(model.parameters())
(which is also going to be much faster, because it won't iterate over all of your model at all).