Views
No views yet
1import torch
2from model import BiLSTMPoetryModel # or UnidirectionalLSTMPoetryModel
3from shared.preprocess import CharVocabulary
4
5# Load vocabulary
6vocab = CharVocabulary.load("vocab.json")
7
8# Load model
9model = BiLSTMPoetryModel(
10 vocab_size=vocab.vocab_size,
11 embedding_dim=256,
12 hidden_dim=512,
13 num_layers=3,
14 dropout=0.0, # No dropout for inference
15 pad_idx=vocab.pad_idx
16)
17
18checkpoint = torch.load("best_model.pt", map_location="cpu")
19model.load_state_dict(checkpoint['model_state_dict'])
20model.eval()
21
22# Generate a poem
23prompt = "Title: The Moon\n\n"
24start_tokens = vocab.encode(prompt, add_special_tokens=False)
25generated = model.generate(start_tokens, vocab, max_length=300, device="cpu")
26print(generated)best_model.pt: PyTorch model checkpointvocab.json: Character vocabulary mappingconfig.json: Training configuration