Post-processing on a Parameter after update

Hello everyone,

I need to make sure that a given Parameter tensor from a Module is kept sorted.

This tensor is updated by the optimizer at each step, but is there an elegant way to have some “callback” function called whenever it is modified, so that I may sort it when necessary ?

thanks !


You can just do sort after optimizer.step.

Yeah just do it everytime on calling the forward function. first sort the parameter and then proceed…

Thanks for your answers.

This is not so easy, because that tensor is a Parameter, which means I cannot just change its values, apparently.

Furthermore, I would like this behaviour of that member from my module to be “built-in”: I don’t want a user having to manually sort it each time he-she calls an optimizer step !

is there some more way to track that a class member for a module has been changed ?

You can still change values of a parameter in a no_grad block, or via its .data.

ok, thanks for that,

but then what about the fact that I would like the user not to care about this but have this encapsulated within the module ?


Subclass the optimizer :slight_smile:

ok, I see, this wouldn’t help me since it would require any user of this custom module to only use my custom optimizer.

I guess I’ll have to go with it and tell ppl to just run some code of mine after the step()

(a feature like monitoring if some attribute from a module was changed to run some code in that case would be nice here for me)