示例#1
0
model = CharacterLevelCNN(ops, embedding)

## 3. 声明loss,metric,optimizer
loss = CrossEntropyLoss
metric = AccuracyMetric
#optimizer= SGD([param for param in model.parameters() if param.requires_grad==True], lr=ops.lr)
optimizer = SGD(
    [param for param in model.parameters() if param.requires_grad == True],
    lr=ops.lr,
    momentum=0.9,
    weight_decay=ops.weight_decay)
callbacks = []
# callbacks.append(LRScheduler(CosineAnnealingLR(optimizer, 5)))
callbacks.append(
    LRScheduler(
        LambdaLR(
            optimizer, lambda epoch: ops.lr
            if epoch < ops.train_epoch * 0.8 else ops.lr * 0.1)))


## 4.定义train方法
def train(model, datainfo, loss, metrics, optimizer, num_epochs=100):
    trainer = Trainer(datainfo.datasets['train'],
                      model,
                      optimizer=optimizer,
                      loss=loss(target='target'),
                      batch_size=ops.batch_size,
                      metrics=[metrics(target='target')],
                      dev_data=datainfo.datasets['test'],
                      device=[0, 1, 2],
                      check_code_level=-1,
                      n_epochs=num_epochs)
示例#2
0
print(datainfo)
print(datainfo.datasets['train'][0])

model = DPCNN(init_embed=embedding, num_cls=len(datainfo.vocabs[C.TARGET]),
              embed_dropout=ops.embed_dropout, cls_dropout=ops.cls_dropout)
print(model)

# 3. 声明loss,metric,optimizer
loss = CrossEntropyLoss(pred=C.OUTPUT, target=C.TARGET)
metric = AccuracyMetric(pred=C.OUTPUT, target=C.TARGET)
optimizer = SGD([param for param in model.parameters() if param.requires_grad == True],
                lr=ops.lr, momentum=0.9, weight_decay=ops.weight_decay)

callbacks = []

callbacks.append(LRScheduler(CosineAnnealingLR(optimizer, 5)))
# callbacks.append(
#     LRScheduler(LambdaLR(optimizer, lambda epoch: ops.lr if epoch <
#                          ops.train_epoch * 0.8 else ops.lr * 0.1))
# )

# callbacks.append(
#     FitlogCallback(data=datainfo.datasets, verbose=1)
# )

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

print(device)

# 4.定义train方法
trainer = Trainer(datainfo.datasets['train'], model, optimizer=optimizer, loss=loss,