0

I wonder what is the right way to use torchtext for inference.

Let's assume I've trained the model and dump all Fields with built vocabularies. It seems the next step is to use torchtext.data.Example to load one single example. Somehow I should numeralize it by using loaded Fields and create an Iterator.

I would appreciate any simple examples of using torchtext for inference.

1 Answer 1

4

For a trained model and vocabulary (which is part of the text field , you don't have to save the whole class) :

    def read_vocab(path):
        #read vocabulary pkl 
        import pickle
        pkl_file = open(path, 'rb')
        vocab = pickle.load(pkl_file)
        pkl_file.close()
        return vocab



    def load_model_and_vocab():
        import torch
        import os.path
    
        my_path = os.path.abspath(os.path.dirname(__file__))
        vocab_path = os.path.join(my_path, vocab_file)
        weights_path = os.path.join(my_path, WEIGHTS)
    
        vocab = read_vocab(vocab_path)
        model = classifier(vocab_size=len(vocab))
        model.load_state_dict(torch.load(weights_path))
        model.eval()
        return model, vocab
    
    
    def predict(model, vocab, sentence):
        tokenized = [w.text.lower() for w in nlp(sentence)]  # tokenize the sentence
        indexed = [vocab.stoi[t] for t in tokenized]         # convert to integer sequence
        length = [len(indexed)]                              # compute no. of words
        tensor = torch.LongTensor(indexed).to('cpu')         # convert to tensor
        tensor = tensor.unsqueeze(1).T                       # reshape in form of batch,no. of words
        length_tensor = torch.LongTensor(length)             # convert to tensor
        prediction = model(tensor, length_tensor)            # prediction
        return round(1-prediction.item())

"classifier" is the class I defined for my model.

For saving the vocabulary pkl :

    def save_vocab(vocab):
        import pickle
        output = open('vocab.pkl', 'wb')
        pickle.dump(vocab, output)
        output.close()

And for saving the model after training you can use :

    torch.save(model.state_dict(), 'saved_weights.pt')

Tell me if it worked for you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.