Using neuralnet with caret train and adjusting the parameters - r

So I've read a paper that had used neural networks to model out a dataset which is similar to a dataset I'm currently using. I have 160 descriptor variables that I want to model out for 160 cases (regression modelling). The paper I read used the following parameters:-
'For each split, a model was developed for each of the 10 individual train-test folds. A three layer back-propagation net with 33 input neurons and 16 hidden neurons was used with online weight updates, 0.25 learning rate, and 0.9 momentum. For each fold, learning was conducted from a total of 50 different random initial weight starting points and the network was allowed to iterate through learning epochs until the mean absolute error (MAE) for the validation set reached a minimum. '
Now they used a specialist software called Emergent in order to do this, which is a very specialised neuronal network model software. However, as I've done previous models before in R, I have to keep to it. So I'm using the caret train function in order to do 10 cross fold validation, 10 times with the neuralnet package. I did the following:-
cadets.nn <- train(RT..seconds.~., data = cadet, method = "neuralnet", algorithm = 'backprop', learningrate = 0.25, hidden = 3, trControl = ctrl, linout = TRUE)
I did this to try and tune the parameters as closely to the ones used in the paper, however I get the following error message:-
layer1 layer2 layer3 RMSE Rsquared RMSESD RsquaredSD
1 1 0 0 NaN NaN NA NA
2 3 0 0 NaN NaN NA NA
3 5 0 0 NaN NaN NA NA
Error in train.default(x, y, weights = w, ...) :
final tuning parameters could not be determined
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Do you know what I'm doing wrong? It works when I do nnet, but I can't tune the parameters for that to make it similar to the ones used in the paper I'm trying to mimic.
This is what I get in the warnings() fifty times:-
1: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=1, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
2: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
3: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=3, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
4: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
5: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=5, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
Thanks!

train sets hidden for you (based on the values given by layer-layer3. You are trying to specify that argument twice, hence:
formal argument "hidden" matched by multiple actual arguments
HTH,
Max

I think for beginners it's not obvious at all that the layer specification cannot be passed directly into the train function.
One must read the documentation very carefully to understand the following passage for ...:
Errors will occur if values for tuning parameters are passed here.
So first, you must realize that the hidden parameter of the neuralnet::neuralnet is defined as a tuning parameter and therefore may not be passed directly to the train function (by ...). You find the tuning parameter definitions by:
getModelInfo("neuralnet")$neuralnet$parameters
parameter class label
1 layer1 numeric #Hidden Units in Layer 1
2 layer2 numeric #Hidden Units in Layer 2
3 layer3 numeric #Hidden Units in Layer 3
Instead, you must pass the hidden layer definition by the tuneGrid parameter - not obvious at all because that is normally reserved for tuning the parameters, not passing them.
So you can define the hidden layers as follows:
tune.grid.neuralnet <- expand.grid(
layer1 = 10,
layer2 = 10,
layer3 = 10
)
and then pass that to the caret::train function call as:
model.neuralnet.caret <- caret::train(
formula.nps,
data = training.set,
method = "neuralnet",
linear.output = TRUE,
tuneGrid = tune.grid.neuralnet, # cannot pass parameter hidden directly!!
metric = "RMSE",
trControl = trainControl(method = "none", seeds = seed)

Related

Error when trying to fit Hierarchical GAMs (Model GS or S) using mgcv

I have a large dataset (~100k observations) of presence/absence data that I am trying to fit a Hierarchical GAM with individual effects that have a Shared penalty (e.g. 'S' in Pedersen et al. 2019). The data consists of temp as numeric, region (5 groups) as a factor.
Here is a simple version of the model that I am trying to fit.
modS1 <- gam(occurrence ~ s(temp, region), family = binomial,
data = df, method = "REML")
modS2 <- gam(occurrence ~ s(temp, region, k= c(10,4), family = binomial,
data = df, method = "REML")
In the first case I received the following error:
Which I assumed it because k was set too high for region given there are only 5 different regions in the data set.
Error in smooth.construct.tp.smooth.spec(object, dk$data, dk$knots) :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
In the second case I attempt to lower k for region and receive this error:
Error in if (k < M + 1) { : the condition has length > 1
In addition: Warning messages:
1: In mean.default(xx) : argument is not numeric or logical: returning NA
2: In Ops.factor(xx, shift[i]) : ‘-’ not meaningful for factors
I can fit Models G and GI and I from Pedersen et al. 2019 with no issues. It is models GS and S where I run into issues.
If anyone has any insights I would really appreciate it!
The bs = "fs" argument in the code you're using as a guide is important. If we start at the ?s help page and click on the link to the ?smooth.terms help page, we see:
Factor smooth interactions
bs="fs" Smooth factor interactions are often produced using by variables (see gam.models), but a special smoother class (see factor.smooth.interaction) is available for the case in which a smooth is required at each of a large number of factor levels (for example a smooth for each patient in a study), and each smooth should have the same smoothing parameter. The "fs" smoothers are set up to be efficient when used with gamm, and have penalties on each null space component (i.e. they are fully ‘random effects’).
You need to use a smoothing basis appropriate for factors.
Notably, if you take your source code and remove the bs = "fs" argument and attempt to run gam(log(uptake) ∼ s(log(conc), Plant_uo, k=5, m=2), data=CO2, method="REML"), it will produce the same error that you got.

Errors with dredge() function in MuMin

I'm trying to use the dredge() function to evaluate models by completing every combination of variables (up to five variables per model) and comparing models using AIC corrected for small sample size (AICc).
However, I'm presented with one error and two warning messages as follows:
Fixed term is "(Intercept)"
Warning messages: 1: In dredge(MaxN.model,
m.min = 2, m.max = 5) : comparing models fitted by REML 2: In
dredge(MaxN.model, m.min = 2, m.max = 5) : arguments 'm.min' and
'm.max' are deprecated, use 'm.lim' instead
I've tried changing to 'm.lim' as specified but it comes up with the error:
Error in dredge(MaxN.model, m.lim = 5) : invalid 'm.lim' value In
addition: Warning message: In dredge(MaxN.model, m.lim = 5) :
comparing models fitted by REML
The code I'm using is:
MaxN.model<-lme(T_MaxN~Seagrass.cover+composition.pca1+composition.pca2+Sg.Richness+traits.pca1+
land.use.pc1+land.use.pc2+seascape.pc2+D.landing.site+T_Depth,
random=~1|site, data = sgdf, na.action = na.fail, method = "REML")
Dd_MaxN<-dredge(MaxN.model, m.min = 2 , m.max = 5)
What am I doing wrong?
You didn't tell us what you tried to specify for m.lim. ?dredge says:
m.lim ...optionally, the limits ‘c(lower, upper)’ for number of terms in a single model
so you should specify a two-element numeric (integer) vector.
You should definitely be using method="ML" rather than method="REML". The warning/error about REML is very serious; comparing models with different fixed effects that are fitted via REML will lead to nonsense.
So you should try:
MaxN.model <- lme(..., method = "ML") ## where ... is the rest of your fit
Dd_MaxN <- dredge(MaxN.model, m.lim=c(2,5))

Resuming an uncoverged ergm run with ergm package in R

I am using the ergm package in R to explore exponential random graph models for network data. Here's the network:
gn
Network attributes:
vertices = 678
directed = TRUE
hyper = FALSE
loops = FALSE
multiple = FALSE
bipartite = FALSE
total edges= 1663
missing edges= 0
non-missing edges= 1663
Vertex attribute names:
indegree membership num_tweets vertex.names
Edge attribute names not shown
After fitting simpler models, one with a term for edges and mutual and one with edges, mutual, and nodefactor("membership", base = 4) (where membership is a factor with four levels), I fit a model with nodematch("membership", diff = T) added, as follows:
model1 <- ergm(gn ~ edges +
mutual +
nodefactor("membership", base = 4) +
nodematch("membership", diff = T)
)
While the previous models converged, this model led to this message:
MCMLE estimation did not converge after 20 iterations. The estimated coefficients may not be accurate. Estimation may be resumed by passing the coefficients as initial values; see 'init' under ?control.ergm for details.
As instructed, I took a look at ?control.ergm: Passing control.ergm(init=coef(prev.fit)) can be used to “resume” an uncoverged ergm run, but see enformulate.curved.
OK, this sounds good, I searched for how control.ergm is used, and found an example from this page, and I tested that this worked by passing control = control.ergm(MCMLE.maxit = 50) as an argument, which increased the number of times the parameters for the MCMC should be updated by maximizing the MCMC likelihood from 20 to 50. I then did the following:
model1a <- ergm(gn ~ edges +
mutual +
nodefactor("membership", base = 4) +
nodematch("membership", diff = T),
control = control.ergm(init=coef(prev.fit))
)
But, this message is returned: Error in coef(prev.fit) : object 'prev.fit' not found.
I also tried to pass the model object (i.e., model1a) instead of prev.fit, but that led to this not-too-productive error:
Error in if (drop) { : argument is not interpretable as logical
In addition: Warning message:
In if (drop) { :
the condition has length > 1 and only the first element will be used
So, how can I "resume" an unconverged model by passing control.ergm(init = coef(prev.fit)) - or via a different approach?

caret::train: specify further non-tuning parameters for mlpWeightDecay (RSNNS package)

I have a problem with specifying the learning rate using the caret package with the method "mlpWeightDecay" from RSNNS package.
The tuning parameters of "mlpWeightDecay" are size and decay.
An example leaving size constant at 4 and tuning decay over c(0,0.0001, 0.001, 0.002):
data(iris)
TrainData <- iris[,1:4]
TrainClasses <- iris[,5]
fit1 <- train(TrainData, TrainClasses,
method = "mlpWeightDecay",
preProcess = c("center", "scale"),
tuneGrid=expand.grid(.size = 4, .decay = c(0,0.0001, 0.001, 0.002)),
trControl = trainControl(method = "cv")
)
But I also want to manipulate the learning rate of the model and not just taking the default learning rate of 0.2.
I know that I can use further arguments of the mlpWeightDecay method from RSNNS via the "..." parameter.
"learnFuncParams" would be the RSNNS parameter I would need to insert. It takes 4 parameters (learning rate, weight decay, dmin, dmax).
Going on with the example it looks like this:
fit1 <- train(TrainData, TrainClasses,
method = "mlpWeightDecay",
preProcess = c("center", "scale"),
tuneGrid=expand.grid(.size = 4, .decay = c(0,0.0001, 0.001, 0.002)),
trControl = trainControl(method = "cv"),
learnFuncParams=c(0.4,0,0,0)
)
BUT the documentation of the caret train function tells me for the "..." parameter:
arguments passed to the classification or regression routine (such as randomForest). Errors will occur if values for tuning parameters are passed here.
The problem is that one of the 4 "learningFuncParams" parameters (weight decay) IS a tuning parameter.
Consequently I get an error and warnings:
Error in train.default(TrainData, TrainClasses, method = "mlpWeightDecay", :
final tuning parameters could not be determined
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Warning messages:
1: In method$fit(x = if (!is.data.frame(x)) as.data.frame(x) else x, ... :
Over-riding weight decay value in the 'learnFuncParams' argument you passed in. Other values are retained
2: In eval(expr, envir, enclos) :
model fit failed for Fold01: size=4, decay=0e+00 Error in mlp.default(x = structure(list(Sepal.Length = c(-0.891390168709482, :
formal argument "learnFuncParams" matched by multiple actual arguments
How can I set the learning rate without coming in conflicts with the tuning parameter "decay" if both is set in the same parameter "learningFuncParams"?
Thanks!
It looks like you can specify your own learnFuncParams in "...". caret checks if you've provided your own set of parameters and will only override learnFuncParams[3] (which is the decay). It will take the learnFuncParams[1,2,4] that you have provided.
A very convenient way to find out what caret does is to type getModelInfo("mlpWeightDecay") and then scroll up to the $mlpWeightDecay$fit part. It shows how caret will call the real training function:
$mlpWeightDecay$fit
if (any(names(theDots) == "learnFuncParams")) {
prms <- theDots$learnFuncParams
prms[3] <- param$decay
warning("Over-riding weight decay value in the 'learnFuncParams' argument you passed in. Other values are retained")
}
It checks if you've provided your own learnFuncParams. If you did, it uses it, but inserts its own decay. You can ignore the warning.
I think the error you've got ("final tuning parameters could not be determined") has another reason. Have you tried a lower learning rate?

Neural network in R using the caret package [duplicate]

This question already has answers here:
Using neuralnet with caret train and adjusting the parameters
(2 answers)
Closed 8 years ago.
So I've read a paper that had used neural networks to model out a dataset which is similar to a dataset I'm currently using. I have 160 descriptor variables that I want to model out for 160 cases (regression modelling). The paper I read used the following parameters:-
'For each split, a model was developed for each of the 10 individual train-test folds. A three layer back-propagation net with 33 input neurons and 16 hidden neurons was used with online weight updates, 0.25 learning rate, and 0.9 momentum. For each fold, learning was conducted from a total of 50 different random initial weight starting points and the network was allowed to iterate through learning epochs until the mean absolute error (MAE) for the validation set reached a minimum. '
Now they used a specialist software called Emergent in order to do this, which is a very specialised neuronal network model software. However, as I've done previous models before in R, I have to keep to it. So I'm using the caret train function in order to do 10 cross fold validation, 10 times with the neuralnet package. I did the following:-
cadets.nn <- train(RT..seconds.~., data = cadet, method = "neuralnet", algorithm = 'backprop', learningrate = 0.25, hidden = 3, trControl = ctrl, linout = TRUE)
I did this to try and tune the parameters as closely to the ones used in the paper, however I get the following error message:-
layer1 layer2 layer3 RMSE Rsquared RMSESD RsquaredSD
1 1 0 0 NaN NaN NA NA
2 3 0 0 NaN NaN NA NA
3 5 0 0 NaN NaN NA NA
Error in train.default(x, y, weights = w, ...) :
final tuning parameters could not be determined
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Do you know what I'm doing wrong? It works when I do nnet, but I can't tune the parameters for that to make it similar to the ones used in the paper I'm trying to mimic.
This is what I get in the warnings() fifty times:-
1: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=1, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
2: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
3: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=3, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
4: In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
5: In eval(expr, envir, enclos) :
model fit failed for Fold01.Rep01: layer1=5, layer2=0, layer3=0 Error in neuralnet(form, data = data, hidden = nodes, ...) :
formal argument "hidden" matched by multiple actual arguments
Thanks!
From the error message, the 'hidden' parameter is not being properly matched. Looking at the documentation, there are only three training parameters for method = "neuralnet", layer1, layer2, layer3. Take a look at the link and use a different a method where you can specify the desired parameters.

Resources