如何在 Tensorflow 中使用逐个事件权重



在我的数据集中,我为每个条目(事件)都有一个权重。此权重由多个数量组成,但基本上表示此事件对数据的重要性,必须加以考虑。

在 Tensorflow 中训练时如何使用这个权重?我不想简单地将其用作另一个功能。

谢谢

一个简单的解决方案是在计算小批量的总成本之前,将每个示例的计算成本乘以其权重。

假设您有以下内容:

# Vector of features per example.
x = tf.placeholder(tf.float32, shape=[batch_size, num_features])
# Scalar weight per example.
x_weights = tf.placeholder(tf.float32, shape=[batch_size])
# Vector of outputs per example.
y = tf.placeholder(tf.float32, shape=[batch_size, num_outputs])
# ...
logits = ...
# Insert appropriate cost function here.
cost = tf.nn.softmax_cross_entropy_with_logits(logits, y)

计算出的cost张量是长度为 batch_size 的向量。您可以简单地使用 x_weights 执行逐元素乘法以获得加权成本。

overall_cost = tf.mul(cost, x_weights) / batch_size

最后,您可以使用 overall_cost 作为优化器中最小化的值。

最新更新