python - 计算损失时检查标签( tensorflow )

标签 python tensorflow neural-network deep-learning

考虑这个问题,我有一个像这样的输出向量:

[0.1, 0.3, 0.6, 0.4, 0.1, 0.5, 0.6 , . 。 .]

和目标标签如下:

[ 0 , 0 , 1 , 0 , 0 , 1 , 1 , . 。 .]

输出和目标标签是三乘三表示特定标签(即 logits [0.1, 0.3, 0.6] 和相关目标标签 [0, 0, 1 ]),

在原来的问题中,实际上不是 3,而是 84,并且标签和输出向量的长度非常大(大约 500 万),并且在大多数情况下(大约 90%)相关标签没有 1,因此没有需要计算该输出的损失,

现在我的问题是,如何忽略相关标签中没有 1 的输出?

或者换句话说当我想要计算损失时,如何在训练期间检查标签?

这是我的损失函数:

score_split = tf.split(1, 64800,  scores)
score_split_output = [tf.nn.softmax(c) for c in score_split]
output = tf.concat(1, score_split_output)
total_loss = tf.reduce_mean(-tf.reduce_sum(labels * tf.log(output), [1]))

我将分数 84 除以 84 (5,443,200/64,800 = 84) 并将它们提供给 softmax,然后连接并计算损失。

最佳答案

输入数据的选择取决于您的问题,而不是技术解决方案的细节。

  1. 没有标签的数据是您在测试时不会遇到的数据。然后您应该简单地从数据集中删除数据。然而,您没有这样做的事实可能表明您不能这样做,因为这些数据属于那里。
  2. 或者,如果您在测试时会遇到并需要处理这些数据,则需要保留它并学习它。例如,您可以添加第 85 个标签,即 1,当且仅当其他 84 个标签为零,否则为零。

编辑

增强标签的示例:

import tensorflow as tf
labels = tf.zeros((100, 84))
label85 = 1-tf.reduce_max(labels)
new_labels = tf.concat([labels, tf.expand_dims(label85,-1)], 0)

关于python - 计算损失时检查标签( tensorflow ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460551/

相关文章:

python - 检查函数中是否使用了某些运算符

python - 将通用日期格式转换为 ISO 周日期格式

python - 在tensorflow python中替换值或创建张量掩码

machine-learning - 反向传播神经网络 - 错误不收敛

python - pytorch 模型中的参数如何不是叶子并且在计算图中?

python - redis, python 和一个存储信息

python - 使用 keras 模型中的 tensorflow 图进行预测

docker - Docker 容器中的 nvidia-docker GPU

python - 如何固定图像生成器的流返回值的维度?

neural-network - 在 keras 或 Tensorflow 中的 LSTM 层之前添加密集层?