插入符号:如何设置自定义模型 deepnet
Caret: How to set up custom model deepnet
我想使用原deepnet
包的一些参数,所以我设置了一个自定义模型。我阅读了 Caret 的文档 (Custom Model),但它不起作用。
这是我设置自定义模型的代码:
# set up custom model
dnn_custom <- list(label = "Stacked AutoEncoder Deep Neural Network",
library = "deepnet",
loop = NULL,
type = c("Classification", "Regression"),
parameters = data.frame(parameter = c("layer1", "layer2", "layer3", "hidden_dropout", "visible_dropout", "learningrate"),
class = rep("numeric", 6),
label = c("Hidden Layer 1", "Hidden Layer 2", "Hidden Layer 3",
"Hidden Dropouts", "Visible Dropout", "Learning rate")),
grid = function(x, y, len = NULL, search = "grid") {
if(search == "grid") {
out <- expand.grid(layer1 = 1:len, layer2 = 0:(len -1), layer3 = 0:(len -1),
hidden_dropout = seq(0, .7, length = len),
visible_dropout = seq(0, .7, length = len),
learningrate = seq(0.0001,1, length = len))
} else {
out <- data.frame(layer1 = sample(2:20, replace = TRUE, size = len),
layer2 = sample(2:20, replace = TRUE, size = len),
layer3 = sample(2:20, replace = TRUE, size = len),
hidden_dropout = runif(len, min = 0, max = .7),
visible_dropout = runif(len, min = 0, max = .7),
learningrate = runif(len, min = 0.0001, max = 1))
}
out
},
fit = function(x, y, wts, param, lev, last, classProbs, ...) {
if(!is.matrix(x)) x <- as.matrix(x)
is_class <- is.factor(y)
if (is_class) y <- caret:::class2ind(y)
layers <- c(param$layer1, param$layer2, param$layer3)
layers <- layers[layers > 0]
deepnet::sae.dnn.train(x, y, hidden = layers,
output = if(is_class) "sigm" else "linear",
hidden_dropout = param$hidden_dropout,
visible_dropout = param$visible_dropout,
learningrate = param$learningrate,
...)
},
predict = function(modelFit, newdata, submodels = NULL) {
pred <- deepnet::nn.predict(modelFit, as.matrix(newdata))
if(ncol(pred) > 1)
pred <- modelFit$obsLevels[apply(pred, 1, which.max)]
pred
},
prob = function(modelFit, newdata, submodels = NULL) {
out <- exp(deepnet::nn.predict(modelFit, as.matrix(newdata)))
out <- apply(out, 1, function(x) x/sum(x))
t(out)
},
predictors = function(x, ...) {
NULL
},
varImp = NULL,
levels = function(x) x$classes,
tags = c("Neural Network"),
sort = function(x) x[order(x[,1]),])
这里我尝试使用模型:
library(caret)
library(MASS)
library(deepnet)
set.seed(132, kind = "Mersenne-Twister", normal.kind = "Inversion")
# create data
dat <- Boston
preds <- dat[1:100, -which(names(dat) == "medv")]
response <- dat[1:100, which(names(dat) == "medv")]
# specifiy trainControl for tuning mtry and with specified folds
control <- caret::trainControl(search = "grid", method="repeatedcv", number=3,
repeats=2,
savePred = T)
# tune hyperparameters and build final model
tunegrid <- expand.grid(layer1 = c(50,100),
layer2 = c(0,50,100),
layer3 = c(0,50,100),
hidden_dropout = c(0, 0.1),
visible_dropout = c(0, 0.1),
learningrate = c(0.9, 1))
model <- caret::train(x = preds,
y = response,
method="dnn_custom",
metric= "RMSE",
tuneGrid=tunegrid,
trControl= control,
preProc = c("scale", "center")
)
我收到错误 dnn_custom is not in caret's built-in library
。
我做错了什么?
我自己找到了答案...
这是一个简单的错误:我在应用自定义模型时不得不删除 method
中的引号:
model <- caret::train(x = preds,
y = response,
method = dnn_custom, # No quotation marks here!
metric = "RMSE",
tuneGrid =tunegrid,
trControl = control,
preProc = c("scale", "center")
)
现在一切正常。引号似乎仅指 Caret 中的内置模型。
我想使用原deepnet
包的一些参数,所以我设置了一个自定义模型。我阅读了 Caret 的文档 (Custom Model),但它不起作用。
这是我设置自定义模型的代码:
# set up custom model
dnn_custom <- list(label = "Stacked AutoEncoder Deep Neural Network",
library = "deepnet",
loop = NULL,
type = c("Classification", "Regression"),
parameters = data.frame(parameter = c("layer1", "layer2", "layer3", "hidden_dropout", "visible_dropout", "learningrate"),
class = rep("numeric", 6),
label = c("Hidden Layer 1", "Hidden Layer 2", "Hidden Layer 3",
"Hidden Dropouts", "Visible Dropout", "Learning rate")),
grid = function(x, y, len = NULL, search = "grid") {
if(search == "grid") {
out <- expand.grid(layer1 = 1:len, layer2 = 0:(len -1), layer3 = 0:(len -1),
hidden_dropout = seq(0, .7, length = len),
visible_dropout = seq(0, .7, length = len),
learningrate = seq(0.0001,1, length = len))
} else {
out <- data.frame(layer1 = sample(2:20, replace = TRUE, size = len),
layer2 = sample(2:20, replace = TRUE, size = len),
layer3 = sample(2:20, replace = TRUE, size = len),
hidden_dropout = runif(len, min = 0, max = .7),
visible_dropout = runif(len, min = 0, max = .7),
learningrate = runif(len, min = 0.0001, max = 1))
}
out
},
fit = function(x, y, wts, param, lev, last, classProbs, ...) {
if(!is.matrix(x)) x <- as.matrix(x)
is_class <- is.factor(y)
if (is_class) y <- caret:::class2ind(y)
layers <- c(param$layer1, param$layer2, param$layer3)
layers <- layers[layers > 0]
deepnet::sae.dnn.train(x, y, hidden = layers,
output = if(is_class) "sigm" else "linear",
hidden_dropout = param$hidden_dropout,
visible_dropout = param$visible_dropout,
learningrate = param$learningrate,
...)
},
predict = function(modelFit, newdata, submodels = NULL) {
pred <- deepnet::nn.predict(modelFit, as.matrix(newdata))
if(ncol(pred) > 1)
pred <- modelFit$obsLevels[apply(pred, 1, which.max)]
pred
},
prob = function(modelFit, newdata, submodels = NULL) {
out <- exp(deepnet::nn.predict(modelFit, as.matrix(newdata)))
out <- apply(out, 1, function(x) x/sum(x))
t(out)
},
predictors = function(x, ...) {
NULL
},
varImp = NULL,
levels = function(x) x$classes,
tags = c("Neural Network"),
sort = function(x) x[order(x[,1]),])
这里我尝试使用模型:
library(caret)
library(MASS)
library(deepnet)
set.seed(132, kind = "Mersenne-Twister", normal.kind = "Inversion")
# create data
dat <- Boston
preds <- dat[1:100, -which(names(dat) == "medv")]
response <- dat[1:100, which(names(dat) == "medv")]
# specifiy trainControl for tuning mtry and with specified folds
control <- caret::trainControl(search = "grid", method="repeatedcv", number=3,
repeats=2,
savePred = T)
# tune hyperparameters and build final model
tunegrid <- expand.grid(layer1 = c(50,100),
layer2 = c(0,50,100),
layer3 = c(0,50,100),
hidden_dropout = c(0, 0.1),
visible_dropout = c(0, 0.1),
learningrate = c(0.9, 1))
model <- caret::train(x = preds,
y = response,
method="dnn_custom",
metric= "RMSE",
tuneGrid=tunegrid,
trControl= control,
preProc = c("scale", "center")
)
我收到错误 dnn_custom is not in caret's built-in library
。
我做错了什么?
我自己找到了答案...
这是一个简单的错误:我在应用自定义模型时不得不删除 method
中的引号:
model <- caret::train(x = preds,
y = response,
method = dnn_custom, # No quotation marks here!
metric = "RMSE",
tuneGrid =tunegrid,
trControl = control,
preProc = c("scale", "center")
)
现在一切正常。引号似乎仅指 Caret 中的内置模型。