Exemplo n.º 1
0
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())  # , lr=args.lr)

# 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))
print(header)

for epoch in range(p.num_iters):
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))
print(header)

best_model_weights = None
best_dev_loss = 100000
Exemplo n.º 3
0
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")
for epoch in tqdm_epoch:
    train_iter.init_epoch()

    tqdm_batch = tqdm(train_iter, desc="Batch")
    loss = None
    for b_id, batch in enumerate(tqdm_batch):
        model.train()
        optimizer.zero_grad()