-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
62 lines (51 loc) · 2.12 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Sample from a trained model.
Given a sentence of characters in the vocabulary, the model will predict the
sorted version of the sentence.
Usage:
$ python sample.py --model_path <path_to_model> --sentence <sentence_to_process>
Example:
$ python sample.py --model_path last_model.pt --sentence ABCABB
"""
import argparse
import torch
from model import Transformer
from config import TRANSFORMER_CONFIG, SENTENCE_LEN
from utils import detokenize_sentence
# Create ArgumentParser object
parser = argparse.ArgumentParser(description='Process a sentence.')
# Add argument for the model path
parser.add_argument('--model_path', type=str, help='The path to the model.')
# Add argument for the sentence
parser.add_argument('--sentence', type=str, help='The sentence to process.')
# Parse the command-line arguments
args = parser.parse_args()
# Convert the sentence to a list of characters
characters = list(args.sentence)
# Determine the device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the checkpoint
checkpoint = torch.load(args.model_path, map_location=device)
checkpoint_inverse_vocabulary = checkpoint["config"]["inverse_vocabulary"]
# Check that the sentence is in the vocabulary
for char in characters:
assert char in checkpoint_inverse_vocabulary.values(), \
f"The character {char} is not in the trained vocabulary"
# Load the model
model = Transformer(TRANSFORMER_CONFIG)
model.load_state_dict(checkpoint["model"])
model.to(device)
# Prepare the sentence for the model => delimiters and padding
src = ["<s>"] + characters + ["<e>"]
padd_needed = SENTENCE_LEN - len(src)
src += ["PAD"] * padd_needed
out_tokens = model.generate(src, device=device, top_k=1)[0].tolist()
out_str = detokenize_sentence(out_tokens, checkpoint_inverse_vocabulary)
# Join and remove the delimiters and padding from the output
out_str = "".join(out_str)
out_str = out_str.replace("<s>", "").replace("<e>", "").replace("PAD", "")
# Check correctness
is_correct = "".join(sorted(characters)) == out_str
# Print the results
print(f"Input: {args.sentence}")
print(f"Output: {out_str} {'(correct)' if is_correct else '(incorrect)'}")