如何使用tensorflow 2中的类权重进行多标签二进制分类?

2024-04-25 15:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是我在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))

sample reference from doc

我已经尝试过的解决方案是使用类权重列表、类权重dict和类权重数组。 我想知道是否有办法将多标签二进制分类类权重注入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}])

Tags: oftheformatfortensorflow二进制分类标签