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

请问如何用该模型预测我的数据? #19

Open
wangmatlab opened this issue Nov 6, 2019 · 5 comments
Open

请问如何用该模型预测我的数据? #19

wangmatlab opened this issue Nov 6, 2019 · 5 comments

Comments

@wangmatlab
Copy link

No description provided.

@wangmatlab
Copy link
Author

训练好了这个模型 如何用来预测我的数据?

@lemonhu
Copy link
Owner

lemonhu commented Nov 6, 2019

谢谢关注。
建议参考文件evaluate.py,即可实现模型的测试评估。

@wangmatlab
Copy link
Author

谢谢关注。
建议参考文件evaluate.py,即可实现模型的测试评估。
感谢作者回信,
我是想用该模型预测任意输入的文本(而不是用于测试已有的测试数据),请问该如何实现?

@lemonhu
Copy link
Owner

lemonhu commented Nov 7, 2019

利用训练好的模型提供离线/在线服务,结合data_loader.pyevaluate.py,进行适当改写即可了。

@temco
Copy link

temco commented Mar 4, 2020

类似于这样,在evaluate.py中添加一个函数

def predict(line, model, data_loader, params): # added by temco for prediction
    """predict the sentence with the model"""
    idx2tag = params.idx2tag
    pred_tags = []
    batch_data = []
    tokens = data_loader.tokenizer.tokenize(line.strip())
    batch_data.append(data_loader.tokenizer.convert_tokens_to_ids(tokens))
    batch_data = torch.tensor(batch_data, dtype=torch.long)
    batch_masks = batch_data.gt(0)
    batch_output = model(batch_data, token_type_ids=None, attention_mask=batch_masks)
    batch_output = batch_output.detach().cpu().numpy()
    pred_tags = []
    pred_tags.extend([idx2tag.get(idx) for indices in np.argmax(batch_output, axis=2) for idx in indices])
    logging.info(pred_tags)

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

No branches or pull requests

3 participants