Skip to main content

A German-to-English machine translation package using PyTorch

Project description

Machine Translation Package

A German-to-English machine translation system built from scratch using PyTorch.

Installation

  1. Install the package via pip:
    pip install machine-translation
    
  2. Install spaCy models:
    python -m spacy download de_core_news_sm
    python -m spacy download en_core_web_sm
    

Usage

Training a Model

from machine_translation import get_data_loaders, Encoder, Decoder, Seq2Seq, train
import torch
import torch.nn as nn
import torch.optim as optim

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_loader, val_loader, _, src_vocab, trg_vocab = get_data_loaders()
enc = Encoder(len(src_vocab), 256, 512, 2, 0.5, device)
dec = Decoder(len(trg_vocab), 256, 512, 2, 0.5)
model = Seq2Seq(enc, dec, device).to(device)
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss(ignore_index=trg_vocab["<PAD>"])
train(model, train_loader, optimizer, criterion, clip=1, device=device)

Translating a Sentence

from machine_translation import translate_sentence, Encoder, Decoder, Seq2Seq
import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
enc = Encoder(len(src_vocab), 256, 512, 2, 0.5, device)
dec = Decoder(len(trg_vocab), 256, 512, 2, 0.5)
model = Seq2Seq(enc, dec, device).to(device)
model.load_state_dict(torch.load("Seq model.pt", weights_only=True))
sentence = "Ein kleines Mädchen spielt im Park."
translation = translate_sentence(sentence, src_vocab, trg_vocab, model, device)
print(" ".join(translation))

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

machine_translation-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

machine_translation-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file machine_translation-0.1.0.tar.gz.

File metadata

  • Download URL: machine_translation-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for machine_translation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4bf0ea0bdc9191efd4c0f07fd2d00723dcad0c40692f29f79ebf0a1f51d83b05
MD5 85fb5e5295f3baf5b60f0d008b94ca43
BLAKE2b-256 55bf91384c66a5dff4a9f75f8a602957ba3f6266cc4abc7e9981ab9f14c46141

See more details on using hashes here.

File details

Details for the file machine_translation-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for machine_translation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d53012e240aebb0db3d2bb6a735fac82a062e74c4ecd8c6c833e5f0bcc98251
MD5 f6bfd7414758c8dd0c010bf86e0aee2c
BLAKE2b-256 9287bed0b37be1d828b6c1e5e5413f6bf8506ed8b18ec05d5890414f3039022d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page