Esempio n. 1
0
def eval_metrics_fn():
    return {
        "accuracy": lambda labels, predictions: accuracy(
            tf.cast(tf.squeeze(tf.round(predictions)), tf.int32),
            tf.cast(labels, tf.int32),
        )
    }
Esempio n. 2
0
def model_fn(features, labels, mode, params):
    scores = predict_scores(features)

    if mode == ModeKeys.INFER:
        return EstimatorSpec(mode, predictions=scores)

    positive_scores = lookup_positives(scores, labels['click_position'])
    logits = create_diffs(positive_scores, scores)
    lbls = create_label(labels['click_position'])
    ele_loss = elementwise_loss(lbls, logits, labels['normal_mask']) * lbls
    loss = reduce_sum(ele_loss)
    true_lbl = true_label(features, labels)

    if mode == ModeKeys.EVAL:
        return EstimatorSpec(mode,
                             loss=loss,
                             eval_metric_ops={
                                 'acc':
                                 mean(
                                     accuracy(
                                         argmax(noise_label(labels), axis=1),
                                         argmax(to_one_hot(scores), axis=1)))
                             })
    else:
        optimizer = AdamOptimizer(learning_rate=params['learning_rate'])
        train_op = optimizer.minimize(loss, global_step=get_global_step())

        return EstimatorSpec(mode, loss=loss, train_op=train_op)