| Title: | Metaheuristic and Gradient-Based Optimization for Neural Network Training and Continuous Problems |
|---|---|
| Description: | Provides tools for general-purpose continuous optimization and feed-forward artificial neural network training using metaheuristic and gradient-based optimization algorithms. The package supports benchmark function optimization, regression, binary classification, and multi-class classification with multilayer perceptrons. The package implements several optimization methods, including particle swarm optimization Kennedy and Eberhart (1995) <doi:10.1109/ICNN.1995.488968>, differential evolution Storn and Price (1997) <doi:10.1023/A:1008202821328>, grey wolf optimizer Mirjalili et al. (2014) <doi:10.1016/j.advengsoft.2013.12.007>, secretary bird optimization Fu et al. (2024) <doi:10.1007/s10462-024-10729-y>, and Adam Kingma and Ba (2015) <doi:10.48550/arXiv.1412.6980>. |
| Authors: | Burak Dilber [aut, cre, cph], A. Fırat Özdemir [aut, ths] |
| Maintainer: | Burak Dilber <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-15 22:03:11 UTC |
| Source: | https://github.com/cran/metANN |
Creates a leaky rectified linear unit activation function object.
activation_leaky_relu(alpha = 0.01)activation_leaky_relu(alpha = 0.01)
alpha |
A non-negative numeric value controlling the slope for negative inputs. |
An object of class "met_activation".
act <- activation_leaky_relu(alpha = 0.01) act$fn(c(-1, 0, 1))act <- activation_leaky_relu(alpha = 0.01) act$fn(c(-1, 0, 1))
Creates a linear activation function object.
activation_linear()activation_linear()
An object of class "met_activation".
act <- activation_linear() act$fn(c(-1, 0, 1))act <- activation_linear() act$fn(c(-1, 0, 1))
Creates a rectified linear unit activation function object.
activation_relu()activation_relu()
An object of class "met_activation".
Nair, V., and Hinton, G. E. (2010). Rectified Linear Units Improve Restricted Boltzmann Machines. Proceedings of the 27th International Conference on Machine Learning, 807–814.
act <- activation_relu() act$fn(c(-1, 0, 1))act <- activation_relu() act$fn(c(-1, 0, 1))
Creates a sigmoid activation function object.
activation_sigmoid()activation_sigmoid()
An object of class "met_activation".
act <- activation_sigmoid() act$fn(c(-1, 0, 1))act <- activation_sigmoid() act$fn(c(-1, 0, 1))
Creates a softmax activation function object.
activation_softmax()activation_softmax()
An object of class "met_activation".
Bridle, J. S. (1990). Probabilistic Interpretation of Feedforward Classification Network Outputs, with Relationships to Statistical Pattern Recognition. In Neurocomputing: Algorithms, Architectures and Applications, 227–236. Springer.
act <- activation_softmax() act$fn(c(1, 2, 3))act <- activation_softmax() act$fn(c(1, 2, 3))
Creates a hyperbolic tangent activation function object.
activation_tanh()activation_tanh()
An object of class "met_activation".
act <- activation_tanh() act$fn(c(-1, 0, 1))act <- activation_tanh() act$fn(c(-1, 0, 1))
Converts a character string such as "relu" into the corresponding
activation function object.
as_activation(activation)as_activation(activation)
activation |
A character string or an object of class
|
An object of class "met_activation".
as_activation("relu") as_activation(activation_leaky_relu(alpha = 0.05))as_activation("relu") as_activation(activation_leaky_relu(alpha = 0.05))
Converts a character string such as "mse" into the corresponding loss
function object.
as_loss(loss)as_loss(loss)
loss |
A character string or an object of class |
An object of class "met_loss".
as_loss("mse") as_loss(loss_huber(delta = 1.5))as_loss("mse") as_loss(loss_huber(delta = 1.5))
Converts a character string such as "rmse" into the corresponding metric
function object.
as_metric(metric)as_metric(metric)
metric |
A character string or an object of class |
An object of class "met_metric".
as_metric("rmse") as_metric(metric_accuracy())as_metric("rmse") as_metric(metric_accuracy())
Converts a character vector or a list of metric objects into a list of metric objects.
as_metrics(metrics)as_metrics(metrics)
metrics |
A character vector, a single metric object, or a list of metric objects. |
A list of objects of class "met_metric".
as_metrics(c("rmse", "mae", "r2")) as_metrics(list(metric_accuracy(), metric_f1()))as_metrics(c("rmse", "mae", "r2")) as_metrics(list(metric_accuracy(), metric_f1()))
Converts a character string such as "pso" into the corresponding optimizer
object.
as_optimizer(optimizer)as_optimizer(optimizer)
optimizer |
A character string or an object of class |
An object of class "met_optimizer".
as_optimizer("pso") as_optimizer(optimizer_adam())as_optimizer("pso") as_optimizer(optimizer_adam())
Returns the names of activation functions currently available in the metANN package.
available_activations()available_activations()
A character vector of activation function names.
available_activations()available_activations()
Returns the names of gradient-based optimizer objects currently available in the metANN package.
available_gradient_optimizers()available_gradient_optimizers()
A character vector of gradient-based optimizer names.
available_gradient_optimizers()available_gradient_optimizers()
Returns the names of loss functions currently available in the metANN package.
available_losses()available_losses()
A character vector of loss function names.
available_losses()available_losses()
Returns the names of metaheuristic optimization algorithms currently available in the metANN package.
available_metaheuristics()available_metaheuristics()
A character vector of metaheuristic optimizer names.
available_metaheuristics()available_metaheuristics()
Returns the names of performance metrics currently available in the metANN package.
available_metrics()available_metrics()
A character vector of metric names.
available_metrics()available_metrics()
Returns the names of optimization algorithms currently available in the metANN package.
available_optimizers()available_optimizers()
A character vector of optimizer names.
available_optimizers()available_optimizers()
Extract the Best Parameters from a metANN Optimization Result
## S3 method for class 'met_optimize_result' coef(object, ...)## S3 method for class 'met_optimize_result' coef(object, ...)
object |
A metANN optimization result object. |
... |
Additional arguments, currently unused. |
A numeric vector containing the best solution found.
Extract Weights from a metANN Model
## S3 method for class 'metann' coef(object, ...)## S3 method for class 'metann' coef(object, ...)
object |
A fitted metANN model. |
... |
Additional arguments, currently unused. |
A numeric vector of fitted network weights.
Computes the total number of weights and bias terms required by a multilayer perceptron architecture.
count_parameters(architecture, input_dim = NULL)count_parameters(architecture, input_dim = NULL)
architecture |
An object created by |
input_dim |
Optional positive integer specifying the number of input
features. If |
A positive integer giving the total number of parameters.
arch <- mlp_architecture( input_dim = 4, layers = list( dense_layer(5, activation = "relu"), dense_layer(1, activation = "linear") ) ) count_parameters(arch)arch <- mlp_architecture( input_dim = 4, layers = list( dense_layer(5, activation = "relu"), dense_layer(1, activation = "linear") ) ) count_parameters(arch)
Converts a numeric parameter vector into layer-wise weight matrices and bias vectors.
decode_weights(weights, architecture, input_dim = NULL)decode_weights(weights, architecture, input_dim = NULL)
weights |
A numeric vector of MLP parameters. |
architecture |
An object created by |
input_dim |
Optional positive integer specifying the number of input
features. If |
A list containing layer-wise weight matrices and bias vectors.
arch <- mlp_architecture( input_dim = 2, layers = list(dense_layer(3), dense_layer(1, activation = "linear")) ) w <- initialize_weights(arch, seed = 123) decoded <- decode_weights(w, arch)arch <- mlp_architecture( input_dim = 2, layers = list(dense_layer(3), dense_layer(1, activation = "linear")) ) w <- initialize_weights(arch, seed = 123) decoded <- decode_weights(w, arch)
Creates a fully connected dense layer object for use in metANN architectures.
dense_layer( units, activation = "relu", use_bias = TRUE, trainable = TRUE, name = NULL )dense_layer( units, activation = "relu", use_bias = TRUE, trainable = TRUE, name = NULL )
units |
A positive integer specifying the number of neurons in the layer. |
activation |
A character string or a |
use_bias |
Logical. Whether to include a bias term in the layer. |
trainable |
Logical. Whether the layer parameters should be trainable. |
name |
An optional character string specifying the layer name. |
An object of class "met_dense_layer".
dense_layer(10, activation = "relu") dense_layer(1, activation = activation_linear())dense_layer(10, activation = "relu") dense_layer(1, activation = activation_linear())
Evaluates a fitted metANN model on new data.
evaluate(object, newdata, y_true = NULL, metrics = NULL, threshold = 0.5, ...)evaluate(object, newdata, y_true = NULL, metrics = NULL, threshold = 0.5, ...)
object |
A fitted object of class |
newdata |
New data used for evaluation. For formula-based models, this should be a data frame containing the response variable. For x-y models, this should be a numeric matrix or numeric data frame. |
y_true |
Optional true response values. Required for x-y models. For
formula-based models, if |
metrics |
Optional performance metrics. If |
threshold |
Classification threshold for binary classification. |
... |
Additional arguments passed to |
An object of class "metann_evaluation".
fit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) evaluate(fit, newdata = iris)fit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) evaluate(fit, newdata = iris)
Computes predictions from input data, an MLP architecture, and a parameter vector.
forward_pass(x, weights, architecture)forward_pass(x, weights, architecture)
x |
A numeric matrix or data frame of input features. |
weights |
A numeric vector of MLP parameters. |
architecture |
An object created by |
A numeric matrix containing network outputs.
x <- matrix(rnorm(10), nrow = 5, ncol = 2) arch <- mlp_architecture( input_dim = 2, layers = list( dense_layer(3, activation = "relu"), dense_layer(1, activation = "linear") ) ) w <- initialize_weights(arch, seed = 123) forward_pass(x, w, arch)x <- matrix(rnorm(10), nrow = 5, ncol = 2) arch <- mlp_architecture( input_dim = 2, layers = list( dense_layer(3, activation = "relu"), dense_layer(1, activation = "linear") ) ) w <- initialize_weights(arch, seed = 123) forward_pass(x, w, arch)
Creates a numeric vector of randomly initialized weights and bias terms for an MLP architecture.
initialize_weights( architecture, input_dim = NULL, method = c("uniform", "normal"), lower = -0.5, upper = 0.5, mean = 0, sd = 0.1, seed = NULL )initialize_weights( architecture, input_dim = NULL, method = c("uniform", "normal"), lower = -0.5, upper = 0.5, mean = 0, sd = 0.1, seed = NULL )
architecture |
An object created by |
input_dim |
Optional positive integer specifying the number of input
features. If |
method |
Initialization method. Currently |
lower |
Lower bound for uniform initialization. |
upper |
Upper bound for uniform initialization. |
mean |
Mean for normal initialization. |
sd |
Standard deviation for normal initialization. |
seed |
Optional random seed. |
A numeric vector containing initialized parameters.
arch <- mlp_architecture( input_dim = 3, layers = list(dense_layer(2), dense_layer(1, activation = "linear")) ) initialize_weights(arch, seed = 123)arch <- mlp_architecture( input_dim = 3, layers = list(dense_layer(2), dense_layer(1, activation = "linear")) ) initialize_weights(arch, seed = 123)
Check Whether an Object is a metANN Activation
is_activation(x)is_activation(x)
x |
An object. |
TRUE if x is a metANN activation object; otherwise FALSE.
is_activation(activation_relu())is_activation(activation_relu())
Check Whether an Object is a metANN Architecture
is_architecture(x)is_architecture(x)
x |
An object. |
TRUE if x is a metANN architecture object; otherwise FALSE.
arch <- mlp_architecture(list(dense_layer(1))) is_architecture(arch)arch <- mlp_architecture(list(dense_layer(1))) is_architecture(arch)
Check Whether an Object is a Dense Layer
is_dense_layer(x)is_dense_layer(x)
x |
An object. |
TRUE if x is a dense layer object; otherwise FALSE.
is_dense_layer(dense_layer(5))is_dense_layer(dense_layer(5))
Check Whether an Object is a metANN Layer
is_layer(x)is_layer(x)
x |
An object. |
TRUE if x is a metANN layer object; otherwise FALSE.
is_layer(dense_layer(5))is_layer(dense_layer(5))
Check Whether an Object is a metANN Loss
is_loss(x)is_loss(x)
x |
An object. |
TRUE if x is a metANN loss object; otherwise FALSE.
is_loss(loss_mse())is_loss(loss_mse())
Check Whether an Object is a metANN Metric
is_metric(x)is_metric(x)
x |
An object. |
TRUE if x is a metANN metric object; otherwise FALSE.
is_metric(metric_rmse())is_metric(metric_rmse())
Check Whether an Object is an MLP Architecture
is_mlp_architecture(x)is_mlp_architecture(x)
x |
An object. |
TRUE if x is an MLP architecture object; otherwise FALSE.
arch <- mlp_architecture(list(dense_layer(1))) is_mlp_architecture(arch)arch <- mlp_architecture(list(dense_layer(1))) is_mlp_architecture(arch)
Check Whether an Object is a metANN Optimizer
is_optimizer(x)is_optimizer(x)
x |
An object. |
TRUE if x is a metANN optimizer object; otherwise FALSE.
is_optimizer(optimizer_pso())is_optimizer(optimizer_pso())
Creates a binary cross-entropy loss function object.
loss_binary_crossentropy(epsilon = 1e-15)loss_binary_crossentropy(epsilon = 1e-15)
epsilon |
A small positive numeric value used for numerical stability. |
An object of class "met_loss".
Bridle, J. S. (1990). Probabilistic Interpretation of Feedforward Classification Network Outputs, with Relationships to Statistical Pattern Recognition. In Neurocomputing: Algorithms, Architectures and Applications, 227–236. Springer.
loss <- loss_binary_crossentropy() loss$fn(c(0, 1, 1), c(0.1, 0.8, 0.9))loss <- loss_binary_crossentropy() loss$fn(c(0, 1, 1), c(0.1, 0.8, 0.9))
Creates a categorical cross-entropy loss function object.
loss_crossentropy(epsilon = 1e-15)loss_crossentropy(epsilon = 1e-15)
epsilon |
A small positive numeric value used for numerical stability. |
An object of class "met_loss".
Bridle, J. S. (1990). Probabilistic Interpretation of Feedforward Classification Network Outputs, with Relationships to Statistical Pattern Recognition. In Neurocomputing: Algorithms, Architectures and Applications, 227–236. Springer.
loss <- loss_crossentropy() y_true <- matrix(c(1, 0, 0, 0, 1, 0), nrow = 2, byrow = TRUE) y_pred <- matrix(c(0.8, 0.1, 0.1, 0.2, 0.7, 0.1), nrow = 2, byrow = TRUE) loss$fn(y_true, y_pred)loss <- loss_crossentropy() y_true <- matrix(c(1, 0, 0, 0, 1, 0), nrow = 2, byrow = TRUE) y_pred <- matrix(c(0.8, 0.1, 0.1, 0.2, 0.7, 0.1), nrow = 2, byrow = TRUE) loss$fn(y_true, y_pred)
Creates a Huber loss function object.
loss_huber(delta = 1)loss_huber(delta = 1)
delta |
A positive numeric value controlling the transition point between squared and absolute loss behavior. |
An object of class "met_loss".
Huber, P. J. (1964). Robust Estimation of a Location Parameter. The Annals of Mathematical Statistics, 35(1), 73–101. doi:10.1214/aoms/1177703732
loss <- loss_huber(delta = 1) loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))loss <- loss_huber(delta = 1) loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a log-cosh loss function object.
loss_log_cosh()loss_log_cosh()
An object of class "met_loss".
loss <- loss_log_cosh() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))loss <- loss_log_cosh() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a mean absolute error loss function object.
loss_mae()loss_mae()
An object of class "met_loss".
loss <- loss_mae() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))loss <- loss_mae() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a mean squared error loss function object.
loss_mse()loss_mse()
An object of class "met_loss".
loss <- loss_mse() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))loss <- loss_mse() loss$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Convenience wrapper around metann() for training feed-forward multilayer
perceptrons.
met_mlp( 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 )met_mlp( 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 )
formula |
Optional model formula. |
data |
Optional data frame used with |
x |
Optional numeric input matrix or data frame. |
y |
Optional response vector. |
architecture |
Optional MLP architecture object. |
|
Integer vector giving the number of units in each hidden layer. |
|
activation |
Activation function for hidden layers. |
output_activation |
Optional output activation function. If |
task |
One of |
optimizer |
Optimizer object. |
loss |
Optional loss function. If |
metrics |
Optional performance metrics. If |
seed |
Optional random seed. |
verbose |
Logical. If |
An object of class "metann".
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
fit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) fitfit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) fit
Performs continuous optimization using metaheuristic or gradient-based optimization algorithms.
met_optimize( fn, optimizer = optimizer_pso(), lower, upper, gr = NULL, initial = NULL, seed = NULL, verbose = TRUE, ... )met_optimize( fn, optimizer = optimizer_pso(), lower, upper, gr = NULL, initial = NULL, seed = NULL, verbose = TRUE, ... )
fn |
Objective function to be minimized. It must accept a numeric vector as its first argument and return a single numeric value. |
optimizer |
Optimizer object created by functions such as
|
lower |
Numeric vector of lower bounds. |
upper |
Numeric vector of upper bounds. |
gr |
Optional gradient function. Required for gradient-based optimizers
such as |
initial |
Optional numeric vector of initial parameter values. If |
seed |
Optional random seed. |
verbose |
Logical. If |
... |
Additional arguments passed to |
An object of class "met_optimize_result".
sphere <- function(x) sum(x^2) result <- met_optimize( fn = sphere, optimizer = optimizer_pso(pop_size = 10, max_iter = 20), lower = rep(-5, 2), upper = rep(5, 2), seed = 123, verbose = FALSE ) result grad_sphere <- function(x) 2 * x result_adam <- met_optimize( fn = sphere, gr = grad_sphere, optimizer = optimizer_adam(learning_rate = 0.1, epochs = 20), lower = rep(-5, 2), upper = rep(5, 2), initial = rep(4, 2), seed = 123, verbose = FALSE ) result_adamsphere <- function(x) sum(x^2) result <- met_optimize( fn = sphere, optimizer = optimizer_pso(pop_size = 10, max_iter = 20), lower = rep(-5, 2), upper = rep(5, 2), seed = 123, verbose = FALSE ) result grad_sphere <- function(x) 2 * x result_adam <- met_optimize( fn = sphere, gr = grad_sphere, optimizer = optimizer_adam(learning_rate = 0.1, epochs = 20), lower = rep(-5, 2), upper = rep(5, 2), initial = rep(4, 2), seed = 123, verbose = FALSE ) result_adam
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.
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 )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 )
formula |
Optional formula specifying the model. |
data |
Optional data frame containing the variables in |
x |
Optional numeric matrix or data frame of input features. |
y |
Optional response vector or one-column matrix. |
architecture |
Optional MLP architecture created by |
|
Optional integer vector specifying hidden layer sizes.
Used when |
|
activation |
Activation function used for hidden layers when
|
output_activation |
Optional activation function used for the output
layer when |
task |
One of |
optimizer |
A character string or a metANN optimizer object. |
loss |
Optional character string or metANN loss object. If |
metrics |
Optional character vector, metric object, or list of metric
objects. If |
seed |
Optional random seed. |
verbose |
Logical. If |
An object of class "metann".
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
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_classfit <- 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
Creates an accuracy metric object for classification tasks.
metric_accuracy()metric_accuracy()
An object of class "met_metric".
metric <- metric_accuracy() metric$fn(c(0, 1, 1), c(0, 1, 0))metric <- metric_accuracy() metric$fn(c(0, 1, 1), c(0, 1, 0))
Creates an F1 score metric object for classification tasks.
metric_f1(positive_class = 1)metric_f1(positive_class = 1)
positive_class |
The class label treated as the positive class.
Defaults to |
An object of class "met_metric".
metric <- metric_f1() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))metric <- metric_f1() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))
Creates a mean absolute error metric object.
metric_mae()metric_mae()
An object of class "met_metric".
metric <- metric_mae() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))metric <- metric_mae() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a mean squared error metric object.
metric_mse()metric_mse()
An object of class "met_metric".
metric <- metric_mse() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))metric <- metric_mse() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a precision metric object for classification tasks.
metric_precision(positive_class = 1)metric_precision(positive_class = 1)
positive_class |
The class label treated as the positive class.
Defaults to |
An object of class "met_metric".
metric <- metric_precision() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))metric <- metric_precision() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))
Creates an R-squared metric object.
metric_r2()metric_r2()
An object of class "met_metric".
metric <- metric_r2() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))metric <- metric_r2() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a recall metric object for classification tasks.
metric_recall(positive_class = 1)metric_recall(positive_class = 1)
positive_class |
The class label treated as the positive class.
Defaults to |
An object of class "met_metric".
metric <- metric_recall() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))metric <- metric_recall() metric$fn(c(0, 1, 1, 0), c(0, 1, 0, 0))
Creates a root mean squared error metric object.
metric_rmse()metric_rmse()
An object of class "met_metric".
metric <- metric_rmse() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))metric <- metric_rmse() metric$fn(c(1, 2, 3), c(1.1, 1.9, 3.2))
Creates a multilayer perceptron architecture object from a list of dense layers.
mlp_architecture(layers, input_dim = NULL, name = "mlp")mlp_architecture(layers, input_dim = NULL, name = "mlp")
layers |
A list of dense layer objects created by |
input_dim |
Optional positive integer specifying the number of input
features. This can be left as |
name |
Optional character string specifying the architecture name. |
An object of class "met_mlp_architecture".
architecture <- mlp_architecture( layers = list( dense_layer(10, activation = "relu"), dense_layer(1, activation = "linear") ) ) architecturearchitecture <- mlp_architecture( layers = list( dense_layer(10, activation = "relu"), dense_layer(1, activation = "linear") ) ) architecture
Creates an Artificial Bee Colony optimizer object for continuous optimization problems.
optimizer_abc(colony_size = 30, max_iter = 100, limit = NULL)optimizer_abc(colony_size = 30, max_iter = 100, limit = NULL)
colony_size |
Total colony size. Half of the colony is used as employed bees and half as onlooker bees. |
max_iter |
Maximum number of iterations. |
limit |
Number of unsuccessful trials before a food source is abandoned. |
An object of class "met_optimizer".
Karaboga, D., and Basturk, B. (2007). A Powerful and Efficient Algorithm for Numerical Function Optimization: Artificial Bee Colony (ABC) Algorithm. Journal of Global Optimization, 39, 459–471. doi:10.1007/s10898-007-9149-x
Karaboga, D., and Ozturk, C. (2009). Neural Networks Training by Artificial Bee Colony Algorithm on Pattern Classification. Neural Network World, 19(3), 279–292.
optimizer_abc()optimizer_abc()
Creates an Adam optimizer object.
optimizer_adam( learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999, epsilon = 1e-08, epochs = 100, batch_size = NULL )optimizer_adam( learning_rate = 0.001, beta1 = 0.9, beta2 = 0.999, epsilon = 1e-08, epochs = 100, batch_size = NULL )
learning_rate |
Learning rate. |
beta1 |
Exponential decay rate for the first moment estimates. |
beta2 |
Exponential decay rate for the second moment estimates. |
epsilon |
Small positive constant for numerical stability. |
epochs |
Number of training epochs. |
batch_size |
Mini-batch size. If |
An object of class "met_optimizer".
Kingma, D. P., and Ba, J. (2015). Adam: A Method for Stochastic Optimization. International Conference on Learning Representations.
optimizer_adam()optimizer_adam()
Creates a Differential Evolution optimizer object.
optimizer_de( pop_size = 30, max_iter = 100, F = 0.5, CR = 0.9, strategy = "rand/1/bin" )optimizer_de( pop_size = 30, max_iter = 100, F = 0.5, CR = 0.9, strategy = "rand/1/bin" )
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
F |
Differential weight. Common values are between 0.4 and 1. |
CR |
Crossover probability. Must be between 0 and 1. |
strategy |
Differential evolution strategy. Currently only
|
An object of class "met_optimizer".
Storn, R., and Price, K. (1997). Differential Evolution – A Simple and Efficient Heuristic for Global Optimization over Continuous Spaces. Journal of Global Optimization, 11, 341–359. doi:10.1023/A:1008202821328
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
optimizer_de()optimizer_de()
Creates a real-coded Genetic Algorithm optimizer object.
optimizer_ga( pop_size = 30, max_iter = 100, crossover_rate = 0.8, mutation_rate = 0.1, mutation_sd = 0.1, elitism = TRUE, selection = "tournament", tournament_size = 2 )optimizer_ga( pop_size = 30, max_iter = 100, crossover_rate = 0.8, mutation_rate = 0.1, mutation_sd = 0.1, elitism = TRUE, selection = "tournament", tournament_size = 2 )
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
crossover_rate |
Probability of crossover. |
mutation_rate |
Probability of mutating each parameter. |
mutation_sd |
Standard deviation of Gaussian mutation noise. |
elitism |
Logical. Whether to preserve the best solution in each generation. |
selection |
Selection method. Currently only |
tournament_size |
Number of individuals used in tournament selection. |
An object of class "met_optimizer".
Goldberg, D. E. (1989). Genetic Algorithms in Search, Optimization, and Machine Learning. Addison-Wesley, Reading, MA.
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.
optimizer_ga()optimizer_ga()
Creates a Grey Wolf Optimizer object for continuous optimization problems.
optimizer_gwo(pop_size = 30, max_iter = 100, a_start = 2, a_end = 0)optimizer_gwo(pop_size = 30, max_iter = 100, a_start = 2, a_end = 0)
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
a_start |
Initial value of the control parameter |
a_end |
Final value of the control parameter |
An object of class "met_optimizer".
Mirjalili, S., Mirjalili, S. M., and Lewis, A. (2014). Grey Wolf Optimizer. Advances in Engineering Software, 69, 46–61. doi:10.1016/j.advengsoft.2013.12.007
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
optimizer_gwo()optimizer_gwo()
Creates a hybrid optimizer object by combining a global optimizer and a local optimizer.
optimizer_hybrid( global = optimizer_pso(), local = optimizer_adam(), strategy = "sequential" )optimizer_hybrid( global = optimizer_pso(), local = optimizer_adam(), strategy = "sequential" )
global |
A metaheuristic optimizer object. |
local |
A gradient-based optimizer object. |
strategy |
Hybrid training strategy. Currently |
An object of class "met_optimizer".
optimizer_hybrid( global = optimizer_pso(max_iter = 10), local = optimizer_adam(epochs = 10) )optimizer_hybrid( global = optimizer_pso(max_iter = 10), local = optimizer_adam(epochs = 10) )
Returns basic information about an optimizer available in the metANN package.
optimizer_info(optimizer)optimizer_info(optimizer)
optimizer |
Character name of an optimizer or an optimizer object created
by functions such as |
An object of class "met_optimizer_info".
optimizer_info("pso") optimizer_info("sboa") optimizer_info(optimizer_adam())optimizer_info("pso") optimizer_info("sboa") optimizer_info(optimizer_adam())
Creates a Particle Swarm Optimization optimizer object.
optimizer_pso( pop_size = 30, max_iter = 100, w = 0.7, c1 = 1.5, c2 = 1.5, velocity_clamp = NULL )optimizer_pso( pop_size = 30, max_iter = 100, w = 0.7, c1 = 1.5, c2 = 1.5, velocity_clamp = NULL )
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
w |
Inertia weight. |
c1 |
Cognitive acceleration coefficient. |
c2 |
Social acceleration coefficient. |
velocity_clamp |
Optional maximum absolute velocity. If |
An object of class "met_optimizer".
Kennedy, J., and Eberhart, R. (1995). Particle Swarm Optimization. Proceedings of ICNN'95 - International Conference on Neural Networks, 4, 1942–1948. doi:10.1109/ICNN.1995.488968
optimizer_pso()optimizer_pso()
Creates a Secretary Bird Optimization Algorithm optimizer object for continuous optimization problems.
optimizer_sboa(pop_size = 30, max_iter = 100)optimizer_sboa(pop_size = 30, max_iter = 100)
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
An object of class "met_optimizer".
Fu, Y., Liu, D., Chen, J., and He, L. (2024). Secretary Bird Optimization Algorithm: A New Metaheuristic for Solving Global Optimization Problems. Artificial Intelligence Review, 57, 123. doi:10.1007/s10462-024-10729-y
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
optimizer_sboa()optimizer_sboa()
Creates a stochastic gradient descent optimizer object.
optimizer_sgd(learning_rate = 0.01, epochs = 100, batch_size = NULL)optimizer_sgd(learning_rate = 0.01, epochs = 100, batch_size = NULL)
learning_rate |
Learning rate. |
epochs |
Number of training epochs. |
batch_size |
Mini-batch size. If |
An object of class "met_optimizer".
Robbins, H., and Monro, S. (1951). A Stochastic Approximation Method. The Annals of Mathematical Statistics, 22(3), 400–407. doi:10.1214/aoms/1177729586
optimizer_sgd()optimizer_sgd()
Creates a Teaching-Learning-Based Optimization optimizer object for continuous optimization problems.
optimizer_tlbo(pop_size = 30, max_iter = 100)optimizer_tlbo(pop_size = 30, max_iter = 100)
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
An object of class "met_optimizer".
Rao, R. V., Savsani, V. J., and Vakharia, D. P. (2011). Teaching-Learning-Based Optimization: A Novel Method for Constrained Mechanical Design Optimization Problems. Computer-Aided Design, 43, 303–315. doi:10.1016/j.cad.2010.12.015
optimizer_tlbo()optimizer_tlbo()
Creates a Whale Optimization Algorithm optimizer object for continuous optimization problems.
optimizer_woa(pop_size = 30, max_iter = 100, a_start = 2, a_end = 0, b = 1)optimizer_woa(pop_size = 30, max_iter = 100, a_start = 2, a_end = 0, b = 1)
pop_size |
Population size. |
max_iter |
Maximum number of iterations. |
a_start |
Initial value of the control parameter |
a_end |
Final value of the control parameter |
b |
Constant defining the spiral shape in the bubble-net mechanism. |
An object of class "met_optimizer".
Mirjalili, S., and Lewis, A. (2016). The Whale Optimization Algorithm. Advances in Engineering Software, 95, 51–67. doi:10.1016/j.advengsoft.2016.01.008
optimizer_woa()optimizer_woa()
Plots the architecture of a feed-forward multilayer perceptron, showing input, hidden, and output layers in a visually enhanced layout.
plot_network( object, max_neurons = 20, show_connections = TRUE, neuron_cex = 2.2, label_cex = 0.9, main = "Neural Network Architecture", ... )plot_network( object, max_neurons = 20, show_connections = TRUE, neuron_cex = 2.2, label_cex = 0.9, main = "Neural Network Architecture", ... )
object |
A fitted |
max_neurons |
Maximum number of neurons to display per layer. If a layer has more neurons than this value, only a subset is displayed and the layer is annotated. |
show_connections |
Logical. If |
neuron_cex |
Size of neuron circles. |
label_cex |
Size of text labels. |
main |
Main title of the plot. |
... |
Additional graphical arguments. |
The input object invisibly.
fit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) plot_network(fit)fit <- met_mlp( formula = Petal.Width ~ Sepal.Length + Sepal.Width + Petal.Length, data = iris, hidden_layers = c(5), optimizer = optimizer_pso(pop_size = 10, max_iter = 10), seed = 123, verbose = FALSE ) plot_network(fit)
Plots the convergence curve of a metANN optimization result.
## S3 method for class 'met_optimize_result' plot(x, log = FALSE, ...)## S3 method for class 'met_optimize_result' plot(x, log = FALSE, ...)
x |
An object of class |
log |
Logical. If |
... |
Additional graphical arguments passed to |
The input object invisibly.
Plot a metANN Model
## S3 method for class 'metann' plot(x, ...)## S3 method for class 'metann' plot(x, ...)
x |
A fitted metANN model. |
... |
Additional arguments passed to |
The input object invisibly.
Generates predictions from a fitted metANN model.
## S3 method for class 'metann' predict( object, newdata, type = c("response", "prob", "class"), threshold = 0.5, ... )## S3 method for class 'metann' predict( object, newdata, type = c("response", "prob", "class"), threshold = 0.5, ... )
object |
A fitted object of class |
newdata |
New data used for prediction. For formula-based models, this should be a data frame. For x-y models, this should be a numeric matrix or numeric data frame. |
type |
Prediction type. For regression models, |
threshold |
Classification threshold for binary classification. |
... |
Additional arguments. |
A numeric vector, matrix, or factor depending on the task and prediction type.
Print a Dense Layer
## S3 method for class 'met_dense_layer' print(x, ...)## S3 method for class 'met_dense_layer' print(x, ...)
x |
A dense layer object. |
... |
Additional arguments, currently unused. |
The input object invisibly.
Print an MLP Architecture
## S3 method for class 'met_mlp_architecture' print(x, ...)## S3 method for class 'met_mlp_architecture' print(x, ...)
x |
An MLP architecture object. |
... |
Additional arguments, currently unused. |
The input object invisibly.
Print a metANN Optimization Result
## S3 method for class 'met_optimize_result' print(x, ...)## S3 method for class 'met_optimize_result' print(x, ...)
x |
A metANN optimization result object. |
... |
Additional arguments, currently unused. |
The input object invisibly.
Print a metANN Optimizer
## S3 method for class 'met_optimizer' print(x, ...)## S3 method for class 'met_optimizer' print(x, ...)
x |
A metANN optimizer object. |
... |
Additional arguments, currently unused. |
The input object invisibly.
Print Optimizer Information
## S3 method for class 'met_optimizer_info' print(x, ...)## S3 method for class 'met_optimizer_info' print(x, ...)
x |
An object of class |
... |
Additional arguments. |
The input object invisibly.
Print a metANN Model
## S3 method for class 'metann' print(x, ...)## S3 method for class 'metann' print(x, ...)
x |
A metANN model object. |
... |
Additional arguments, currently unused. |
The input object invisibly.
Print metANN Evaluation Results
## S3 method for class 'metann_evaluation' print(x, ...)## S3 method for class 'metann_evaluation' print(x, ...)
x |
An object of class |
... |
Additional arguments. |
The input object invisibly.
Summarize a metANN Optimization Result
## S3 method for class 'met_optimize_result' summary(object, ...)## S3 method for class 'met_optimize_result' summary(object, ...)
object |
A metANN optimization result object. |
... |
Additional arguments, currently unused. |
A list containing the main optimization results.
Summarize a metANN Model
## S3 method for class 'metann' summary(object, ...)## S3 method for class 'metann' summary(object, ...)
object |
A metANN model object. |
... |
Additional arguments, currently unused. |
A list containing model summary information.