如何在 Tensorflow 中使用逐个事件的权重?
How to use event-by-event weights in 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
作为要在 optimizer 中最小化的值。
在我的数据集中,每个条目(事件)都有一个权重。这个权重由几个数量组成,但基本上代表了这个事件对数据的重要性,必须考虑在内。
在 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
作为要在 optimizer 中最小化的值。