I have a program that builds a fairly standard Transformer-based machine translation model for translating English to Portuguese. I tested it locally on a small dataset of about 15,000 segments. By around the 5th epoch, the model starts to produce some form of translation. Although it’s far from correct, it’s evident that the model is learning and continues to improve in subsequent epochs. Here is an example of the output:
to overestimate somebody’s skills<—> as cabelos de férias
to change one’s mind<—> um cão de cabelos
to give somebody back their freedom<—> um terreno de um quadro
It breaks my heart.<—> Eles estão estão as mãos .
opponents to the regime<—> um vento de cabelos
a library’s collection<—> as cabelos de cores
to grow something<—> as cabelos de cabelos
to hold the door open for somebody<—> um cão de papel
I have to go.<—> Eles estão as mãos .
I then took the same program, without making any changes, and used the Portuguese tokenizer (Spacy) to run it on a dataset of about 20,000 segments of English paired with Hebrew sentences (Hebrew is not supported by Spacy). This time, even after reaching the 20th epoch, the model showed no signs of learning. Here is an example of the output:
מהם הקודים הנכונים עבור הוראת העבודה?<—> The the the the the the the
the the the .התקנת מכשיר מיזוג שמן, ציין שירות שאיבה, נשם סופג ומחבר מהיר על כניסת
מילוי במעטפת סטאטית, סגירת המערכת לציין מד השמן החיצוני.<—> the the
the the the the the the the the the the the the the the the the the
the the the the the the the the the the the the the the the the the
the the the the the the theחלק מהבדיקות רגישות למיקום הדגימה, בעוד שאחרות מצריכות לקיחת דגיה
במיקום המדויק או שהדגימה תבודד מהמזהמים הסביבתיים כדי לוודא שהמידע
מייצג את מצב היעד.<—> the the the the the the the the the the the
the the the the the the the the the the the the the the the the the
the the the the the the the the the the the the the the the the the
the the the theהזמינות של סבונים וקרמים שמשמשים כדי להסיר חומרי סיכה מהעור.<—> The
the the the the the the the the the the the the the the the the .TK<—>
תיאור המכלולים העיקריים<—>
I modified the program so that both English and Hebrew tokenization were handled by XLM-R and also used the embeddings from XLM-R. Despite using a dataset that was 10 times larger, I got the same results. Here are some output examples (this time I have the reference English translation next to the MT translation:
Please close the existing coaching instance before creating.<—> The
The The The The The The The The The The The The The TheCoaching tips for stow missort scan<—> The The The The The The The
The The The The The The ThePicker Reported Item as Unscannable<—> The The The The The The The
The The The The The The ThePack Item Missing<—> The The The The The The The The The
Total Touches:<—> The The The The The The The The The The
Maximum Threshold<—> The The The The The The The The
Can you provide any ideas on why the model is not learning?
Here is a little bit of code:
if __name__ == "__main__":
torch.manual_seed(0)
src_train_base = "data/src_train_ds_"
tgt_train_base = "data/tgt_train_ds_"
src_test_base = "data/src_test_ds_"
tgt_test_base = "data/tgt_test_ds_"
FILES_COUNT_TRAIN = 308
FILES_COUNT_TEST = 16
SRC_VOCAB_SIZE = len(vocab_transform[SRC_LANGUAGE])
TGT_VOCAB_SIZE = len(vocab_transform[TGT_LANGUAGE])
#EMB_SIZE = 512
EMB_SIZE = 768 # modified to match xlm-r's embedding size
NHEAD = 8
FFN_HID_DIM = 512
BATCH_SIZE = 16
# BATCH_SIZE = 128
NUM_ENCODER_LAYERS = 6
NUM_DECODER_LAYERS = 6
transformer = Seq2SeqTransformer(NUM_ENCODER_LAYERS, NUM_DECODER_LAYERS, EMB_SIZE,
NHEAD, SRC_VOCAB_SIZE, TGT_VOCAB_SIZE, FFN_HID_DIM)
for p in transformer.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
transformer = transformer.to(DEVICE)
loss_fn = torch.nn.CrossEntropyLoss(ignore_index=PAD_IDX)
optimizer = torch.optim.Adam(transformer.parameters(), lr=0.0001, betas=(0.9, 0.98), eps=1e-9)
LOAD_MODEL = True
if LOAD_MODEL:
transformer = torch.load("model/_transformer_model")
my_test(src_test_base, tgt_test_base, FILES_COUNT_TEST, -1, 0,
0, 0, 0)
EPOCHS_NUM = 20
for epoch in range(EPOCHS_NUM):
epoch_start_time = int(timer())
print("epoch number: " + str(epoch + 1) + " Time: " + str(epoch_start_time))
for i in range(FILES_COUNT_TRAIN):
start_time = int(timer())
print("START: file number: " + str(i) + " Time: " + str(start_time))
# Load Hebrew train data
src_train_sentences = load_data(src_train_base + str(i) + ".txt")
# Load English train data
tgt_train_sentences = load_data(tgt_train_base + str(i) + ".txt")
train_and_save(src_train_sentences, tgt_train_sentences)
end_time = int(timer())
print("END: file number: " + str(i) + " Time: " + str(end_time) + " Total: " + str(
(end_time - start_time) / 60))
if i%10 == 0:
try:
my_test(src_test_base, src_test_base, FILES_COUNT_TEST, epoch, 0,
0, epoch_end_time, epoch_start_time)
except:
print("error occurred")
epoch_end_time = int(timer())
try:
my_test(src_test_base, src_test_base, FILES_COUNT_TEST, epoch, 0, 0, epoch_end_time, epoch_start_time)
except:
print("error occurred")