question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How can I use an optimizer with the extracted parameters?

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
egrefencommented, May 13, 2020

@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 šŸ˜„

0reactions
egrefencommented, May 13, 2020

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 can pip 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:

  1. Is DropoutNet a ā€œvanillaā€ torch.nn.Module instance here?
  2. Why would p.grad not be None here, as no call to loss.backward is made, or to torch.autograd.grad, or equivalent? Iā€™d expect the add_ to fail with an AttributeError.
  3. Even if it doesnā€™t, why do you need the optimizer step to be differentiable here? Itā€™s not clear what the gradients youā€™d be seeking to define after this step are.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found