added code for translating
Browse files- translator.py +8 -8
translator.py
CHANGED
|
@@ -19,13 +19,6 @@ WARMUP_STEPS = 4000
|
|
| 19 |
MODEL_NAME = 'pico_model_colab.pth'
|
| 20 |
VOCAB_NAME = 'pico_translator_colab.vocab'
|
| 21 |
|
| 22 |
-
# Import token2idx
|
| 23 |
-
|
| 24 |
-
with open(VOCAB_NAME, 'r') as f:
|
| 25 |
-
token2idx = json.load(f)
|
| 26 |
-
idx2token = {idx: char for char, idx in token2idx.items()}
|
| 27 |
-
VOCAB_SIZE = len(token2idx)
|
| 28 |
-
|
| 29 |
# What device is available
|
| 30 |
|
| 31 |
if torch.cuda.is_available():
|
|
@@ -37,6 +30,13 @@ else:
|
|
| 37 |
DEVICE = torch.device(device)
|
| 38 |
DEVICE
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Embedding and Transformer
|
| 41 |
|
| 42 |
class Embedding(nn.Module):
|
|
@@ -163,7 +163,7 @@ model = Translator(d_model=D_MODEL,
|
|
| 163 |
num_encoder_layers=NUM_DECODER_LAYERS,
|
| 164 |
dim_feedforward=DIM_FF,
|
| 165 |
vocab_size=VOCAB_SIZE,
|
| 166 |
-
max_len=MAX_LEN)
|
| 167 |
|
| 168 |
model.load_state_dict(
|
| 169 |
torch.load(MODEL_NAME, map_location=torch.device(DEVICE))
|
|
|
|
| 19 |
MODEL_NAME = 'pico_model_colab.pth'
|
| 20 |
VOCAB_NAME = 'pico_translator_colab.vocab'
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# What device is available
|
| 23 |
|
| 24 |
if torch.cuda.is_available():
|
|
|
|
| 30 |
DEVICE = torch.device(device)
|
| 31 |
DEVICE
|
| 32 |
|
| 33 |
+
# Import token2idx
|
| 34 |
+
|
| 35 |
+
with open(VOCAB_NAME, 'r') as f:
|
| 36 |
+
token2idx = json.load(f)
|
| 37 |
+
idx2token = {idx: char for char, idx in token2idx.items()}
|
| 38 |
+
VOCAB_SIZE = len(token2idx)
|
| 39 |
+
|
| 40 |
# Embedding and Transformer
|
| 41 |
|
| 42 |
class Embedding(nn.Module):
|
|
|
|
| 163 |
num_encoder_layers=NUM_DECODER_LAYERS,
|
| 164 |
dim_feedforward=DIM_FF,
|
| 165 |
vocab_size=VOCAB_SIZE,
|
| 166 |
+
max_len=MAX_LEN).to(DEVICE)
|
| 167 |
|
| 168 |
model.load_state_dict(
|
| 169 |
torch.load(MODEL_NAME, map_location=torch.device(DEVICE))
|