params_to_update = [{'params': model.fc.parameters(), 'lr': 0.001}]
for d in params_to_update:
for param in d['params']:
print(param.requires_grad)
optimizer = optim.Adam(params_to_update)
print(optimizer.param_groups)
where the 'params': [] is empty now (before inserting the for-loops it was not empty). I find it very strange since I havenât altered the params_to_update dictionary and just made some print statements in the for-loop.
Without the list cast you have an iterator that you can only consume once. If you do that in the for loop, there wonât be anything left when you pass it to the optimizer.
No thatâs iterators in general. I believe that if one would want to avoid that, youâd need something like generators (e.g. what datasets do), where you can get multiple (ârewound to startâ) iterators from a generator, but then youâd probably have other drawbacks.