How can I use an optimizer with the extracted parameters?
See original GitHub issueI would like to use an optimizer with the extracted parameters but I keep getting some unexpected behavior like the parameters are still referring to the parameter list in the original modelā¦
model = DropoutNet(in_dim, args.h_dim, out_dim).to(args.device) optimizer = torch.optim.SGD(model.parameters(), lr=1, weight_decay=1e-4)
params = OrderedDict(model.parameters())
p_opt = torch.optim.SGD(params.values(), lr=1e-3)
yhat = model(x, params=params)
loss = criterion(y, yhat)
for p in params.values():
p.grad.add_(loss)
p_opt.step()
p_opt.zero_grad()
If I do something like this and then print out the parameters, the parameters in the model will be the same as the ones in the params
dict. Is there a better way to do this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
2.4) How to Extract Optimal Parameter Values in ... - YouTube
... extracting the parameters with the best possible edge from an optimization process using a technique called 'Statistical Power Analysis'Ā ...
Read more >Model Parameter Extraction Using Optimization Method
In this chapter we will be concerned with the nonlinear optimization techniques for extracting the device model parameters for various DC and AC...
Read more >An Efficient Heap-Based Optimizer for Parameters ...
The variables of three PV models (TPVM) and modified three PV models (MTPVM) are extracted using a new optimization algorithm (called heap-based optimizer)....
Read more >Improved gradientābased optimizer for parameters extraction ...
Gradient-based optimizer (GBO), which was proposed in 2020, is a method with swarm characteristics that was developed from Newton's method.
Read more >(PDF) Improved gradientābased optimizer for parameters ...
To validate the performance of IGBO, it is applied to the parameter extraction of different PV models. The PV models used in this...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tristandeleu The post above inspired me to test higher with torchmeta dataloaders (allow users to use any torch optimizers or arbitrary third party modules instead of MetaModules). See this gist for an example: https://gist.github.com/egrefen/19ad0b65cf4d997a4b5bebf6e98b0562
It works beautifully š
Hello @deltaskelta!
Thanks for the plug, @tristandeleu š You are right that
higher
is your friend here, and AFAIK is totally compatible with Torchmeta, although it would primarily aim to supplant the use to MetaModule so you just use normal pytorch modules and patch them on the fly, but the data loaders would be valuable. I havenāt tried this yet, but it should work out of the box. And as of yesterday, you canpip install higher
to install v0.2 š@deltaskelta please help me understand what you are doing a little more, and maybe I can advise on how to mix
higher
and TorchMeta here for fun and profit(?). It seems to me like you are computing a loss, and then adding it to the grads of your model parameters and then taking an optimizer step using those grads. My questions:DropoutNet
a āvanillaātorch.nn.Module
instance here?p.grad
not beNone
here, as no call toloss.backward
is made, or totorch.autograd.grad
, or equivalent? Iād expect theadd_
to fail with anAttributeError
.