fastnlp / TENER

Codes for "TENER: Adapting Transformer Encoder for Named Entity Recognition"
370 stars 55 forks source link

how to inference after the train has been completed? #14

Closed TorRient closed 4 years ago

TorRient commented 4 years ago

i want to load model TENER after trained for predict predict with input type Str please!

yhcc commented 4 years ago

I will try to give you a demo recently.

TorRient commented 4 years ago

I will try to give you a demo recently.

i waiting for you thank you so much

7228TigersFan commented 4 years ago

Hello,

First and foremost I hope this email finds you well. I need to test this model. Can you please send this to me as well? The input type is str. It is really important to me.

yhcc commented 4 years ago

Sorry, since the last issue has been closed, I forgot to give the demo later. code to add in the training file: https://github.com/fastnlp/TENER/blob/master/train_tener_en.py

# before this line https://github.com/fastnlp/TENER/blob/d2614d509dffb9b30636e3523a2f8f0dc4876708/train_tener_en.py#L101 add the following code to save the vocabulary for later usage
data_bundle.get_vocab('chars').save('/path/to/save/vocab.txt')
data_bundle.get_vocab('target').save('/path/to/save/target.txt')
# change save_path=None in https://github.com/fastnlp/TENER/blob/d2614d509dffb9b30636e3523a2f8f0dc4876708/train_tener_en.py#L123 to save_path='directory/to/save/model/', this will save a model in this directory
import torch
from fastNLP import Vocabulary

model = torch.load('/path/to/your/model')  # I assume you use the Trainer save_path parameter to save the model in whole, you can find a file in the directory you pointed during training and that is the saved model.

vocab = Vocabulary.load('/path/to/save/vocab.txt')
target_vocab = Vocabulary.load('/path/to/save/target_vocab.txt')

text = "China is a country".split()  # I use this simple text as an example
indices = torch.LongTensor([vocab.to_index(w) for w in text]).unsqueeze(0)  # change to indices, and add a batch dimension

targets = model.predict(indices)  # will be of shape [1, 5]
targets = targets[0].tolist()
targets = [target_vocab.to_word(t) for t in targets]  # will be like ['S-ORG', 'O', 'O', ...]
7228TigersFan commented 4 years ago

Thank you very much for providing the Demo. I have a question: Should I train it again?

7228TigersFan commented 4 years ago

Also how can I save my model? Because you said # I assume you use the Trainer save_path parameter to save the model in whole, otherwise, you need to initialize your model then load the weights.

yhcc commented 4 years ago

Sorry, i did not understand what do you mean by "Should I train it again ?". For "how can i save my model ?", i assume you are asking how to save the model in pytorch, here is the refrence https://pytorch.org/docs/stable/torch.html#torch.save

7228TigersFan commented 4 years ago

It says that I need permission when I run the code above. Also, the model.pt is a directory. Is that normal?

7228TigersFan commented 4 years ago

Can you please send the model.pt because my model.pt is not a file but it is a folder.

7228TigersFan commented 4 years ago

It says Permission Denied. I am using windows and I tried to launch it as administrator.

yhcc commented 4 years ago

Sorry, I didn't save my model. And sorry for the mistake I made, you need to load 'model.pt/xxxx' (xxxx is what in your directory, that is the model). I forgot that the save_path parameter in Trainer is pointing a directory to save the model (Trainer will assign a name to the saved model by timestamp).

7228TigersFan commented 4 years ago

It gives an error: No module named 'models'

yhcc commented 4 years ago

It seems like that you are using the model outside the folder where you trained your model, in this case, model has to been saved by parameters. Right now, fastNLP does not support save and load trained model very well (I am working on these stuff, but it may take some time to finish), therefore, if you want to save the model for later use, the only help this repo can provide right now is a reference to the implementation of the model. Sorry for the inconvenience, as long as i finish coding the fastNLP part, i will update this issue.