Machine Translation Model Fails: Why Isn’t My Transformer Model Learning?

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 The

Coaching tips for stow missort scan<—> The The The The The The The
The The The The The The The

Picker Reported Item as Unscannable<—> The The The The The The The
The The The The The The The

Pack 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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")
</code>
<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") </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")

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật