コード例 #1
0
def model_fn(features, mode, params, run_config):
    ernie = ErnieModelForSequenceClassification(params, name='')
    if mode is not propeller.RunMode.TRAIN:
        ernie.eval()
    else:
        ernie.train()

    metrics, loss = None, None
    if mode is propeller.RunMode.PREDICT:
        src_ids, sent_ids = features
        _, logits = ernie(src_ids, sent_ids)
        predictions = [
            logits,
        ]
    else:
        src_ids, sent_ids, labels = features
        if mode is propeller.RunMode.EVAL:
            loss, logits = ernie(src_ids, sent_ids, labels=labels)
            pred = logits.argmax(axis=1)
            acc = propeller.metrics.Acc(labels, pred)
            metrics = {'acc': acc}
            predictions = [pred]
            train_hooks = None
        else:
            loss, logits = ernie(src_ids, sent_ids, labels=labels)
            lr_step_hook, loss_scale_coef = optimization(
                loss=loss,
                warmup_steps=int(run_config.max_steps *
                                 params['warmup_proportion']),
                num_train_steps=run_config.max_steps,
                learning_rate=params['learning_rate'],
                train_program=P.static.default_main_program(),
                startup_prog=P.static.default_startup_program(),
                use_fp16=args.use_amp,
                weight_decay=params['weight_decay'],
                scheduler="linear_warmup_decay",
            )
            scheduled_lr = P.static.default_main_program().global_block().var(
                'learning_rate_0')
            propeller.summary.scalar('lr', scheduled_lr)
            predictions = [
                logits,
            ]
            train_hooks = [lr_step_hook]

    return propeller.ModelSpec(loss=loss,
                               mode=mode,
                               metrics=metrics,
                               predictions=predictions,
                               train_hooks=train_hooks)
コード例 #2
0
def model_fn(features, mode, params, run_config):
    ernie = ErnieModelForSequenceClassification(params, name='')
    if not params is propeller.RunMode.TRAIN:
        ernie.eval()

    metrics, loss = None, None
    if mode is propeller.RunMode.PREDICT:
        src_ids, sent_ids = features
        _, logits = ernie(src_ids, sent_ids)
        predictions = [
            logits,
        ]
    else:
        src_ids, sent_ids, labels = features
        if mode is propeller.RunMode.EVAL:
            loss, logits = ernie(src_ids, sent_ids, labels=labels)
            pred = L.argmax(logits, axis=1)
            acc = propeller.metrics.Acc(labels, pred)
            metrics = {'acc': acc}
            predictions = [pred]
        else:
            loss, logits = ernie(src_ids, sent_ids, labels=labels)
            scheduled_lr, _ = optimization(
                loss=loss,
                warmup_steps=int(run_config.max_steps *
                                 params['warmup_proportion']),
                num_train_steps=run_config.max_steps,
                learning_rate=params['learning_rate'],
                train_program=F.default_main_program(),
                startup_prog=F.default_startup_program(),
                use_fp16=params.use_fp16,
                weight_decay=params['weight_decay'],
                scheduler="linear_warmup_decay",
            )
            propeller.summary.scalar('lr', scheduled_lr)
            predictions = [
                logits,
            ]

    return propeller.ModelSpec(loss=loss,
                               mode=mode,
                               metrics=metrics,
                               predictions=predictions)
コード例 #3
0
ファイル: pretrain_static.py プロジェクト: zs960114/ERNIE
def ernie_pretrain_model_fn(features, mode, params, run_config):
    """propeller Model wraper for paddle-ERNIE """
    src_ids, sent_ids, mlm_label, mask_pos, nsp_label = features

    ernie = ErnieModelForPretraining(params, name='')
    total_loss, mlm_loss, nsp_loss = ernie(src_ids,
                                           sent_ids,
                                           labels=mlm_label,
                                           mlm_pos=mask_pos,
                                           nsp_labels=nsp_label)

    metrics = None
    inf_spec = None

    propeller.summary.scalar('loss', total_loss)
    propeller.summary.scalar('nsp-loss', nsp_loss)
    propeller.summary.scalar('mlm-loss', mlm_loss)

    lr_step_hook, loss_scale_coef = optimization(
        loss=total_loss,
        warmup_steps=params['warmup_steps'],
        num_train_steps=run_config.max_steps,
        learning_rate=params['learning_rate'],
        train_program=P.static.default_main_program(),
        startup_prog=P.static.default_startup_program(),
        weight_decay=params['weight_decay'],
        scheduler="linear_warmup_decay",
        use_fp16=args.use_amp,
    )
    scheduled_lr = P.static.default_main_program().global_block().var(
        'learning_rate_0')
    propeller.summary.scalar('lr', scheduled_lr)
    if args.use_amp:
        propeller.summary.scalar('loss_scaling', loss_scale_coef)
    pred = [total_loss]

    return propeller.ModelSpec(loss=total_loss,
                               mode=mode,
                               metrics=metrics,
                               predictions=pred,
                               train_hooks=[lr_step_hook])