Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
inst/doc
docs
__pycache__
smoke_test*
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Depends: R (>= 3.5.0)
Imports:
Rcpp,
generics,
matrixStats,
pracma
matrixStats
Suggests:
keras, tensorflow, reticulate,
luz, torch,
Expand All @@ -37,7 +36,7 @@ LinkingTo:
RcppArmadillo
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
Config/roxygen2/version: 8.0.0
Config/testthat/edition: 3
URL: https://ibidat.github.io/nn2poly/, https://github.com/IBiDat/nn2poly
BugReports: https://github.com/IBiDat/nn2poly/issues
Config/roxygen2/version: 8.0.0
24 changes: 18 additions & 6 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

select_allowed_partitions <- function(equivalent_label, q_previous_layer, labels, partitions) {
.Call(`_nn2poly_select_allowed_partitions`, equivalent_label, q_previous_layer, labels, partitions)
obtain_final_poly_order <- function(max_order, taylor_orders) {
.Call(`_nn2poly_obtain_final_poly_order`, max_order, taylor_orders)
}

obtain_taylor_vector <- function(taylor_orders, af_string_list) {
.Call(`_nn2poly_obtain_taylor_vector`, taylor_orders, af_string_list)
}

obtain_derivatives_list <- function(taylor_orders, af_string_list) {
.Call(`_nn2poly_obtain_derivatives_list`, taylor_orders, af_string_list)
}

obtain_partitions_with_labels <- function(p, q_max) {
.Call(`_nn2poly_obtain_partitions_with_labels`, p, q_max)
}

alg_non_linear <- function(coeffs_input, labels_input, labels_output, taylor_orders, current_layer, g, partitions_labels, partitions) {
.Call(`_nn2poly_alg_non_linear`, coeffs_input, labels_input, labels_output, taylor_orders, current_layer, g, partitions_labels, partitions)
}

nn2poly_algorithm <- function(layers, af_list, max_order, keep_layers, taylor_orders) {
.Call(`_nn2poly_nn2poly_algorithm`, layers, af_list, max_order, keep_layers, taylor_orders)
}

combinations_with_repetition <- function(n, k) {
.Call(`_nn2poly_combinations_with_repetition`, n, k)
}
Expand All @@ -17,7 +33,3 @@ generate_partitions <- function(p, q_max) {
.Call(`_nn2poly_generate_partitions`, p, q_max)
}

generate_partitions_full <- function(p, q_max) {
.Call(`_nn2poly_generate_partitions_full`, p, q_max)
}

54 changes: 8 additions & 46 deletions R/nn2poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ NULL
#'
#' @param ... Ignored.
#'
#' @param all_partitions Optional argument containing the needed multipartitions
#' as list of lists of lists. If set to \code{NULL}, nn2poly will compute said
#' multipartitions. This step can be computationally expensive when the chosen
#' polynomial order or the dimension are too high. In such cases, it is
#' encouraged that the multipartitions are stored and reused when possible.
#' Default set to \code{NULL}.
#'
#' @return Returns an object of class `nn2poly`.
#'
#' If \code{keep_layers = FALSE} (default case), it returns a list with two
Expand Down Expand Up @@ -121,49 +114,18 @@ nn2poly <- function(object,
max_order = 2,
keep_layers = FALSE,
taylor_orders = 8,
...,
all_partitions = NULL
) {
...) {
UseMethod("nn2poly")
}

#' @export
nn2poly.list <- function(object, ...) {
result_raw <- nn2poly_algorithm(object, names(object), ...)

# Check if object is a single polynomial or a list of polynomials.
# This is controlled by keep_layers.
bool_layers = is.null(result_raw$labels)

if(bool_layers){
n_items <- length(result_raw)
n_layers <- ceiling(n_items/2)
result <- list()
for (i in 1:n_layers){
layer_name <- paste0("layer_", i)
result[[layer_name]] <- list()
result[[layer_name]][["input"]] <- result_raw[[(2*i-1)]]
# Transpose to have polynomials as columns
result[[layer_name]][["input"]][["values"]] <-
t(result[[layer_name]][["input"]][["values"]])
if (2*i <= n_items){
result[[layer_name]][["output"]] <- result_raw[[(2*i)]]
# Transpose to have polynomials as columns
result[[layer_name]][["output"]][["values"]] <-
t(result[[layer_name]][["output"]][["values"]])
} else {
# If there is a linear output, i.e. single polynomial in final layer
# and odd number of items, then we repeat the input as the output, as
# the activation functions takes no effect on the polynomial
result[[layer_name]][["output"]] <- result[[layer_name]][["input"]]
}
}

} else {
result_raw$values <- t(result_raw$values)
result = result_raw
}

nn2poly.list <- function(object,
max_order = 2,
keep_layers = FALSE,
taylor_orders = 8,
...) {
result <- nn2poly_algorithm(object, names(object),
max_order, keep_layers, taylor_orders)
class(result) <- "nn2poly"
result
}
Expand Down
Loading