如何在 TensorFlow 中为二进制分类设置部分正样本权重 [英] How to set parts of positive samples weight in TensorFlow for binary classfication

查看:75
本文介绍了如何在 TensorFlow 中为二进制分类设置部分正样本权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为正样本的部分设置相同的权重.但是,tf.nn.weighted_cross_entropy_with_logits 在我看来只能为所有正样本设置权重.

I want to set the same weight for parts of positive samples. However,tf.nn.weighted_cross_entropy_with_logits can only set the weight for all positive samples in my opinion.

比如在ctr预测中,我想为订单样本设置10个权重,点击样本和未点击样本的权重仍然是1.

for example, in the ctr predicition, I want set 10 weights for the order samples, and the weight of click samples and the unclick sample is still 1.

这是我未加权的代码

def my_model(features, labels, mode, params):
    net = tf.feature_column.input_layer(features, params['feature_columns'])
    for units in params['hidden_units']:
       net = tf.layers.dense(net, units=units, activation=params["activation"])  
    logits = tf.layers.dense(net, params['n_classes'], activation=None)

    predicted_classes = tf.argmax(logits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
       predictions = {
        'class_ids': predicted_classes, #predicted_classes[:, tf.newaxis],
        'probabilities': tf.nn.softmax(logits),
        'logits': logits,
       }
       return tf.estimator.EstimatorSpec(mode, predictions=predictions)

    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    metrics = {'auc': tf.metrics.auc(labels=labels, predictions=tf.nn.softmax(logits)[:,1])}

    if mode == tf.estimator.ModeKeys.EVAL:
       return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

    assert mode == tf.estimator.ModeKeys.TRAIN
    optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)
    train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) 
    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

火车

train_input_fn = tf.estimator.inputs.pandas_input_fn(x=data_train, y=data_train_click, batch_size = 1024, num_epochs=1, shuffle=False)
classifier.train(input_fn=train_input_fn)

这里data_train_click是一个系列,点击样本为1,未点击样本为0.我有一个名为data_train_order的系列,订单样本为1其他都是0

Here data_train_click is a Series, which the click samples are 1 and the unclicked samples are 0. And I have a Series named data_train_order, which the order samples are 1 and the others are 0

推荐答案

最简单的方法是使用 keras

The easiest way to do this is by using keras

https://keras.io/models/model/

fit 函数有一个 sample_weight 参数.

The fit function has a sample_weight parameter.

这篇关于如何在 TensorFlow 中为二进制分类设置部分正样本权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆