我正在尝试将包 ClusterR 的 MiniBatchKmeans 功能集成到 mlr。根据文档,我做了以下更改:
- 创建 makeRLearner.cluster.MiniBatchKmeans
- 创建 trainLearner.cluster.MiniBatchKmeans
- 创建 predictLearner.cluster.MiniBatchKmeans
- 注册了上述 S3 方法(如此处所述)
在这一点上,我能够创建学习器,并调用训练和预测它们。但是,当尝试创建没有提供任何“集群”值的学习器时,就会出现问题。
底层包(在ClusterR中)没有为参数“clusters”定义的默认值。根据 mlr 方法,我尝试使用 par.vals 参数提供“集群”的默认值。但是,忽略此默认参数。
我的代码:
#' @export
makeRLearner.cluster.MiniBatchKmeans = function() {
makeRLearnerCluster(
cl = "cluster.MiniBatchKmeans",
package = "ClusterR",
par.set = makeParamSet(
makeIntegerLearnerParam(id = "clusters", lower = 1L),
makeIntegerLearnerParam(id = "batch_size", default = 10L, lower = 1L),
makeIntegerLearnerParam(id = "num_init", default = 1L, lower = 1L),
makeIntegerLearnerParam(id = "max_iters", default = 100L, lower = 1L),
makeNumericLearnerParam(id = "init_fraction", default = 1, lower = 0),
makeDiscreteLearnerParam(id = "initializer", default = "kmeans++",
values = c("optimal_init", "quantile_init", "kmeans++", "random")),
makeIntegerLearnerParam(id = "early_stop_iter", default = 10L, lower = 1L),
makeLogicalLearnerParam(id = "verbose", default = FALSE,
tunable = FALSE),
makeUntypedLearnerParam(id = "CENTROIDS", default = NULL),
makeNumericLearnerParam(id = "tol", default = 1e-04, lower = 0),
makeNumericLearnerParam(id = "tol_optimal_init", default = 0.3, lower = 0),
makeIntegerLearnerParam(id = "seed", default = 1L)
),
par.vals = list(clusters = 2L),
properties = c("numerics", "prob"),
name = "MiniBatchKmeans",
note = "Note",
short.name = "MBatchKmeans",
callees = c("MiniBatchKmeans", "predict_MBatchKMeans")
)
}
#' @export
trainLearner.cluster.MiniBatchKmeans = function(.learner, .task, .subset, .weights = NULL, ...) {
ClusterR::MiniBatchKmeans(getTaskData(.task, .subset), ...)
}
#' @export
predictLearner.cluster.MiniBatchKmeans = function(.learner, .model, .newdata, ...) {
if (.learner$predict.type == "prob") {
pred = ClusterR::predict_MBatchKMeans(data = .newdata,
CENTROIDS = .model$learner.model$centroids,
fuzzy = TRUE, ...)
res = pred$fuzzy_clusters
return(res)
} else {
pred = ClusterR::predict_MBatchKMeans(data = .newdata,
CENTROIDS = .model$learner.model$centroids,
fuzzy = FALSE, ...)
res = as.integer(pred)
return(res)
}
}
问题(忽略上面 par.vals 中集群的默认值):
## When defining a value of clusters, it works as expected
lrn <- makeLearner("cluster.MiniBatchKmeans", clusters = 3L)
getLearnerParVals(lrn)
# The below commented lines are printed
# $clusters
# [1] 3
## When not providing a value for clusters, default is not used
lrn <- makeLearner("cluster.MiniBatchKmeans")
getLearnerParVals(lrn)
# The below commented lines are printed
# named list()
关于我为什么会看到这种行为的任何建议?我检查了其他学习者(如 cluster.kmeans、cluster.kkmeans 等)的代码,我发现他们能够以与我所做的相同格式成功定义默认值。此外,这里有文档证明这是正确的方法。
这是我在 github 上的代码,以防它有助于重现问题。有一个添加的测试文件(在 tests/testthat 中),但它有其自身的问题。
编辑 1 - 实际错误消息 这是我在尝试训练学习者时看到的实际错误消息,而没有明确提供“集群”的默认值:
lrn <- makeLearner("cluster.MiniBatchKmeans")
train(lrn, cluster_task)
Error in ClusterR::MiniBatchKmeans(getTaskData(.task, .subset), ...) :
argument "clusters" is missing, with no default
10.
ClusterR::MiniBatchKmeans(getTaskData(.task, .subset), ...) at RLearner_cluster_MiniBatchKmeans.R#32
9.
trainLearner.cluster.MiniBatchKmeans(.learner = structure(list(
id = "cluster.MiniBatchKmeans", type = "cluster", package = "ClusterR",
properties = c("numerics", "prob"), par.set = structure(list(
pars = list(clusters = structure(list(id = "clusters", ... at trainLearner.R#24
8.
(function (.learner, .task, .subset, .weights = NULL, ...)
{
UseMethod("trainLearner")
})(.learner = structure(list(id = "cluster.MiniBatchKmeans", ...
7.
do.call(trainLearner, pars) at train.R#96
6.
fun3(do.call(trainLearner, pars)) at train.R#96
5.
fun2(fun3(do.call(trainLearner, pars))) at train.R#96
4.
fun1({
learner.model = fun2(fun3(do.call(trainLearner, pars)))
}) at train.R#96
3.
force(expr) at helpers.R#93
2.
measureTime(fun1({
learner.model = fun2(fun3(do.call(trainLearner, pars)))
})) at train.R#96
1.
train(lrn, cluster_task)