Skip to content

Commit 16572fb

Browse files
authored
Merge pull request #1306 from rstudio/tf-2.8-compat
TF 2.8 compat
2 parents 6debf31 + 9fcb975 commit 16572fb

36 files changed

+408
-47
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ jobs:
3838
# - {os: 'windows-latest', tf: 'release', r: 'release'}
3939
# - {os: 'macOS-latest' , tf: 'release', r: 'release'}
4040

41+
- {os: 'ubuntu-20.04', tf: '2.7', r: 'release'}
4142
- {os: 'ubuntu-20.04', tf: '2.6', r: 'release'}
4243
- {os: 'ubuntu-20.04', tf: '2.5', r: 'release'}
4344
- {os: 'ubuntu-20.04', tf: '2.4', r: 'release'}
4445
- {os: 'ubuntu-20.04', tf: '2.3', r: 'release'}
45-
- {os: 'ubuntu-20.04', tf: '2.2', r: 'release'}
46+
# - {os: 'ubuntu-20.04', tf: '2.2', r: 'release'}
4647

4748
# these are allowed to fail
4849
# - {os: 'ubuntu-20.04', tf: 'default', r: 'devel'}
4950
# - {os: 'ubuntu-20.04', tf: '2.7.0rc1', r: 'release'}
50-
- {os: 'ubuntu-20.04', tf: 'nightly' , r: 'release'}
51+
# - {os: 'ubuntu-20.04', tf: 'nightly' , r: 'release'}
5152

5253
runs-on: ${{ matrix.os }}
5354
continue-on-error: ${{ matrix.tf == 'nightly' || contains(matrix.tf, 'rc') || matrix.r == 'devel' }}
@@ -57,12 +58,6 @@ jobs:
5758
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
5859

5960
steps:
60-
- name: Configure RSPM
61-
if: runner.os == 'Linux'
62-
shell: bash
63-
run: |
64-
RSPM="https://packagemanager.rstudio.com/all/__linux__/$(lsb_release -cs)/latest"
65-
echo "RSPM=$RSPM" >> $GITHUB_ENV
6661

6762
- uses: actions/checkout@v2
6863

@@ -71,7 +66,7 @@ jobs:
7166
with:
7267
r-version: ${{ matrix.r }}
7368
Ncpus: '2L'
74-
crayon.enabled: 'TRUE'
69+
use-public-rspm: true
7570

7671
- uses: r-lib/actions/setup-pandoc@v2
7772

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ S3method(expm1,keras.engine.keras_tensor.KerasTensor)
3939
S3method(export_savedmodel,keras.engine.training.Model)
4040
S3method(fit,keras.engine.training.Model)
4141
S3method(floor,keras.engine.keras_tensor.KerasTensor)
42+
S3method(format,keras.engine.training.Model)
4243
S3method(length,keras.engine.keras_tensor.KerasTensor)
4344
S3method(lgamma,keras.engine.keras_tensor.KerasTensor)
4445
S3method(log,keras.engine.keras_tensor.KerasTensor)
@@ -48,6 +49,7 @@ S3method(log2,keras.engine.keras_tensor.KerasTensor)
4849
S3method(plot,keras_training_history)
4950
S3method(predict,keras.engine.training.Model)
5051
S3method(print,keras.engine.keras_tensor.KerasTensor)
52+
S3method(print,keras.engine.training.Model)
5153
S3method(print,keras_training_history)
5254
S3method(print,kerastools.model.RModel)
5355
S3method(print,py_R6ClassGenerator)
@@ -405,6 +407,7 @@ export(layer_cudnn_gru)
405407
export(layer_cudnn_lstm)
406408
export(layer_dense)
407409
export(layer_dense_features)
410+
export(layer_depthwise_conv_1d)
408411
export(layer_depthwise_conv_2d)
409412
export(layer_discretization)
410413
export(layer_dot)

NEWS.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
accept a named list for `y`, with names matching to the
2525
names of `outputs` when the model was constructed.
2626

27+
- New layer `layer_depthwise_conv_1d()`.
28+
29+
- Models gain default `format()` and `print()` S3 methods for compatibility
30+
with the latest reticulate. Both are powered by `model$summary()`.
31+
2732
- `keras_model_custom()` is soft deprecated. Please define custom models by
2833
subclassing `keras$Model` directly using `%py_class%` or `R6::R6Class()`.
2934

@@ -35,9 +40,9 @@
3540

3641
- Fixed issue where `create_layer_wrapper()` would not include arguments
3742
with a `NULL` default value in the returned wrapper.
38-
43+
3944
- Fixed issue in `r_to_py.R6ClassGenerator` (and `%py_class%`) where
40-
single-expression `initialize` functions defined without `{` would error.
45+
single-expression `initialize` functions defined without `{` would error.
4146

4247
- Deprecated functions are no longer included in the package documentation index.
4348

R/install.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ install_keras <- function(method = c("auto", "virtualenv", "conda"),
6060
)
6161
}
6262

63-
default_version <- numeric_version("2.7")
63+
default_version <- numeric_version("2.8")
6464

6565
default_extra_packages <- function(tensorflow_version) {
6666
pkgs <- c("tensorflow-hub", "scipy", "requests", "pyyaml", "Pillow", "h5py", "pandas")

R/layers-convolutional.R

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,123 @@ layer_separable_conv_2d <- function(object, filters, kernel_size, strides = c(1,
650650

651651
}
652652

653+
654+
#' Depthwise 1D convolution
655+
#'
656+
#' @details
657+
#' Depthwise convolution is a type of convolution in which each input channel is
658+
#' convolved with a different kernel (called a depthwise kernel). You
659+
#' can understand depthwise convolution as the first step in a depthwise
660+
#' separable convolution.
661+
#'
662+
#' It is implemented via the following steps:
663+
#'
664+
#' - Split the input into individual channels.
665+
#' - Convolve each channel with an individual depthwise kernel with
666+
#' `depth_multiplier` output channels.
667+
#' - Concatenate the convolved outputs along the channels axis.
668+
#'
669+
#' Unlike a regular 1D convolution, depthwise convolution does not mix
670+
#' information across different input channels.
671+
#'
672+
#' The `depth_multiplier` argument determines how many filter are applied to one
673+
#' input channel. As such, it controls the amount of output channels that are
674+
#' generated per input channel in the depthwise step.
675+
#'
676+
#' @param kernel_size An integer, specifying the height and width of the 1D
677+
#' convolution window. Can be a single integer to specify the same value for
678+
#' all spatial dimensions.
679+
#'
680+
#' @param strides An integer, specifying the strides of the convolution along the
681+
#' height and width. Can be a single integer to specify the same value for
682+
#' all spatial dimensions. Specifying any stride value != 1 is incompatible
683+
#' with specifying any `dilation_rate` value != 1.
684+
#'
685+
#' @param padding one of `'valid'` or `'same'` (case-insensitive). `"valid"` means no
686+
#' padding. `"same"` results in padding with zeros evenly to the left/right
687+
#' or up/down of the input such that output has the same height/width
688+
#' dimension as the input.
689+
#'
690+
#' @param depth_multiplier The number of depthwise convolution output channels for
691+
#' each input channel. The total number of depthwise convolution output
692+
#' channels will be equal to `filters_in * depth_multiplier`.
693+
#'
694+
#' @param data_format A string, one of `"channels_last"` (default) or `"channels_first"`.
695+
#' The ordering of the dimensions in the inputs. `channels_last` corresponds
696+
#' to inputs with shape `(batch_size, height, width, channels)` while
697+
#' `channels_first` corresponds to inputs with shape `(batch_size, channels,
698+
#' height, width)`. It defaults to the `image_data_format` value found in
699+
#' your Keras config file at `~/.keras/keras.json`. If you never set it, then
700+
#' it will be 'channels_last'.
701+
#'
702+
#' @param dilation_rate A single integer, specifying the dilation rate to use for
703+
#' dilated convolution. Currently, specifying any `dilation_rate` value != 1
704+
#' is incompatible with specifying any stride value != 1.
705+
#'
706+
#' @param activation Activation function to use. If you don't specify anything, no
707+
#' activation is applied (see `?activation_relu`).
708+
#'
709+
#' @param use_bias Boolean, whether the layer uses a bias vector.
710+
#'
711+
#' @param depthwise_initializer Initializer for the depthwise kernel matrix (see
712+
#' [`initializer_glorot_uniform`]). If NULL, the default initializer
713+
#' (`"glorot_uniform"`) will be used.
714+
#'
715+
#' @param bias_initializer Initializer for the bias vector (see
716+
#' `keras.initializers`). If NULL, the default initializer ('zeros') will be
717+
#' used.
718+
#'
719+
#' @param depthwise_regularizer Regularizer function applied to the depthwise kernel
720+
#' matrix (see [`regularizer_l1()`]).
721+
#'
722+
#' @param bias_regularizer Regularizer function applied to the bias vector (see
723+
#' [`regularizer_l1()`]).
724+
#'
725+
#' @param activity_regularizer Regularizer function applied to the output of the
726+
#' layer (its 'activation') (see [`regularizer_l1()`]).
727+
#'
728+
#' @param depthwise_constraint Constraint function applied to the depthwise kernel
729+
#' matrix (see [`constraint_maxnorm()`]).
730+
#'
731+
#' @param bias_constraint Constraint function applied to the bias vector (see
732+
#' [`constraint_maxnorm()`]).
733+
#'
734+
#' @param ... standard layer arguments.
735+
#'
736+
#' @inheritParams layer_depthwise_conv_2d
737+
#' @family convolutional layers
738+
#'
739+
#' @seealso
740+
#' + <https://www.tensorflow.org/api_docs/python/tf/keras/layers/DepthwiseConv1D>
741+
#' @export
742+
layer_depthwise_conv_1d <-
743+
function(object,
744+
kernel_size,
745+
strides = 1L,
746+
padding = "valid",
747+
depth_multiplier = 1L,
748+
data_format = NULL,
749+
dilation_rate = 1L,
750+
activation = NULL,
751+
use_bias = TRUE,
752+
depthwise_initializer = "glorot_uniform",
753+
bias_initializer = "zeros",
754+
depthwise_regularizer = NULL,
755+
bias_regularizer = NULL,
756+
activity_regularizer = NULL,
757+
depthwise_constraint = NULL,
758+
bias_constraint = NULL,
759+
...) {
760+
args <- capture_args(match.call(),
761+
list(kernel_size = as.integer,
762+
strides = as.integer,
763+
depth_multiplier = as.integer,
764+
dilation_rate = as.integer),
765+
ignore = "object"
766+
)
767+
create_layer(keras$layers$DepthwiseConv1D, object, args)
768+
}
769+
653770
#' Depthwise separable 2D convolution.
654771
#'
655772
#' Depthwise Separable convolutions consists in performing just the first step

R/model.R

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,34 +1424,67 @@ pop_layer <- function(object) {
14241424

14251425
#' Print a summary of a Keras model
14261426
#'
1427-
#' @param object Keras model instance
1427+
#' @param object,x Keras model instance
14281428
#' @param line_length Total length of printed lines
14291429
#' @param positions Relative or absolute positions of log elements in each line.
14301430
#' If not provided, defaults to `c(0.33, 0.55, 0.67, 1.0)`.
1431-
#' @param ... Unused
1431+
#' @param expand_nested Whether to expand the nested models. If not provided,
1432+
#' defaults to `FALSE`.
1433+
#' @param show_trainable Whether to show if a layer is trainable. If not
1434+
#' provided, defaults to `FALSE`.
1435+
#' @param ... for `summary()` and `print()`, passed on to `format()`. For
1436+
#' `format()`, passed on to `model$summary()`.
14321437
#'
14331438
#' @family model functions
14341439
#'
1440+
#' @return `format()` returns a length 1 character vector. `print()` returns the
1441+
#' model object invisibly. `summary()` returns the output of `format()`
1442+
#' invisibly after printing it.
1443+
#'
14351444
#' @export
1436-
summary.keras.engine.training.Model <- function(object, line_length = getOption("width"), positions = NULL, ...) {
1437-
if (py_is_null_xptr(object))
1438-
cat("<pointer: 0x0>\n")
1439-
else {
1440-
if (keras_version() >= "2.0.6")
1441-
object$summary(line_length = getOption("width"), print_fn = function(object) cat(object, "\n", sep = ""))
1442-
else
1443-
cat(py_str(object, line_length = line_length, positions = positions), "\n")
1444-
}
1445+
summary.keras.engine.training.Model <- function(object, ...) {
1446+
writeLines(f <- format.keras.engine.training.Model(object, ...))
1447+
invisible(f)
1448+
}
1449+
1450+
#' @rdname summary.keras.engine.training.Model
1451+
#' @export
1452+
format.keras.engine.training.Model <-
1453+
function(x,
1454+
line_length = getOption("width"),
1455+
positions = NULL,
1456+
expand_nested = FALSE,
1457+
show_trainable = FALSE,
1458+
...) {
1459+
if (py_is_null_xptr(x))
1460+
return("<pointer: 0x0>")
1461+
1462+
args <- capture_args(match.call(), ignore = "x")
1463+
1464+
# ensure `line_length` in args, even if not passed by user
1465+
args$line_length <- as_nullable_integer(line_length)
1466+
1467+
if (x$built)
1468+
trimws(py_capture_output(do.call(x$summary, args),
1469+
type = "stdout"))
1470+
else
1471+
"Model: <no summary available, model was not built>"
1472+
}
1473+
1474+
#
1475+
#' @rdname summary.keras.engine.training.Model
1476+
#' @export
1477+
print.keras.engine.training.Model <- function(x, ...) {
1478+
writeLines(format.keras.engine.training.Model(x, ...))
1479+
invisible(x)
14451480
}
14461481

14471482
#' @importFrom reticulate py_str
14481483
#' @export
1449-
py_str.keras.engine.training.Model <- function(object, line_length = getOption("width"), positions = NULL, ...) {
1450-
if (!object$built) {
1451-
cat("Model\n<no summary available, model was not built>\n")
1452-
} else {
1453-
paste0("Model\n", py_capture_output(object$summary(line_length = line_length, positions = positions), type = "stdout"))
1454-
}
1484+
py_str.keras.engine.training.Model <- function(object, line_length = getOption("width"), positions = NULL, ...) {
1485+
# still invoked by utils::str()
1486+
# warning("`py_str()` generic is deprecated")
1487+
format.keras.engine.training.Model(object, line_length = line_length, positions = positions, ...)
14551488
}
14561489

14571490

man/install_keras.Rd

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_conv_1d.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_conv_1d_transpose.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_conv_2d.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)