A shortcut to create a feed-forward block (MLP block)

nn_mlp(..., activation = nnf_relu)

Arguments

...

(nn_module, function integer, character) An arbitrary number of arguments, than can be: * nn_module - e.g. torch::nn_relu() * function - e.g. torch::nnf_relu * character - e.g. selu, which is converted to nnf_selu * integer -

activation

Used if only integers are specified. By default: nnf_relu

Examples

nn_mlp(10, 1)
#> An `nn_module` containing 11 parameters.
#> 
#> ── Modules ─────────────────────────────────────────────────────────────────────
#> • layer_1: <nn_linear> #11 parameters
nn_mlp(30, 10, 1)
#> An `nn_module` containing 321 parameters.
#> 
#> ── Modules ─────────────────────────────────────────────────────────────────────
#> • layer_1: <nn_linear> #310 parameters
#> • layer_2: <nn_linear> #11 parameters

# Simple forward pass
net <- nn_mlp(4, 2, 1)
x <- as_tensor(iris[, 1:4])
net(x)
#> torch_tensor
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#>  0
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{150,1} ][ grad_fn = <ReluBackward0> ]

# Simple forward pass with identity function
net <- nn_mlp(4, 2, 1, activation = function (x) x)
x <- as_tensor(iris[, 1:4])
net(x)
#> torch_tensor
#>  1.1206
#>  1.0234
#>  1.0382
#>  1.0559
#>  1.1376
#>  1.2832
#>  1.1056
#>  1.1192
#>  0.9958
#>  1.0487
#>  1.1825
#>  1.1348
#>  1.0091
#>  0.9430
#>  1.1884
#>  1.3440
#>  1.2068
#>  1.1331
#>  1.2572
#>  1.2084
#>  1.1645
#>  1.2022
#>  1.0541
#>  1.1780
#>  1.1921
#>  1.0634
#>  1.1634
#>  1.1415
#>  1.1036
#>  1.0955
#> ... [the output was truncated (use n=-1 to disable)]
#> [ CPUFloatType{150,1} ][ grad_fn = <AddmmBackward> ]