python - tensorflow compute_weighted_loss示例

标签 python tensorflow

我想使用 Tensorflows tf.losses.compute_weighted_loss 但找不到任何好的例子。我有一个多类分类问题,并使用 tf.nn.sigmoid_cross_entropy_with_logits 作为损失。现在我想独立权衡每个标签的错误。假设我有 n 个标签,这意味着我需要一个 n 大小的权重向量。不幸的是 tf 希望我传递一个 (b, n) 形的误差权重矩阵,其中 b 是批量大小。所以基本上我需要重复权重向量 b 次。给定固定的批量大小是可以的,但是如果我的批量大小是可变的(例如数据集末尾的较小批量),我必须适应。有办法解决这个问题还是我错过了什么?

最佳答案

我只需将向量从 (n,) reshape 为 (1,n) 即可使广播成为可能:

error_weights = error_weights.reshape(1, error_weights.shape[0])

关于python - tensorflow compute_weighted_loss示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378448/

相关文章:

python - 具有 dB 刻度和负角度值的半极坐标图(方向性图)

python - 无法使用keras.load_model保存/加载模型-IndexError : list index out of range

python - 如何在 Apple M1 芯片的 Mac 上使用 Tensorflow 检查 GPU 可访问性?

python - 随机数生成器在 tensorflow 1.0.1 和 0.12.1 之间有所不同

python - 范围为 “class” 的 Pytest fixture 不适用于 "setup_class"方法

python - 对 numpy.unique() 感到困惑

python - sklearn : calculating accuracy score of k-means on the test data set

python - opc ua客户端到服务器

python - 如何在tensorflow中实现DBSCAN聚类?

tensorflow - TensorFlow 中的步骤和纪元之间有什么关系?