feedly/transfer-nlp

have the possibility to build object with a function instead of a class

Closed this issue · 2 comments

When you want to experiment with someone else's code, you don't want to copy-paste their code.

If you want to use a class AwesomeClass from an awesome github repo, you can do:

from transfer_nlp.pluginf.config import register_plugin
from awesome_repo.module import AwesomeClass

register_plugin(AwesomeClass)

and then use it in your experiments.

However, when reusing complex objects, it might complicated to configure them.
An example is the pre-trained model from the pytorch-pretrained-bert repo, where you can build complex models with nice one-liners such as model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=4)

It's possible to encapsulate these into other classes and have Transfer NLP build them, but it can feel awkward and adds unnecessary complexity / lines of code compared to the initial one-liner.
An alternative is to build these objects with a method, in the previous example we would only write:

@register_function
def bert_classifier(bert_version: str='bert-base-uncased', num_labels: int=4):
    return BertForSequenceClassification.from_pretrained(pretrained_model_name_or_path=bert_version, num_labels=num_labels)

and we could use functions just as methods in the config loading.

could we also be able to avoid the wrapper function? is it possible to call the decorator directly with an existing function rather than using the @ syntax?

Yes definitely!