首页 > 解决方案 > 如何在 tensorflow 2 中使用类权重进行多标签二元分类?

问题描述

问题是我只在 TensorFlow 2 文档中找到了一个标签二进制分类的示例。

# Scaling by total/2 helps keep the loss to a similar magnitude.
# The sum of the weights of all examples stays the same.
weight_for_0 = (1 / neg)*(total)/2.0 
weight_for_1 = (1 / pos)*(total)/2.0

class_weight = {0: weight_for_0, 1: weight_for_1}

print('Weight for class 0: {:.2f}'.format(weight_for_0))
print('Weight for class 1: {:.2f}'.format(weight_for_1))

来自 doc 的示例参考

我已经尝试过的解决方案是使用 class_weight 列表、class_weight dict 和 class_weight 数组。我想知道是否有办法将多标签二进制分类类权重注入TensorFlow 2的fit()

我尝试过的例子

import numpy as np
# list of  duct
[{0: 0.474964234620887, 1: 0.525035765379113},{0: 0.48783977110157367, 1: 0.5121602288984263},{0: 0.5135908440629471, 1: 0.4864091559370529},{0: 0.46494992846924177, 1: 0.5350500715307582}]
# dict of dict
{0: {0: 0.474964234620887, 1: 0.525035765379113},
 1: {0: 0.48783977110157367, 1: 0.5121602288984263},
 2: {0: 0.5135908440629471, 1: 0.4864091559370529},
 3: {0: 0.46494992846924177, 1: 0.5350500715307582}}
# array of dict
np.asarray([{0: 0.474964234620887, 1: 0.525035765379113},
            {0: 0.48783977110157367, 1: 0.5121602288984263},
            {0: 0.5135908440629471, 1: 0.4864091559370529},
            {0: 0.46494992846924177, 1: 0.5350500715307582}])

标签: pythontensorflow2.x

解决方案


推荐阅读