Пример #1
0
其次,Adam 中 没有使用 bias_correct
"""
optimizer = extend_with_piecewise_linear_lr(Adam)
optimizer = extend_with_wight_decay(optimizer)

params = {
    'lr_schedule': lr_schedule,
    'learning_rate': learning_rate,
    'weight_decay_rate': weight_decay_rate,
    'exclude_from_weight_decay': exclude_from_weight_decay,
    'bias_correct': False
}

# grad acc
if grad_accum_steps > 1:
    optimizer = extend_with_gradient_accumulation(optimizer)
    params['grad_accum_steps'] = grad_accum_steps

optimizer = optimizer(**params)

# compile
train_model.compile(optimizer=optimizer, loss=loss)

# load init ckpt
if ckpt is not None:
    bert.load_weights_from_checkpoint(ckpt)


# callback
class ModelCheckpoint(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
Пример #2
0
        trans = K.eval(CRF.trans)
        wordseg.trans = trans
        print(trans)
        acc = evaluate(val_data)

        if acc > self.best_acc:
            self.best_acc = acc
            model.save_weights('./best_model.weights')
        print('acc is: {:.3f}, best acc is :{:.4f}'.format(acc, self.best_acc))

    def on_train_end(self, logs=None):
        model.load_weights('./best_model.weights')
        public_evaluate(test_path, test_result_path, test_score_path)


opt = extend_with_gradient_accumulation(Adam)
opt = opt(learning_rate=lr)
model.compile(loss=CRF.sparse_loss,
              optimizer=opt,
              metrics=[CRF.sparse_accuracy])

if __name__ == '__main__':
    evaluator = Evaluator()
    train_genarator = data_generator(train_data, batch_size)
    model.fit_generator(train_genarator.generator(),
                        steps_per_epoch=len(train_genarator),
                        epochs=epochs,
                        callbacks=[evaluator])
else:
    model.load_weights('./best_model.weights')