microsoft / ContextualSP

Multiple paper open-source codes of the Microsoft Research Asia DKI group
MIT License
374 stars 62 forks source link

about prediction problem #4

Closed LLLLLLoki closed 4 years ago

LLLLLLoki commented 4 years ago

I'm not very familiar with AllenNLP api,How do you use the prediction code? I write the following code that reports an " TypeError: is_bidirectional() missing 1 required positional argument: 'self'" error

@Predictor.register("rewrite") class RewritePredictor(Predictor):

@overrides
def _json_to_instance(self, json_dict: JsonDict) -> Instance:
    """
    Expects JSON that looks like `{"source": "..."}`.
    """
    context = json_dict["context"]
    current = json_dict["current"]
    # placeholder
    # restate_utt = "hi"
    restate_utt = json_dict["restate_utt"]
    return self._dataset_reader.text_to_instance(context, current, restate_utt, training=False)

inputs = { "context":'浙 江 省 温 州 市 鹿 城 区 有 好 天 气 这 种 天 气 最 适 合 出 门 了 骑 骑 车 兜 兜 风', "current":'明 天 天 气 咋 样', "restate_utt":'hi' } model = UnifiedFollowUp( Vocabulary, Seq2SeqEncoder, TextFieldEmbedder ) dataset_reader = RewriteDatasetReader()

pred_fun = RewritePredictor(model = model,dataset_reader = dataset_reader) result = pred_fun._json_to_instance(inputs)

SivilTaram commented 4 years ago

@LLLLLLoki Do you want to predict in python script or command line? Predictor is usually used in allennlp predict command such as below:

allennlp predict \
--predictor rewrite \
--include-package data_reader \
--include-package model \
--include-package predictor \
--dataset-reader-choice validation \
--batch-size 24 \
--cuda-device 0 \
--output-file predict_test.jsonl \

BTW, this requires developers to convert the txt file into .jsonl format.

Which one do you need? A in-code function or just a command? I will prepare one for your convenience.

LLLLLLoki commented 4 years ago

Could you provide A in-code function for me ? thank you

SivilTaram commented 4 years ago

@LLLLLLoki I will update in 2 days. (since it's also new for me to write an in-code function lol)

SivilTaram commented 4 years ago

@LLLLLLoki Hi, I have uploaded an in-code predict function here. Please check and enjoy it :)

LLLLLLoki commented 4 years ago

I learned a lot from your code and your papers,Thank you very much.

SivilTaram commented 4 years ago

@LLLLLLoki Very pleased to hear that ☕