metann: Train an Artificial Neural Network with metANN

View source: R/metann.R

metannR Documentation

Train an Artificial Neural Network with metANN

Description

Trains a feed-forward multilayer perceptron using metaheuristic or gradient-based optimization algorithms. The function supports regression and classification tasks through either an x-y interface or a formula-data interface.

Usage

metann(
  formula = NULL,
  data = NULL,
  x = NULL,
  y = NULL,
  architecture = NULL,
  hidden_layers = NULL,
  activation = "relu",
  output_activation = NULL,
  task = c("auto", "regression", "classification"),
  optimizer = optimizer_pso(),
  loss = NULL,
  metrics = NULL,
  seed = NULL,
  verbose = TRUE
)

Arguments

formula

Optional formula specifying the model.

data

Optional data frame containing the variables in formula.

x

Optional numeric matrix or data frame of input features.

y

Optional response vector or one-column matrix.

architecture

Optional MLP architecture created by mlp_architecture().

hidden_layers

Optional integer vector specifying hidden layer sizes. Used when architecture is not supplied.

activation

Activation function used for hidden layers when hidden_layers is supplied. It can be a single value or a vector with the same length as hidden_layers.

output_activation

Optional activation function used for the output layer when hidden_layers is supplied. If NULL, it is selected automatically based on the task.

task

One of "auto", "regression", or "classification". If "auto", the task is detected from the response variable.

optimizer

A character string or a metANN optimizer object.

loss

Optional character string or metANN loss object. If NULL, it is selected automatically based on the task.

metrics

Optional character vector, metric object, or list of metric objects. If NULL, default metrics are selected automatically based on the task.

seed

Optional random seed.

verbose

Logical. If TRUE, optimization or training progress is printed.

Value

An object of class "metann".

References

Montana, D. J., and Davis, L. (1989). Training Feedforward Neural Networks Using Genetic Algorithms. Proceedings of the 11th International Joint Conference on Artificial Intelligence, 762–767.

Ilonen, J., Kamarainen, J.-K., and Lampinen, J. (2003). Differential Evolution Training Algorithm for Feed-Forward Neural Networks. Neural Processing Letters, 17, 93–105. doi:10.1023/A:1022995128597

Karaboga, D., and Ozturk, C. (2009). Neural Networks Training by Artificial Bee Colony Algorithm on Pattern Classification. Neural Network World, 19(3), 279–292.

Mirjalili, S. (2015). How Effective is the Grey Wolf Optimizer in Training Multi-Layer Perceptrons. Applied Intelligence, 43, 150–161. doi:10.1007/s10489-014-0645-7

Dilber, B., and Ozdemir, A. F. (2026). A novel approach to training feed-forward multi-layer perceptrons with recently proposed secretary bird optimization algorithm. Neural Computing and Applications, 38(5). doi:10.1007/s00521-026-11874-x

Examples

fit <- metann(
  formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length,
  data = iris,
  hidden_layers = c(5),
  optimizer = optimizer_pso(pop_size = 10, max_iter = 20),
  loss = "mse",
  metrics = c("rmse", "mae", "r2"),
  seed = 123,
  verbose = FALSE
)
fit

iris_bin <- iris
iris_bin$IsSetosa <- factor(
  ifelse(iris_bin$Species == "setosa", "setosa", "other")
)

fit_class <- metann(
  formula = IsSetosa ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
  data = iris_bin,
  hidden_layers = c(5),
  optimizer = optimizer_pso(pop_size = 10, max_iter = 20),
  seed = 123,
  verbose = FALSE
)
fit_class

metANN documentation built on May 16, 2026, 1:06 a.m.