-
Notifications
You must be signed in to change notification settings - Fork 812
Example docs #576
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
Merged
zhangguanheng66
merged 22 commits into
pytorch:master
from
zhangguanheng66:example_docs
Jul 31, 2019
Merged
Example docs #576
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e93f8b5
Docs to model.py.
32c83f5
docs for train.py
67c8720
Comments to predict.
332b6a7
Add docs for create_datasets.py
951b8fb
A few commits to address reviewer's comments.
88b4064
Flake 8
53633d4
Merge branch 'master' into example_docs
03092e7
Add docs to iterable_train.py
2e8de64
update docs in iterable_train.py
1a9edd5
Add links to docs rst files.
95ffe82
Minor edit.
a693c6d
Fix predict file.
fb1ed11
Change the lr in train.py.
ee815d1
Minor edits.
d1b12b0
flake8 edit
f935719
Minor edit.
40d8115
Use StepLR scheduler.
baccab0
Fix predict.py
d95838c
Add ngrams to predict.py
646d2a7
Add help strings to examples.
564bf3b
Minor edit.
eaf439a
Remove _default_unk_index from docs.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import torch | ||
import sys | ||
import argparse | ||
from torchtext.data.utils import get_tokenizer | ||
from torchtext.data.utils import ngrams_iterator | ||
|
||
from torchtext.datasets.text_classification import text_normalize | ||
|
||
def predict(text, model, dictionary, ngrams): | ||
r""" | ||
The predict() function here is used to test the model on a sample text. | ||
The input text is numericalized with the vocab and then sent to | ||
the model for inference. | ||
|
||
def predict(text, model, dictionary): | ||
Arguments: | ||
text: a sample text string | ||
model: the trained model | ||
dictionary: a vocab object for the information of string-to-index | ||
ngrams: the number of ngrams. | ||
""" | ||
tokenizer = get_tokenizer("basic_english") | ||
with torch.no_grad(): | ||
text = torch.tensor([dictionary.get(token, dictionary['<unk>']) | ||
for token in text_normalize(text)]) | ||
text = torch.tensor([dictionary[token] | ||
for token in ngrams_iterator(tokenizer(text), ngrams)]) | ||
output = model(text, torch.tensor([0])) | ||
return output.argmax(1).item() + 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description='Predict text from stdin given model and dictionary') | ||
parser.add_argument('model') | ||
parser.add_argument('dictionary') | ||
parser.add_argument('model', help='the path for model') | ||
parser.add_argument('dictionary', help='the path for dictionary') | ||
parser.add_argument('--ngrams', type=int, default=2, | ||
help='ngrams (default=2)') | ||
args = parser.parse_args() | ||
|
||
model = torch.load(args.model) | ||
dictionary = torch.load(args.dictionary) | ||
for line in sys.stdin: | ||
print(predict(line, model, dictionary)) | ||
print(predict(line, model, dictionary, args.ngrams)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.