Views
No views yet
1{
2 "model_type": "transformer_mt",
3 "d_model": 256,
4 "num_encoder_layers": 3,
5 "num_decoder_layers": 3,
6 "num_heads": 4,
7 "d_ff": 512,
8 "dropout": 0.1,
9 "max_seq_len": 5000,
10 "src_language": "de",
11 "tgt_language": "en",
12 "src_vocab_size": 357611,
13 "tgt_vocab_size": 191005,
14 "pad_idx": 1,
15 "bos_idx": 2,
16 "eos_idx": 3,
17 "unk_idx": 0,
18 "architecture": "TransformerSeq2Seq",
19 "framework": "pytorch"
20}1from huggingface_hub import hf_hub_download
2import torch
3import json
4
5# Define your Transformer class (copy from model.py)
6# class Transformer(...): ...
7
8REPO_ID = "posity/attention_is_all_you_need_de_en_wmt14"
9
10# Download files
11model_weights_path = hf_hub_download(repo_id=REPO_ID, filename="transformer_model_wmt14_epoch_3.pth")
12config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
13vocab_src_path = hf_hub_download(repo_id=REPO_ID, filename="vocab_src_wmt14.json")
14vocab_tgt_path = hf_hub_download(repo_id=REPO_ID, filename="vocab_tgt_wmt14.json")
15
16# Load config
17with open(config_path, 'r') as f:
18 config = json.load(f)
19
20# Load vocabularies
21with open(vocab_src_path, 'r') as f:
22 vocab_src_data = json.load(f)
23with open(vocab_tgt_path, 'r') as f:
24 vocab_tgt_data = json.load(f)
25
26# Create vocabulary objects
27class SimpleVocab:
28 def __init__(self, vocab_data):
29 self.stoi = vocab_data['stoi']
30 self.itos = vocab_data['itos']
31
32 def __getitem__(self, token):
33 return self.stoi.get(token, self.stoi.get('<unk>', 0))
34
35 def __len__(self):
36 return len(self.stoi)
37
38vocab_src = SimpleVocab(vocab_src_data)
39vocab_tgt = SimpleVocab(vocab_tgt_data)
40
41# Instantiate model
42model = Transformer(
43 src_vocab_size=len(vocab_src),
44 tgt_vocab_size=len(vocab_tgt),
45 d_model=config['d_model'],
46 num_encoder_layers=config['num_encoder_layers'],
47 num_decoder_layers=config['num_decoder_layers'],
48 num_heads=config['num_heads'],
49 d_ff=config['d_ff'],
50 dropout=config['dropout'],
51 max_seq_len=config['max_seq_len']
52)
53model.load_state_dict(torch.load(model_weights_path, map_location=torch.device('cpu')))
54model.eval()