Esempio n. 1
0
def get_model(config, args, train_dataset, device):

    # Pass vocabulary to construct Embedding layer.
    encoder = Encoder(config["model"], train_dataset.vocabulary)
    decoder = Decoder(config["model"], train_dataset.vocabulary)
    print("Encoder: {}".format(config["model"]["encoder"]))
    print("Decoder: {}".format(config["model"]["decoder"]))

    # New: Initializing word_embed using GloVe
    if "glove_npy" in config["dataset"]:
        encoder.word_embed.weight.data = torch.from_numpy(
            np.load(config["dataset"]["glove_npy"]))
        print("Loaded glove vectors from {}".format(
            config["dataset"]["glove_npy"]))

    # Share word embedding between encoder and decoder.
    if encoder.word_embed and decoder.word_embed:
        decoder.word_embed = encoder.word_embed

    # Wrap encoder and decoder in a model.
    model = EncoderDecoderModel(encoder, decoder).to(device)
    if -1 not in args.gpu_ids:
        model = nn.DataParallel(model, args.gpu_ids)
    return model
Esempio n. 2
0
    val_dataset,
    batch_size=config["solver"]["batch_size"],
    num_workers=args.cpu_workers,
    shuffle=True,
)

# Pass vocabulary to construct Embedding layer.
encoder = Encoder(config["model"], val_dataset.vocabulary)
decoder = Decoder(config["model"], val_dataset.vocabulary)
print("Encoder: {}".format(config["model"]["encoder"]))
print("Decoder: {}".format(config["model"]["decoder"]))

# Share word embedding between encoder and decoder.
if args.load_pthpath == "":
    print('load glove')
    decoder.word_embed = encoder.word_embed
    glove = np.load('data/glove.npy')
    encoder.word_embed.weight.data = torch.tensor(glove)

# Wrap encoder and decoder in a model.
model = EncoderDecoderModel(encoder, decoder).to(device)
if -1 not in args.gpu_ids:
    model = nn.DataParallel(model, args.gpu_ids)

# =============================================================================
#   SETUP BEFORE TRAINING LOOP
# =============================================================================
start_time = datetime.datetime.strftime(datetime.datetime.utcnow(), '%d-%b-%Y-%H:%M:%S')

sparse_metrics = SparseGTMetrics()
ndcg = NDCG()