Skip to contents

Predicted values obtained with a nn2poly object on given data.

Usage

# S3 method for class 'nn2poly'
predict(object, newdata, monomials = FALSE, layers = NULL, ...)

Arguments

object

Object of class inheriting from 'nn2poly'.

newdata

Input data as matrix, vector or dataframe. Number of columns (or elements in vector) should be the number of variables in the polynomial (dimension p). Response variable to be predicted should not be included.

monomials

Boolean determining if the returned item should contain the evaluations of all the monomials of the provided polynomials (monomials==TRUE), or if the final polynomial evaluation should be computed, i.e., adding up all the monomials (monomials==FALSE). Defaults to FALSE.

layers

Vector containing the chosen layers from object to be evaluated. If set to NULL, all layers are computed. Default is set to NULL.

...

Further arguments passed to or from other methods.

Value

Returns a matrix or list of matrices with the evaluation of each polynomial at each layer as given by the provided object of class nn2poly. The format can be as follows, depending on the layers contained in object and the parameters layers and monomials values:

  • If object contains the polynomials of the last layer, as given by nn2poly(object, keep_layers = FALSE), then the output is:

    • A matrix: if monomials==FALSE, returns a matrix containing the evaluation of the polynomials on the given data. The matrix has dimensions (n_sample, n_polynomials), meaning that each column corresponds to the result of evaluating all the data for a polynomial. If a single polynomial is provided, the output is a vector instead of a row matrix.

    • A 3D array: If monomials==TRUE, returns a 3D array containing the monomials of each polynomial evaluated on the given data. The array has dimensions (n_sample, n_monomial_terms, n_polynomials), where element [i,j,k] contains the evaluation on observation i on monomial j of polynomial k, where monomial j corresponds to the one on poly$labels[[j]].

  • If object contains all the internal polynomials, as given by nn2poly(object, keep_layers = TRUE), then the output is a list of layers (represented by layer_i), where each of them is another list with input and output elements. Each of those elements contains the corresponding evaluation of the "input" or "output" polynomial at the given layer, as explained in the last layer case, which will be a matrix if monomials==FALSE and a 3D array if monomials==TRUE.

Details

Internally uses eval_poly() to obtain the predictions. However, this only works with a objects of class nn2poly while eval_poly() can be used with a manually created polynomial in list form.

When object contains all the internal polynomials also, as given by nn2poly(object, keep_layers = TRUE), it is important to note that there are two polynomial items per layer (input/output). These polynomial items will also contain several polynomials of the same structure, one per neuron in the layer, stored as matrix rows in $values. Please see the NN2Poly original paper for more details.

Note also that "linear" layers will contain the same input and output results as Taylor expansion is not used and thus the polynomials are also the same. Because of this, in the situation of evaluating multiple layers we provide the final layer with "input" and "output" even if they are the same, for consistency.

See also

nn2poly(): function that obtains the nn2poly polynomial object, eval_poly(): function that can evaluate polynomials in general, stats::predict(): generic predict function.

Examples

# Build a NN structure with random weights, with 2 (+ bias) inputs,
# 4 (+bias) neurons in the first hidden layer with "tanh" activation
# function, 4 (+bias) neurons in the second hidden layer with "softplus",
# and 1 "linear" output unit

weights_layer_1 <- matrix(rnorm(12), nrow = 3, ncol = 4)
weights_layer_2 <- matrix(rnorm(20), nrow = 5, ncol = 4)
weights_layer_3 <- matrix(rnorm(5), nrow = 5, ncol = 1)

# Set it as a list with activation functions as names
nn_object = list("tanh" = weights_layer_1,
                 "softplus" = weights_layer_2,
                 "linear" = weights_layer_3)

# Obtain the polynomial representation (order = 3) of that neural network
final_poly <- nn2poly(nn_object, max_order = 3)

# Define some new data, it can be vector, matrix or dataframe
newdata <- matrix(rnorm(10), ncol = 2, nrow = 5)

# Predict using the obtained polynomial
predict(object = final_poly, newdata = newdata)
#> [1]   -187.8372 -39928.6031    484.2835   5728.3026  33157.9147

# Predict the values of each monomial of the obtained polynomial
predict(object = final_poly, newdata = newdata, monomials = TRUE)
#> , , 1
#> 
#>          [,1]      [,2]       [,3]       [,4]       [,5]       [,6]       [,7]
#> [1,] 3.513261  2.085832 -0.9767128 -106.05289   78.26172  -14.28478  -883.2954
#> [2,] 3.513261  1.668730  7.0684472  -67.87907 -453.11999 -748.14865  -452.2994
#> [3,] 3.513261  2.248785 -3.3289988 -123.27058  287.58393 -165.94613 -1106.9075
#> [4,] 3.513261 -1.028708 -3.6640994  -25.79572 -144.79793 -201.03624   105.9604
#> [5,] 3.513261 -2.768480 -5.3540672 -186.82993 -569.41422 -429.24723  2065.3414
#>            [,8]        [,9]        [,10]
#> [1,]  1040.3706   -356.2025     48.74379
#> [2,] -4819.0179 -14925.1243 -18475.26426
#> [3,]  4121.6558  -4461.2748   1930.00981
#> [4,]   949.3221   2472.3521   2573.47736
#> [5,] 10046.8266  14206.6881   8029.15924
#> 

# Change the last layer to have 3 outputs (as in a multiclass classification)
# problem
weights_layer_4 <- matrix(rnorm(20), nrow = 5, ncol = 4)

# Set it as a list with activation functions as names
nn_object = list("tanh" = weights_layer_1,
                 "softplus" = weights_layer_2,
                 "linear" = weights_layer_4)

# Obtain the polynomial representation of that neural network
# Polynomial representation of each hidden neuron is given by
final_poly <- nn2poly(nn_object, max_order = 3, keep_layers = TRUE)

# Define some new data, it can be vector, matrix or dataframe
newdata <- matrix(rnorm(10), ncol = 2, nrow = 5)

# Predict using the obtained polynomials (for all layers)
predict(object = final_poly, newdata = newdata)
#> $layer_1
#> $layer_1$input
#>             [,1]       [,2]       [,3]      [,4]
#> [1,] -2.62401445  1.8188597 -0.3543759 0.5645582
#> [2,]  2.21743082 -1.7261670  2.5971104 5.4244951
#> [3,] -1.90623760  1.0108695  0.7964167 0.6549163
#> [4,] -0.84490338  0.5278190  0.7007539 2.3765011
#> [5,]  0.03021762 -0.4802582  2.1618137 2.4354195
#> 
#> $layer_1$output
#>             [,1]        [,2]       [,3]         [,4]
#> [1,]  2.55979238 -0.05156903  126.17275   13.5694684
#> [2,]  0.96627363  1.46296363 -183.38882 -126.5851436
#> [3,] -0.20150872  0.66431045   12.06814    0.1345584
#> [4,] -0.64226760  0.49067820   13.89172   -6.2204705
#> [5,]  0.03186566 -0.40981375  -10.28230   -4.6008917
#> 
#> 
#> $layer_2
#> $layer_2$input
#>             [,1]        [,2]        [,3]       [,4]
#> [1,]   2.3561693  2.12300785  116.816764 -142.30171
#> [2,] 122.2628369 39.50262885 -133.710641  159.13016
#> [3,]  -0.2124414  0.09674539   10.610109  -13.52429
#> [4,]   6.4922421  1.85018160   14.255976  -16.97538
#> [5,]   2.3442393  0.44160798   -6.612954   10.50599
#> 
#> $layer_2$output
#>             [,1]       [,2]        [,3]       [,4]
#> [1,]   32.014312  0.7261721 -38.9407722 15206.8488
#> [2,] -230.619175 10.9465024 -27.4589902 -5836.6763
#> [3,]    2.435578  0.4999311  -2.3683187  1656.7308
#> [4,]   11.450233  0.9040130   5.2396058  2480.8518
#> [5,]   -1.761550  0.6042637  -0.6636513  -148.4675
#> 
#> 
#> $layer_3
#> $layer_3$input
#>             [,1]        [,2]       [,3]         [,4]
#> [1,]  39023.2866 -12465.2876 17357.4781 -1109.970518
#> [2,] -15067.8516   4565.8468 -7062.4930   231.596136
#> [3,]   4252.7714  -1358.5749  1889.4238  -118.096296
#> [4,]   6370.7828  -2029.3069  2846.6748  -160.796821
#> [5,]   -378.8749    122.2192  -172.9164     9.520727
#> 
#> $layer_3$output
#>             [,1]        [,2]       [,3]         [,4]
#> [1,]  39023.2866 -12465.2876 17357.4781 -1109.970518
#> [2,] -15067.8516   4565.8468 -7062.4930   231.596136
#> [3,]   4252.7714  -1358.5749  1889.4238  -118.096296
#> [4,]   6370.7828  -2029.3069  2846.6748  -160.796821
#> [5,]   -378.8749    122.2192  -172.9164     9.520727
#> 
#> 

# Predict using the obtained polynomials (for chosen layers)
predict(object = final_poly, newdata = newdata, layers = c(2,3))
#> $layer_2
#> $layer_2$input
#>             [,1]        [,2]        [,3]       [,4]
#> [1,]   2.3561693  2.12300785  116.816764 -142.30171
#> [2,] 122.2628369 39.50262885 -133.710641  159.13016
#> [3,]  -0.2124414  0.09674539   10.610109  -13.52429
#> [4,]   6.4922421  1.85018160   14.255976  -16.97538
#> [5,]   2.3442393  0.44160798   -6.612954   10.50599
#> 
#> $layer_2$output
#>             [,1]       [,2]        [,3]       [,4]
#> [1,]   32.014312  0.7261721 -38.9407722 15206.8488
#> [2,] -230.619175 10.9465024 -27.4589902 -5836.6763
#> [3,]    2.435578  0.4999311  -2.3683187  1656.7308
#> [4,]   11.450233  0.9040130   5.2396058  2480.8518
#> [5,]   -1.761550  0.6042637  -0.6636513  -148.4675
#> 
#> 
#> $layer_3
#> $layer_3$input
#>             [,1]        [,2]       [,3]         [,4]
#> [1,]  39023.2866 -12465.2876 17357.4781 -1109.970518
#> [2,] -15067.8516   4565.8468 -7062.4930   231.596136
#> [3,]   4252.7714  -1358.5749  1889.4238  -118.096296
#> [4,]   6370.7828  -2029.3069  2846.6748  -160.796821
#> [5,]   -378.8749    122.2192  -172.9164     9.520727
#> 
#> $layer_3$output
#>             [,1]        [,2]       [,3]         [,4]
#> [1,]  39023.2866 -12465.2876 17357.4781 -1109.970518
#> [2,] -15067.8516   4565.8468 -7062.4930   231.596136
#> [3,]   4252.7714  -1358.5749  1889.4238  -118.096296
#> [4,]   6370.7828  -2029.3069  2846.6748  -160.796821
#> [5,]   -378.8749    122.2192  -172.9164     9.520727
#> 
#>