| Title: | Neural Network with Levenberg-Marquardt Optimization |
|---|---|
| Description: | An implementation of a Neural Network using the Levenberg-Marquardt optimization from 'minpack.lm', ideal for small datasets. For more details see Moré (1978) <doi:10.1007/BFb0067700>. |
| Authors: | Umberto Minora [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-2151-6500>) |
| Maintainer: | Umberto Minora <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.1.9000 |
| Built: | 2026-05-27 08:56:52 UTC |
| Source: | https://github.com/umbe1987/nnetlm |
Flattens the network parameters so they can be passed to [minipack.lm::nls.lm] 'par' argument
flatten_params(object)flatten_params(object)
object |
an object of class "nnetLM" |
flattened vector with network parameters (weights and biases)
Initalize the neural network object
nnetLM(X, y, hidden, actFn)nnetLM(X, y, hidden, actFn)
X |
Matrix of independent variables |
y |
Vector of dependent variables |
|
Vector of number of nodes in each hidden layer |
|
actFn |
List of activation functions (must be length(hidden)+1 for the output node) |
The activation functions within [actFn] list can be any existing or user-defined function. They must have a single numeric argument (e.g. [x]), and must return a numeric value of the same length as [x].
An object with S3 class "nnetLM"
set.seed(123) x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn)set.seed(123) x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn)
Performs a forward pass
## S3 method for class 'nnetLM' predict(object, newdata)## S3 method for class 'nnetLM' predict(object, newdata)
object |
a trained network object of class "nnetLM" |
newdata |
Matrix of predictors |
a numeric vector with predicted values
set.seed(123) x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn) nnet.obj <- train.nnetLM(nnet.obj,50) pred.nnetLM <- predict(nnet.obj, X)set.seed(123) x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn) nnet.obj <- train.nnetLM(nnet.obj,50) pred.nnetLM <- predict(nnet.obj, X)
Residual function needed by [minipack.lm::nls.lm]
residFun(params, observed, object, xx)residFun(params, observed, object, xx)
params |
vector of flattened network parameters (weights and biases) |
observed |
an object of class "nnetLM" |
object |
an object of class "nnetLM" |
xx |
an object of class "nnetLM" |
unflattened list with network parameters (weights and biases)
Train the neural network with Levenberg-Marquardt optimization using [minipack.lm::nls.lm]
train.nnetLM(object, epochs, progress = FALSE)train.nnetLM(object, epochs, progress = FALSE)
object |
an object of class "nnetLM" |
epochs |
maximum number of iteration |
progress |
flag for printing network progress. Default is FALSE |
the trained network object
[minipack.lm::nls.lm()]
x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn) nnet.obj <- train.nnetLM(nnet.obj,1)x <- seq(-10, 10, by = 0.1) y <- sin(x) + rnorm(length(x), mean = 0, sd = 0.1) X <- matrix(x, nrow = length(x), ncol = 1) hidden <- c(10) linear <- function(x) x actFn <- list(tanh, linear) nnet.obj <- nnetLM(X, y, hidden, actFn) nnet.obj <- train.nnetLM(nnet.obj,1)
Unflattens the network parameters after they have been used by [minipack.lm::nls.lm]
unflatten_params(params, object)unflatten_params(params, object)
params |
vector of flattened network parameters (weights and biases) |
object |
an object of class "nnetLM" |
unflattened list with network parameters (weights and biases)