• 【函数讲解】botorch中的函数 is_non_dominated():用于计算非支配(non-dominated)前沿


    默认求最大的Pareto前沿 

    1. # 获取训练目标值,计算Pareto前沿(非支配解集合),然后从样本中提取出Pareto最优解。
    2. train_obj = self.samples[1]
    3. pareto_mask = is_non_dominated(train_obj)
    4. pareto_y = train_obj[pareto_mask]

    源码

    这里用到了一个函数 is_non_dominated(),来看下该函数的源码:

    1. from __future__ import annotations
    2. import torch
    3. from torch import Tensor
    4. def is_non_dominated(Y: Tensor, deduplicate: bool = True) -> Tensor:
    5. r"""Computes the non-dominated front.
    6. Note: this assumes maximization.
    7. Args:
    8. 输入:张量 Y,其维度为 (batch_shape) x n x m,这里 n 代表样本数量,m 代表每个样本的目标数量。,其中括号括住的batch_shape意思是可选,可以有这个维度或者没有
    9. Y: A `(batch_shape) x n x m`-dim tensor of outcomes.
    10. deduplicate: A boolean indicating whether to only return unique points on the pareto frontier.
    11. Returns:
    12. 返回:布尔张量,指示每个样本是否是非支配点
    13. A `(batch_shape) x n`-dim boolean tensor indicating whether each point is non-dominated.
    14. """
    15. Y1 = Y.unsqueeze(-3)
    16. Y2 = Y.unsqueeze(-2)
    17. dominates = (Y1 >= Y2).all(dim=-1) & (Y1 > Y2).any(dim=-1)
    18. nd_mask = ~(dominates.any(dim=-1))
    19. if deduplicate:
    20. # remove duplicates
    21. # find index of first occurrence of each unique element
    22. indices = (Y1 == Y2).all(dim=-1).long().argmax(dim=-1)
    23. keep = torch.zeros_like(nd_mask)
    24. keep.scatter_(dim=-1, index=indices, value=1.0)
    25. return nd_mask & keep
    26. return nd_mask

    示例:

    有一组解,每个解有两个目标值,找出这组解中的非支配解:

    1. import torch
    2. from botorch.utils.multi_objective import is_non_dominated
    3. # 假设有5个解,每个解有2个目标
    4. Y = torch.tensor([
    5. [0.5, 0.7],
    6. [0.6, 0.6],
    7. [0.8, 0.3],
    8. [0.4, 0.9],
    9. [0.7, 0.5]
    10. ])
    11. # 调用 is_non_dominated 函数
    12. non_dominated_mask = is_non_dominated(Y)
    13. # 打印非支配解
    14. print("Non-dominated solutions:", Y[non_dominated_mask])

    实例:

    这里就是先拿到所有的目标值,然后计算哪些是Pareto(True或者False),最后再原始数据中选出所有True的数据

    1. # 获取训练目标值,计算Pareto前沿(非支配解集合),然后从样本中提取出Pareto最优解。
    2. train_obj = self.samples[1]
    3. pareto_mask = is_non_dominated(train_obj)
    4. pareto_y = train_obj[pareto_mask]

  • 相关阅读:
    基于SSM的图书管理系统
    求解组合数
    sql解决连续登录问题变形-节假日过滤
    openharmony容器组件之GridItem
    307. 区域和检索 - 数组可修改 --力扣 --JAVA
    将文字转为UTF-8编码(即HTML可识别的)
    PerfView专题 (第十三篇):洞察 .NET程序 的非托管句柄泄露
    【Linux】03_编译原理
    Linux系统移植三:移植Kernel生成zImage和dtb文件
    杰理之、产线装配环节【篇】
  • 原文地址:https://blog.csdn.net/weixin_43135178/article/details/134337446