python - 如何在 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))
我已经尝试过的解决方案是使用 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}])
解决方案
推荐阅读
- json - 在没有私人数据的情况下发布设置的最佳做法是什么?
- nuxt.js - Nuxt child:父路由工作不正确
- html - 键入“npm run start”后,CSS 样式表未加载到 html 网页上
- sql - 需要帮助计算与特定条件匹配的列中的值之间的差异
- java - 如何在用户输入的数组中找到最小的元素?为什么最小的元素显示为零?
- apache-spark - 如何从 Spark 将数据放入 Ignite Cache
- pytorch - 在 Ubuntu 20.04 上安装适当版本的 CUDA 和 CuDNN 后,torch.cuda.is_available() 会返回 FALSE
- java - java.sql.SQLRecoverableException:IO 错误:连接重置
- jinja2 - 如何在 Jinja 的 for 循环后省略空行?
- javascript - 设置 initialSlide 时,光滑的滑块不起作用