Beispiel #1
0
    'params': [
        p for n, p in parameters_to_optimize
        if 'bert' not in n and 'crf' not in n
    ],
    'lr':
    args.cnnlr
}, {
    'params':
    [p for n, p in parameters_to_optimize if 'bert' not in n and 'crf' in n],
    'lr':
    args.crflr
}]
optimizer = AdamW(parameters_to_optimize, lr=args.bertlr, correct_bias=False)
total_steps = int(11958 // args.batch_size) * args.epoch
scheduler = get_linear_schedule_with_warmup(optimizer,
                                            num_warmup_steps=int(
                                                args.warm_rate * total_steps),
                                            num_training_steps=total_steps)
max_f1 = 0
for epoch in range(5):
    train_epoch(train_data, epoch + 1, model, scheduler, optimizer, batch_size,
                device)
    f1 = dev_epoch(valid_data, model, epoch + 1, batch_size, device)
    if f1 > max_f1:
        torch.save(model.state_dict(), "./models/{}.pkl".format(log_name))

e_model = Model(1, batch_size, device)
e_model.load_state_dict(torch.load("./models/" + log_name + ".pkl"))
print("model loaded ...")
test_epoch(test_data, e_model, batch_size, device)
Beispiel #2
0
train_data = FontDataset(train_dir)

test_dir = '~/datasets/font/npy_test'.replace('~', os.path.expanduser('~'))
test_data = FontDataset(test_dir)

### Define Dataloader
train_loader = torch.utils.data.DataLoader(dataset=train_data,
                                           batch_size=batch_size)

test_loader = torch.utils.data.DataLoader(dataset=test_data,
                                           batch_size=batch_size)

### Define Model and Load Params
model = ConvNet().to(device)
print("========================== Original Model =============================", "\n", model)
model.load_state_dict(torch.load('./pths/cifar10_pre_model.pth', map_location=device))

### User pre-trained model and Only change last layer
for param in model.parameters():
    param.requires_grad = False

model.fc2 = nn.Linear(120, 50)
modle = model.to(device)

print("========================== Modified Model =============================", "\n", model)

### Define Loss and Optim
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)

### Train
Beispiel #3
0
# Set Configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Prepare Test Data
test_data = torchvision.datasets.MNIST(root='./datasets',
                                       train=False,
                                       transform=transforms.ToTensor())

# Define Test Dataloader
test_loader = torch.utils.data.DataLoader(dataset=test_data,
                                          batch_size=100,
                                          shuffle=True)

# Load Model and Trained Parameters
model_test = ConvNet(10).to(device)
model_test.load_state_dict(torch.load('./model.pth', map_location=device))
model_test.eval()

# Evaluate
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model_test(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the {} test images: {} %'.format(len(test_loader)*100, 100 * correct / total))