Utility function to build a Keras MLP.

build_keras_net(
  n_in,
  n_out,
  nodes = c(32L, 32L),
  layer_pars = list(),
  activation = "linear",
  act_pars = list(),
  dropout = 0.1,
  batch_norm = TRUE,
  batch_pars = list()
)

Arguments

n_in

(integer(1))
Number of input features.

n_out

(integer(1))
Number of targets.

nodes

(numeric())
Hidden nodes in network, each element in vector represents number of hidden nodes in respective layer.

layer_pars

(list())
Arguments passed to keras::layer_dense.

activation

(character(1))
Activation function passed to keras::layer_activation. Default is linear.

act_pars

(list())
Parameters for activation function, see keras::layer_activation.

dropout

(numeric(1))
Optional dropout layer, if NULL then no dropout layer added otherwise either same dropout will be added to all layers.

batch_norm

(logical(1))
If TRUE (default) then batch normalisation is applied to all layers.

batch_pars

(list())
Parameters for batch normalisation, see keras::layer_batch_normalization.

Details

This function is a helper for R users with less Python experience. Currently it is limited to simple MLPs and with identical layers. More advanced networks will require manual creation with keras.

Examples

# \donttest{
if (requireNamespaces("keras")) {
  build_keras_net(4L, 2L)

  build_keras_net(n_in = 4L, n_out = 2L, nodes = c(32L, 64L, 32L),
    activation = "elu", dropout = 0.4)
}
#> Error: Valid installation of TensorFlow not found.
#> 
#> Python environments searched for 'tensorflow' package:
#>  /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12
#>  /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12
#> 
#> Python exception encountered:
#>  Traceback (most recent call last):
#>   File "/Users/runner/work/_temp/Library/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
#>     return _run_hook(name, _hook)
#>            ^^^^^^^^^^^^^^^^^^^^^^
#>   File "/Users/runner/work/_temp/Library/reticulate/python/rpytools/loader.py", line 93, in _run_hook
#>     module = hook()
#>              ^^^^^^
#>   File "/Users/runner/work/_temp/Library/reticulate/python/rpytools/loader.py", line 117, in _hook
#>     return _find_and_load(name, import_)
#>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#> ModuleNotFoundError: No module named 'tensorflow'
#> 
#> 
#> You can install TensorFlow using the install_tensorflow() function.
# }