跳到内容

多目标模型

smac.model.multi_objective_model #

MultiObjectiveModel #

MultiObjectiveModel(
    models: AbstractModel | list[AbstractModel],
    objectives: list[str],
    seed: int = 0,
)

基类: AbstractModel

预测多个目标的代理模型包装器。

参数#

models : AbstractModel | list[AbstractModel] 应使用哪个模型。如果它是一个列表,则必须提供与目标数量相同的模型。如果只有一个模型,则该模型用于所有目标。 objectives : list[str] 应使用哪些目标。 seed : int

源代码位于 smac/model/multi_objective_model.py
def __init__(
    self,
    models: AbstractModel | list[AbstractModel],
    objectives: list[str],
    seed: int = 0,
) -> None:
    self._n_objectives = len(objectives)
    if isinstance(models, list):
        assert len(models) == len(objectives)

        # Make sure the configspace is the same
        configspace = models[0]._configspace
        for m in models:
            assert configspace == m._configspace

        self._models = models
    else:
        configspace = models._configspace
        self._models = [models for _ in range(self._n_objectives)]

    super().__init__(
        configspace=configspace,
        instance_features=None,
        pca_components=None,
        seed=seed,
    )

meta property #

meta: dict[str, Any]

返回创建对象的元数据。

models property #

内部使用的代理模型。

predict #

predict(
    X: ndarray, covariance_type: str | None = "diagonal"
) -> tuple[ndarray, ndarray | None]

预测给定 X 的均值和方差。内部调用方法 _predict

参数#

X : np.ndarray [#samples, #hyperparameters + #features] 输入数据点。 covariance_type: str | None,默认为 "diagonal" 指定除均值外返回的内容。仅应用于高斯过程。接受四个有效输入: * None:仅返回均值。 * "std":返回测试点的标准差。 * "diagonal":返回协方差矩阵的对角线。 * "full":返回测试点之间的整个协方差矩阵。

返回#

means : np.ndarray [#samples, #objectives] 预测均值。 vars : np.ndarray [#samples, #objectives] 或 [#samples, #samples] | None 预测方差或标准差。

源代码位于 smac/model/abstract_model.py
def predict(
    self,
    X: np.ndarray,
    covariance_type: str | None = "diagonal",
) -> tuple[np.ndarray, np.ndarray | None]:
    """Predicts mean and variance for a given X. Internally, calls the method `_predict`.

    Parameters
    ----------
    X : np.ndarray [#samples, #hyperparameters + #features]
        Input data points.
    covariance_type: str | None, defaults to "diagonal"
        Specifies what to return along with the mean. Applied only to Gaussian Processes.
        Takes four valid inputs:
        * None: Only the mean is returned.
        * "std": Standard deviation at test points is returned.
        * "diagonal": Diagonal of the covariance matrix is returned.
        * "full": Whole covariance matrix between the test points is returned.

    Returns
    -------
    means : np.ndarray [#samples, #objectives]
        The predictive mean.
    vars : np.ndarray [#samples, #objectives] or [#samples, #samples] | None
        Predictive variance or standard deviation.
    """
    if len(X.shape) != 2:
        raise ValueError("Expected 2d array, got %dd array!" % len(X.shape))

    if X.shape[1] != self._n_hps + self._n_features:
        raise ValueError(
            f"Feature mismatch: X should have {self._n_hps} hyperparameters + {self._n_features} features, "
            f"but has {X.shape[1]} in total."
        )

    if self._apply_pca:
        try:
            X_feats = X[:, -self._n_features :]
            X_feats = self._scaler.transform(X_feats)
            X_feats = self._pca.transform(X_feats)
            X = np.hstack((X[:, : self._n_hps], X_feats))
        except NotFittedError:
            # PCA not fitted if only one training sample
            pass

    if X.shape[1] != len(self._types):
        raise ValueError("Rows in X should have %d entries but have %d!" % (len(self._types), X.shape[1]))

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", "Predicted variances smaller than 0. Setting those variances to 0.")
        mean, var = self._predict(X, covariance_type)

    if len(mean.shape) == 1:
        mean = mean.reshape((-1, 1))

    if var is not None and len(var.shape) == 1:
        var = var.reshape((-1, 1))

    return mean, var

train #

train(X: ndarray, Y: ndarray) -> Self

在 X 和 Y 上训练随机森林。内部调用方法 _train

参数#

X : np.ndarray [#samples, #hyperparameters + #features] 输入数据点。 Y : np.ndarray [#samples, #objectives] 对应的目标值。

返回#

self : AbstractModel

源代码位于 smac/model/abstract_model.py
def train(self: Self, X: np.ndarray, Y: np.ndarray) -> Self:
    """Trains the random forest on X and Y. Internally, calls the method `_train`.

    Parameters
    ----------
    X : np.ndarray [#samples, #hyperparameters + #features]
        Input data points.
    Y : np.ndarray [#samples, #objectives]
        The corresponding target values.

    Returns
    -------
    self : AbstractModel
    """
    if len(X.shape) != 2:
        raise ValueError("Expected 2d array, got %dd array!" % len(X.shape))

    if X.shape[1] != self._n_hps + self._n_features:
        raise ValueError(
            f"Feature mismatch: X should have {self._n_hps} hyperparameters + {self._n_features} features, "
            f"but has {X.shape[1]} in total."
        )

    if X.shape[0] != Y.shape[0]:
        raise ValueError("X.shape[0] ({}) != y.shape[0] ({})".format(X.shape[0], Y.shape[0]))

    # Reduce dimensionality of features if larger than PCA_DIM
    if (
        self._pca_components is not None
        and X.shape[0] > self._pca.n_components
        and self._n_features >= self._pca_components
    ):
        X_feats = X[:, -self._n_features :]

        # Scale features
        X_feats = self._scaler.fit_transform(X_feats)
        X_feats = np.nan_to_num(X_feats)  # if features with max == min

        # PCA
        X_feats = self._pca.fit_transform(X_feats)
        X = np.hstack((X[:, : self._n_hps], X_feats))

        if hasattr(self, "_types"):
            # For RF, adapt types list
            # if X_feats.shape[0] < self._pca, X_feats.shape[1] == X_feats.shape[0]
            self._types = np.array(
                np.hstack((self._types[: self._n_hps], np.zeros(X_feats.shape[1]))),
                dtype=np.uint,
            )  # type: ignore

        self._apply_pca = True
    else:
        self._apply_pca = False

        if hasattr(self, "_types"):
            self._types = copy.deepcopy(self._initial_types)

    return self._train(X, Y)