config.d_out = len(answers.vocab)
config.n_cells = config.n_layers

# double the number of cells for bidirectional networks
if config.birnn:
    config.n_cells *= 2

if args.resume_snapshot:
    model = torch.load(
        args.resume_snapshot,
        map_location=lambda storage, location: storage.cuda(args.gpu))
else:
    model = LSTMSentiment(config)
    if args.word_vectors:
        model.embed.weight.data = inputs.vocab.vectors
        model.cuda()

criterion = nn.CrossEntropyLoss()

opt = O.Adam(model.parameters())

# model.embed.requires_grad = False

iterations = 0
start_time = time.time()
best_dev_acc = -1
train_iter.repeat = False
header = '  Time Epoch     Loss   Dev/Loss  CD Loss    Accuracy  Dev/Accuracy'
dev_log_template = ' '.join(
    '{:6.0f},{:5.0f},{:9.4f},{:8.6f},{:8.6f},{:12.4f},{:12.4f}'.split(','))
print(len(train))
Ejemplo n.º 2
0
inputs.build_vocab(train, dev, test)
inputs.vocab.load_vectors('glove.6B.300d')
answers.build_vocab(train)
train_iter, dev_iter, test_iter = data.BucketIterator.splits(
    (train, dev, test), batch_size=50, repeat=False, device=DEVICE)

############################################
# CREATING MODEL
print("Creating model")
model = LSTMSentiment(embedding_dim=300,
                      hidden_dim=168,
                      vocab_size=300,
                      label_size=2,
                      gpu_device=DEVICE)
model.word_embeddings.weight.data = inputs.vocab.vectors
model.cuda(device=DEVICE)

# Load previously checkpointed model if it exists
model_path = "model.pt"
if os.path.exists(model_path) and RESUME_CKPT:
    print("Loading previously stored model")
    model = torch.load(model_path)

loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

#############################################
# TRAINING
print("Beginning training")
early_stop_test = 0
tqdm_epoch = tqdm(range(5), desc="Epoch")