title: Caret: How to set up custom model deepnet
body: <p>I want to use some of the parameters of the original <code>deepnet</code> package, so I set up a custom model. I read Caret's documentation (<a href="https://topepo.github.io/caret/using-your-own-model-in-train.html" rel="nofollow noreferrer">Custom Model</a>), but it doesn't work.</p>
  <p>Here is my code for setting up the customized model:</p>
  <pre><code># set up custom model
  dnn_custom &lt;- list(label = &quot;Stacked AutoEncoder Deep Neural Network&quot;,
                    library = &quot;deepnet&quot;,
                    loop = NULL,
                    type = c(&quot;Classification&quot;, &quot;Regression&quot;),
                    parameters = data.frame(parameter = c(&quot;layer1&quot;, &quot;layer2&quot;, &quot;layer3&quot;, &quot;hidden_dropout&quot;, &quot;visible_dropout&quot;, &quot;learningrate&quot;),
                                            class = rep(&quot;numeric&quot;, 6),
                                            label = c(&quot;Hidden Layer 1&quot;, &quot;Hidden Layer 2&quot;, &quot;Hidden Layer 3&quot;, 
                                                      &quot;Hidden Dropouts&quot;, &quot;Visible Dropout&quot;, &quot;Learning rate&quot;)),
                    grid = function(x, y, len = NULL, search = &quot;grid&quot;) {
                      if(search == &quot;grid&quot;) {
                        out &lt;- 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 &lt;- 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 &lt;- as.matrix(x)
                      is_class &lt;- is.factor(y)
                      if (is_class) y &lt;- caret:::class2ind(y)
                      layers &lt;- c(param$layer1, param$layer2, param$layer3)
                      layers &lt;- layers[layers &gt; 0]
                      deepnet::sae.dnn.train(x, y, hidden = layers,
                                             output = if(is_class) &quot;sigm&quot; else &quot;linear&quot;,
                                             hidden_dropout = param$hidden_dropout,
                                             visible_dropout = param$visible_dropout,
                                             learningrate = param$learningrate,
                                             ...)
                    },
                    predict = function(modelFit, newdata, submodels = NULL) {
                      pred &lt;- deepnet::nn.predict(modelFit, as.matrix(newdata))
                      if(ncol(pred) &gt; 1)
                        pred &lt;- modelFit$obsLevels[apply(pred, 1, which.max)]
                      pred
                    },
                    prob = function(modelFit, newdata, submodels = NULL) {
                      out &lt;- exp(deepnet::nn.predict(modelFit, as.matrix(newdata)))
                      out &lt;- apply(out, 1, function(x) x/sum(x))
                      t(out)
                    },
                    predictors = function(x, ...) {
                      NULL
                    },
                    varImp = NULL,
                    levels = function(x) x$classes,
                    tags = c(&quot;Neural Network&quot;),
                    sort = function(x) x[order(x[,1]),])
  </code></pre>
  <p>Here I try to use the model:</p>
  <pre><code>library(caret)
  library(MASS)
  library(deepnet)
  
  set.seed(132, kind = &quot;Mersenne-Twister&quot;, normal.kind = &quot;Inversion&quot;)
  
  # create data
  dat &lt;- Boston
  
  preds &lt;- dat[1:100, -which(names(dat) == &quot;medv&quot;)]
  response &lt;- dat[1:100, which(names(dat) == &quot;medv&quot;)]
  
  # specifiy trainControl for tuning mtry and with specified folds
  control &lt;- caret::trainControl(search = &quot;grid&quot;, method=&quot;repeatedcv&quot;, number=3, 
                                 repeats=2, 
                                 savePred = T)
  
  # tune hyperparameters and build final model
  tunegrid &lt;- 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 &lt;- caret::train(x = preds,
                        y = response, 
                        method=&quot;dnn_custom&quot;,
                        metric= &quot;RMSE&quot;, 
                        tuneGrid=tunegrid, 
                        trControl= control,
                        preProc = c(&quot;scale&quot;, &quot;center&quot;) 
  )
  </code></pre>
  <p>I am getting the error <code>dnn_custom is not in caret's built-in library</code>.
  What am I doing wrong?</p>