dmlc / gluon-nlp

NLP made easy
https://nlp.gluon.ai/
Apache License 2.0
2.56k stars 538 forks source link

How to use nlp.model.translation.NMTModel inside a HybridBlock? #973

Open zeeshansayyed opened 5 years ago

zeeshansayyed commented 5 years ago

I want to use NMTModel inside a network which is HybridBlock. Something like this:

from mxnet imort nn
import gluonnlp as nlp
class MyNetwork(nn.HybridBlock):
    def __init __(self):
        super(MyNework, self).__init__():
        self.nmt_model = nlp.model.translation.NMTModel()

I run into the following error:

ValueError: Children of HybridBlock must also be HybridBlock, but NMTModel() has type <class 'gluonnlp.model.translation.NMTModel'>. If you are using Sequential, please try HybridSequential instead.

What would be the easiest way of solving this problem? Should I implement my own version of NMTModel which inherits HybridBlock instead of Block?

sxjscience commented 5 years ago

Is it possible to change MyNetwork to inherit from Block?

zeeshansayyed commented 5 years ago

Unfortunately, no. What if I write custom NMTModelHybrid class and copy the code from NMTModel, add a hybrid_forward() function and call forward() inside hybrid_forward(). This seems to be working, but I am not sure whether it is working correctly as the original NMTModel is intended to be.

sxjscience commented 5 years ago

HybridBlock should only be used when the whole model can be hybridized, which means that it can be written via the symbolic APIs in MXNet. We should use Block if we cannot make such guarantee.

Get Outlook for iOShttps://aka.ms/o0ukef


From: Zeeshan Sayyed notifications@github.com Sent: Tuesday, October 15, 2019 2:00:15 AM To: dmlc/gluon-nlp gluon-nlp@noreply.github.com Cc: Xingjian SHI xshiab@connect.ust.hk; Comment comment@noreply.github.com Subject: Re: [dmlc/gluon-nlp] How to use nlp.model.translation.NMTModel inside a HybridBlock? (#973)

Unfortunately, no. What if I write custom NMTModelHybrid class and copy the code from NMTModel, add a hybrid_forward() function and call forward() inside hybrid_forward(). This seems to be working, but I am not sure whether it is working correctly as the original NMTModel is intended to be.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/dmlc/gluon-nlp/issues/973?email_source=notifications&email_token=ABHQH3VD2E5YQB6WRAYFMGLQOWBB7A5CNFSM4JAQ5GNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBH7ZRY#issuecomment-542112967, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABHQH3TD2OIIEFL2KNXS52TQOWBB7ANCNFSM4JAQ5GNA.