examples/text_matching/sentence_transformers/train.py
def evaluate(model, criterion, metric, data_loader):
    """
    Given a dataset, it evals model and computes the metric.

    Args:
        model(obj:`paddle.nn.Layer`): A model to classify texts.
        data_loader(obj:`paddle.io.DataLoader`): The dataset loader which generates batches.
        criterion(obj:`paddle.nn.Layer`): It can compute the loss.
        metric(obj:`paddle.metric.Metric`): The evaluation metric.
    """
    model.eval()
    metric.reset()
    losses = []
    for batch in data_loader:
        query_input_ids, query_token_type_ids, title_input_ids, title_token_type_ids, labels = batch
        logits = model(
            query_input_ids=query_input_ids,
            title_input_ids=title_input_ids,
            query_token_type_ids=query_token_type_ids,
            title_token_type_ids=title_token_type_ids)
        loss = criterion(logits, labels)
        losses.append(loss.numpy())
        correct = metric.compute(logits, labels)
        metric.update(correct)
        accu = metric.accumulate()
    print("eval loss: %.5f, accu: %.5f" % (np.mean(losses), accu))
    model.train()
    metric.reset()

    Given a dataset, it evals model and computes the metric.

    Args:
        model(obj:`paddle.nn.Layer`): A model to classify texts.
        data_loader(obj:`paddle.io.DataLoader`): The dataset loader which generates batches.
        criterion(obj:`paddle.nn.Layer`): It can compute the loss.
        metric(obj:`paddle.metric.Metric`): The evaluation metric.
    
Given a dataset, it evals model and computes the metric.
Given a dataset it evals model and computes the metric .
def evaluate(model, criterion, metric, data_loader):
    model.eval()
    metric.reset()
    losses = []
    for batch in data_loader:
        query_input_ids, query_token_type_ids, title_input_ids, title_token_type_ids, labels = batch
        logits = model(
            query_input_ids=query_input_ids,
            title_input_ids=title_input_ids,
            query_token_type_ids=query_token_type_ids,
            title_token_type_ids=title_token_type_ids)
        loss = criterion(logits, labels)
        losses.append(loss.numpy())
        correct = metric.compute(logits, labels)
        metric.update(correct)
        accu = metric.accumulate()
    print("eval loss: %.5f, accu: %.5f" % (np.mean(losses), accu))
    model.train()
    metric.reset()
def evaluate ( model , criterion , metric , data_loader ) : model . eval ( ) metric . reset ( ) losses = [ ] for batch in data_loader : query_input_ids , query_token_type_ids , title_input_ids , title_token_type_ids , labels = batch logits = model ( query_input_ids = query_input_ids , title_input_ids = title_input_ids , query_token_type_ids = query_token_type_ids , title_token_type_ids = title_token_type_ids ) loss = criterion ( logits , labels ) losses . append ( loss . numpy ( ) ) correct = metric . compute ( logits , labels ) metric . update ( correct ) accu = metric . accumulate ( ) print ( "eval loss: %.5f, accu: %.5f" % ( np . mean ( losses ) , accu ) ) model . train ( ) metric . reset ( )
Given a dataset, it evals model and computes the metric.
Given a dataset it evals model and computes the metric .
"""
    Given a dataset, it evals model and computes the metric.

    Args:
        model(obj:`paddle.nn.Layer`): A model to classify texts.
        data_loader(obj:`paddle.io.DataLoader`): The dataset loader which generates batches.
        criterion(obj:`paddle.nn.Layer`): It can compute the loss.
        metric(obj:`paddle.metric.Metric`): The evaluation metric.
    """
model
criterion
metric
data_loader
model(obj
`paddle.nn.Layer`): A model to classify texts.
` paddle . nn . Layer ` ) : A model to classify texts .
data_loader(obj
`paddle.io.DataLoader`): The dataset loader which generates batches.
` paddle . io . DataLoader ` ) : The dataset loader which generates batches .
criterion(obj
`paddle.nn.Layer`): It can compute the loss.
` paddle . nn . Layer ` ) : It can compute the loss .
metric(obj
`paddle.metric.Metric`): The evaluation metric.
` paddle . metric . Metric ` ) : The evaluation metric .