def __init__(self, inp_dim, out_dim, emb_dim, enc_hid, dec_hid, enc_drop,
                 dec_drop, epoch, clip, sparse_max, tf, max_length, vocab,
                 batch, device):
        self.inp_dim = inp_dim
        self.out_dim = out_dim
        self.emb_dim = emb_dim
        self.enc_hid = enc_hid
        self.dec_hid = dec_hid
        self.enc_drop = enc_drop
        self.dec_drop = dec_drop
        self.tf = tf
        self.max_length = max_length
        self.batch = batch
        self.device = device
        self.vocab = vocab

        self.attn = Attention(enc_hid, dec_hid, sparse_max=sparse_max)
        self.enc = Encoder(inp_dim, emb_dim, enc_hid, dec_hid, enc_drop)
        self.dec = Decoder(out_dim, emb_dim, enc_hid, dec_hid, dec_drop,
                           self.attn)
        self.model = Seq2Seq(self.enc, self.dec, device).to(device)

        self.model.apply(self.init_weights)
        self.count_parameters()
        self.optimizer = optim.Adam(self.model.parameters())
        if sparse_max:
            self.criterion = SparsemaxLoss(ignore_index=0)
        else:
            self.criterion = nn.CrossEntropyLoss(ignore_index=0)  # pad_idx 0
        self.epoch = epoch
        self.clip = clip
Exemplo n.º 2
0
def loadModel(modelFile):

    voc = Vocab()
    if modelFile:
        # If loading on same machine the model was trained on
        checkpoint = torch.load(modelFile, map_location='cpu')
        # checkpoint = torch.load(loadFilename)
        encoder_sd = checkpoint['en']
        decoder_sd = checkpoint['de']
        encoder_optimizer_sd = checkpoint['en_opt']
        decoder_optimizer_sd = checkpoint['de_opt']
        embedding_sd = checkpoint['embedding']
        voc.__dict__ = checkpoint['voc_dict']

    model = Seq2Seq(voc, device)

    print('[INFO] Building encoder and decoder ...')

    if modelFile:
        model.embedding.load_state_dict(embedding_sd)

    if modelFile:
        model.encoder.load_state_dict(encoder_sd)
        model.decoder.load_state_dict(decoder_sd)
    # Use appropriate device
    model.encoder = model.encoder.to(device)
    model.decoder = model.decoder.to(device)
    model.encoder.eval()
    model.decoder.eval()

    # Initialize search module
    searcher = GreedySearchDecoder(model.encoder, model.decoder, device)
    return model, searcher
import torch
from DataPrepare import Data
from Model import Seq2Seq
import config


class SpellingCorrection:
    def __init__(self):
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")
        self.corpus_name = config.data['corpus']
        self.data = Data()


if __name__ == "__main__":
    sc = SpellingCorrection()
    pairs = sc.data.formatDataset()
    print('[INFO] Building encoder and decoder ...')
    model = Seq2Seq(sc.data.voc)
    print('[INFO] Models built and ready to go!')
    trainPairs = pairs[:int(0.8 * len(pairs))]
    testPairs = pairs[int(0.8 * len(pairs)):]
    model.training(trainPairs, sc.corpus_name, sc.data.save_dir)
    model.testing(testPairs)
Exemplo n.º 4
0
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

INPUT_DIM = len(SRC.vocab)
OUTPUT_DIM = len(TRG.vocab)
ENC_EMB_DIM = 256
DEC_EMB_DIM = 256
HID_DIM = 512
N_LAYERS = 2
ENC_DROPOUT = 0.5
DEC_DROPOUT = 0.5

enc = Encoder(INPUT_DIM, ENC_EMB_DIM, HID_DIM, N_LAYERS, ENC_DROPOUT)
dec = Decoder(OUTPUT_DIM, DEC_EMB_DIM, HID_DIM, N_LAYERS, DEC_DROPOUT)

model = Seq2Seq(enc, dec, device).to(device)

##########################################################################

model.apply(init_weights)

optimizer = optim.Adam(model.parameters())

TRG_PAD_IDX = TRG.vocab.stoi[TRG.pad_token]

criterion = nn.CrossEntropyLoss(ignore_index=TRG_PAD_IDX)

##########################################################################

N_EPOCHS = 10
CLIP = 1
from torch.utils.data import DataLoader
from my_dataloader import *
from create_vocabulary import *
from Model import Encoder, Decoder, Seq2Seq
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


#encoder = Encoder(input_dim=2999, name='emb_inspec.npy')
#decoder = Decoder(output_dim=2999, name='emb_inspec.npy')
encoder = Encoder()
decoder = Decoder()
model = Seq2Seq(encoder, decoder, device).to(device)
#model.load_state_dict(torch.load('train.pt'))

def init_weights(m):
    for name, param in m.named_parameters():
        nn.init.normal_(param.data, mean=0, std=0.01)

batch=64

tot_epoch = 100

vocab = np.load('vocab_kp20k2.npy', allow_pickle=True).item()
#vocab = np.load('vocab_inspec.npy', allow_pickle=True).item()
TRG_PAD_IDX = vocab('<pad>')
criterion = nn.CrossEntropyLoss(ignore_index = TRG_PAD_IDX)
optimizer = optim.Adam(model.parameters(), lr=0.0005)