From 210478add28d001a91c017ed0ff6ecb4a7f476c4 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Sun, 5 May 2024 11:45:07 -0400 Subject: [PATCH] Force first arg in `layer_` funcs before creating layer closes #1440 --- NEWS.md | 3 +++ R/layer-r-helpers.R | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 676365de4..d14383c56 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # keras3 (development version) +- Chains of `layer_*` calls with `|>` now instantiate layers in the + same order as `%>%` pipe chains: left-hand-side first (#1440). + User facing changes with upstream Keras v3.3.2: - new function: `op_ctc_decode()` diff --git a/R/layer-r-helpers.R b/R/layer-r-helpers.R index 0e0b31349..18cb3c313 100644 --- a/R/layer-r-helpers.R +++ b/R/layer-r-helpers.R @@ -20,6 +20,11 @@ #' @noRd create_layer <- function(LayerClass, object, args = list()) { + # force `object` before instantiating the layer, so pipe chains create layers + # in the the intutitively expected order. + # https://github.com/rstudio/keras/issues/1440 + object <- if (missing(object)) NULL else object + # Starting in Keras 3.1, constraints can't be simple callable functions, they # *must* inherit from keras.constraints.Constraint() args <- imap(args, function(arg, name) { @@ -37,7 +42,7 @@ create_layer <- function(LayerClass, object, args = list()) { layer <- do.call(LayerClass, args) # compose if we have an `object` - if (missing(object) || is.null(object)) + if (is.null(object)) layer else invisible(compose_layer(object, layer))