Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update parameter with moving average method #557

Closed
hustzxd opened this issue Jan 7, 2019 · 2 comments
Closed

How to update parameter with moving average method #557

hustzxd opened this issue Jan 7, 2019 · 2 comments
Labels

Comments

@hustzxd
Copy link

hustzxd commented Jan 7, 2019

I want to add a parameter in my custom layer, and I want to update it by moving average. So far, I have implemented a simple version, but it can't perform hybridize. Hope some body's help.

class CustomLayer(HybridBlock):
    def __init__(self, prefix=None, params=None):
        super(CustomLayer, self).__init__(prefix=prefix, params=params)
        init_scale = mx.init.Constant([32])
        with self.name_scope():
            self.scale = self.params.get('scale',
                                         shape=[1],
                                         init=init_scale,
                                         allow_deferred_init=False,
                                         differentiable=False)

    def hybrid_forward(self, F, x, scale=None):
        if autograd.is_training():
            with autograd.pause():
                scale = scale * 0.9 + new_scale * 0.1
                self.scale.set_data(scale)
        y = x
        return y
@zhreshold
Copy link
Member

I think you don't need to call set_data explicitly in hybrid_forward, which is not recommended way in the computation graph.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants