Title: | R Interface to 'Keras' |
---|---|
Description: | Interface to 'Keras' <https://keras.io>, a high-level neural networks 'API'. 'Keras' was developed with a focus on enabling fast experimentation, supports both convolution based networks and recurrent networks (as well as combinations of the two), and runs seamlessly on both 'CPU' and 'GPU' devices. |
Authors: | Tomasz Kalinowski [ctb, cph, cre], Daniel Falbel [ctb, cph], JJ Allaire [aut, cph], François Chollet [aut, cph], RStudio [ctb, cph, fnd], Google [ctb, cph, fnd], Yuan Tang [ctb, cph] , Wouter Van Der Bijl [ctb, cph], Martin Studer [ctb, cph], Sigrid Keydana [ctb] |
Maintainer: | Tomasz Kalinowski <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.15.0 |
Built: | 2024-11-17 04:41:08 UTC |
Source: | https://github.com/cran/keras |
Keras is a high-level neural networks API, developed with a focus on enabling fast experimentation. Keras has the following key features:
Allows the same code to run on CPU or on GPU, seamlessly.
User-friendly API which makes it easy to quickly prototype deep learning models.
Built-in support for convolutional networks (for computer vision), recurrent networks (for sequence processing), and any combination of both.
Supports arbitrary network architectures: multi-input or multi-output models, layer sharing, model sharing, etc. This means that Keras is appropriate for building essentially any deep learning model, from a memory network to a neural Turing machine.
Is capable of running on top of multiple back-ends including TensorFlow, CNTK, or Theano.
See the package website at https://tensorflow.rstudio.com for complete documentation.
Maintainer: Tomasz Kalinowski [email protected] [contributor, copyright holder]
Authors:
JJ Allaire [copyright holder]
François Chollet [copyright holder]
Other contributors:
Daniel Falbel [email protected] [contributor, copyright holder]
RStudio [contributor, copyright holder, funder]
Google [contributor, copyright holder, funder]
Yuan Tang [email protected] (ORCID) [contributor, copyright holder]
Wouter Van Der Bijl [contributor, copyright holder]
Martin Studer [contributor, copyright holder]
Sigrid Keydana [contributor]
Useful links:
Report bugs at https://github.com/rstudio/keras/issues
Make an Active Binding
sym %<-active% value
sym %<-active% value
sym |
symbol to bind |
value |
A function to call when the value of |
Active bindings defined in a %py_class%
are converted to
@property
decorated methods.
value
, invisibly
set.seed(1234) x %<-active% function(value) { message("Evaluating function of active binding") if(missing(value)) runif(1) else message("Received: ", value) } x x x <- "foo" x <- "foo" x rm(x) # cleanup
set.seed(1234) x %<-active% function(value) { message("Evaluating function of active binding") if(missing(value)) runif(1) else message("Received: ", value) } x x x <- "foo" x <- "foo" x rm(x) # cleanup
Make a python class constructor
spec %py_class% body
spec %py_class% body
spec |
a bare symbol |
body |
an expression that can be evaluated to construct the class methods. |
The python class constructor, invisibly. Note, the same constructor is also assigned in the parent frame.
## Not run: MyClass %py_class% { initialize <- function(x) { print("Hi from MyClass$initialize()!") self$x <- x } my_method <- function() { self$x } } my_class_instance <- MyClass(42) my_class_instance$my_method() MyClass2(MyClass) %py_class% { "This will be a __doc__ string for MyClass2" initialize <- function(...) { "This will be the __doc__ string for the MyClass2.__init__() method" print("Hi from MyClass2$initialize()!") super$initialize(...) } } my_class_instance2 <- MyClass2(42) my_class_instance2$my_method() reticulate::py_help(MyClass2) # see the __doc__ strings and more! # In addition to `self`, there is also `private` available. # This is an R environment unique to each class instance, where you can # store objects that you don't want converted to Python, but still want # available from methods. You can also assign methods to private, and # `self` and `private` will be available in private methods. MyClass %py_class% { initialize <- function(x) { print("Hi from MyClass$initialize()!") private$y <- paste("A Private field:", x) } get_private_field <- function() { private$y } private$a_private_method <- function() { cat("a_private_method() was called.\n") cat("private$y is ", sQuote(private$y), "\n") } call_private_method <- function() private$a_private_method() # equivalent of @property decorator in python an_active_property %<-active% function(x = NULL) { if(!is.null(x)) { cat("`an_active_property` was assigned", x, "\n") return(x) } else { cat("`an_active_property` was accessed\n") return(42) } } } inst1 <- MyClass(1) inst2 <- MyClass(2) inst1$get_private_field() inst2$get_private_field() inst1$call_private_method() inst2$call_private_method() inst1$an_active_property inst1$an_active_property <- 11 ## End(Not run)
## Not run: MyClass %py_class% { initialize <- function(x) { print("Hi from MyClass$initialize()!") self$x <- x } my_method <- function() { self$x } } my_class_instance <- MyClass(42) my_class_instance$my_method() MyClass2(MyClass) %py_class% { "This will be a __doc__ string for MyClass2" initialize <- function(...) { "This will be the __doc__ string for the MyClass2.__init__() method" print("Hi from MyClass2$initialize()!") super$initialize(...) } } my_class_instance2 <- MyClass2(42) my_class_instance2$my_method() reticulate::py_help(MyClass2) # see the __doc__ strings and more! # In addition to `self`, there is also `private` available. # This is an R environment unique to each class instance, where you can # store objects that you don't want converted to Python, but still want # available from methods. You can also assign methods to private, and # `self` and `private` will be available in private methods. MyClass %py_class% { initialize <- function(x) { print("Hi from MyClass$initialize()!") private$y <- paste("A Private field:", x) } get_private_field <- function() { private$y } private$a_private_method <- function() { cat("a_private_method() was called.\n") cat("private$y is ", sQuote(private$y), "\n") } call_private_method <- function() private$a_private_method() # equivalent of @property decorator in python an_active_property %<-active% function(x = NULL) { if(!is.null(x)) { cat("`an_active_property` was assigned", x, "\n") return(x) } else { cat("`an_active_property` was accessed\n") return(42) } } } inst1 <- MyClass(1) inst2 <- MyClass(2) inst1$get_private_field() inst2$get_private_field() inst1$call_private_method() inst2$call_private_method() inst1$an_active_property inst1$an_active_property <- 11 ## End(Not run)
relu(...)
: Applies the rectified linear unit activation function.
elu(...)
: Exponential Linear Unit.
selu(...)
: Scaled Exponential Linear Unit (SELU).
hard_sigmoid(...)
: Hard sigmoid activation function.
linear(...)
: Linear activation function (pass-through).
sigmoid(...)
: Sigmoid activation function, sigmoid(x) = 1 / (1 + exp(-x))
.
softmax(...)
: Softmax converts a vector of values to a probability distribution.
softplus(...)
: Softplus activation function, softplus(x) = log(exp(x) + 1)
.
softsign(...)
: Softsign activation function, softsign(x) = x / (abs(x) + 1)
.
tanh(...)
: Hyperbolic tangent activation function.
exponential(...)
: Exponential activation function.
gelu(...)
: Applies the Gaussian error linear unit (GELU) activation function.
swish(...)
: Swish activation function, swish(x) = x * sigmoid(x)
.
activation_relu(x, alpha = 0, max_value = NULL, threshold = 0) activation_elu(x, alpha = 1) activation_selu(x) activation_hard_sigmoid(x) activation_linear(x) activation_sigmoid(x) activation_softmax(x, axis = -1) activation_softplus(x) activation_softsign(x) activation_tanh(x) activation_exponential(x) activation_gelu(x, approximate = FALSE) activation_swish(x)
activation_relu(x, alpha = 0, max_value = NULL, threshold = 0) activation_elu(x, alpha = 1) activation_selu(x) activation_hard_sigmoid(x) activation_linear(x) activation_sigmoid(x) activation_softmax(x, axis = -1) activation_softplus(x) activation_softsign(x) activation_tanh(x) activation_exponential(x) activation_gelu(x, approximate = FALSE) activation_swish(x)
x |
Tensor |
alpha |
Alpha value |
max_value |
Max value |
threshold |
Threshold value for thresholded activation. |
axis |
Integer, axis along which the softmax normalization is applied |
approximate |
A bool, whether to enable approximation. |
Activations functions can either be used through layer_activation()
, or
through the activation argument supported by all forward layers.
activation_selu()
to be used together with the initialization "lecun_normal".
activation_selu()
to be used together with the dropout variant "AlphaDropout".
Tensor with the same shape and dtype as x
.
activation_swish()
: Searching for Activation Functions
activation_gelu()
: Gaussian Error Linear Units (GELUs)
activation_selu()
: Self-Normalizing Neural Networks
activation_elu()
: Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)
https://www.tensorflow.org/api_docs/python/tf/keras/activations
Fits the state of the preprocessing layer to the data being passed
adapt(object, data, ..., batch_size = NULL, steps = NULL)
adapt(object, data, ..., batch_size = NULL, steps = NULL)
object |
Preprocessing layer object |
data |
The data to train on. It can be passed either as a
|
... |
Used for forwards and backwards compatibility. Passed on to the underlying method. |
batch_size |
Integer or |
steps |
Integer or |
After calling adapt
on a layer, a preprocessing layer's state will not
update during training. In order to make preprocessing layers efficient in
any distribution context, they are kept constant with respect to any
compiled tf.Graph
s that call the layer. This does not affect the layer use
when adapting each layer only once, but if you adapt a layer multiple times
you will need to take care to re-compile any compiled functions as follows:
If you are adding a preprocessing layer to a keras.Model
, you need to
call compile(model)
after each subsequent call to adapt()
.
If you are calling a preprocessing layer inside tfdatasets::dataset_map()
,
you should call dataset_map()
again on the input tf.data.Dataset
after each
adapt()
.
If you are using a tensorflow::tf_function()
directly which calls a preprocessing
layer, you need to call tf_function
again on your callable after
each subsequent call to adapt()
.
keras_model
example with multiple adapts:
layer <- layer_normalization(axis=NULL) adapt(layer, c(0, 2)) model <- keras_model_sequential(layer) predict(model, c(0, 1, 2)) # [1] -1 0 1 adapt(layer, c(-1, 1)) compile(model) # This is needed to re-compile model.predict! predict(model, c(0, 1, 2)) # [1] 0 1 2
tf.data.Dataset
example with multiple adapts:
layer <- layer_normalization(axis=NULL) adapt(layer, c(0, 2)) input_ds <- tfdatasets::range_dataset(0, 3) normalized_ds <- input_ds %>% tfdatasets::dataset_map(layer) str(reticulate::iterate(normalized_ds)) # List of 3 # $ :tf.Tensor([-1.], shape=(1,), dtype=float32) # $ :tf.Tensor([0.], shape=(1,), dtype=float32) # $ :tf.Tensor([1.], shape=(1,), dtype=float32) adapt(layer, c(-1, 1)) normalized_ds <- input_ds %>% tfdatasets::dataset_map(layer) # Re-map over the input dataset. str(reticulate::iterate(normalized_ds$as_numpy_iterator())) # List of 3 # $ : num [1(1d)] -1 # $ : num [1(1d)] 0 # $ : num [1(1d)] 1
Instantiates the DenseNet architecture.
application_densenet( blocks, include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet121( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet169( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet201( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) densenet_preprocess_input(x, data_format = NULL)
application_densenet( blocks, include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet121( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet169( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) application_densenet201( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000 ) densenet_preprocess_input(x, data_format = NULL)
blocks |
numbers of building blocks for the four dense layers. |
include_top |
whether to include the fully-connected layer at the top of the network. |
weights |
one of |
input_tensor |
optional Keras tensor (i.e. output of |
input_shape |
optional shape list, only to be specified if |
pooling |
optional pooling mode for feature extraction when
|
classes |
optional number of classes to classify images into, only to be
specified if |
x |
a 3D or 4D array consists of RGB values within |
data_format |
data format of the image tensor. |
Optionally loads weights pre-trained
on ImageNet. Note that when using TensorFlow,
for best performance you should set
image_data_format='channels_last'
in your Keras config
at ~/.keras/keras.json.
The model and the weights are compatible with TensorFlow, Theano, and CNTK. The data format convention used by the model is the one specified in your Keras config file.
Instantiates the EfficientNetB0 architecture
application_efficientnet_b0( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b1( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b3( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b4( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b5( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b6( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b7( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... )
application_efficientnet_b0( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b1( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b3( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b4( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b5( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b6( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) application_efficientnet_b7( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... )
include_top |
Whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
Optional shape list, only to be specified
if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
Reference:
This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet.
For image classification use cases, see this page for detailed examples.
For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning.
EfficientNet models expect their inputs to be float tensors of pixels with values in the [0-255]
range.
Each Keras Application typically expects a specific kind of input preprocessing.
For EfficientNet, input preprocessing is included as part of the model
(as a Rescaling
layer), and thus a calling a preprocessing function is not necessary.
Inception-ResNet v2 model, with weights trained on ImageNet
application_inception_resnet_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) inception_resnet_v2_preprocess_input(x)
application_inception_resnet_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) inception_resnet_v2_preprocess_input(x)
include_top |
Whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
optional shape list, only to be specified
if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
x |
|
Do note that the input image format for this model is different than for the VGG16 and ResNet models (299x299 instead of 224x224).
The inception_resnet_v2_preprocess_input()
function should be used for image
preprocessing.
A Keras model instance.
Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning(https://arxiv.org/abs/1512.00567)
Inception V3 model, with weights pre-trained on ImageNet.
application_inception_v3( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) inception_v3_preprocess_input(x)
application_inception_v3( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) inception_v3_preprocess_input(x)
include_top |
Whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
optional shape list, only to be specified
if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
x |
|
Do note that the input image format for this model is different than for the VGG16 and ResNet models (299x299 instead of 224x224).
The inception_v3_preprocess_input()
function should be used for image
preprocessing.
A Keras model instance.
MobileNet model architecture.
application_mobilenet( input_shape = NULL, alpha = 1, depth_multiplier = 1L, dropout = 0.001, include_top = TRUE, weights = "imagenet", input_tensor = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) mobilenet_preprocess_input(x) mobilenet_decode_predictions(preds, top = 5) mobilenet_load_model_hdf5(filepath)
application_mobilenet( input_shape = NULL, alpha = 1, depth_multiplier = 1L, dropout = 0.001, include_top = TRUE, weights = "imagenet", input_tensor = NULL, pooling = NULL, classes = 1000L, classifier_activation = "softmax", ... ) mobilenet_preprocess_input(x) mobilenet_decode_predictions(preds, top = 5) mobilenet_load_model_hdf5(filepath)
input_shape |
optional shape list, only to be specified if |
alpha |
controls the width of the network.
|
depth_multiplier |
depth multiplier for depthwise convolution (also called the resolution multiplier) |
dropout |
dropout rate |
include_top |
whether to include the fully-connected layer at the top of the network. |
weights |
|
input_tensor |
optional Keras tensor (i.e. output of |
pooling |
Optional pooling mode for feature extraction when
|
classes |
optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
x |
input tensor, 4D |
preds |
Tensor encoding a batch of predictions. |
top |
integer, how many top-guesses to return. |
filepath |
File path |
The mobilenet_preprocess_input()
function should be used for image
preprocessing. To load a saved instance of a MobileNet model use
the mobilenet_load_model_hdf5()
function. To prepare image input
for MobileNet use mobilenet_preprocess_input()
. To decode
predictions use mobilenet_decode_predictions()
.
application_mobilenet()
and mobilenet_load_model_hdf5()
return a
Keras model instance. mobilenet_preprocess_input()
returns image input
suitable for feeding into a mobilenet model. mobilenet_decode_predictions()
returns a list of data frames with variables class_name
, class_description
,
and score
(one data frame per sample in batch input).
MobileNetV2 model architecture
application_mobilenet_v2( input_shape = NULL, alpha = 1, include_top = TRUE, weights = "imagenet", input_tensor = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) mobilenet_v2_preprocess_input(x) mobilenet_v2_decode_predictions(preds, top = 5) mobilenet_v2_load_model_hdf5(filepath)
application_mobilenet_v2( input_shape = NULL, alpha = 1, include_top = TRUE, weights = "imagenet", input_tensor = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) mobilenet_v2_preprocess_input(x) mobilenet_v2_decode_predictions(preds, top = 5) mobilenet_v2_load_model_hdf5(filepath)
input_shape |
optional shape list, only to be specified if |
alpha |
controls the width of the network.
|
include_top |
whether to include the fully-connected layer at the top of the network. |
weights |
|
input_tensor |
optional Keras tensor (i.e. output of |
pooling |
Optional pooling mode for feature extraction when
|
classes |
optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
x |
input tensor, 4D |
preds |
Tensor encoding a batch of predictions. |
top |
integer, how many top-guesses to return. |
filepath |
File path |
application_mobilenet_v2()
and mobilenet_v2_load_model_hdf5()
return a
Keras model instance. mobilenet_v2_preprocess_input()
returns image input
suitable for feeding into a mobilenet v2 model. mobilenet_v2_decode_predictions()
returns a list of data frames with variables class_name
, class_description
,
and score
(one data frame per sample in batch input).
application_mobilenet
Instantiates the MobileNetV3Large architecture
application_mobilenet_v3_large( input_shape = NULL, alpha = 1, minimalistic = FALSE, include_top = TRUE, weights = "imagenet", input_tensor = NULL, classes = 1000L, pooling = NULL, dropout_rate = 0.2, classifier_activation = "softmax", include_preprocessing = TRUE ) application_mobilenet_v3_small( input_shape = NULL, alpha = 1, minimalistic = FALSE, include_top = TRUE, weights = "imagenet", input_tensor = NULL, classes = 1000L, pooling = NULL, dropout_rate = 0.2, classifier_activation = "softmax", include_preprocessing = TRUE )
application_mobilenet_v3_large( input_shape = NULL, alpha = 1, minimalistic = FALSE, include_top = TRUE, weights = "imagenet", input_tensor = NULL, classes = 1000L, pooling = NULL, dropout_rate = 0.2, classifier_activation = "softmax", include_preprocessing = TRUE ) application_mobilenet_v3_small( input_shape = NULL, alpha = 1, minimalistic = FALSE, include_top = TRUE, weights = "imagenet", input_tensor = NULL, classes = 1000L, pooling = NULL, dropout_rate = 0.2, classifier_activation = "softmax", include_preprocessing = TRUE )
input_shape |
Optional shape vector, to be specified if you would
like to use a model with an input image resolution that is not
|
alpha |
controls the width of the network. This is known as the depth multiplier in the MobileNetV3 paper, but the name is kept for consistency with MobileNetV1 in Keras.
|
minimalistic |
In addition to large and small models this module also contains so-called minimalistic models, these models have the same per-layer dimensions characteristic as MobilenetV3 however, they don't utilize any of the advanced blocks (squeeze-and-excite units, hard-swish, and 5x5 convolutions). While these models are less efficient on CPU, they are much more performant on GPU/DSP. |
include_top |
Boolean, whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
String, one of |
input_tensor |
Optional Keras tensor (i.e. output of
|
classes |
Integer, optional number of classes to classify images
into, only to be specified if |
pooling |
String, optional pooling mode for feature extraction
when
|
dropout_rate |
fraction of the input units to drop on the last layer. |
classifier_activation |
A string or callable. The activation function to use
on the "top" layer. Ignored unless |
include_preprocessing |
Boolean, whether to include the preprocessing
layer ( |
Reference:
Searching for MobileNetV3 (ICCV 2019)
MACs stands for Multiply Adds
Classification Checkpoint | MACs(M) | Parameters(M) | Top1 Accuracy | Pixel1 CPU(ms) |
mobilenet_v3_large_1.0_224 | 217 | 5.4 | 75.6 | 51.2 |
mobilenet_v3_large_0.75_224 | 155 | 4.0 | 73.3 | 39.8 |
mobilenet_v3_large_minimalistic_1.0_224 | 209 | 3.9 | 72.3 | 44.1 |
mobilenet_v3_small_1.0_224 | 66 | 2.9 | 68.1 | 15.8 |
mobilenet_v3_small_0.75_224 | 44 | 2.4 | 65.4 | 12.8 |
mobilenet_v3_small_minimalistic_1.0_224 | 65 | 2.0 | 61.9 | 12.2 |
For image classification use cases, see this page for detailed examples.
For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning.
A keras Model
instance
Each Keras application typically expects a specific kind of input preprocessing.
For ModelNetV3, by default input preprocessing is included as a part of the
model (as a Rescaling
layer), and thus
a preprocessing function is not necessary. In this use case, ModelNetV3 models expect their inputs
to be float tensors of pixels with values in the [0-255]
range.
At the same time, preprocessing as a part of the model (i.e. Rescaling
layer) can be disabled by setting include_preprocessing
argument to FALSE.
With preprocessing disabled ModelNetV3 models expect their inputs to be float
tensors of pixels with values in the [-1, 1]
range.
https://www.tensorflow.org/api_docs/python/tf/keras/applications/MobileNetV3Large
https://www.tensorflow.org/api_docs/python/tf/keras/applications/MobileNetV3Small
Note that only TensorFlow is supported for now,
therefore it only works with the data format
image_data_format='channels_last'
in your Keras config
at ~/.keras/keras.json
.
application_nasnet( input_shape = NULL, penultimate_filters = 4032L, num_blocks = 6L, stem_block_filters = 96L, skip_reduction = TRUE, filter_multiplier = 2L, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000, default_size = NULL ) application_nasnetlarge( input_shape = NULL, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000 ) application_nasnetmobile( input_shape = NULL, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000 ) nasnet_preprocess_input(x)
application_nasnet( input_shape = NULL, penultimate_filters = 4032L, num_blocks = 6L, stem_block_filters = 96L, skip_reduction = TRUE, filter_multiplier = 2L, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000, default_size = NULL ) application_nasnetlarge( input_shape = NULL, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000 ) application_nasnetmobile( input_shape = NULL, include_top = TRUE, weights = NULL, input_tensor = NULL, pooling = NULL, classes = 1000 ) nasnet_preprocess_input(x)
input_shape |
Optional shape list, the input shape is by default |
penultimate_filters |
Number of filters in the penultimate layer.
NASNet models use the notation |
num_blocks |
Number of repeated blocks of the NASNet model. NASNet
models use the notation |
stem_block_filters |
Number of filters in the initial stem block |
skip_reduction |
Whether to skip the reduction step at the tail end
of the network. Set to |
filter_multiplier |
Controls the width of the network.
|
include_top |
Whether to include the fully-connected layer at the top of the network. |
weights |
|
input_tensor |
Optional Keras tensor (i.e. output of |
pooling |
Optional pooling mode for feature extraction when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
default_size |
Specifies the default image size of the model |
x |
a 4D array consists of RGB values within |
Instantiates the ResNet architecture
application_resnet50( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet101( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet152( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet50_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) application_resnet101_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) application_resnet152_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) resnet_preprocess_input(x) resnet_v2_preprocess_input(x)
application_resnet50( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet101( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet152( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, ... ) application_resnet50_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) application_resnet101_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) application_resnet152_v2( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) resnet_preprocess_input(x) resnet_v2_preprocess_input(x)
include_top |
Whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
optional shape list, only to be specified
if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
... |
For backwards and forwards compatibility |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
x |
|
Reference:
Deep Residual Learning for Image Recognition (CVPR 2015)
For image classification use cases, see this page for detailed examples.
For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning.
Note: each Keras Application expects a specific kind of input preprocessing.
For ResNet, call tf.keras.applications.resnet.preprocess_input
on your
inputs before passing them to the model.
resnet.preprocess_input
will convert the input images from RGB to BGR,
then will zero-center each color channel with respect to the ImageNet dataset,
without scaling.
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet50/ResNet50
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet/ResNet101
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet/ResNet152
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet_v2/ResNet50V2
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet_v2/ResNet101V2
https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet_v2/ResNet152V2
## Not run: library(keras) # instantiate the model model <- application_resnet50(weights = 'imagenet') # load the image img_path <- "elephant.jpg" img <- image_load(img_path, target_size = c(224,224)) x <- image_to_array(img) # ensure we have a 4d tensor with single element in the batch dimension, # the preprocess the input for prediction using resnet50 x <- array_reshape(x, c(1, dim(x))) x <- imagenet_preprocess_input(x) # make predictions then decode and print them preds <- model %>% predict(x) imagenet_decode_predictions(preds, top = 3)[[1]] ## End(Not run)
## Not run: library(keras) # instantiate the model model <- application_resnet50(weights = 'imagenet') # load the image img_path <- "elephant.jpg" img <- image_load(img_path, target_size = c(224,224)) x <- image_to_array(img) # ensure we have a 4d tensor with single element in the batch dimension, # the preprocess the input for prediction using resnet50 x <- array_reshape(x, c(1, dim(x))) x <- imagenet_preprocess_input(x) # make predictions then decode and print them preds <- model %>% predict(x) imagenet_decode_predictions(preds, top = 3)[[1]] ## End(Not run)
VGG16 and VGG19 models for Keras.
application_vgg16( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax" ) application_vgg19( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax" )
application_vgg16( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax" ) application_vgg19( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax" )
include_top |
whether to include the 3 fully-connected layers at the top of the network. |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
optional shape list, only to be specified if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
Optionally loads weights pre-trained on ImageNet.
The imagenet_preprocess_input()
function should be used for image preprocessing.
Keras model instance.
- Very Deep Convolutional Networks for Large-Scale Image Recognition
## Not run: library(keras) model <- application_vgg16(weights = 'imagenet', include_top = FALSE) img_path <- "elephant.jpg" img <- image_load(img_path, target_size = c(224,224)) x <- image_to_array(img) x <- array_reshape(x, c(1, dim(x))) x <- imagenet_preprocess_input(x) features <- model %>% predict(x) ## End(Not run)
## Not run: library(keras) model <- application_vgg16(weights = 'imagenet', include_top = FALSE) img_path <- "elephant.jpg" img <- image_load(img_path, target_size = c(224,224)) x <- image_to_array(img) x <- array_reshape(x, c(1, dim(x))) x <- imagenet_preprocess_input(x) features <- model %>% predict(x) ## End(Not run)
Instantiates the Xception architecture
application_xception( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) xception_preprocess_input(x)
application_xception( include_top = TRUE, weights = "imagenet", input_tensor = NULL, input_shape = NULL, pooling = NULL, classes = 1000, classifier_activation = "softmax", ... ) xception_preprocess_input(x)
include_top |
Whether to include the fully-connected
layer at the top of the network. Defaults to |
weights |
One of |
input_tensor |
Optional Keras tensor
(i.e. output of |
input_shape |
optional shape list, only to be specified
if |
pooling |
Optional pooling mode for feature extraction
when
|
classes |
Optional number of classes to classify images into, only to be
specified if |
classifier_activation |
A string or callable. The activation function to
use on the "top" layer. Ignored unless |
... |
For backwards and forwards compatibility |
x |
|
For image classification use cases, see this page for detailed examples.
For transfer learning use cases, make sure to read the guide to transfer learning & fine-tuning.
The default input image size for this model is 299x299.
Each Keras Application typically expects a specific kind of input preprocessing.
For Xception, call xception_preprocess_input()
on your
inputs before passing them to the model.
xception_preprocess_input()
will scale input pixels between -1 and 1.
Obtain a reference to the keras.backend
Python module used to implement
tensor operations.
backend(convert = TRUE)
backend(convert = TRUE)
convert |
Boolean; should Python objects be automatically converted
to their R equivalent? If set to |
Reference to Keras backend python module.
See the documentation here https://keras.io/backend/ for additional details on the available functions.
Bidirectional wrapper for RNNs
bidirectional( object, layer, merge_mode = "concat", weights = NULL, backward_layer = NULL, ... )
bidirectional( object, layer, merge_mode = "concat", weights = NULL, backward_layer = NULL, ... )
object |
What to compose the new
|
layer |
A
|
merge_mode |
Mode by which outputs of the forward and backward RNNs will
be combined. One of |
weights |
Split and propagated to the |
backward_layer |
Optional |
... |
standard layer arguments. |
Other layer wrappers:
time_distributed()
Callback to back up and restore the training state
callback_backup_and_restore(backup_dir, ...)
callback_backup_and_restore(backup_dir, ...)
backup_dir |
String, path to store the checkpoint.
e.g. |
... |
For backwards and forwards compatibility |
BackupAndRestore
callback is intended to recover training from an
interruption that has happened in the middle of a fit(model)
execution, by
backing up the training states in a temporary checkpoint file (with the help
of a tf.train.CheckpointManager
), at the end of each epoch. Each backup
overwrites the previously written checkpoint file, so at any given time there
is at most one such checkpoint file for backup/restoring purpose.
If training restarts before completion, the training state (which includes the
Model
weights and epoch number) is restored to the most recently saved state
at the beginning of a new fit()
run. At the completion of a fit()
run, the temporary checkpoint file is deleted.
Note that the user is responsible to bring jobs back after the interruption. This callback is important for the backup and restore mechanism for fault tolerance purpose, and the model to be restored from an previous checkpoint is expected to be the same as the one used to back up. If user changes arguments passed to compile or fit, the checkpoint saved for fault tolerance can become invalid.
Note:
This callback is not compatible with eager execution disabled.
A checkpoint is saved at the end of each epoch. After restoring,
fit()
redoes any partial work during the unfinished epoch in which the
training got restarted (so the work done before the interruption doesn't
affect the final model state).
This works for both single worker and multi-worker modes. When fit()
is used with tf.distribute
, it supports tf.distribute.MirroredStrategy
,
tf.distribute.MultiWorkerMirroredStrategy
, tf.distribute.TPUStrategy
, and
tf.distribute.experimental.ParameterServerStrategy
.
Supports all values that can be represented as a string
callback_csv_logger(filename, separator = ",", append = FALSE)
callback_csv_logger(filename, separator = ",", append = FALSE)
filename |
filename of the csv file, e.g. 'run/log.csv'. |
separator |
string used to separate elements in the csv file. |
append |
|
Other callbacks:
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
Stop training when a monitored quantity has stopped improving.
callback_early_stopping( monitor = "val_loss", min_delta = 0, patience = 0, verbose = 0, mode = c("auto", "min", "max"), baseline = NULL, restore_best_weights = FALSE )
callback_early_stopping( monitor = "val_loss", min_delta = 0, patience = 0, verbose = 0, mode = c("auto", "min", "max"), baseline = NULL, restore_best_weights = FALSE )
monitor |
quantity to be monitored. |
min_delta |
minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement. |
patience |
number of epochs with no improvement after which training will be stopped. |
verbose |
verbosity mode, 0 or 1. |
mode |
one of "auto", "min", "max". In |
baseline |
Baseline value for the monitored quantity to reach. Training will stop if the model doesn't show improvement over the baseline. |
restore_best_weights |
Whether to restore model weights from
the epoch with the best value of the monitored quantity.
If |
Other callbacks:
callback_csv_logger()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
This callback is constructed with anonymous functions that will be called at the appropriate time. Note that the callbacks expects positional arguments, as:
callback_lambda( on_epoch_begin = NULL, on_epoch_end = NULL, on_batch_begin = NULL, on_batch_end = NULL, on_train_batch_begin = NULL, on_train_batch_end = NULL, on_train_begin = NULL, on_train_end = NULL, on_predict_batch_begin = NULL, on_predict_batch_end = NULL, on_predict_begin = NULL, on_predict_end = NULL, on_test_batch_begin = NULL, on_test_batch_end = NULL, on_test_begin = NULL, on_test_end = NULL )
callback_lambda( on_epoch_begin = NULL, on_epoch_end = NULL, on_batch_begin = NULL, on_batch_end = NULL, on_train_batch_begin = NULL, on_train_batch_end = NULL, on_train_begin = NULL, on_train_end = NULL, on_predict_batch_begin = NULL, on_predict_batch_end = NULL, on_predict_begin = NULL, on_predict_end = NULL, on_test_batch_begin = NULL, on_test_batch_end = NULL, on_test_begin = NULL, on_test_end = NULL )
on_epoch_begin |
called at the beginning of every epoch. |
on_epoch_end |
called at the end of every epoch. |
on_batch_begin |
called at the beginning of every training batch. |
on_batch_end |
called at the end of every training batch. |
on_train_batch_begin |
called at the beginning of every batch. |
on_train_batch_end |
called at the end of every batch. |
on_train_begin |
called at the beginning of model training. |
on_train_end |
called at the end of model training. |
on_predict_batch_begin |
called at the beginning of a batch in predict methods. |
on_predict_batch_end |
called at the end of a batch in predict methods. |
on_predict_begin |
called at the beginning of prediction. |
on_predict_end |
called at the end of prediction. |
on_test_batch_begin |
called at the beginning of a batch in evaluate methods. Also called at the beginning of a validation batch in the fit methods, if validation data is provided. |
on_test_batch_end |
called at the end of a batch in evaluate methods. Also called at the end of a validation batch in the fit methods, if validation data is provided. |
on_test_begin |
called at the beginning of evaluation or validation. |
on_test_end |
called at the end of evaluation or validation. |
on_epoch_begin
and on_epoch_end
expect two positional arguments: epoch
, logs
on_batch_*
, on_train_batch_*
, on_predict_batch_*
and on_test_batch_*
, expect
two positional arguments: batch
, logs
on_train_*
, on_test_*
and on_predict_*
expect one positional argument: logs
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
Learning rate scheduler.
callback_learning_rate_scheduler(schedule)
callback_learning_rate_scheduler(schedule)
schedule |
a function that takes an epoch index as input (integer, indexed from 0) and current learning rate and returns a new learning rate as output (float). |
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
filepath
can contain named formatting options, which will be filled the
value of epoch
and keys in logs
(passed in on_epoch_end
). For example:
if filepath
is weights.{epoch:02d}-{val_loss:.2f}.hdf5
, then the model
checkpoints will be saved with the epoch number and the validation loss in
the filename.
callback_model_checkpoint( filepath, monitor = "val_loss", verbose = 0, save_best_only = FALSE, save_weights_only = FALSE, mode = c("auto", "min", "max"), period = NULL, save_freq = "epoch" )
callback_model_checkpoint( filepath, monitor = "val_loss", verbose = 0, save_best_only = FALSE, save_weights_only = FALSE, mode = c("auto", "min", "max"), period = NULL, save_freq = "epoch" )
filepath |
string, path to save the model file. |
monitor |
quantity to monitor. |
verbose |
verbosity mode, 0 or 1. |
save_best_only |
if |
save_weights_only |
if |
mode |
one of "auto", "min", "max". If |
period |
Interval (number of epochs) between checkpoints. |
save_freq |
|
if filepath
is
weights.{epoch:02d}-{val_loss:.2f}.hdf5
,: then the model checkpoints will
be saved with the epoch number and the validation loss in the filename.
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
Callback that prints metrics to stdout.
callback_progbar_logger(count_mode = "samples", stateful_metrics = NULL)
callback_progbar_logger(count_mode = "samples", stateful_metrics = NULL)
count_mode |
One of "steps" or "samples". Whether the progress bar should count samples seens or steps (batches) seen. |
stateful_metrics |
List of metric names that should not
be averaged onver an epoch. Metrics in this list will be logged
as-is in |
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
Models often benefit from reducing the learning rate by a factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced.
callback_reduce_lr_on_plateau( monitor = "val_loss", factor = 0.1, patience = 10, verbose = 0, mode = c("auto", "min", "max"), min_delta = 1e-04, cooldown = 0, min_lr = 0 )
callback_reduce_lr_on_plateau( monitor = "val_loss", factor = 0.1, patience = 10, verbose = 0, mode = c("auto", "min", "max"), min_delta = 1e-04, cooldown = 0, min_lr = 0 )
monitor |
quantity to be monitored. |
factor |
factor by which the learning rate will be reduced. new_lr = lr \* factor |
patience |
number of epochs with no improvement after which learning rate will be reduced. |
verbose |
int. 0: quiet, 1: update messages. |
mode |
one of "auto", "min", "max". In min mode, lr will be reduced when the quantity monitored has stopped decreasing; in max mode it will be reduced when the quantity monitored has stopped increasing; in auto mode, the direction is automatically inferred from the name of the monitored quantity. |
min_delta |
threshold for measuring the new optimum, to only focus on significant changes. |
cooldown |
number of epochs to wait before resuming normal operation after lr has been reduced. |
min_lr |
lower bound on the learning rate. |
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_remote_monitor()
,
callback_tensorboard()
,
callback_terminate_on_naan()
Callback used to stream events to a server.
callback_remote_monitor( root = "https://localhost:9000", path = "/publish/epoch/end/", field = "data", headers = NULL, send_as_json = FALSE )
callback_remote_monitor( root = "https://localhost:9000", path = "/publish/epoch/end/", field = "data", headers = NULL, send_as_json = FALSE )
root |
root url of the target server. |
path |
path relative to root to which the events will be sent. |
field |
JSON field under which the data will be stored. |
headers |
Optional named list of custom HTTP headers. Defaults to:
|
send_as_json |
Whether the request should be sent as application/json. |
Events are sent to root + '/publish/epoch/end/'
by default. Calls
are HTTP POST, with a data
argument which is a JSON-encoded dictionary
of event data. If send_as_json is set to True, the content type of the
request will be application/json. Otherwise the serialized JSON will be
send within a form
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_tensorboard()
,
callback_terminate_on_naan()
This callback writes a log for TensorBoard, which allows you to visualize dynamic graphs of your training and test metrics, as well as activation histograms for the different layers in your model.
callback_tensorboard( log_dir = NULL, histogram_freq = 0, batch_size = NULL, write_graph = TRUE, write_grads = FALSE, write_images = FALSE, embeddings_freq = 0, embeddings_layer_names = NULL, embeddings_metadata = NULL, embeddings_data = NULL, update_freq = "epoch", profile_batch = 0 )
callback_tensorboard( log_dir = NULL, histogram_freq = 0, batch_size = NULL, write_graph = TRUE, write_grads = FALSE, write_images = FALSE, embeddings_freq = 0, embeddings_layer_names = NULL, embeddings_metadata = NULL, embeddings_data = NULL, update_freq = "epoch", profile_batch = 0 )
log_dir |
The path of the directory where to save the log files to be
parsed by Tensorboard. The default is |
histogram_freq |
frequency (in epochs) at which to compute activation histograms for the layers of the model. If set to 0, histograms won't be computed. |
batch_size |
size of batch of inputs to feed to the network for histograms computation. No longer needed, ignored since TF 1.14. |
write_graph |
whether to visualize the graph in Tensorboard. The log
file can become quite large when write_graph is set to |
write_grads |
whether to visualize gradient histograms in TensorBoard.
|
write_images |
whether to write model weights to visualize as image in Tensorboard. |
embeddings_freq |
frequency (in epochs) at which selected embedding layers will be saved. |
embeddings_layer_names |
a list of names of layers to keep eye on. If
|
embeddings_metadata |
a named list which maps layer name to a file name in which metadata for this embedding layer is saved. See the details about the metadata file format. In case if the same metadata file is used for all embedding layers, string can be passed. |
embeddings_data |
Data to be embedded at layers specified in
|
update_freq |
|
profile_batch |
Profile the batch to sample compute characteristics. By default, it will disbale profiling. Set profile_batch=2 profile the second batch. Must run in TensorFlow eager mode. (TF >= 1.14) |
TensorBoard is a visualization tool provided with TensorFlow.
You can find more information about TensorBoard here.
When using a backend other than TensorFlow, TensorBoard will still work (if you have TensorFlow installed), but the only feature available will be the display of the losses and metrics plots.
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_terminate_on_naan()
Callback that terminates training when a NaN loss is encountered.
callback_terminate_on_naan()
callback_terminate_on_naan()
Other callbacks:
callback_csv_logger()
,
callback_early_stopping()
,
callback_lambda()
,
callback_learning_rate_scheduler()
,
callback_model_checkpoint()
,
callback_progbar_logger()
,
callback_reduce_lr_on_plateau()
,
callback_remote_monitor()
,
callback_tensorboard()
Model cloning is similar to calling a model on new inputs, except that it creates new layers (and thus new weights) instead of sharing the weights of the existing layers.
clone_model(model, input_tensors = NULL, clone_function = NULL)
clone_model(model, input_tensors = NULL, clone_function = NULL)
model |
Instance of Keras model (could be a functional model or a Sequential model). |
input_tensors |
Optional list of input tensors to build the model upon. If not provided, placeholders will be created. |
clone_function |
Callable to be used to clone each layer in the target
model (except
By passing a custom callable, you can customize your copy of the model,
e.g. by wrapping certain layers of interest (you might want to replace all
LSTM instances with equivalent |
Configure a Keras model for training
## S3 method for class 'keras.engine.training.Model' compile( object, optimizer = NULL, loss = NULL, metrics = NULL, loss_weights = NULL, weighted_metrics = NULL, run_eagerly = NULL, steps_per_execution = NULL, ..., target_tensors = NULL, sample_weight_mode = NULL )
## S3 method for class 'keras.engine.training.Model' compile( object, optimizer = NULL, loss = NULL, metrics = NULL, loss_weights = NULL, weighted_metrics = NULL, run_eagerly = NULL, steps_per_execution = NULL, ..., target_tensors = NULL, sample_weight_mode = NULL )
object |
Model object to compile. |
optimizer |
String (name of optimizer) or optimizer instance. For most
models, this defaults to |
loss |
String (name of objective function), objective function or a
|
metrics |
List of metrics to be evaluated by the model during training
and testing. Each of this can be a string (name of a built-in function),
function or a |
loss_weights |
Optional list, dictionary, or named vector specifying
scalar numeric coefficients to weight the loss contributions of different
model outputs. The loss value that will be minimized by the model will then
be the weighted sum of all individual losses, weighted by the
|
weighted_metrics |
List of metrics to be evaluated and weighted by
|
run_eagerly |
Bool. Defaults to |
steps_per_execution |
Int. Defaults to 1. The number of batches to run
during each |
... |
Arguments supported for backwards compatibility only. |
target_tensors |
By default, Keras will create a placeholder for the
model's target, which will be fed with the target data during training. If
instead you would like to use your own target tensor (in turn, Keras will
not expect external data for these targets at training time), you can
specify them via the |
sample_weight_mode |
If you need to do timestep-wise sample weighting
(2D weights), set this to "temporal". |
Other model functions:
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Functions that impose constraints on weight values.
constraint_maxnorm(max_value = 2, axis = 0) constraint_nonneg() constraint_unitnorm(axis = 0) constraint_minmaxnorm(min_value = 0, max_value = 1, rate = 1, axis = 0)
constraint_maxnorm(max_value = 2, axis = 0) constraint_nonneg() constraint_unitnorm(axis = 0) constraint_minmaxnorm(min_value = 0, max_value = 1, rate = 1, axis = 0)
max_value |
The maximum norm for the incoming weights. |
axis |
The axis along which to calculate weight norms. For instance, in
a dense layer the weight matrix has shape |
min_value |
The minimum norm for the incoming weights. |
rate |
The rate for enforcing the constraint: weights will be rescaled to yield (1 - rate) * norm + rate * norm.clip(low, high). Effectively, this means that rate=1.0 stands for strict enforcement of the constraint, while rate<1.0 means that weights will be rescaled at each step to slowly move towards a value inside the desired interval. |
constraint_maxnorm()
constrains the weights incident to each
hidden unit to have a norm less than or equal to a desired value.
constraint_nonneg()
constraints the weights to be non-negative
constraint_unitnorm()
constrains the weights incident to each hidden
unit to have unit norm.
constraint_minmaxnorm()
constrains the weights incident to each
hidden unit to have the norm between a lower bound and an upper bound.
You can implement your own constraint functions in R. A custom
constraint is an R function that takes weights (w
) as input
and returns modified weights. Note that keras backend()
tensor
functions (e.g. k_greater_equal()
) should be used in the
implementation of custom constraints. For example:
nonneg_constraint <- function(w) { w * k_cast(k_greater_equal(w, 0), k_floatx()) } layer_dense(units = 32, input_shape = c(784), kernel_constraint = nonneg_constraint)
Note that models which use custom constraints cannot be serialized using
save_model_hdf5()
. Rather, the weights of the model should be saved
and restored using save_model_weights_hdf5()
.
Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014
Count the total number of scalars composing the weights.
count_params(object)
count_params(object)
object |
Layer or model object |
An integer count
Other layer methods:
get_config()
,
get_input_at()
,
get_weights()
,
reset_states()
Create a Keras Layer
create_layer(layer_class, object, args = list())
create_layer(layer_class, object, args = list())
layer_class |
Python layer class or R6 class of type KerasLayer |
object |
Object to compose layer with. This is either a
|
args |
List of arguments to layer constructor function |
A Keras layer
The object
parameter can be missing, in which case the
layer is created without a connection to an existing graph.
Create a Keras Layer wrapper
create_layer_wrapper(Layer, modifiers = NULL, convert = TRUE)
create_layer_wrapper(Layer, modifiers = NULL, convert = TRUE)
Layer |
A R6 or Python class generator that inherits from
|
modifiers |
A named list of functions to modify to user-supplied
arguments before they are passed on to the class constructor. (e.g.,
|
convert |
Boolean, whether the Python class and its methods should by default convert python objects to R objects. See guide 'making_new_layers_and_models_via_subclassing.Rmd' for example usage. |
An R function that behaves similarly to the builtin keras layer_*
functions. When called, it will create the class instance, and also
optionally call it on a supplied argument object
if it is present. This
enables keras layers to compose nicely with the pipe (%>%
).
The R function will arguments taken from the initialize
(or __init__
)
method of the Layer.
If Layer is an R6 object, this will delay initializing the python session, so it is safe to use in an R package.
Custom metric function
custom_metric(name, metric_fn)
custom_metric(name, metric_fn)
name |
name used to show training progress output |
metric_fn |
An R function with signature |
You can provide an arbitrary R function as a custom metric. Note that
the y_true
and y_pred
parameters are tensors, so computations on
them should use backend tensor functions.
Use the custom_metric()
function to define a custom metric.
Note that a name ('mean_pred') is provided for the custom metric
function: this name is used within training progress output.
If you want to save and load a model with custom metrics, you should
also specify the metric in the call the load_model_hdf5()
. For example:
load_model_hdf5("my_model.h5", c('mean_pred' = metric_mean_pred))
.
Alternatively, you can wrap all of your code in a call to
with_custom_object_scope()
which will allow you to refer to the
metric by name just like you do with built in keras metrics.
Documentation on the available backend tensor functions can be found at https://tensorflow.rstudio.com/reference/keras/#backend.
Alternative ways of supplying custom metrics:
custom_metric():
Arbitrary R function.
metric_mean_wrapper()
: Wrap an arbitrary R function in a Metric
instance.
subclass keras$metrics$Metric
: see ?Metric
for example.
Other metrics:
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Dataset taken from the StatLib library which is maintained at Carnegie Mellon University.
dataset_boston_housing( path = "boston_housing.npz", test_split = 0.2, seed = 113L )
dataset_boston_housing( path = "boston_housing.npz", test_split = 0.2, seed = 113L )
path |
Path where to cache the dataset locally (relative to ~/.keras/datasets). |
test_split |
fraction of the data to reserve as test set. |
seed |
Random seed for shuffling the data before computing the test split. |
Lists of training and test data: train$x, train$y, test$x, test$y
.
Samples contain 13 attributes of houses at different locations around the Boston suburbs in the late 1970s. Targets are the median values of the houses at a location (in k$).
Other datasets:
dataset_cifar10()
,
dataset_cifar100()
,
dataset_fashion_mnist()
,
dataset_imdb()
,
dataset_mnist()
,
dataset_reuters()
Dataset of 50,000 32x32 color training images, labeled over 10 categories, and 10,000 test images.
dataset_cifar10()
dataset_cifar10()
Lists of training and test data: train$x, train$y, test$x, test$y
.
The x
data is an array of RGB image data with shape (num_samples, 3, 32,
32).
The y
data is an array of category labels (integers in range 0-9) with
shape (num_samples).
Other datasets:
dataset_boston_housing()
,
dataset_cifar100()
,
dataset_fashion_mnist()
,
dataset_imdb()
,
dataset_mnist()
,
dataset_reuters()
Dataset of 50,000 32x32 color training images, labeled over 100 categories, and 10,000 test images.
dataset_cifar100(label_mode = c("fine", "coarse"))
dataset_cifar100(label_mode = c("fine", "coarse"))
label_mode |
one of "fine", "coarse". |
Lists of training and test data: train$x, train$y, test$x, test$y
.
The x
data is an array of RGB image data with shape (num_samples, 3, 32, 32).
The y
data is an array of category labels with shape (num_samples).
Other datasets:
dataset_boston_housing()
,
dataset_cifar10()
,
dataset_fashion_mnist()
,
dataset_imdb()
,
dataset_mnist()
,
dataset_reuters()
Dataset of 60,000 28x28 grayscale images of the 10 fashion article classes, along with a test set of 10,000 images. This dataset can be used as a drop-in replacement for MNIST. The class labels are encoded as integers from 0-9 which correspond to T-shirt/top, Trouser, Pullover, Dress, Coat, Sandal, Shirt,
dataset_fashion_mnist()
dataset_fashion_mnist()
Dataset of 60,000 28x28 grayscale images of 10 fashion categories, along with a test set of 10,000 images. This dataset can be used as a drop-in replacement for MNIST. The class labels are:
0 - T-shirt/top
1 - Trouser
2 - Pullover
3 - Dress
4 - Coat
5 - Sandal
6 - Shirt
7 - Sneaker
8 - Bag
9 - Ankle boot
Lists of training and test data: train$x, train$y, test$x, test$y
, where
x
is an array of grayscale image data with shape (num_samples, 28, 28) and y
is an array of article labels (integers in range 0-9) with shape (num_samples).
Other datasets:
dataset_boston_housing()
,
dataset_cifar10()
,
dataset_cifar100()
,
dataset_imdb()
,
dataset_mnist()
,
dataset_reuters()
Dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a sequence of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".
dataset_imdb( path = "imdb.npz", num_words = NULL, skip_top = 0L, maxlen = NULL, seed = 113L, start_char = 1L, oov_char = 2L, index_from = 3L ) dataset_imdb_word_index(path = "imdb_word_index.json")
dataset_imdb( path = "imdb.npz", num_words = NULL, skip_top = 0L, maxlen = NULL, seed = 113L, start_char = 1L, oov_char = 2L, index_from = 3L ) dataset_imdb_word_index(path = "imdb_word_index.json")
path |
Where to cache the data (relative to |
num_words |
Max number of words to include. Words are ranked by how often they occur (in the training set) and only the most frequent words are kept |
skip_top |
Skip the top N most frequently occuring words (which may not be informative). |
maxlen |
sequences longer than this will be filtered out. |
seed |
random seed for sample shuffling. |
start_char |
The start of a sequence will be marked with this character. Set to 1 because 0 is usually the padding character. |
oov_char |
Words that were cut out because of the |
index_from |
Index actual words with this index and higher. |
As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.
Lists of training and test data: train$x, train$y, test$x, test$y
.
The x
data includes integer sequences. If the num_words
argument was
specific, the maximum possible index value is num_words-1
. If the
maxlen
argument was specified, the largest possible sequence length is
maxlen
.
The y
data includes a set of integer labels (0 or 1).
The dataset_imdb_word_index()
function returns a list where the
names are words and the values are integer.
Other datasets:
dataset_boston_housing()
,
dataset_cifar10()
,
dataset_cifar100()
,
dataset_fashion_mnist()
,
dataset_mnist()
,
dataset_reuters()
Dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images.
dataset_mnist(path = "mnist.npz")
dataset_mnist(path = "mnist.npz")
path |
Path where to cache the dataset locally (relative to ~/.keras/datasets). |
Lists of training and test data: train$x, train$y, test$x, test$y
, where
x
is an array of grayscale image data with shape (num_samples, 28, 28) and y
is an array of digit labels (integers in range 0-9) with shape (num_samples).
Other datasets:
dataset_boston_housing()
,
dataset_cifar10()
,
dataset_cifar100()
,
dataset_fashion_mnist()
,
dataset_imdb()
,
dataset_reuters()
Dataset of 11,228 newswires from Reuters, labeled over 46 topics. As with
dataset_imdb()
, each wire is encoded as a sequence of word indexes (same
conventions).
dataset_reuters( path = "reuters.npz", num_words = NULL, skip_top = 0L, maxlen = NULL, test_split = 0.2, seed = 113L, start_char = 1L, oov_char = 2L, index_from = 3L ) dataset_reuters_word_index(path = "reuters_word_index.pkl")
dataset_reuters( path = "reuters.npz", num_words = NULL, skip_top = 0L, maxlen = NULL, test_split = 0.2, seed = 113L, start_char = 1L, oov_char = 2L, index_from = 3L ) dataset_reuters_word_index(path = "reuters_word_index.pkl")
path |
Where to cache the data (relative to |
num_words |
Max number of words to include. Words are ranked by how often they occur (in the training set) and only the most frequent words are kept |
skip_top |
Skip the top N most frequently occuring words (which may not be informative). |
maxlen |
Truncate sequences after this length. |
test_split |
Fraction of the dataset to be used as test data. |
seed |
Random seed for sample shuffling. |
start_char |
The start of a sequence will be marked with this character. Set to 1 because 0 is usually the padding character. |
oov_char |
words that were cut out because of the |
index_from |
index actual words with this index and higher. |
Lists of training and test data: train$x, train$y, test$x, test$y
with same format as dataset_imdb()
. The dataset_reuters_word_index()
function returns a list where the names are words and the values are
integer. e.g. word_index[["giraffe"]]
might return 1234
.
Other datasets:
dataset_boston_housing()
,
dataset_cifar10()
,
dataset_cifar100()
,
dataset_fashion_mnist()
,
dataset_imdb()
,
dataset_mnist()
Evaluate a Keras model
## S3 method for class 'keras.engine.training.Model' evaluate( object, x = NULL, y = NULL, batch_size = NULL, verbose = "auto", sample_weight = NULL, steps = NULL, callbacks = NULL, ... )
## S3 method for class 'keras.engine.training.Model' evaluate( object, x = NULL, y = NULL, batch_size = NULL, verbose = "auto", sample_weight = NULL, steps = NULL, callbacks = NULL, ... )
object |
Model object to evaluate |
x |
Vector, matrix, or array of test data (or list if the model has
multiple inputs). If all inputs in the model are named, you can also pass a
list mapping input names to data. |
y |
Vector, matrix, or array of target (label) data (or list if the model has
multiple outputs). If all outputs in the model are named, you can also pass
a list mapping output names to data. |
batch_size |
Integer or |
verbose |
Verbosity mode (0 = silent, 1 = progress bar, 2 = one line per epoch). Defaults to 1 in most contexts, 2 if in knitr render or running on a distributed training server. |
sample_weight |
Optional array of the same length as x, containing
weights to apply to the model's loss for each sample. In the case of
temporal data, you can pass a 2D array with shape (samples,
sequence_length), to apply a different weight to every timestep of every
sample. In this case you should make sure to specify
|
steps |
Total number of steps (batches of samples) before declaring the
evaluation round finished. Ignored with the default value of |
callbacks |
List of callbacks to apply during evaluation. |
... |
Unused |
Named list of model test loss (or losses for models with multiple outputs) and model metrics.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Serialize a model to disk.
## S3 method for class 'keras.engine.training.Model' export_savedmodel( object, export_dir_base, overwrite = TRUE, versioned = !overwrite, remove_learning_phase = TRUE, as_text = FALSE, ... )
## S3 method for class 'keras.engine.training.Model' export_savedmodel( object, export_dir_base, overwrite = TRUE, versioned = !overwrite, remove_learning_phase = TRUE, as_text = FALSE, ... )
object |
An R object. |
export_dir_base |
A string containing a directory in which to export the SavedModel. |
overwrite |
Should the |
versioned |
Should the model be exported under a versioned subdirectory? |
remove_learning_phase |
Should the learning phase be removed by saving
and reloading the model? Defaults to |
as_text |
Whether to write the SavedModel in text format. |
... |
Other arguments passed to tf.saved_model.save. (Used only if TensorFlow version >= 2.0) |
The path to the exported directory, as a string.
Required for featurewise_center
, featurewise_std_normalization
and zca_whitening
.
fit_image_data_generator(object, x, augment = FALSE, rounds = 1, seed = NULL)
fit_image_data_generator(object, x, augment = FALSE, rounds = 1, seed = NULL)
object |
|
x |
array, the data to fit on (should have rank 4). In case of grayscale data, the channels axis should have value 1, and in case of RGB data, it should have value 3. |
augment |
Whether to fit on randomly augmented samples |
rounds |
If |
seed |
random seed. |
Other image preprocessing:
flow_images_from_data()
,
flow_images_from_dataframe()
,
flow_images_from_directory()
,
image_load()
,
image_to_array()
Update tokenizer internal vocabulary based on a list of texts or list of sequences.
fit_text_tokenizer(object, x)
fit_text_tokenizer(object, x)
object |
Tokenizer returned by |
x |
Vector/list of strings, or a generator of strings (for memory-efficiency); Alternatively a list of "sequence" (a sequence is a list of integer word indices). |
Required before using texts_to_sequences()
, texts_to_matrix()
, or
sequences_to_matrix()
.
Other text tokenization:
save_text_tokenizer()
,
sequences_to_matrix()
,
text_tokenizer()
,
texts_to_matrix()
,
texts_to_sequences()
,
texts_to_sequences_generator()
Trains the model for a fixed number of epochs (iterations on a dataset).
## S3 method for class 'keras.engine.training.Model' fit( object, x = NULL, y = NULL, batch_size = NULL, epochs = 10, verbose = getOption("keras.fit_verbose", default = "auto"), callbacks = NULL, view_metrics = getOption("keras.view_metrics", default = "auto"), validation_split = 0, validation_data = NULL, shuffle = TRUE, class_weight = NULL, sample_weight = NULL, initial_epoch = 0, steps_per_epoch = NULL, validation_steps = NULL, ... )
## S3 method for class 'keras.engine.training.Model' fit( object, x = NULL, y = NULL, batch_size = NULL, epochs = 10, verbose = getOption("keras.fit_verbose", default = "auto"), callbacks = NULL, view_metrics = getOption("keras.view_metrics", default = "auto"), validation_split = 0, validation_data = NULL, shuffle = TRUE, class_weight = NULL, sample_weight = NULL, initial_epoch = 0, steps_per_epoch = NULL, validation_steps = NULL, ... )
object |
Model to train. |
x |
Vector, matrix, or array of training data (or list if the model has
multiple inputs). If all inputs in the model are named, you can also pass a
list mapping input names to data. |
y |
Vector, matrix, or array of target (label) data (or list if the
model has multiple outputs). If all outputs in the model are named, you can
also pass a list mapping output names to data. |
batch_size |
Integer or |
epochs |
Number of epochs to train the model. Note that in conjunction
with |
verbose |
Verbosity mode (0 = silent, 1 = progress bar, 2 = one line per epoch). Defaults to 1 in most contexts, 2 if in knitr render or running on a distributed training server. |
callbacks |
List of callbacks to be called during training. |
view_metrics |
View realtime plot of training metrics (by epoch). The
default ( |
validation_split |
Float between 0 and 1. Fraction of the training data
to be used as validation data. The model will set apart this fraction of
the training data, will not train on it, and will evaluate the loss and any
model metrics on this data at the end of each epoch. The validation data is
selected from the last samples in the |
validation_data |
Data on which to evaluate the loss and any model
metrics at the end of each epoch. The model will not be trained on this
data. This could be a list (x_val, y_val) or a list (x_val, y_val,
val_sample_weights). |
shuffle |
shuffle: Logical (whether to shuffle the training data before
each epoch) or string (for "batch"). "batch" is a special option for
dealing with the limitations of HDF5 data; it shuffles in batch-sized
chunks. Has no effect when |
class_weight |
Optional named list mapping indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class. |
sample_weight |
Optional array of the same length as x, containing
weights to apply to the model's loss for each sample. In the case of
temporal data, you can pass a 2D array with shape (samples,
sequence_length), to apply a different weight to every timestep of every
sample. In this case you should make sure to specify
|
initial_epoch |
Integer, Epoch at which to start training (useful for resuming a previous training run). |
steps_per_epoch |
Total number of steps (batches of samples) before
declaring one epoch finished and starting the next epoch. When training
with input tensors such as TensorFlow data tensors, the default |
validation_steps |
Only relevant if |
... |
Unused |
A history
object that contains all information collected during
training.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Generates batches of augmented/normalized data from image data and labels
flow_images_from_data( x, y = NULL, generator = image_data_generator(), batch_size = 32, shuffle = TRUE, sample_weight = NULL, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", subset = NULL )
flow_images_from_data( x, y = NULL, generator = image_data_generator(), batch_size = 32, shuffle = TRUE, sample_weight = NULL, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", subset = NULL )
x |
data. Should have rank 4. In case of grayscale data, the channels axis should have value 1, and in case of RGB data, it should have value 3. |
y |
labels (can be |
generator |
Image data generator to use for augmenting/normalizing image data. |
batch_size |
int (default: |
shuffle |
boolean (defaut: |
sample_weight |
Sample weights. |
seed |
int (default: |
save_to_dir |
|
save_prefix |
str (default: ”). Prefix to use for filenames of saved
pictures (only relevant if |
save_format |
one of "png", "jpeg" (only relevant if save_to_dir is set). Default: "png". |
subset |
Subset of data ( |
Yields batches indefinitely, in an infinite loop.
(x, y)
where x
is an array of image data and y
is a
array of corresponding labels. The generator loops indefinitely.
Other image preprocessing:
fit_image_data_generator()
,
flow_images_from_dataframe()
,
flow_images_from_directory()
,
image_load()
,
image_to_array()
Takes the dataframe and the path to a directory and generates batches of augmented/normalized data.
flow_images_from_dataframe( dataframe, directory = NULL, x_col = "filename", y_col = "class", generator = image_data_generator(), target_size = c(256, 256), color_mode = "rgb", classes = NULL, class_mode = "categorical", batch_size = 32, shuffle = TRUE, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", subset = NULL, interpolation = "nearest", drop_duplicates = NULL )
flow_images_from_dataframe( dataframe, directory = NULL, x_col = "filename", y_col = "class", generator = image_data_generator(), target_size = c(256, 256), color_mode = "rgb", classes = NULL, class_mode = "categorical", batch_size = 32, shuffle = TRUE, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", subset = NULL, interpolation = "nearest", drop_duplicates = NULL )
dataframe |
|
directory |
character, path to the directory to read images from.
If |
x_col |
character, column in dataframe that contains the filenames
(or absolute paths if directory is |
y_col |
string or list, column/s in dataframe that has the target data. |
generator |
Image data generator to use for augmenting/normalizing image data. |
target_size |
Either |
color_mode |
one of "grayscale", "rgb". Default: "rgb". Whether the images will be converted to have 1 or 3 color channels. |
classes |
optional list of classes (e.g. |
class_mode |
one of "categorical", "binary", "sparse", "input", "other" or None. Default: "categorical". Mode for yielding the targets:
|
batch_size |
int (default: |
shuffle |
boolean (defaut: |
seed |
int (default: |
save_to_dir |
|
save_prefix |
str (default: ”). Prefix to use for filenames of saved
pictures (only relevant if |
save_format |
one of "png", "jpeg" (only relevant if save_to_dir is set). Default: "png". |
subset |
Subset of data ( |
interpolation |
Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. |
drop_duplicates |
(deprecated in TF >= 2.3) Boolean, whether to drop
duplicate rows based on filename. The default value is |
Yields batches indefinitely, in an infinite loop.
(x, y)
where x
is an array of image data and y
is a
array of corresponding labels. The generator loops indefinitely.
This functions requires that pandas
(Python module) is installed in the
same environment as tensorflow
and keras
.
If you are using r-tensorflow
(the default environment) you can install
pandas
by running reticulate::virtualenv_install("pandas", envname = "r-tensorflow")
or reticulate::conda_install("pandas", envname = "r-tensorflow")
depending on
the kind of environment you are using.
Other image preprocessing:
fit_image_data_generator()
,
flow_images_from_data()
,
flow_images_from_directory()
,
image_load()
,
image_to_array()
Generates batches of data from images in a directory (with optional augmented/normalized data)
flow_images_from_directory( directory, generator = image_data_generator(), target_size = c(256, 256), color_mode = "rgb", classes = NULL, class_mode = "categorical", batch_size = 32, shuffle = TRUE, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", follow_links = FALSE, subset = NULL, interpolation = "nearest" )
flow_images_from_directory( directory, generator = image_data_generator(), target_size = c(256, 256), color_mode = "rgb", classes = NULL, class_mode = "categorical", batch_size = 32, shuffle = TRUE, seed = NULL, save_to_dir = NULL, save_prefix = "", save_format = "png", follow_links = FALSE, subset = NULL, interpolation = "nearest" )
directory |
path to the target directory. It should contain one subdirectory per class. Any PNG, JPG, BMP, PPM, or TIF images inside each of the subdirectories directory tree will be included in the generator. See this script for more details. |
generator |
Image data generator (default generator does no data augmentation/normalization transformations) |
target_size |
integer vector, default: |
color_mode |
one of "grayscale", "rbg". Default: "rgb". Whether the images will be converted to have 1 or 3 color channels. |
classes |
optional list of class subdirectories (e.g. |
class_mode |
one of "categorical", "binary", "sparse" or |
batch_size |
int (default: |
shuffle |
boolean (defaut: |
seed |
int (default: |
save_to_dir |
|
save_prefix |
str (default: ”). Prefix to use for filenames of saved
pictures (only relevant if |
save_format |
one of "png", "jpeg" (only relevant if save_to_dir is set). Default: "png". |
follow_links |
whether to follow symlinks inside class subdirectories
(default: |
subset |
Subset of data ( |
interpolation |
Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. |
Yields batches indefinitely, in an infinite loop.
(x, y)
where x
is an array of image data and y
is a
array of corresponding labels. The generator loops indefinitely.
Other image preprocessing:
fit_image_data_generator()
,
flow_images_from_data()
,
flow_images_from_dataframe()
,
image_load()
,
image_to_array()
Freeze weights in a model or layer so that they are no longer trainable.
freeze_weights(object, from = NULL, to = NULL, which = NULL) unfreeze_weights(object, from = NULL, to = NULL, which = NULL)
freeze_weights(object, from = NULL, to = NULL, which = NULL) unfreeze_weights(object, from = NULL, to = NULL, which = NULL)
object |
Keras model or layer object |
from |
Layer instance, layer name, or layer index within model |
to |
Layer instance, layer name, or layer index within model |
which |
layer names, integer positions, layers, logical vector (of
|
The from
and to
layer arguments are both inclusive.
When applied to a model, the freeze or unfreeze is a global operation over all layers in the model (i.e. layers not within the specified range will be set to the opposite value, e.g. unfrozen for a call to freeze).
Models must be compiled again after weights are frozen or unfrozen.
## Not run: conv_base <- application_vgg16( weights = "imagenet", include_top = FALSE, input_shape = c(150, 150, 3) ) # freeze it's weights freeze_weights(conv_base) conv_base # create a composite model that includes the base + more layers model <- keras_model_sequential() %>% conv_base() %>% layer_flatten() %>% layer_dense(units = 256, activation = "relu") %>% layer_dense(units = 1, activation = "sigmoid") # compile model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_rmsprop(lr = 2e-5), metrics = c("accuracy") ) model print(model, expand_nested = TRUE) # unfreeze weights from "block5_conv1" on unfreeze_weights(conv_base, from = "block5_conv1") # compile again since we froze or unfroze weights model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_rmsprop(lr = 2e-5), metrics = c("accuracy") ) conv_base print(model, expand_nested = TRUE) # freeze only the last 5 layers freeze_weights(conv_base, from = -5) conv_base # equivalently, also freeze only the last 5 layers unfreeze_weights(conv_base, to = -6) conv_base # Freeze only layers of a certain type, e.g, BatchNorm layers batch_norm_layer_class_name <- class(layer_batch_normalization())[1] is_batch_norm_layer <- function(x) inherits(x, batch_norm_layer_class_name) model <- application_efficientnet_b0() freeze_weights(model, which = is_batch_norm_layer) model # equivalent to: for(layer in model$layers) { if(is_batch_norm_layer(layer)) layer$trainable <- FALSE else layer$trainable <- TRUE } ## End(Not run)
## Not run: conv_base <- application_vgg16( weights = "imagenet", include_top = FALSE, input_shape = c(150, 150, 3) ) # freeze it's weights freeze_weights(conv_base) conv_base # create a composite model that includes the base + more layers model <- keras_model_sequential() %>% conv_base() %>% layer_flatten() %>% layer_dense(units = 256, activation = "relu") %>% layer_dense(units = 1, activation = "sigmoid") # compile model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_rmsprop(lr = 2e-5), metrics = c("accuracy") ) model print(model, expand_nested = TRUE) # unfreeze weights from "block5_conv1" on unfreeze_weights(conv_base, from = "block5_conv1") # compile again since we froze or unfroze weights model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_rmsprop(lr = 2e-5), metrics = c("accuracy") ) conv_base print(model, expand_nested = TRUE) # freeze only the last 5 layers freeze_weights(conv_base, from = -5) conv_base # equivalently, also freeze only the last 5 layers unfreeze_weights(conv_base, to = -6) conv_base # Freeze only layers of a certain type, e.g, BatchNorm layers batch_norm_layer_class_name <- class(layer_batch_normalization())[1] is_batch_norm_layer <- function(x) inherits(x, batch_norm_layer_class_name) model <- application_efficientnet_b0() freeze_weights(model, which = is_batch_norm_layer) model # equivalent to: for(layer in model$layers) { if(is_batch_norm_layer(layer)) layer$trainable <- FALSE else layer$trainable <- TRUE } ## End(Not run)
Use to retrieve items from generators (e.g. image_data_generator()
). Will return
either the next item or NULL
if there are no more items.
generator_next(generator, completed = NULL)
generator_next(generator, completed = NULL)
generator |
Generator |
completed |
Sentinel value to return from |
A layer config is an object returned from get_config()
that contains the
configuration of a layer or model. The same layer or model can be
reinstantiated later (without its trained weights) from this configuration
using from_config()
. The config does not include connectivity information,
nor the class name (those are handled externally).
get_config(object) from_config(config, custom_objects = NULL)
get_config(object) from_config(config, custom_objects = NULL)
object |
Layer or model object |
config |
Object with layer or model configuration |
custom_objects |
list of custom objects needed to instantiate the layer,
e.g., custom layers defined by |
get_config()
returns an object with the configuration,
from_config()
returns a re-instantiation of the object.
Objects returned from get_config()
are not serializable. Therefore,
if you want to save and restore a model across sessions, you can use the
model_to_json()
function (for model configuration only, not weights) or
the save_model_tf()
function to save the model configuration and weights
to the filesystem.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Other layer methods:
count_params()
,
get_input_at()
,
get_weights()
,
reset_states()
Passing the MD5 hash will verify the file after download as well as if it is already present in the cache.
get_file( fname, origin, file_hash = NULL, cache_subdir = "datasets", hash_algorithm = "auto", extract = FALSE, archive_format = "auto", cache_dir = NULL, untar = FALSE )
get_file( fname, origin, file_hash = NULL, cache_subdir = "datasets", hash_algorithm = "auto", extract = FALSE, archive_format = "auto", cache_dir = NULL, untar = FALSE )
fname |
Name of the file. If an absolute path |
origin |
Original URL of the file. |
file_hash |
The expected hash string of the file after download. The sha256 and md5 hash algorithms are both supported. |
cache_subdir |
Subdirectory under the Keras cache dir where the file is
saved. If an absolute path |
hash_algorithm |
Select the hash algorithm to verify the file. options are 'md5', 'sha256', and 'auto'. The default 'auto' detects the hash algorithm in use. |
extract |
True tries extracting the file as an Archive, like tar or zip. |
archive_format |
Archive format to try for extracting the file. Options are 'auto', 'tar', 'zip', and None. 'tar' includes tar, tar.gz, and tar.bz files. The default 'auto' is ('tar', 'zip'). None or an empty list will return no matches found. |
cache_dir |
Location to store cached files, when |
untar |
Deprecated in favor of 'extract'. boolean, whether the file should be decompressed |
Path to the downloaded file
Whenever you are calling a layer on some input, you are creating a new tensor (the output of the layer), and you are adding a "node" to the layer, linking the input tensor to the output tensor. When you are calling the same layer multiple times, that layer owns multiple nodes indexed as 1, 2, 3. These functions enable you to retrieve various tensor properties of layers with multiple nodes.
get_input_at(object, node_index) get_output_at(object, node_index) get_input_shape_at(object, node_index) get_output_shape_at(object, node_index) get_input_mask_at(object, node_index) get_output_mask_at(object, node_index)
get_input_at(object, node_index) get_output_at(object, node_index) get_input_shape_at(object, node_index) get_output_shape_at(object, node_index) get_input_mask_at(object, node_index) get_output_mask_at(object, node_index)
object |
Layer or model object |
node_index |
Integer, index of the node from which to retrieve the
attribute. E.g. |
A tensor (or list of tensors if the layer has multiple inputs/outputs).
Other layer methods:
count_params()
,
get_config()
,
get_weights()
,
reset_states()
Indices are based on order of horizontal graph traversal (bottom-up) and are
1-based. If name
and index
are both provided, index
will take
precedence.
get_layer(object, name = NULL, index = NULL)
get_layer(object, name = NULL, index = NULL)
object |
Keras model object |
name |
String, name of layer. |
index |
Integer, index of layer (1-based). Also valid are negative values, which count from the end of model. |
A layer instance.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Layer/Model weights as R arrays
get_weights(object, trainable = NA) set_weights(object, weights)
get_weights(object, trainable = NA) set_weights(object, weights)
object |
Layer or model object |
trainable |
if |
weights |
Weights as R array |
You can access the Layer/Model as tf.Tensors
or tf.Variables
at
object$weights
, object$trainable_weights
, or
object$non_trainable_weights
Other model persistence:
model_to_json()
,
model_to_yaml()
,
save_model_hdf5()
,
save_model_tf()
,
save_model_weights_hdf5()
,
serialize_model()
Other layer methods:
count_params()
,
get_config()
,
get_input_at()
,
reset_states()
Representation of HDF5 dataset to be used instead of an R array
hdf5_matrix(datapath, dataset, start = 0, end = NULL, normalizer = NULL)
hdf5_matrix(datapath, dataset, start = 0, end = NULL, normalizer = NULL)
datapath |
string, path to a HDF5 file |
dataset |
string, name of the HDF5 dataset in the file specified in datapath |
start |
int, start of desired slice of the specified dataset |
end |
int, end of desired slice of the specified dataset |
normalizer |
function to be called on data when retrieved |
Providing start
and end
allows use of a slice of the dataset.
Optionally, a normalizer function (or lambda) can be given. This will be called on every slice of data retrieved.
An array-like HDF5 dataset.
Deprecated: image_data_generator
is not
recommended for new code. Prefer loading images with
image_dataset_from_directory
and transforming the output
TF Dataset with preprocessing layers. For more information, see the
tutorials for loading images and augmenting images, as well as the
preprocessing layer guide.
image_data_generator( featurewise_center = FALSE, samplewise_center = FALSE, featurewise_std_normalization = FALSE, samplewise_std_normalization = FALSE, zca_whitening = FALSE, zca_epsilon = 1e-06, rotation_range = 0, width_shift_range = 0, height_shift_range = 0, brightness_range = NULL, shear_range = 0, zoom_range = 0, channel_shift_range = 0, fill_mode = "nearest", cval = 0, horizontal_flip = FALSE, vertical_flip = FALSE, rescale = NULL, preprocessing_function = NULL, data_format = NULL, validation_split = 0 )
image_data_generator( featurewise_center = FALSE, samplewise_center = FALSE, featurewise_std_normalization = FALSE, samplewise_std_normalization = FALSE, zca_whitening = FALSE, zca_epsilon = 1e-06, rotation_range = 0, width_shift_range = 0, height_shift_range = 0, brightness_range = NULL, shear_range = 0, zoom_range = 0, channel_shift_range = 0, fill_mode = "nearest", cval = 0, horizontal_flip = FALSE, vertical_flip = FALSE, rescale = NULL, preprocessing_function = NULL, data_format = NULL, validation_split = 0 )
featurewise_center |
Set input mean to 0 over the dataset, feature-wise. |
samplewise_center |
Boolean. Set each sample mean to 0. |
featurewise_std_normalization |
Divide inputs by std of the dataset, feature-wise. |
samplewise_std_normalization |
Divide each input by its std. |
zca_whitening |
apply ZCA whitening. |
zca_epsilon |
Epsilon for ZCA whitening. Default is 1e-6. |
rotation_range |
degrees (0 to 180). |
width_shift_range |
fraction of total width. |
height_shift_range |
fraction of total height. |
brightness_range |
the range of brightness to apply |
shear_range |
shear intensity (shear angle in radians). |
zoom_range |
amount of zoom. if scalar z, zoom will be randomly picked
in the range |
channel_shift_range |
shift range for each channels. |
fill_mode |
One of "constant", "nearest", "reflect" or "wrap". Points outside the boundaries of the input are filled according to the given mode:
|
cval |
value used for points outside the boundaries when fill_mode is 'constant'. Default is 0. |
horizontal_flip |
whether to randomly flip images horizontally. |
vertical_flip |
whether to randomly flip images vertically. |
rescale |
rescaling factor. If NULL or 0, no rescaling is applied, otherwise we multiply the data by the value provided (before applying any other transformation). |
preprocessing_function |
function that will be implied on each input. The function will run before any other modification on it. The function should take one argument: one image (tensor with rank 3), and should output a tensor with the same shape. |
data_format |
'channels_first' or 'channels_last'. In 'channels_first'
mode, the channels dimension (the depth) is at index 1, in 'channels_last'
mode it is at index 3. It defaults to the |
validation_split |
fraction of images reserved for validation (strictly between 0 and 1). |
Generates a tf.data.Dataset
from image files in a directory.
image_dataset_from_directory( directory, labels = "inferred", label_mode = "int", class_names = NULL, color_mode = "rgb", batch_size = 32, image_size = c(256, 256), shuffle = TRUE, seed = NULL, validation_split = NULL, subset = NULL, interpolation = "bilinear", follow_links = FALSE, crop_to_aspect_ratio = FALSE, ... )
image_dataset_from_directory( directory, labels = "inferred", label_mode = "int", class_names = NULL, color_mode = "rgb", batch_size = 32, image_size = c(256, 256), shuffle = TRUE, seed = NULL, validation_split = NULL, subset = NULL, interpolation = "bilinear", follow_links = FALSE, crop_to_aspect_ratio = FALSE, ... )
directory |
Directory where the data is located. If labels is "inferred", it should contain subdirectories, each containing images for a class. Otherwise, the directory structure is ignored. |
labels |
Either "inferred" (labels are generated from the directory structure), or a list/tuple of integer labels of the same size as the number of image files found in the directory. Labels should be sorted according to the alphanumeric order of the image file paths (obtained via os.walk(directory) in Python). |
label_mode |
Valid values:
|
class_names |
Only valid if "labels" is "inferred". This is the explict list of class names (must match names of subdirectories). Used to control the order of the classes (otherwise alphanumerical order is used). |
color_mode |
One of "grayscale", "rgb", "rgba". Default: "rgb". Whether the images will be converted to have 1, 3, or 4 channels. |
batch_size |
Size of the batches of data. Default: 32. |
image_size |
Size to resize images to after they are read from disk. Defaults to (256, 256). Since the pipeline processes batches of images that must all have the same size, this must be provided. |
shuffle |
Whether to shuffle the data. Default: TRUE. If set to FALSE, sorts the data in alphanumeric order. |
seed |
Optional random seed for shuffling and transformations. |
validation_split |
Optional float between 0 and 1, fraction of data to reserve for validation. |
subset |
One of "training", "validation", or "both" (available for TF>=2.10).
Only used if validation_split is set. When |
interpolation |
String, the interpolation method used when resizing images. Defaults to bilinear. Supports bilinear, nearest, bicubic, area, lanczos3, lanczos5, gaussian, mitchellcubic. |
follow_links |
Whether to visits subdirectories pointed to by symlinks. Defaults to FALSE. |
crop_to_aspect_ratio |
If |
... |
Legacy arguments |
If your directory structure is:
main_directory/ ...class_a/ ......a_image_1.jpg ......a_image_2.jpg ...class_b/ ......b_image_1.jpg ......b_image_2.jpg
Then calling image_dataset_from_directory(main_directory, labels='inferred')
will return a tf.data.Dataset
that yields batches of images from the
subdirectories class_a and class_b, together with labels 0 and 1 (0
corresponding to class_a and 1 corresponding to class_b).
Supported image formats: jpeg, png, bmp, gif. Animated gifs are truncated to the first frame.
A tf.data.Dataset object. If label_mode is NULL
, it yields float32
tensors of shape (batch_size, image_size[1], image_size[2], num_channels)
,
encoding images (see below for rules regarding num_channels
).
Otherwise, it yields pairs of (images, labels)
, where images has shape
(batch_size, image_size[1], image_size[2], num_channels)
, and labels
follows the format described below.
Rules regarding labels format:
if label_mode is int, the labels are an int32 tensor of shape
(batch_size)
.
if label_mode is binary, the labels are a float32 tensor of 1s and 0s of
shape (batch_size, 1)
.
if label_mode is categorial, the labels are a float32 tensor of shape
(batch_size, num_classes)
, representing a one-hot encoding of the class
index.
Rules regarding number of channels in the yielded images:
if color_mode is grayscale, there's 1 channel in the image tensors.
if color_mode is rgb, there are 3 channel in the image tensors.
if color_mode is rgba, there are 4 channel in the image tensors.
https://www.tensorflow.org/api_docs/python/tf/keras/utils/image_dataset_from_directory
Loads an image into PIL format.
image_load( path, grayscale = FALSE, color_mode = "rgb", target_size = NULL, interpolation = "nearest" )
image_load( path, grayscale = FALSE, color_mode = "rgb", target_size = NULL, interpolation = "nearest" )
path |
Path to image file |
grayscale |
DEPRECATED use |
color_mode |
One of |
target_size |
Either |
interpolation |
Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. |
A PIL Image instance.
Other image preprocessing:
fit_image_data_generator()
,
flow_images_from_data()
,
flow_images_from_dataframe()
,
flow_images_from_directory()
,
image_to_array()
3D array that represents an image with dimensions (height,width,channels) or (channels,height,width) depending on the data_format.
image_to_array(img, data_format = c("channels_last", "channels_first")) image_array_resize( img, height, width, data_format = c("channels_last", "channels_first") ) image_array_save( img, path, data_format = NULL, file_format = NULL, scale = TRUE )
image_to_array(img, data_format = c("channels_last", "channels_first")) image_array_resize( img, height, width, data_format = c("channels_last", "channels_first") ) image_array_save( img, path, data_format = NULL, file_format = NULL, scale = TRUE )
img |
Image |
data_format |
Image data format ("channels_last" or "channels_first") |
height |
Height to resize to |
width |
Width to resize to |
path |
Path to save image to |
file_format |
Optional file format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used. |
scale |
Whether to rescale image values to be within 0,255 |
Other image preprocessing:
fit_image_data_generator()
,
flow_images_from_data()
,
flow_images_from_dataframe()
,
flow_images_from_directory()
,
image_load()
Decodes the prediction of an ImageNet model.
imagenet_decode_predictions(preds, top = 5)
imagenet_decode_predictions(preds, top = 5)
preds |
Tensor encoding a batch of predictions. |
top |
integer, how many top-guesses to return. |
List of data frames with variables class_name
, class_description
,
and score
(one data frame per sample in batch input).
Preprocesses a tensor or array encoding a batch of images.
imagenet_preprocess_input(x, data_format = NULL, mode = "caffe")
imagenet_preprocess_input(x, data_format = NULL, mode = "caffe")
x |
Input Numpy or symbolic tensor, 3D or 4D. |
data_format |
Data format of the image tensor/array. |
mode |
One of "caffe", "tf", or "torch"
|
Preprocessed tensor or array.
Obtain a reference to the Python module used for the implementation of Keras.
implementation()
implementation()
There are currently two Python modules which implement Keras:
keras ("keras")
tensorflow.keras ("tensorflow")
This function returns a reference to the implementation being currently
used by the keras package. The default implementation is "keras".
You can override this by setting the KERAS_IMPLEMENTATION
environment
variable to "tensorflow".
Reference to the Python module used for the implementation of Keras.
Initializer that generates tensors initialized to a constant value.
initializer_constant(value = 0)
initializer_constant(value = 0)
value |
float; the value of the generator tensors. |
Other initializers:
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a truncated normal distribution centered on 0
with stddev = sqrt(2 / (fan_in + fan_out))
where fan_in
is the number of input units in the weight tensor
and fan_out
is the number of output units in the weight tensor.
initializer_glorot_normal(seed = NULL)
initializer_glorot_normal(seed = NULL)
seed |
Integer used to seed the random generator. |
Glorot & Bengio, AISTATS 2010 https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf
Other initializers:
initializer_constant()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a uniform distribution within -limit, limit
where limit
is sqrt(6 / (fan_in + fan_out))
where fan_in
is the number of input units in the weight tensor
and fan_out
is the number of output units in the weight tensor.
initializer_glorot_uniform(seed = NULL)
initializer_glorot_uniform(seed = NULL)
seed |
Integer used to seed the random generator. |
Glorot & Bengio, AISTATS 2010 https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a truncated normal distribution centered on 0 with
stddev = sqrt(2 / fan_in)
where fan_in
is the number of input units in
the weight tensor.
initializer_he_normal(seed = NULL)
initializer_he_normal(seed = NULL)
seed |
Integer used to seed the random generator. |
He et al., https://arxiv.org/abs/1502.01852
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a uniform distribution within -limit, limit
where
limit`` is
sqrt(6 / fan_in)where
fan_in' is the number of input units in the
weight tensor.
initializer_he_uniform(seed = NULL)
initializer_he_uniform(seed = NULL)
seed |
Integer used to seed the random generator. |
He et al., https://arxiv.org/abs/1502.01852
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
Only use for square 2D matrices.
initializer_identity(gain = 1)
initializer_identity(gain = 1)
gain |
Multiplicative factor to apply to the identity matrix |
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a truncated normal distribution centered on 0 with
stddev <- sqrt(1 / fan_in)
where fan_in
is the number of input units in
the weight tensor..
initializer_lecun_normal(seed = NULL)
initializer_lecun_normal(seed = NULL)
seed |
A Python integer. Used to seed the random generator. |
Efficient Backprop, LeCun, Yann et al. 1998
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
It draws samples from a uniform distribution within -limit, limit
where
limit
is sqrt(3 / fan_in)
where fan_in
is the number of input units in
the weight tensor.
initializer_lecun_uniform(seed = NULL)
initializer_lecun_uniform(seed = NULL)
seed |
Integer used to seed the random generator. |
LeCun 98, Efficient Backprop,
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
Initializer that generates tensors initialized to 1.
initializer_ones()
initializer_ones()
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
Initializer that generates a random orthogonal matrix.
initializer_orthogonal(gain = 1, seed = NULL)
initializer_orthogonal(gain = 1, seed = NULL)
gain |
Multiplicative factor to apply to the orthogonal matrix. |
seed |
Integer used to seed the random generator. |
Saxe et al., https://arxiv.org/abs/1312.6120
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
Initializer that generates tensors with a normal distribution.
initializer_random_normal(mean = 0, stddev = 0.05, seed = NULL)
initializer_random_normal(mean = 0, stddev = 0.05, seed = NULL)
mean |
Mean of the random values to generate. |
stddev |
Standard deviation of the random values to generate. |
seed |
Integer used to seed the random generator. |
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
Initializer that generates tensors with a uniform distribution.
initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = NULL)
initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = NULL)
minval |
Lower bound of the range of random values to generate. |
maxval |
Upper bound of the range of random values to generate. Defaults to 1 for float types. |
seed |
seed |
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
,
initializer_zeros()
These values are similar to values from an initializer_random_normal()
except that values more than two standard deviations from the mean
are discarded and re-drawn. This is the recommended initializer for
neural network weights and filters.
initializer_truncated_normal(mean = 0, stddev = 0.05, seed = NULL)
initializer_truncated_normal(mean = 0, stddev = 0.05, seed = NULL)
mean |
Mean of the random values to generate. |
stddev |
Standard deviation of the random values to generate. |
seed |
Integer used to seed the random generator. |
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_variance_scaling()
,
initializer_zeros()
With distribution="normal"
, samples are drawn from a truncated normal
distribution centered on zero, with stddev = sqrt(scale / n)
where n is:
number of input units in the weight tensor, if mode = "fan_in"
number of output units, if mode = "fan_out"
average of the numbers of input and output units, if mode = "fan_avg"
initializer_variance_scaling( scale = 1, mode = c("fan_in", "fan_out", "fan_avg"), distribution = c("normal", "uniform", "truncated_normal", "untruncated_normal"), seed = NULL )
initializer_variance_scaling( scale = 1, mode = c("fan_in", "fan_out", "fan_avg"), distribution = c("normal", "uniform", "truncated_normal", "untruncated_normal"), seed = NULL )
scale |
Scaling factor (positive float). |
mode |
One of "fan_in", "fan_out", "fan_avg". |
distribution |
One of "truncated_normal", "untruncated_normal" and "uniform". For backward compatibility, "normal" will be accepted and converted to "untruncated_normal". |
seed |
Integer used to seed the random generator. |
With distribution="uniform"
, samples are drawn from a uniform distribution
within -limit, limit
, with limit = sqrt(3 * scale / n)
.
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_zeros()
Initializer that generates tensors initialized to 0.
initializer_zeros()
initializer_zeros()
Other initializers:
initializer_constant()
,
initializer_glorot_normal()
,
initializer_glorot_uniform()
,
initializer_he_normal()
,
initializer_he_uniform()
,
initializer_identity()
,
initializer_lecun_normal()
,
initializer_lecun_uniform()
,
initializer_ones()
,
initializer_orthogonal()
,
initializer_random_normal()
,
initializer_random_uniform()
,
initializer_truncated_normal()
,
initializer_variance_scaling()
This function will install Tensorflow and all Keras dependencies. This is a
thin wrapper around tensorflow::install_tensorflow()
, with the only
difference being that this includes by default additional extra packages that
keras expects, and the default version of tensorflow installed by
install_keras()
may at times be different from the default installed
install_tensorflow()
. The default version of tensorflow installed by
install_keras()
is "2.15".
install_keras( method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", tensorflow = version, extra_packages = NULL, ... )
install_keras( method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", tensorflow = version, extra_packages = NULL, ... )
method |
Installation method. By default, "auto" automatically finds a method that will work in the local environment. Change the default to force a specific installation method. Note that the "virtualenv" method is not available on Windows. |
conda |
The path to a |
version |
TensorFlow version to install. Valid values include:
|
tensorflow |
Synonym for |
extra_packages |
Additional Python packages to install along with TensorFlow. |
... |
other arguments passed to |
The default additional packages are: tensorflow-hub, tensorflow-datasets, scipy, requests, pyyaml, Pillow, h5py, pandas, pydot, with their versions potentially constrained for compatibility with the requested tensorflow version.
tensorflow::install_tensorflow()
Probe to see whether the Keras Python package is available in the current system environment.
is_keras_available(version = NULL)
is_keras_available(version = NULL)
version |
Minimum required version of Keras (defaults to |
Logical indicating whether Keras (or the specified minimum version of Keras) is available.
## Not run: # testthat utilty for skipping tests when Keras isn't available skip_if_no_keras <- function(version = NULL) { if (!is_keras_available(version)) skip("Required keras version not available for testing") } # use the function within a test test_that("keras function works correctly", { skip_if_no_keras() # test code here }) ## End(Not run)
## Not run: # testthat utilty for skipping tests when Keras isn't available skip_if_no_keras <- function(version = NULL) { if (!is_keras_available(version)) skip("Required keras version not available for testing") } # use the function within a test test_that("keras function works correctly", { skip_if_no_keras() # test code here }) ## End(Not run)
Element-wise absolute value.
k_abs(x)
k_abs(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Bitwise reduction (logical AND).
k_all(x, axis = NULL, keepdims = FALSE)
k_all(x, axis = NULL, keepdims = FALSE)
x |
Tensor or variable. |
axis |
Axis along which to perform the reduction (axis indexes are 1-based). |
keepdims |
whether the drop or broadcast the reduction axes. |
A uint8 tensor (0s and 1s).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Bitwise reduction (logical OR).
k_any(x, axis = NULL, keepdims = FALSE)
k_any(x, axis = NULL, keepdims = FALSE)
x |
Tensor or variable. |
axis |
Axis along which to perform the reduction (axis indexes are 1-based). |
keepdims |
whether the drop or broadcast the reduction axes. |
A uint8 tensor (0s and 1s).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
The function arguments use the same convention as Theano's arange: if only
one argument is provided, it is in fact the "stop" argument. The default
type of the returned tensor is 'int32'
to match TensorFlow's default.
k_arange(start, stop = NULL, step = 1, dtype = "int32")
k_arange(start, stop = NULL, step = 1, dtype = "int32")
start |
Start value. |
stop |
Stop value. |
step |
Difference between two successive values. |
dtype |
Integer dtype to use. |
An integer tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the index of the maximum value along an axis.
k_argmax(x, axis = -1)
k_argmax(x, axis = -1)
x |
Tensor or variable. |
axis |
Axis along which to perform the reduction (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the index of the minimum value along an axis.
k_argmin(x, axis = -1)
k_argmin(x, axis = -1)
x |
Tensor or variable. |
axis |
Axis along which to perform the reduction (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Active Keras backend
k_backend()
k_backend()
The name of the backend Keras is currently using.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
batch_dot
is used to compute dot product of x
and y
when x
and y
are data in batch, i.e. in a shape of (batch_size)
. batch_dot
results in
a tensor or variable with less dimensions than the input. If the number of
dimensions is reduced to 1, we use expand_dims
to make sure that ndim is
at least 2.
k_batch_dot(x, y, axes)
k_batch_dot(x, y, axes)
x |
Keras tensor or variable with 2 more more axes. |
y |
Keras tensor or variable with 2 or more axes |
axes |
List of (or single) integer with target dimensions (axis indexes
are 1-based). The lengths of |
A tensor with shape equal to the concatenation of x
's shape (less
the dimension that was summed over) and y
's shape (less the batch
dimension and the dimension that was summed over). If the final rank is 1,
we reshape it to (batch_size, 1)
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
In other words, it flattens each data samples of a batch.
k_batch_flatten(x)
k_batch_flatten(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the value of more than one tensor variable.
k_batch_get_value(ops)
k_batch_get_value(ops)
ops |
List of ops to evaluate. |
A list of arrays.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
i.e. returns
output <- (x - mean) / (sqrt(var) + epsilon) * gamma + beta
k_batch_normalization(x, mean, var, beta, gamma, axis = -1, epsilon = 0.001)
k_batch_normalization(x, mean, var, beta, gamma, axis = -1, epsilon = 0.001)
x |
Input tensor or variable. |
mean |
Mean of batch. |
var |
Variance of batch. |
beta |
Tensor with which to center the input. |
gamma |
Tensor by which to scale the input. |
axis |
Axis (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
epsilon |
Fuzz factor. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Sets the values of many tensor variables at once.
k_batch_set_value(lists)
k_batch_set_value(lists)
lists |
a list of lists |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Adds a bias vector to a tensor.
k_bias_add(x, bias, data_format = NULL)
k_bias_add(x, bias, data_format = NULL)
x |
Tensor or variable. |
bias |
Bias tensor to add. |
data_format |
string, |
Output tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Binary crossentropy between an output tensor and a target tensor.
k_binary_crossentropy(target, output, from_logits = FALSE)
k_binary_crossentropy(target, output, from_logits = FALSE)
target |
A tensor with the same shape as |
output |
A tensor. |
from_logits |
Whether |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
You can cast a Keras variable but it still returns a Keras tensor.
k_cast(x, dtype)
k_cast(x, dtype)
x |
Keras tensor (or variable). |
dtype |
String, either ( |
Keras tensor with dtype dtype
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Cast an array to the default Keras float type.
k_cast_to_floatx(x)
k_cast_to_floatx(x)
x |
Array. |
The same array, cast to its new type.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Categorical crossentropy between an output tensor and a target tensor.
k_categorical_crossentropy(target, output, from_logits = FALSE, axis = -1)
k_categorical_crossentropy(target, output, from_logits = FALSE, axis = -1)
target |
A tensor of the same shape as |
output |
A tensor resulting from a softmax (unless |
from_logits |
Logical, whether |
axis |
Axis (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
Output tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Useful to avoid clutter from old models / layers.
k_clear_session()
k_clear_session()
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise value clipping.
k_clip(x, min_value = NULL, max_value = NULL)
k_clip(x, min_value = NULL, max_value = NULL)
x |
Tensor or variable. |
min_value |
Float or integer. |
max_value |
Float or integer. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Concatenates a list of tensors alongside the specified axis.
k_concatenate(tensors, axis = -1)
k_concatenate(tensors, axis = -1)
tensors |
list of tensors to concatenate. |
axis |
concatenation axis (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Creates a constant tensor.
k_constant(value, dtype = NULL, shape = NULL, name = NULL)
k_constant(value, dtype = NULL, shape = NULL, name = NULL)
value |
A constant value |
dtype |
The type of the elements of the resulting tensor. |
shape |
Optional dimensions of resulting tensor. |
name |
Optional name for the tensor. |
A Constant Tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
1D convolution.
k_conv1d( x, kernel, strides = 1, padding = "valid", data_format = NULL, dilation_rate = 1 )
k_conv1d( x, kernel, strides = 1, padding = "valid", data_format = NULL, dilation_rate = 1 )
x |
Tensor or variable. |
kernel |
kernel tensor. |
strides |
stride integer. |
padding |
string, |
data_format |
string, |
dilation_rate |
integer dilate rate. |
A tensor, result of 1D convolution.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
2D convolution.
k_conv2d( x, kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
k_conv2d( x, kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
x |
Tensor or variable. |
kernel |
kernel tensor. |
strides |
strides |
padding |
string, |
data_format |
string, |
dilation_rate |
vector of 2 integers. |
A tensor, result of 2D convolution.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
2D deconvolution (i.e. transposed convolution).
k_conv2d_transpose( x, kernel, output_shape, strides = c(1, 1), padding = "valid", data_format = NULL )
k_conv2d_transpose( x, kernel, output_shape, strides = c(1, 1), padding = "valid", data_format = NULL )
x |
Tensor or variable. |
kernel |
kernel tensor. |
output_shape |
1D int tensor for the output shape. |
strides |
strides list. |
padding |
string, |
data_format |
string, |
A tensor, result of transposed 2D convolution.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
3D convolution.
k_conv3d( x, kernel, strides = c(1, 1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1, 1) )
k_conv3d( x, kernel, strides = c(1, 1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1, 1) )
x |
Tensor or variable. |
kernel |
kernel tensor. |
strides |
strides |
padding |
string, |
data_format |
string, |
dilation_rate |
list of 3 integers. |
A tensor, result of 3D convolution.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
3D deconvolution (i.e. transposed convolution).
k_conv3d_transpose( x, kernel, output_shape, strides = c(1, 1, 1), padding = "valid", data_format = NULL )
k_conv3d_transpose( x, kernel, output_shape, strides = c(1, 1, 1), padding = "valid", data_format = NULL )
x |
input tensor. |
kernel |
kernel tensor. |
output_shape |
1D int tensor for the output shape. |
strides |
strides |
padding |
string, "same" or "valid". |
data_format |
string, |
A tensor, result of transposed 3D convolution.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Computes cos of x element-wise.
k_cos(x)
k_cos(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the static number of elements in a Keras variable or tensor.
k_count_params(x)
k_count_params(x)
x |
Keras variable or tensor. |
Integer, the number of elements in x
, i.e., the product of the array's static dimensions.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Runs CTC loss algorithm on each batch element.
k_ctc_batch_cost(y_true, y_pred, input_length, label_length)
k_ctc_batch_cost(y_true, y_pred, input_length, label_length)
y_true |
tensor |
y_pred |
tensor |
input_length |
tensor |
label_length |
tensor |
Tensor with shape (samples,1) containing the CTC loss of each element.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Can use either greedy search (also known as best path) or a constrained dictionary search.
k_ctc_decode( y_pred, input_length, greedy = TRUE, beam_width = 100L, top_paths = 1 )
k_ctc_decode( y_pred, input_length, greedy = TRUE, beam_width = 100L, top_paths = 1 )
y_pred |
tensor |
input_length |
tensor |
greedy |
perform much faster best-path search if |
beam_width |
if |
top_paths |
if |
If greedy
is TRUE
, returns a list of one element
that contains the decoded sequence. If FALSE
, returns the top_paths
most probable decoded sequences. Important: blank labels are returned as
-1
. Tensor (top_paths)
that contains the log probability of each
decoded sequence.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Converts CTC labels from dense to sparse.
k_ctc_label_dense_to_sparse(labels, label_lengths)
k_ctc_label_dense_to_sparse(labels, label_lengths)
labels |
dense CTC labels. |
label_lengths |
length of the labels. |
A sparse tensor representation of the labels.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Cumulative product of the values in a tensor, alongside the specified axis.
k_cumprod(x, axis = 1)
k_cumprod(x, axis = 1)
x |
A tensor or variable. |
axis |
An integer, the axis to compute the product (axis indexes are 1-based). |
A tensor of the cumulative product of values of x
along axis
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Cumulative sum of the values in a tensor, alongside the specified axis.
k_cumsum(x, axis = 1)
k_cumsum(x, axis = 1)
x |
A tensor or variable. |
axis |
An integer, the axis to compute the sum (axis indexes are 1-based). |
A tensor of the cumulative sum of values of x
along axis
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Depthwise 2D convolution with separable filters.
k_depthwise_conv2d( x, depthwise_kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
k_depthwise_conv2d( x, depthwise_kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
x |
input tensor |
depthwise_kernel |
convolution kernel for the depthwise convolution. |
strides |
strides (length 2). |
padding |
string, |
data_format |
string, |
dilation_rate |
vector of integers, dilation rates for the separable convolution. |
Output tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
When attempting to multiply a nD tensor
with a nD tensor, it reproduces the Theano behavior.
(e.g. (2, 3) * (4, 3, 5) -> (2, 4, 5)
)
k_dot(x, y)
k_dot(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A tensor, dot product of x
and y
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
to zero at random, while scaling the entire tensor.Sets entries in x
to zero at random, while scaling the entire tensor.
k_dropout(x, level, noise_shape = NULL, seed = NULL)
k_dropout(x, level, noise_shape = NULL, seed = NULL)
x |
tensor |
level |
fraction of the entries in the tensor that will be set to 0. |
noise_shape |
shape for randomly generated keep/drop flags, must be
broadcastable to the shape of |
seed |
random seed to ensure determinism. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the dtype of a Keras tensor or variable, as a string.
k_dtype(x)
k_dtype(x)
x |
Tensor or variable. |
String, dtype of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Exponential linear unit.
k_elu(x, alpha = 1)
k_elu(x, alpha = 1)
x |
A tensor or variable to compute the activation function for. |
alpha |
A scalar, slope of negative section. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Fuzz factor used in numeric expressions.
k_epsilon() k_set_epsilon(e)
k_epsilon() k_set_epsilon(e)
e |
float. New value of epsilon. |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise equality between two tensors.
k_equal(x, y)
k_equal(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Evaluates the value of a variable.
k_eval(x)
k_eval(x)
x |
A variable. |
An R array.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise exponential.
k_exp(x)
k_exp(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
axis
.Adds a 1-sized dimension at index axis
.
k_expand_dims(x, axis = -1)
k_expand_dims(x, axis = -1)
x |
A tensor or variable. |
axis |
Position where to add a new axis (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
A tensor with expanded dimensions.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiate an identity matrix and returns it.
k_eye(size, dtype = NULL, name = NULL)
k_eye(size, dtype = NULL, name = NULL)
size |
Integer, number of rows/columns. |
dtype |
String, data type of returned Keras variable. |
name |
String, name of returned Keras variable. |
A Keras variable, an identity matrix.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Flatten a tensor.
k_flatten(x)
k_flatten(x)
x |
A tensor or variable. |
A tensor, reshaped into 1-D
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Default float type
k_floatx() k_set_floatx(floatx)
k_floatx() k_set_floatx(floatx)
floatx |
String, 'float16', 'float32', or 'float64'. |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Reduce elems using fn to combine them from left to right.
k_foldl(fn, elems, initializer = NULL, name = NULL)
k_foldl(fn, elems, initializer = NULL, name = NULL)
fn |
Function that will be called upon each element in elems and an accumulator |
elems |
tensor |
initializer |
The first value used (first element of |
name |
A string name for the foldl node in the graph |
Tensor with same type and shape as initializer
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Reduce elems using fn to combine them from right to left.
k_foldr(fn, elems, initializer = NULL, name = NULL)
k_foldr(fn, elems, initializer = NULL, name = NULL)
fn |
Function that will be called upon each element in elems and an accumulator |
elems |
tensor |
initializer |
The first value used (last element of |
name |
A string name for the foldr node in the graph |
Tensor with same type and shape as initializer
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates a Keras function
k_function(inputs, outputs, updates = NULL, ...)
k_function(inputs, outputs, updates = NULL, ...)
inputs |
List of placeholder tensors. |
outputs |
List of output tensors. |
updates |
List of update ops. |
... |
Named arguments passed to |
Output values as R arrays.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
indices
in the tensor reference
.Retrieves the elements of indices indices
in the tensor reference
.
k_gather(reference, indices)
k_gather(reference, indices)
reference |
A tensor. |
indices |
Indices. Dimension indices are 1-based. Note however that if you pass a
tensor for |
A tensor of same type as reference
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
If a default TensorFlow session is available, we will return it. Else, we
will return the global Keras session. If no global Keras session exists at
this point: we will create a new global session. Note that you can manually
set the global session via k_set_session()
.
k_get_session() k_set_session(session)
k_get_session() k_set_session(session)
session |
A TensorFlow Session. |
A TensorFlow session
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Get the uid for the default graph.
k_get_uid(prefix = "")
k_get_uid(prefix = "")
prefix |
An optional prefix of the graph. |
A unique identifier for the graph.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the value of a variable.
k_get_value(x)
k_get_value(x)
x |
input variable. |
An R array.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the shape of a variable.
k_get_variable_shape(x)
k_get_variable_shape(x)
x |
A variable. |
A vector of integers.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
variables
w.r.t. loss
.Returns the gradients of variables
w.r.t. loss
.
k_gradients(loss, variables)
k_gradients(loss, variables)
loss |
Scalar tensor to minimize. |
variables |
List of variables. |
A gradients tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise truth value of (x > y).
k_greater(x, y)
k_greater(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise truth value of (x >= y).
k_greater_equal(x, y)
k_greater_equal(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Faster than sigmoid.
Returns 0.
if x < -2.5
, 1.
if x > 2.5
.
In -2.5 <= x <= 2.5
, returns 0.2 * x + 0.5
.
k_hard_sigmoid(x)
k_hard_sigmoid(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns a tensor with the same content as the input tensor.
k_identity(x, name = NULL)
k_identity(x, name = NULL)
x |
The input tensor. |
name |
String, name for the variable to create. |
A tensor of the same shape, type and content.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Default image data format convention ('channels_first' or 'channels_last').
k_image_data_format() k_set_image_data_format(data_format)
k_image_data_format() k_set_image_data_format(data_format)
data_format |
string. |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
in test phase, and alt
otherwise.Note that alt
should have the same shape as x
.
k_in_test_phase(x, alt, training = NULL)
k_in_test_phase(x, alt, training = NULL)
x |
What to return in test phase (tensor or function that returns a tensor). |
alt |
What to return otherwise (tensor or function that returns a tensor). |
training |
Optional scalar tensor (or R logical or integer) specifying the learning phase. |
Either x
or alt
based on k_learning_phase()
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
targets
are in the top k
predictions
.Returns whether the targets
are in the top k
predictions
.
k_in_top_k(predictions, targets, k)
k_in_top_k(predictions, targets, k)
predictions |
A tensor of shape |
targets |
A 1D tensor of length |
k |
An |
A 1D tensor of length batch_size
and type bool
. output[[i]]
is
TRUE
if predictions[i, targets[[i]]
is within top-k
values of
predictions[[i]]
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
in train phase, and alt
otherwise.Note that alt
should have the same shape as x
.
k_in_train_phase(x, alt, training = NULL)
k_in_train_phase(x, alt, training = NULL)
x |
What to return in train phase (tensor or function that returns a tensor). |
alt |
What to return otherwise (tensor or function that returns a tensor). |
training |
Optional scalar tensor (or R logical or integer) specifying the learning phase. |
Either x
or alt
based on the training
flag. the training
flag defaults to k_learning_phase()
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the shape of tensor or variable as a list of int or NULL entries.
k_int_shape(x)
k_int_shape(x)
x |
Tensor or variable. |
A list of integers (or NULL entries).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
is a Keras tensor.A "Keras tensor" is a tensor that was returned by a Keras layer
k_is_keras_tensor(x)
k_is_keras_tensor(x)
x |
A candidate tensor. |
A logical: Whether the argument is a Keras tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
is a placeholder.Returns whether x
is a placeholder.
k_is_placeholder(x)
k_is_placeholder(x)
x |
A candidate placeholder. |
A logical
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns whether a tensor is a sparse tensor.
k_is_sparse(tensor)
k_is_sparse(tensor)
tensor |
A tensor instance. |
A logical
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
is a symbolic tensor.Returns whether x
is a symbolic tensor.
k_is_tensor(x)
k_is_tensor(x)
x |
A candidate tensor. |
A logical: Whether the argument is a symbolic tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Normalizes a tensor wrt the L2 norm alongside the specified axis.
k_l2_normalize(x, axis = NULL)
k_l2_normalize(x, axis = NULL)
x |
Tensor or variable. |
axis |
Axis along which to perform normalization (axis indexes are 1-based) |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
The learning phase flag is a bool tensor (0 = test, 1 = train) to be passed as input to any Keras function that uses a different behavior at train time and test time.
k_learning_phase()
k_learning_phase()
Learning phase (scalar integer tensor or R integer).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise truth value of (x < y).
k_less(x, y)
k_less(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise truth value of (x <= y).
k_less_equal(x, y)
k_less_equal(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Apply 1D conv with un-shared weights.
k_local_conv1d(inputs, kernel, kernel_size, strides, data_format = NULL)
k_local_conv1d(inputs, kernel, kernel_size, strides, data_format = NULL)
inputs |
3D tensor with shape: (batch_size, steps, input_dim) |
kernel |
the unshared weight for convolution, with shape (output_length, feature_dim, filters) |
kernel_size |
a list of a single integer, specifying the length of the 1D convolution window |
strides |
a list of a single integer, specifying the stride length of the convolution |
data_format |
the data format, channels_first or channels_last |
the tensor after 1d conv with un-shared weights, with shape (batch_size, output_length, filters)
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Apply 2D conv with un-shared weights.
k_local_conv2d( inputs, kernel, kernel_size, strides, output_shape, data_format = NULL )
k_local_conv2d( inputs, kernel, kernel_size, strides, output_shape, data_format = NULL )
inputs |
4D tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'. |
kernel |
the unshared weight for convolution, with shape (output_items, feature_dim, filters) |
kernel_size |
a list of 2 integers, specifying the width and height of the 2D convolution window. |
strides |
a list of 2 integers, specifying the strides of the convolution along the width and height. |
output_shape |
a list with (output_row, output_col) |
data_format |
the data format, channels_first or channels_last |
A 4d tensor with shape: (batch_size, filters, new_rows, new_cols) if data_format='channels_first' or 4D tensor with shape: (batch_size, new_rows, new_cols, filters) if data_format='channels_last'.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise log.
k_log(x)
k_log(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
This boolean flag determines whether variables should be initialized as they
are instantiated (default), or if the user should handle the initialization
(e.g. via tf$initialize_all_variables()
).
k_manual_variable_initialization(value)
k_manual_variable_initialization(value)
value |
Logical |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Map the function fn over the elements elems and return the outputs.
k_map_fn(fn, elems, name = NULL, dtype = NULL)
k_map_fn(fn, elems, name = NULL, dtype = NULL)
fn |
Function that will be called upon each element in elems |
elems |
tensor |
name |
A string name for the map node in the graph |
dtype |
Output data type. |
Tensor with dtype dtype
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Maximum value in a tensor.
k_max(x, axis = NULL, keepdims = FALSE)
k_max(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, the axis to find maximum values (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with maximum values of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise maximum of two tensors.
k_maximum(x, y)
k_maximum(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Mean of a tensor, alongside the specified axis.
k_mean(x, axis = NULL, keepdims = FALSE)
k_mean(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
A list of axes to compute the mean over (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with the mean of elements of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Minimum value in a tensor.
k_min(x, axis = NULL, keepdims = FALSE)
k_min(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, axis to find minimum values (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with miminum values of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise minimum of two tensors.
k_minimum(x, y)
k_minimum(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Compute the moving average of a variable.
k_moving_average_update(x, value, momentum)
k_moving_average_update(x, value, momentum)
x |
A |
value |
A tensor with the same shape as |
momentum |
The moving average momentum. |
An operation to update the variable.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the number of axes in a tensor, as an integer.
k_ndim(x)
k_ndim(x)
x |
Tensor or variable. |
Integer (scalar), number of axes.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Computes mean and std for batch then apply batch_normalization on batch.
k_normalize_batch_in_training(x, gamma, beta, reduction_axes, epsilon = 0.001)
k_normalize_batch_in_training(x, gamma, beta, reduction_axes, epsilon = 0.001)
x |
Input tensor or variable. |
gamma |
Tensor by which to scale the input. |
beta |
Tensor with which to center the input. |
reduction_axes |
iterable of integers, axes over which to normalize. |
epsilon |
Fuzz factor. |
A list length of 3, (normalized_tensor, mean, variance)
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise inequality between two tensors.
k_not_equal(x, y)
k_not_equal(x, y)
x |
Tensor or variable. |
y |
Tensor or variable. |
A bool tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Computes the one-hot representation of an integer tensor.
k_one_hot(indices, num_classes)
k_one_hot(indices, num_classes)
indices |
nD integer tensor of shape |
num_classes |
Integer, number of classes to consider. |
(n + 1)D one hot representation of the input with shape
(batch_size, dim1, dim2, ... dim(n-1), num_classes)
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates an all-ones tensor variable and returns it.
k_ones(shape, dtype = NULL, name = NULL)
k_ones(shape, dtype = NULL, name = NULL)
shape |
Tuple of integers, shape of returned Keras variable. |
dtype |
String, data type of returned Keras variable. |
name |
String, name of returned Keras variable. |
A Keras variable, filled with 1.0
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates an all-ones variable of the same shape as another tensor.
k_ones_like(x, dtype = NULL, name = NULL)
k_ones_like(x, dtype = NULL, name = NULL)
x |
Keras variable or tensor. |
dtype |
String, dtype of returned Keras variable. NULL uses the dtype of x. |
name |
String, name for the variable to create. |
A Keras variable with the shape of x filled with ones.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Permutes axes in a tensor.
k_permute_dimensions(x, pattern)
k_permute_dimensions(x, pattern)
x |
Tensor or variable. |
pattern |
A list of dimension indices, e.g. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates a placeholder tensor and returns it.
k_placeholder( shape = NULL, ndim = NULL, dtype = NULL, sparse = FALSE, name = NULL )
k_placeholder( shape = NULL, ndim = NULL, dtype = NULL, sparse = FALSE, name = NULL )
shape |
Shape of the placeholder (integer list, may include |
ndim |
Number of axes of the tensor. At least one of |
dtype |
Placeholder type. |
sparse |
Logical, whether the placeholder should have a sparse type. |
name |
Optional name string for the placeholder. |
Tensor instance (with Keras metadata included).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
2D Pooling.
k_pool2d( x, pool_size, strides = c(1, 1), padding = "valid", data_format = NULL, pool_mode = "max" )
k_pool2d( x, pool_size, strides = c(1, 1), padding = "valid", data_format = NULL, pool_mode = "max" )
x |
Tensor or variable. |
pool_size |
list of 2 integers. |
strides |
list of 2 integers. |
padding |
string, |
data_format |
string, |
pool_mode |
string, |
A tensor, result of 2D pooling.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
3D Pooling.
k_pool3d( x, pool_size, strides = c(1, 1, 1), padding = "valid", data_format = NULL, pool_mode = "max" )
k_pool3d( x, pool_size, strides = c(1, 1, 1), padding = "valid", data_format = NULL, pool_mode = "max" )
x |
Tensor or variable. |
pool_size |
list of 3 integers. |
strides |
list of 3 integers. |
padding |
string, |
data_format |
string, |
pool_mode |
string, |
A tensor, result of 3D pooling.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise exponentiation.
k_pow(x, a)
k_pow(x, a)
x |
Tensor or variable. |
a |
R integer. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
message
and the tensor value when evaluated.Note that print_tensor
returns a new tensor identical to x
which should
be used in the following code. Otherwise the print operation is not taken
into account during evaluation.
k_print_tensor(x, message = "")
k_print_tensor(x, message = "")
x |
Tensor to print. |
message |
Message to print jointly with the tensor. |
The same tensor x
, unchanged.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Multiplies the values in a tensor, alongside the specified axis.
k_prod(x, axis = NULL, keepdims = FALSE)
k_prod(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, axis to compute the product over (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with the product of elements of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
k_random_binomial()
and k_random_bernoulli()
are aliases for the same
function. Both are maintained for backwards compatibility. New code
should prefer k_random_bernoulli()
.
k_random_binomial(shape, p = 0, dtype = NULL, seed = NULL) k_random_bernoulli(shape, p = 0, dtype = NULL, seed = NULL)
k_random_binomial(shape, p = 0, dtype = NULL, seed = NULL) k_random_bernoulli(shape, p = 0, dtype = NULL, seed = NULL)
shape |
A list of integers, the shape of tensor to create. |
p |
A float, |
dtype |
String, dtype of returned tensor. |
seed |
Integer, random seed. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns a tensor with normal distribution of values.
k_random_normal(shape, mean = 0, stddev = 1, dtype = NULL, seed = NULL)
k_random_normal(shape, mean = 0, stddev = 1, dtype = NULL, seed = NULL)
shape |
A list of integers, the shape of tensor to create. |
mean |
A float, mean of the normal distribution to draw samples. |
stddev |
A float, standard deviation of the normal distribution to draw samples. |
dtype |
String, dtype of returned tensor. |
seed |
Integer, random seed. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates a variable with values drawn from a normal distribution.
k_random_normal_variable( shape, mean, scale, dtype = NULL, name = NULL, seed = NULL )
k_random_normal_variable( shape, mean, scale, dtype = NULL, name = NULL, seed = NULL )
shape |
Tuple of integers, shape of returned Keras variable. |
mean |
Float, mean of the normal distribution. |
scale |
Float, standard deviation of the normal distribution. |
dtype |
String, dtype of returned Keras variable. |
name |
String, name of returned Keras variable. |
seed |
Integer, random seed. |
A Keras variable, filled with drawn samples.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns a tensor with uniform distribution of values.
k_random_uniform(shape, minval = 0, maxval = 1, dtype = NULL, seed = NULL)
k_random_uniform(shape, minval = 0, maxval = 1, dtype = NULL, seed = NULL)
shape |
A list of integers, the shape of tensor to create. |
minval |
A float, lower boundary of the uniform distribution to draw samples. |
maxval |
A float, upper boundary of the uniform distribution to draw samples. |
dtype |
String, dtype of returned tensor. |
seed |
Integer, random seed. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates a variable with values drawn from a uniform distribution.
k_random_uniform_variable( shape, low, high, dtype = NULL, name = NULL, seed = NULL )
k_random_uniform_variable( shape, low, high, dtype = NULL, name = NULL, seed = NULL )
shape |
Tuple of integers, shape of returned Keras variable. |
low |
Float, lower boundary of the output interval. |
high |
Float, upper boundary of the output interval. |
dtype |
String, dtype of returned Keras variable. |
name |
String, name of returned Keras variable. |
seed |
Integer, random seed. |
A Keras variable, filled with drawn samples.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
With default values, it returns element-wise max(x, 0)
.
k_relu(x, alpha = 0, max_value = NULL)
k_relu(x, alpha = 0, max_value = NULL)
x |
A tensor or variable. |
alpha |
A scalar, slope of negative section (default= |
max_value |
Saturation threshold. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
If x has shape (samples, dim) and n is 2, the output will have shape (samples, 2, dim).
k_repeat(x, n)
k_repeat(x, n)
x |
Tensor or variable. |
n |
Integer, number of times to repeat. |
A tensor
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
If x
has shape (s1, s2, s3)
and axis
is 2
, the output
will have shape (s1, s2 * rep, s3)
.
k_repeat_elements(x, rep, axis)
k_repeat_elements(x, rep, axis)
x |
Tensor or variable. |
rep |
Integer, number of times to repeat. |
axis |
Axis along which to repeat (axis indexes are 1-based) |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Reset graph identifiers.
k_reset_uids()
k_reset_uids()
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Reshapes a tensor to the specified shape.
k_reshape(x, shape)
k_reshape(x, shape)
x |
Tensor or variable. |
shape |
Target shape list. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Resizes the images contained in a 4D tensor.
k_resize_images(x, height_factor, width_factor, data_format)
k_resize_images(x, height_factor, width_factor, data_format)
x |
Tensor or variable to resize. |
height_factor |
Positive integer. |
width_factor |
Positive integer. |
data_format |
string, |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Resizes the volume contained in a 5D tensor.
k_resize_volumes(x, depth_factor, height_factor, width_factor, data_format)
k_resize_volumes(x, depth_factor, height_factor, width_factor, data_format)
x |
Tensor or variable to resize. |
depth_factor |
Positive integer. |
height_factor |
Positive integer. |
width_factor |
Positive integer. |
data_format |
string, |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Reverse a tensor along the specified axes.
k_reverse(x, axes)
k_reverse(x, axes)
x |
Tensor to reverse. |
axes |
Integer or list of integers of axes to reverse (axis indexes are 1-based). |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Iterates over the time dimension of a tensor
k_rnn( step_function, inputs, initial_states, go_backwards = FALSE, mask = NULL, constants = NULL, unroll = FALSE, input_length = NULL )
k_rnn( step_function, inputs, initial_states, go_backwards = FALSE, mask = NULL, constants = NULL, unroll = FALSE, input_length = NULL )
step_function |
RNN step function. |
inputs |
Tensor with shape (samples, ...) (no time dimension), representing input for the batch of samples at a certain time step. |
initial_states |
Tensor with shape (samples, output_dim) (no time dimension), containing the initial values for the states used in the step function. |
go_backwards |
Logical If |
mask |
Binary tensor with shape (samples, time, 1), with a zero for every element that is masked. |
constants |
A list of constant values passed at each step. |
unroll |
Whether to unroll the RNN or to use a symbolic loop (while_loop or scan depending on backend). |
input_length |
Not relevant in the TensorFlow implementation. Must be specified if using unrolling with Theano. |
A list with:
last_output
: the latest output of the rnn, of shape (samples, ...)
outputs
: tensor with shape (samples, time, ...) where each entry
outputs[s, t]
is the output of the step function at time t for sample s.
new_states
: list of tensors, latest states returned by the step
function, of shape (samples, ...).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
In case of tie, the rounding mode used is "half to even".
k_round(x)
k_round(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
2D convolution with separable filters.
k_separable_conv2d( x, depthwise_kernel, pointwise_kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
k_separable_conv2d( x, depthwise_kernel, pointwise_kernel, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = c(1, 1) )
x |
input tensor |
depthwise_kernel |
convolution kernel for the depthwise convolution. |
pointwise_kernel |
kernel for the 1x1 convolution. |
strides |
strides list (length 2). |
padding |
string, |
data_format |
string, |
dilation_rate |
list of integers, dilation rates for the separable convolution. |
Output tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Sets the learning phase to a fixed value.
k_set_learning_phase(value)
k_set_learning_phase(value)
value |
Learning phase value, either 0 or 1 (integers). |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Sets the value of a variable, from an R array.
k_set_value(x, value)
k_set_value(x, value)
x |
Tensor to set to a new value. |
value |
Value to set the tensor to, as an R array (of the same shape). |
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Returns the symbolic shape of a tensor or variable.
k_shape(x)
k_shape(x)
x |
A tensor or variable. |
A symbolic shape (which is itself a tensor).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise sigmoid.
k_sigmoid(x)
k_sigmoid(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise sign.
k_sign(x)
k_sign(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Computes sin of x element-wise.
k_sin(x)
k_sin(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Softmax of a tensor.
k_softmax(x, axis = -1)
k_softmax(x, axis = -1)
x |
A tensor or variable. |
axis |
The dimension softmax would be performed on. The default is -1 which indicates the last dimension. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Softplus of a tensor.
k_softplus(x)
k_softplus(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Softsign of a tensor.
k_softsign(x)
k_softsign(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Categorical crossentropy with integer targets.
k_sparse_categorical_crossentropy( target, output, from_logits = FALSE, axis = -1 )
k_sparse_categorical_crossentropy( target, output, from_logits = FALSE, axis = -1 )
target |
An integer tensor. |
output |
A tensor resulting from a softmax (unless |
from_logits |
Boolean, whether |
axis |
Axis (axis indexes are 1-based). Pass -1 (the default) to select the last axis. |
Output tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Pads the 2nd and 3rd dimensions of a 4D tensor.
k_spatial_2d_padding( x, padding = list(list(1, 1), list(1, 1)), data_format = NULL )
k_spatial_2d_padding( x, padding = list(list(1, 1), list(1, 1)), data_format = NULL )
x |
Tensor or variable. |
padding |
Tuple of 2 lists, padding pattern. |
data_format |
string, |
A padded 4D tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Pads these dimensions with respectively padding[[1]]
, padding[[2]]
, and
padding[[3]]
zeros left and right. For 'channels_last' data_format, the
2nd, 3rd and 4th dimension will be padded. For 'channels_first' data_format,
the 3rd, 4th and 5th dimension will be padded.
k_spatial_3d_padding( x, padding = list(list(1, 1), list(1, 1), list(1, 1)), data_format = NULL )
k_spatial_3d_padding( x, padding = list(list(1, 1), list(1, 1), list(1, 1)), data_format = NULL )
x |
Tensor or variable. |
padding |
List of 3 lists, padding pattern. |
data_format |
string, |
A padded 5D tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise square root.
k_sqrt(x)
k_sqrt(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise square.
k_square(x)
k_square(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
axis
.Removes a 1-dimension from the tensor at index axis
.
k_squeeze(x, axis = NULL)
k_squeeze(x, axis = NULL)
x |
A tensor or variable. |
axis |
Axis to drop (axis indexes are 1-based). |
A tensor with the same data as x
but reduced dimensions.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
R
tensors into a rank R+1
tensor.Stacks a list of rank R
tensors into a rank R+1
tensor.
k_stack(x, axis = 1)
k_stack(x, axis = 1)
x |
List of tensors. |
axis |
Axis along which to perform stacking (axis indexes are 1-based). |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Standard deviation of a tensor, alongside the specified axis.
k_std(x, axis = NULL, keepdims = FALSE)
k_std(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, the axis to compute the standard deviation over (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with the standard deviation of elements of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
variables
but with zero gradient w.r.t. every other variable.Returns variables
but with zero gradient w.r.t. every other variable.
k_stop_gradient(variables)
k_stop_gradient(variables)
variables |
tensor or list of tensors to consider constant with respect to any other variable. |
A single tensor or a list of tensors (depending on the passed argument) that has constant gradient with respect to any other variable.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Sum of the values in a tensor, alongside the specified axis.
k_sum(x, axis = NULL, keepdims = FALSE)
k_sum(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, the axis to sum over (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with sum of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Note that both then_expression
and else_expression
should be symbolic tensors of the same shape.
k_switch(condition, then_expression, else_expression)
k_switch(condition, then_expression, else_expression)
condition |
tensor ( |
then_expression |
either a tensor, or a function that returns a tensor. |
else_expression |
either a tensor, or a function that returns a tensor. |
The selected tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Element-wise tanh.
k_tanh(x)
k_tanh(x)
x |
A tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Pads the middle dimension of a 3D tensor.
k_temporal_padding(x, padding = c(1, 1))
k_temporal_padding(x, padding = c(1, 1))
x |
Tensor or variable. |
padding |
List of 2 integers, how many zeros to add at the start and end of dim 1. |
A padded 3D tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
by n
.Creates a tensor by tiling x
by n
.
k_tile(x, n)
k_tile(x, n)
x |
A tensor or variable |
n |
A list of integers. The length must be the same as the number of dimensions in |
A tiled tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Converts a sparse tensor into a dense tensor and returns it.
k_to_dense(tensor)
k_to_dense(tensor)
tensor |
A tensor instance (potentially sparse). |
A dense tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Transposes a tensor and returns it.
k_transpose(x)
k_transpose(x)
x |
Tensor or variable. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than two standard deviations from the mean are dropped and re-picked.
k_truncated_normal(shape, mean = 0, stddev = 1, dtype = NULL, seed = NULL)
k_truncated_normal(shape, mean = 0, stddev = 1, dtype = NULL, seed = NULL)
shape |
A list of integers, the shape of tensor to create. |
mean |
Mean of the values. |
stddev |
Standard deviation of the values. |
dtype |
String, dtype of returned tensor. |
seed |
Integer, random seed. |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
R
tensor into a list of rank R-1
tensors.Unstack rank R
tensor into a list of rank R-1
tensors.
k_unstack(x, axis = 1L, num = NULL, name = NULL)
k_unstack(x, axis = 1L, num = NULL, name = NULL)
x |
a tensor. |
axis |
Axis along which to perform stacking (axis indexes are 1-based).
Negative values wrap around, so the valid range is |
num |
An int. The length of the dimension axis. Automatically inferred if NULL (the default). |
name |
A name for the operation (optional). |
A tensor.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
to new_x
.Update the value of x
to new_x
.
k_update(x, new_x)
k_update(x, new_x)
x |
A |
new_x |
A tensor of same shape as |
The variable x
updated.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
by adding increment
.Update the value of x
by adding increment
.
k_update_add(x, increment)
k_update_add(x, increment)
x |
A |
increment |
A tensor of same shape as |
The variable x
updated.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
x
by subtracting decrement
.Update the value of x
by subtracting decrement
.
k_update_sub(x, decrement)
k_update_sub(x, decrement)
x |
A |
decrement |
A tensor of same shape as |
The variable x
updated.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Variance of a tensor, alongside the specified axis.
k_var(x, axis = NULL, keepdims = FALSE)
k_var(x, axis = NULL, keepdims = FALSE)
x |
A tensor or variable. |
axis |
An integer, the axis to compute the variance over (axis indexes are 1-based). |
keepdims |
A boolean, whether to keep the dimensions or not. If
|
A tensor with the variance of elements of x
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates a variable and returns it.
k_variable(value, dtype = NULL, name = NULL, constraint = NULL)
k_variable(value, dtype = NULL, name = NULL, constraint = NULL)
value |
Numpy array, initial value of the tensor. |
dtype |
Tensor type. |
name |
Optional name string for the tensor. |
constraint |
Optional projection function to be applied to the variable after an optimizer update. |
A variable instance (with Keras metadata included).
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates an all-zeros variable and returns it.
k_zeros(shape, dtype = NULL, name = NULL)
k_zeros(shape, dtype = NULL, name = NULL)
shape |
Tuple of integers, shape of returned Keras variable |
dtype |
String, data type of returned Keras variable |
name |
String, name of returned Keras variable |
A variable (including Keras metadata), filled with 0.0
.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
Instantiates an all-zeros variable of the same shape as another tensor.
k_zeros_like(x, dtype = NULL, name = NULL)
k_zeros_like(x, dtype = NULL, name = NULL)
x |
Keras variable or Keras tensor. |
dtype |
String, dtype of returned Keras variable. NULL uses the dtype of x. |
name |
String, name for the variable to create. |
A Keras variable with the shape of x filled with zeros.
This function is part of a set of Keras backend functions that enable lower level access to the core operations of the backend tensor engine (e.g. TensorFlow, CNTK, Theano, etc.).
You can see a list of all available backend functions here: https://tensorflow.rstudio.com/reference/keras/index.html#backend.
The keras
module object is the equivalent of
keras <- tensorflow::tf$keras
and provided mainly as a convenience.
keras
keras
An object of class python.builtin.module
(inherits from python.builtin.object
) of length 0.
the keras Python module
Convert an R vector, matrix, or array object to an array that has the optimal in-memory layout and floating point data type for the current Keras backend.
keras_array(x, dtype = NULL)
keras_array(x, dtype = NULL)
x |
Object or list of objects to convert |
dtype |
NumPy data type (e.g. float32, float64). If this is unspecified then R doubles will be converted to the default floating point type for the current Keras backend. |
Keras does frequent row-oriented access to arrays (for shuffling and drawing batches) so the order of arrays created by this function is always row-oriented ("C" as opposed to "Fortran" ordering, which is the default for R arrays).
If the passed array is already a NumPy array with the desired dtype
and "C"
order then it is returned unmodified (no additional copies are made).
NumPy array with the specified dtype
(or list of NumPy arrays if a
list was passed for x
).
A model is a directed acyclic graph of layers.
keras_model(inputs, outputs = NULL, ...)
keras_model(inputs, outputs = NULL, ...)
inputs |
Input layer |
outputs |
Output layer |
... |
Any additional arguments |
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
## Not run: library(keras) # input layer inputs <- layer_input(shape = c(784)) # outputs compose input + dense layers predictions <- inputs %>% layer_dense(units = 64, activation = 'relu') %>% layer_dense(units = 64, activation = 'relu') %>% layer_dense(units = 10, activation = 'softmax') # create and compile model model <- keras_model(inputs = inputs, outputs = predictions) model %>% compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = c('accuracy') ) ## End(Not run)
## Not run: library(keras) # input layer inputs <- layer_input(shape = c(784)) # outputs compose input + dense layers predictions <- inputs %>% layer_dense(units = 64, activation = 'relu') %>% layer_dense(units = 64, activation = 'relu') %>% layer_dense(units = 10, activation = 'softmax') # create and compile model model <- keras_model(inputs = inputs, outputs = predictions) model %>% compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = c('accuracy') ) ## End(Not run)
Keras Model composed of a linear stack of layers
keras_model_sequential(layers = NULL, name = NULL, ...)
keras_model_sequential(layers = NULL, name = NULL, ...)
layers |
List of layers to add to the model |
name |
Name of model |
... |
Arguments passed on to
|
If any arguments are provided to ...
, then the sequential model is
initialized with a InputLayer
instance. If not, then the first layer passed
to a Sequential model should have a defined input shape. What that means is
that it should have received an input_shape
or batch_input_shape
argument, or for some type of layers (recurrent, Dense...) an input_dim
argument.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
## Not run: library(keras) model <- keras_model_sequential() model %>% layer_dense(units = 32, input_shape = c(784)) %>% layer_activation('relu') %>% layer_dense(units = 10) %>% layer_activation('softmax') model %>% compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = c('accuracy') ) # alternative way to provide input shape model <- keras_model_sequential(input_shape = c(784)) %>% layer_dense(units = 32) %>% layer_activation('relu') %>% layer_dense(units = 10) %>% layer_activation('softmax') ## End(Not run)
## Not run: library(keras) model <- keras_model_sequential() model %>% layer_dense(units = 32, input_shape = c(784)) %>% layer_activation('relu') %>% layer_dense(units = 10) %>% layer_activation('softmax') model %>% compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = c('accuracy') ) # alternative way to provide input shape model <- keras_model_sequential(input_shape = c(784)) %>% layer_dense(units = 32) %>% layer_activation('relu') %>% layer_dense(units = 10) %>% layer_activation('softmax') ## End(Not run)
Apply an activation function to an output.
layer_activation( object, activation, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation( object, activation, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
activation |
Name of activation function to use. If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x). |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other core layers:
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
Other activation layers:
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
It follows: f(x) = alpha * (exp(x) - 1.0)
for x < 0
, f(x) = x
for x >= 0
.
layer_activation_elu( object, alpha = 1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_elu( object, alpha = 1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
alpha |
Scale for the negative factor. |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs).
Other activation layers:
layer_activation()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
Allows a small gradient when the unit is not active: f(x) = alpha * x
for
x < 0
, f(x) = x
for x >= 0
.
layer_activation_leaky_relu( object, alpha = 0.3, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_leaky_relu( object, alpha = 0.3, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
alpha |
float >= 0. Negative slope coefficient. |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Rectifier Nonlinearities Improve Neural Network Acoustic Models.
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
It follows: f(x) = alpha * x`` for
x < 0,
f(x) = xfor
x >= 0', where
alpha is a learned array with the same shape as x.
layer_activation_parametric_relu( object, alpha_initializer = "zeros", alpha_regularizer = NULL, alpha_constraint = NULL, shared_axes = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_parametric_relu( object, alpha_initializer = "zeros", alpha_regularizer = NULL, alpha_constraint = NULL, shared_axes = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
alpha_initializer |
Initializer function for the weights. |
alpha_regularizer |
Regularizer for the weights. |
alpha_constraint |
Constraint for the weights. |
shared_axes |
The axes along which to share learnable parameters for the activation function. For example, if the incoming feature maps are from a 2D convolution with output shape (batch, height, width, channels), and you wish to share parameters across space so that each filter only has one set of parameters, set shared_axes=c(1, 2). |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification.
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
Rectified Linear Unit activation function
layer_activation_relu( object, max_value = NULL, negative_slope = 0, threshold = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_relu( object, max_value = NULL, negative_slope = 0, threshold = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
max_value |
loat, the maximum output value. |
negative_slope |
float >= 0 Negative slope coefficient. |
threshold |
float. Threshold value for thresholded activation. |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
SELU is equal to: scale * elu(x, alpha)
, where alpha and scale
are pre-defined constants.
layer_activation_selu( object, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_selu( object, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
The values of alpha
and scale
are
chosen so that the mean and variance of the inputs are preserved
between two consecutive layers as long as the weights are initialized
correctly (see initializer_lecun_normal) and the number of inputs
is "large enough" (see article for more information).
Note:
To be used together with the initialization "lecun_normal".
To be used together with the dropout variant "AlphaDropout".
Self-Normalizing Neural Networks, initializer_lecun_normal
, layer_alpha_dropout
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_softmax()
,
layer_activation_thresholded_relu()
It follows: f(x) = alpha * (exp(x) - 1.0)
for x < 0
, f(x) = x
for x >= 0
.
layer_activation_softmax( object, axis = -1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_softmax( object, axis = -1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
axis |
Integer, axis along which the softmax normalization is applied. |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_thresholded_relu()
It follows: f(x) = x
for x > theta
, f(x) = 0
otherwise.
layer_activation_thresholded_relu( object, theta = 1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activation_thresholded_relu( object, theta = 1, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
theta |
float >= 0. Threshold location of activation. |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Zero-bias autoencoders and the benefits of co-adapting features.
Other activation layers:
layer_activation()
,
layer_activation_elu()
,
layer_activation_leaky_relu()
,
layer_activation_parametric_relu()
,
layer_activation_relu()
,
layer_activation_selu()
,
layer_activation_softmax()
Layer that applies an update to the cost function based input activity.
layer_activity_regularization( object, l1 = 0, l2 = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_activity_regularization( object, l1 = 0, l2 = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
l1 |
L1 regularization factor (positive float). |
l2 |
L2 regularization factor (positive float). |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Arbitrary. Use the keyword argument input_shape
(list
of integers, does not include the samples axis) when using this layer as
the first layer in a model.
Same shape as input.
Other core layers:
layer_activation()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
layer_add(inputs, ...)
layer_add(inputs, ...)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the sum of the inputs. If inputs
is missing, a keras
layer instance is returned.
Additive attention layer, a.k.a. Bahdanau-style attention
layer_additive_attention( object, use_scale = TRUE, ..., causal = FALSE, dropout = 0 )
layer_additive_attention( object, use_scale = TRUE, ..., causal = FALSE, dropout = 0 )
object |
What to compose the new
|
use_scale |
If |
... |
standard layer arguments. |
causal |
Boolean. Set to |
dropout |
Float between 0 and 1. Fraction of the units to drop for the attention scores. |
Inputs are query
tensor of shape [batch_size, Tq, dim]
, value
tensor of
shape [batch_size, Tv, dim]
and key
tensor of shape
[batch_size, Tv, dim]
. The calculation follows the steps:
Reshape query
and key
into shapes [batch_size, Tq, 1, dim]
and [batch_size, 1, Tv, dim]
respectively.
Calculate scores with shape [batch_size, Tq, Tv]
as a non-linear
sum: scores = tf.reduce_sum(tf.tanh(query + key), axis=-1)
Use scores to calculate a distribution with shape
[batch_size, Tq, Tv]
: distribution = tf$nn$softmax(scores)
.
Use distribution
to create a linear combination of value
with
shape [batch_size, Tq, dim]
:
return tf$matmul(distribution, value)
.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/AdditiveAttention
https://keras.io/api/layers/attention_layers/additive_attention/
Alpha Dropout is a dropout that keeps mean and variance of inputs to their original values, in order to ensure the self-normalizing property even after this dropout.
layer_alpha_dropout(object, rate, noise_shape = NULL, seed = NULL, ...)
layer_alpha_dropout(object, rate, noise_shape = NULL, seed = NULL, ...)
object |
What to compose the new
|
rate |
float, drop probability (as with |
noise_shape |
Noise shape |
seed |
An integer to use as random seed. |
... |
standard layer arguments. |
Alpha Dropout fits well to Scaled Exponential Linear Units by randomly setting activations to the negative saturation value.
Arbitrary. Use the keyword argument input_shape
(list
of integers, does not include the samples axis) when using this layer as
the first layer in a model.
Same shape as input.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/AlphaDropout
Other noise layers:
layer_gaussian_dropout()
,
layer_gaussian_noise()
Dot-product attention layer, a.k.a. Luong-style attention
layer_attention( inputs, use_scale = FALSE, score_mode = "dot", ..., dropout = NULL )
layer_attention( inputs, use_scale = FALSE, score_mode = "dot", ..., dropout = NULL )
inputs |
List of the following tensors:
|
use_scale |
If |
score_mode |
Function to use to compute attention scores, one of
|
... |
standard layer arguments (e.g., batch_size, dtype, name, trainable, weights) |
dropout |
Float between 0 and 1. Fraction of the units to drop for the attention scores. Defaults to 0.0. |
inputs are query
tensor of shape [batch_size, Tq, dim]
, value
tensor
of shape [batch_size, Tv, dim]
and key
tensor of shape
[batch_size, Tv, dim]
. The calculation follows the steps:
Calculate scores with shape [batch_size, Tq, Tv]
as a query
-key
dot
product: scores = tf$matmul(query, key, transpose_b=TRUE)
.
Use scores to calculate a distribution with shape
[batch_size, Tq, Tv]
: distribution = tf$nn$softmax(scores)
.
Use distribution
to create a linear combination of value
with
shape [batch_size, Tq, dim]
:
return tf$matmul(distribution, value)
.
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
layer_average(inputs, ...)
layer_average(inputs, ...)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the average of the inputs. If inputs
is missing, a keras
layer instance is returned.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/average
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Average
Other merge layers:
layer_concatenate()
,
layer_dot()
,
layer_maximum()
,
layer_minimum()
,
layer_multiply()
,
layer_subtract()
Average pooling for temporal data.
layer_average_pooling_1d( object, pool_size = 2L, strides = NULL, padding = "valid", data_format = "channels_last", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_average_pooling_1d( object, pool_size = 2L, strides = NULL, padding = "valid", data_format = "channels_last", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
Integer, size of the average pooling windows. |
strides |
Integer, or NULL. Factor by which to downscale. E.g. 2 will
halve the input. If NULL, it will default to |
padding |
One of |
data_format |
One of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch_size, steps, features)
.
3D tensor with shape: (batch_size, downsampled_steps, features)
.
Other pooling layers:
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Average pooling operation for spatial data.
layer_average_pooling_2d( object, pool_size = c(2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_average_pooling_2d( object, pool_size = c(2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
integer or list of 2 integers, factors by which to downscale (vertical, horizontal). (2, 2) will halve the input in both spatial dimension. If only one integer is specified, the same window length will be used for both dimensions. |
strides |
Integer, list of 2 integers, or NULL. Strides values. If NULL,
it will default to |
padding |
One of |
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
If data_format='channels_last'
: 4D tensor with shape: (batch_size, rows, cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, rows, cols)
If data_format='channels_last'
: 4D tensor with shape: (batch_size, pooled_rows, pooled_cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, pooled_rows, pooled_cols)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Average pooling operation for 3D data (spatial or spatio-temporal).
layer_average_pooling_3d( object, pool_size = c(2L, 2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_average_pooling_3d( object, pool_size = c(2L, 2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
list of 3 integers, factors by which to downscale (dim1, dim2, dim3). (2, 2, 2) will halve the size of the 3D input in each dimension. |
strides |
list of 3 integers, or NULL. Strides values. |
padding |
One of |
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
If data_format='channels_last'
: 5D tensor with shape: (batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)
If data_format='channels_last'
: 5D tensor with shape: (batch_size, pooled_dim1, pooled_dim2, pooled_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Layer that normalizes its inputs
layer_batch_normalization( object, axis = -1L, momentum = 0.99, epsilon = 0.001, center = TRUE, scale = TRUE, beta_initializer = "zeros", gamma_initializer = "ones", moving_mean_initializer = "zeros", moving_variance_initializer = "ones", beta_regularizer = NULL, gamma_regularizer = NULL, beta_constraint = NULL, gamma_constraint = NULL, synchronized = FALSE, ... )
layer_batch_normalization( object, axis = -1L, momentum = 0.99, epsilon = 0.001, center = TRUE, scale = TRUE, beta_initializer = "zeros", gamma_initializer = "ones", moving_mean_initializer = "zeros", moving_variance_initializer = "ones", beta_regularizer = NULL, gamma_regularizer = NULL, beta_constraint = NULL, gamma_constraint = NULL, synchronized = FALSE, ... )
object |
Layer or model object |
axis |
Integer, the axis that should be normalized (typically the features
axis). For instance, after a |
momentum |
Momentum for the moving average. |
epsilon |
Small float added to variance to avoid dividing by zero. |
center |
If |
scale |
If |
beta_initializer |
Initializer for the beta weight. |
gamma_initializer |
Initializer for the gamma weight. |
moving_mean_initializer |
Initializer for the moving mean. |
moving_variance_initializer |
Initializer for the moving variance. |
beta_regularizer |
Optional regularizer for the beta weight. |
gamma_regularizer |
Optional regularizer for the gamma weight. |
beta_constraint |
Optional constraint for the beta weight. |
gamma_constraint |
Optional constraint for the gamma weight. |
synchronized |
If |
... |
standard layer arguments. |
Batch normalization applies a transformation that maintains the mean output close to 0 and the output standard deviation close to 1.
Importantly, batch normalization works differently during training and during inference.
During training (i.e. when using fit()
or when calling the layer/model
with the argument training=TRUE
), the layer normalizes its output using
the mean and standard deviation of the current batch of inputs. That is to
say, for each channel being normalized, the layer returns
gamma * (batch - mean(batch)) / sqrt(var(batch) + epsilon) + beta
, where:
epsilon
is small constant (configurable as part of the constructor
arguments)
gamma
is a learned scaling factor (initialized as 1), which
can be disabled by passing scale=FALSE
to the constructor.
beta
is a learned offset factor (initialized as 0), which
can be disabled by passing center=FALSE
to the constructor.
During inference (i.e. when using evaluate()
or predict()
or when
calling the layer/model with the argument training=FALSE
(which is the
default), the layer normalizes its output using a moving average of the
mean and standard deviation of the batches it has seen during training. That
is to say, it returns
gamma * (batch - self.moving_mean) / sqrt(self.moving_var+epsilon) + beta
.
self$moving_mean
and self$moving_var
are non-trainable variables that
are updated each time the layer in called in training mode, as such:
moving_mean = moving_mean * momentum + mean(batch) * (1 - momentum)
moving_var = moving_var * momentum + var(batch) * (1 - momentum)
As such, the layer will only normalize its inputs during inference after having been trained on data that has similar statistics as the inference data.
When synchronized=TRUE
is set and if this layer is used within a
tf$distribute
strategy, there will be an allreduce
call
to aggregate batch statistics across all replicas at every
training step. Setting synchronized
has no impact when the model is
trained without specifying any distribution strategy.
Example usage:
strategy <- tf$distribute$MirroredStrategy() with(strategy$scope(), { model <- keras_model_sequential() model %>% layer_dense(16) %>% layer_batch_normalization(synchronized=TRUE) })
This layer provides options for condensing data into a categorical encoding
when the total number of tokens are known in advance. It accepts integer
values as inputs, and it outputs a dense or sparse representation of those
inputs. For integer inputs where the total number of tokens is not known, use
layer_integer_lookup()
instead.
layer_category_encoding( object, num_tokens = NULL, output_mode = "multi_hot", sparse = FALSE, ... )
layer_category_encoding( object, num_tokens = NULL, output_mode = "multi_hot", sparse = FALSE, ... )
object |
What to compose the new
|
num_tokens |
The total number of tokens the layer should support. All
inputs to the layer must integers in the range |
output_mode |
Specification for the output of the layer. Defaults to
For all output modes, currently only output up to rank 2 is supported. |
sparse |
Boolean. If |
... |
standard layer arguments. |
https://www.tensorflow.org/api_docs/python/tf/keras/layers/CategoryEncoding
https://keras.io/api/layers/preprocessing_layers/categorical/category_encoding/
Other categorical features preprocessing layers:
layer_hashing()
,
layer_integer_lookup()
,
layer_string_lookup()
Other preprocessing layers:
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Crop the central portion of the images to target height and width
layer_center_crop(object, height, width, ...)
layer_center_crop(object, height, width, ...)
object |
What to compose the new
|
height |
Integer, the height of the output shape. |
width |
Integer, the width of the output shape. |
... |
standard layer arguments. |
Input shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
Output shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., target_height, target_width, channels)
.
If the input height/width is even and the target height/width is odd (or inversely), the input image is left-padded by 1 pixel.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/CenterCrop
https://keras.io/api/layers/preprocessing_layers/image_preprocessing/center_crop
Other image preprocessing layers:
layer_rescaling()
,
layer_resizing()
Other preprocessing layers:
layer_category_encoding()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
It takes as input a list of tensors, all of the same shape expect for the concatenation axis, and returns a single tensor, the concatenation of all inputs.
layer_concatenate(inputs, ..., axis = -1)
layer_concatenate(inputs, ..., axis = -1)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
axis |
Concatenation axis. |
A tensor, the concatenation of the inputs alongside axis axis
. If
inputs
is missing, a keras layer instance is returned.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/concatenate
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Concatenate
Other merge layers:
layer_average()
,
layer_dot()
,
layer_maximum()
,
layer_minimum()
,
layer_multiply()
,
layer_subtract()
This layer creates a convolution kernel that is convolved with the layer
input over a single spatial (or temporal) dimension to produce a tensor of
outputs. If use_bias
is TRUE, a bias vector is created and added to the
outputs. Finally, if activation
is not NULL
, it is applied to the outputs
as well. When using this layer as the first layer in a model, provide an
input_shape
argument (list of integers or NULL
, e.g. (10, 128)
for
sequences of 10 vectors of 128-dimensional vectors, or (NULL, 128)
for
variable-length sequences of 128-dimensional vectors.
layer_conv_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = "channels_last", dilation_rate = 1L, groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = "channels_last", dilation_rate = 1L, groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of a single integer, specifying the length of the 1D convolution window. |
strides |
An integer or list of a single integer, specifying the stride
length of the convolution. Specifying any stride value != 1 is incompatible
with specifying any |
padding |
One of |
data_format |
A string, one of |
dilation_rate |
an integer or list of a single integer, specifying the
dilation rate to use for dilated convolution. Currently, specifying any
|
groups |
A positive integer specifying the number of groups in which the
input is split along the channel axis. Each group is convolved separately
with |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch_size, steps, input_dim)
3D tensor with shape: (batch_size, new_steps, filters)
steps
value might have changed due to padding or strides.
Other convolutional layers:
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
The need for transposed convolutions generally arises from the desire to use
a transformation going in the opposite direction of a normal convolution,
i.e., from something that has the shape of the output of some convolution to
something that has the shape of its input while maintaining a connectivity
pattern that is compatible with said convolution.
When using this layer as the first layer in a model,
provide the keyword argument input_shape
(tuple of integers, does not include the sample axis),
e.g. input_shape=(128, 3)
for data with 128 time steps and 3 channels.
layer_conv_1d_transpose( object, filters, kernel_size, strides = 1, padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = 1, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_1d_transpose( object, filters, kernel_size, strides = 1, padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = 1, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of a single integer, specifying the length of the 1D convolution window. |
strides |
An integer or list of a single integer, specifying the stride
length of the convolution. Specifying any stride value != 1 is incompatible
with specifying any |
padding |
one of |
output_padding |
An integer specifying the amount of padding along
the time dimension of the output tensor.
The amount of output padding must be lower than the stride.
If set to |
data_format |
A string, one of |
dilation_rate |
an integer or list of a single integer, specifying the
dilation rate to use for dilated convolution. Currently, specifying any
|
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch, steps, channels)
3D tensor with shape: (batch, new_steps, filters)
If output_padding
is specified:
new_timesteps = ((timesteps - 1) * strides + kernel_size - 2 * padding + output_padding)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
This layer creates a convolution kernel that is convolved with the layer
input to produce a tensor of outputs. If use_bias
is TRUE, a bias vector is
created and added to the outputs. Finally, if activation
is not NULL
, it
is applied to the outputs as well. When using this layer as the first layer
in a model, provide the keyword argument input_shape
(list of integers,
does not include the sample axis), e.g. input_shape=c(128, 128, 3)
for
128x128 RGB pictures in data_format="channels_last"
.
layer_conv_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L), groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L), groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
data_format |
A string, one of |
dilation_rate |
an integer or list of 2 integers, specifying the
dilation rate to use for dilated convolution. Can be a single integer to
specify the same value for all spatial dimensions. Currently, specifying
any |
groups |
A positive integer specifying the number of groups in which the
input is split along the channel axis. Each group is convolved separately
with |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape: (samples, channels, rows, cols)
if data_format='channels_first' or 4D tensor with shape: (samples, rows, cols, channels)
if data_format='channels_last'.
4D tensor with shape: (samples, filters, new_rows, new_cols)
if data_format='channels_first' or 4D tensor with shape:
(samples, new_rows, new_cols, filters)
if data_format='channels_last'.
rows
and cols
values might have changed due to padding.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
The need for transposed convolutions generally arises from the desire to use
a transformation going in the opposite direction of a normal convolution,
i.e., from something that has the shape of the output of some convolution to
something that has the shape of its input while maintaining a connectivity
pattern that is compatible with said convolution. When using this layer as
the first layer in a model, provide the keyword argument input_shape
(list
of integers, does not include the sample axis), e.g. input_shape=c(128L, 128L, 3L)
for 128x128 RGB pictures in data_format="channels_last"
.
layer_conv_2d_transpose( object, filters, kernel_size, strides = c(1, 1), padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = c(1, 1), activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_2d_transpose( object, filters, kernel_size, strides = c(1, 1), padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = c(1, 1), activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
output_padding |
An integer or list of 2 integers,
specifying the amount of padding along the height and width
of the output tensor. Can be a single integer to specify the same
value for all spatial dimensions. The amount of output padding along a
given dimension must be lower than the stride along that same dimension.
If set to |
data_format |
A string, one of |
dilation_rate |
Dialation rate. |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape: (batch, channels, rows, cols)
if data_format='channels_first' or 4D tensor with shape: (batch, rows, cols, channels)
if data_format='channels_last'.
4D tensor with shape: (batch, filters, new_rows, new_cols)
if data_format='channels_first' or 4D tensor with shape:
(batch, new_rows, new_cols, filters)
if data_format='channels_last'.
rows
and cols
values might have changed due to padding.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
This layer creates a convolution kernel that is convolved with the layer
input to produce a tensor of outputs. If use_bias
is TRUE, a bias vector is
created and added to the outputs. Finally, if activation
is not NULL
, it
is applied to the outputs as well. When using this layer as the first layer
in a model, provide the keyword argument input_shape
(list of integers,
does not include the sample axis), e.g. input_shape=c(128L, 128L, 128L, 3L)
for 128x128x128 volumes with a single channel, in
data_format="channels_last"
.
layer_conv_3d( object, filters, kernel_size, strides = c(1L, 1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L, 1L), groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_3d( object, filters, kernel_size, strides = c(1L, 1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L, 1L), groups = 1L, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 3 integers, specifying the depth, height, and width of the 3D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 3 integers, specifying the strides of
the convolution along each spatial dimension. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
data_format |
A string, one of |
dilation_rate |
an integer or list of 3 integers, specifying the
dilation rate to use for dilated convolution. Can be a single integer to
specify the same value for all spatial dimensions. Currently, specifying
any |
groups |
A positive integer specifying the number of groups in which the
input is split along the channel axis. Each group is convolved separately
with |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
5D tensor with shape: (samples, channels, conv_dim1, conv_dim2, conv_dim3)
if data_format='channels_first' or 5D tensor with
shape: (samples, conv_dim1, conv_dim2, conv_dim3, channels)
if
data_format='channels_last'.
5D tensor with shape: (samples, filters, new_conv_dim1, new_conv_dim2, new_conv_dim3)
if
data_format='channels_first' or 5D tensor with shape: (samples, new_conv_dim1, new_conv_dim2, new_conv_dim3, filters)
if
data_format='channels_last'. new_conv_dim1
, new_conv_dim2
and
new_conv_dim3
values might have changed due to padding.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
The need for transposed convolutions generally arises from the desire to use a transformation going in the opposite direction of a normal convolution, i.e., from something that has the shape of the output of some convolution to something that has the shape of its input while maintaining a connectivity pattern that is compatible with said convolution.
layer_conv_3d_transpose( object, filters, kernel_size, strides = c(1, 1, 1), padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = c(1L, 1L, 1L), activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_conv_3d_transpose( object, filters, kernel_size, strides = c(1, 1, 1), padding = "valid", output_padding = NULL, data_format = NULL, dilation_rate = c(1L, 1L, 1L), activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 3 integers, specifying the depth, height, and width of the 3D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 3 integers, specifying the strides of
the convolution along the depth, height and width.. Can be a single integer
to specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
output_padding |
An integer or list of 3 integers,
specifying the amount of padding along the depth, height, and width
of the output tensor. Can be a single integer to specify the same
value for all spatial dimensions. The amount of output padding along a
given dimension must be lower than the stride along that same dimension.
If set to |
data_format |
A string, one of |
dilation_rate |
An integer or vector of 3 integers, specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. |
activation |
Activation function to use. If you don't specify anything, no
activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation"). |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
When using this layer as the first layer in a model, provide the keyword argument
input_shape
(list of integers, does not include the sample axis), e.g.
input_shape = list(128, 128, 128, 3)
for a 128x128x128 volume with 3 channels if
data_format="channels_last"
.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
1D Convolutional LSTM
layer_conv_lstm_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = NULL, dilation_rate = 1L, activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, ... )
layer_conv_lstm_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = NULL, dilation_rate = 1L, activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, ... )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of n integers, specifying the dimensions of the convolution window. |
strides |
An integer or list of n integers, specifying the strides of
the convolution. Specifying any stride value != 1 is incompatible with
specifying any |
padding |
One of |
data_format |
A string, one of |
dilation_rate |
An integer or list of n integers, specifying the
dilation rate to use for dilated convolution. Currently, specifying any
|
activation |
Activation function to use. By default hyperbolic tangent
activation function is applied ( |
recurrent_activation |
Activation function to use for the recurrent step. |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
unit_forget_bias |
Boolean. If TRUE, add 1 to the bias of the forget gate at
initialization. Use in combination with |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. (default FALSE) |
return_state |
Boolean Whether to return the last state in addition to the output. (default FALSE) |
go_backwards |
Boolean (default FALSE). If TRUE, process the input sequence backwards. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
... |
standard layer arguments. |
Similar to an LSTM layer, but the input transformations and recurrent transformations are both convolutional.
It is similar to an LSTM layer, but the input transformations and recurrent transformations are both convolutional.
layer_conv_lstm_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L), activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL, input_shape = NULL )
layer_conv_lstm_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L), activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL, input_shape = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of n integers, specifying the dimensions of the convolution window. |
strides |
An integer or list of n integers, specifying the strides of
the convolution. Specifying any stride value != 1 is incompatible with
specifying any |
padding |
One of |
data_format |
A string, one of |
dilation_rate |
An integer or list of n integers, specifying the
dilation rate to use for dilated convolution. Currently, specifying any
|
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
recurrent_activation |
Activation function to use for the recurrent step. |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
unit_forget_bias |
Boolean. If TRUE, add 1 to the bias of the forget
gate at initialization. Use in combination with |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the
|
bias_constraint |
Constraint function applied to the bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. |
return_state |
Boolean. Whether to return the last state in addition to the output. |
go_backwards |
Boolean (default FALSE). If TRUE, rocess the input sequence backwards. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
if data_format='channels_first' 5D tensor with shape:
(samples,time, channels, rows, cols)
if data_format='channels_last' 5D
tensor with shape: (samples,time, rows, cols, channels)
Convolutional LSTM Network: A Machine Learning Approach for Precipitation Nowcasting The current implementation does not include the feedback loop on the cells output
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
3D Convolutional LSTM
layer_conv_lstm_3d( object, filters, kernel_size, strides = c(1L, 1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L, 1L), activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, ... )
layer_conv_lstm_3d( object, filters, kernel_size, strides = c(1L, 1L, 1L), padding = "valid", data_format = NULL, dilation_rate = c(1L, 1L, 1L), activation = "tanh", recurrent_activation = "hard_sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, dropout = 0, recurrent_dropout = 0, ... )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of n integers, specifying the dimensions of the convolution window. |
strides |
An integer or list of n integers, specifying the strides of
the convolution. Specifying any stride value != 1 is incompatible with
specifying any |
padding |
One of |
data_format |
A string, one of |
dilation_rate |
An integer or list of n integers, specifying the
dilation rate to use for dilated convolution. Currently, specifying any
|
activation |
Activation function to use. By default hyperbolic tangent
activation function is applied ( |
recurrent_activation |
Activation function to use for the recurrent step. |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
unit_forget_bias |
Boolean. If TRUE, add 1 to the bias of the forget gate at
initialization. Use in combination with |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. (default FALSE) |
return_state |
Boolean Whether to return the last state in addition to the output. (default FALSE) |
go_backwards |
Boolean (default FALSE). If TRUE, process the input sequence backwards. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
... |
standard layer arguments. |
Similar to an LSTM layer, but the input transformations and recurrent transformations are both convolutional.
It crops along the time dimension (axis 1).
layer_cropping_1d( object, cropping = c(1L, 1L), batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_cropping_1d( object, cropping = c(1L, 1L), batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
cropping |
int or list of int (length 2) How many units should be trimmed off at the beginning and end of the cropping dimension (axis 1). If a single int is provided, the same value will be used for both. |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape (batch, axis_to_crop, features)
3D tensor with shape (batch, cropped_axis, features)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
It crops along spatial dimensions, i.e. width and height.
layer_cropping_2d( object, cropping = list(c(0L, 0L), c(0L, 0L)), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_cropping_2d( object, cropping = list(c(0L, 0L), c(0L, 0L)), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
cropping |
int, or list of 2 ints, or list of 2 lists of 2 ints.
|
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape:
If data_format
is "channels_last"
: (batch, rows, cols, channels)
If data_format
is "channels_first"
: (batch, channels, rows, cols)
4D tensor with shape:
If data_format
is "channels_last"
: (batch, cropped_rows, cropped_cols, channels)
If data_format
is "channels_first"
: (batch, channels, cropped_rows, cropped_cols)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Cropping layer for 3D data (e.g. spatial or spatio-temporal).
layer_cropping_3d( object, cropping = list(c(1L, 1L), c(1L, 1L), c(1L, 1L)), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_cropping_3d( object, cropping = list(c(1L, 1L), c(1L, 1L), c(1L, 1L)), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
cropping |
int, or list of 3 ints, or list of 3 lists of 2 ints.
|
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
5D tensor with shape:
If data_format
is "channels_last"
: (batch, first_axis_to_crop, second_axis_to_crop, third_axis_to_crop, depth)
If data_format
is "channels_first"
:
(batch, depth, first_axis_to_crop, second_axis_to_crop, third_axis_to_crop)
5D tensor with shape:
If data_format
is "channels_last"
: (batch, first_cropped_axis, second_cropped_axis, third_cropped_axis, depth)
If data_format
is "channels_first"
: (batch, depth, first_cropped_axis, second_cropped_axis, third_cropped_axis)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Implements the operation: output = activation(dot(input, kernel) + bias)
where activation
is the element-wise activation function passed as the
activation
argument, kernel
is a weights matrix created by the layer, and
bias
is a bias vector created by the layer (only applicable if use_bias
is TRUE
). Note: if the input to the layer has a rank greater than 2, then
it is flattened prior to the initial dot product with kernel
.
layer_dense( object, units, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_dense( object, units, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
units |
Positive integer, dimensionality of the output space. |
activation |
Name of activation function to use. If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x). |
use_bias |
Whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Input shape: nD tensor with shape: (batch_size, ..., input_dim)
. The most
common situation would be a 2D input with shape (batch_size, input_dim)
.
Output shape: nD tensor with shape: (batch_size, ..., units)
. For
instance, for a 2D input with shape (batch_size, input_dim)
, the output
would have shape (batch_size, unit)
.
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
A layer that produces a dense Tensor based on given feature_columns.
layer_dense_features( object, feature_columns, name = NULL, trainable = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, weights = NULL )
layer_dense_features( object, feature_columns, name = NULL, trainable = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, weights = NULL )
object |
What to compose the new
|
feature_columns |
An iterable containing the FeatureColumns to use as
inputs to your model. All items should be instances of classes derived from
|
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
weights |
Initial weights for layer. |
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
Depthwise 1D convolution
layer_depthwise_conv_1d( object, kernel_size, strides = 1L, padding = "valid", depth_multiplier = 1L, data_format = NULL, dilation_rate = 1L, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, bias_constraint = NULL, ... )
layer_depthwise_conv_1d( object, kernel_size, strides = 1L, padding = "valid", depth_multiplier = 1L, data_format = NULL, dilation_rate = 1L, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, bias_constraint = NULL, ... )
object |
What to compose the new
|
kernel_size |
An integer, specifying the height and width of the 1D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer, specifying the strides of the convolution along the
height and width. Can be a single integer to specify the same value for
all spatial dimensions. Specifying any stride value != 1 is incompatible
with specifying any |
padding |
one of |
depth_multiplier |
The number of depthwise convolution output channels for
each input channel. The total number of depthwise convolution output
channels will be equal to |
data_format |
A string, one of |
dilation_rate |
A single integer, specifying the dilation rate to use for
dilated convolution. Currently, specifying any |
activation |
Activation function to use. If you don't specify anything, no
activation is applied (see |
use_bias |
Boolean, whether the layer uses a bias vector. |
depthwise_initializer |
Initializer for the depthwise kernel matrix (see
|
bias_initializer |
Initializer for the bias vector (see
|
depthwise_regularizer |
Regularizer function applied to the depthwise kernel
matrix (see |
bias_regularizer |
Regularizer function applied to the bias vector (see
|
activity_regularizer |
Regularizer function applied to the output of the
layer (its 'activation') (see |
depthwise_constraint |
Constraint function applied to the depthwise kernel
matrix (see |
bias_constraint |
Constraint function applied to the bias vector (see
|
... |
standard layer arguments. |
Depthwise convolution is a type of convolution in which each input channel is convolved with a different kernel (called a depthwise kernel). You can understand depthwise convolution as the first step in a depthwise separable convolution.
It is implemented via the following steps:
Split the input into individual channels.
Convolve each channel with an individual depthwise kernel with
depth_multiplier
output channels.
Concatenate the convolved outputs along the channels axis.
Unlike a regular 1D convolution, depthwise convolution does not mix information across different input channels.
The depth_multiplier
argument determines how many filter are applied to one
input channel. As such, it controls the amount of output channels that are
generated per input channel in the depthwise step.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Depthwise Separable convolutions consists in performing just the first step
in a depthwise spatial convolution (which acts on each input channel
separately). The depth_multiplier
argument controls how many output
channels are generated per input channel in the depthwise step.
layer_depthwise_conv_2d( object, kernel_size, strides = c(1, 1), padding = "valid", depth_multiplier = 1, data_format = NULL, dilation_rate = c(1, 1), activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_depthwise_conv_2d( object, kernel_size, strides = c(1, 1), padding = "valid", depth_multiplier = 1, data_format = NULL, dilation_rate = c(1, 1), activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
depth_multiplier |
The number of depthwise convolution output channels
for each input channel. The total number of depthwise convolution output
channels will be equal to |
data_format |
A string, one of |
dilation_rate |
an integer or list of 2 integers, specifying the
dilation rate to use for dilated convolution. Can be a single integer to
specify the same value for all spatial dimensions. Currently, specifying
any |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
depthwise_initializer |
Initializer for the depthwise kernel matrix. |
bias_initializer |
Initializer for the bias vector. |
depthwise_regularizer |
Regularizer function applied to the depthwise kernel matrix. |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
depthwise_constraint |
Constraint function applied to the depthwise kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
A preprocessing layer which buckets continuous features by ranges.
layer_discretization( object, bin_boundaries = NULL, num_bins = NULL, epsilon = 0.01, output_mode = "int", sparse = FALSE, ... )
layer_discretization( object, bin_boundaries = NULL, num_bins = NULL, epsilon = 0.01, output_mode = "int", sparse = FALSE, ... )
object |
What to compose the new
|
bin_boundaries |
A list of bin boundaries. The leftmost and rightmost bins
will always extend to |
num_bins |
The integer number of bins to compute. If this option is set,
|
epsilon |
Error tolerance, typically a small fraction close to zero (e.g. 0.01). Higher values of epsilon increase the quantile approximation, and hence result in more unequal buckets, but could improve performance and resource consumption. |
output_mode |
Specification for the output of the layer. Defaults to
|
sparse |
Boolean. Only applicable to |
... |
standard layer arguments. |
This layer will place each element of its input data into one of several contiguous ranges and output an integer index indicating which range each element was placed in.
Input shape:
Any tf.Tensor
or tf.RaggedTensor
of dimension 2 or higher.
Output shape: Same as input shape.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Discretization
https://keras.io/api/layers/preprocessing_layers/numerical/discretization
Other numerical features preprocessing layers:
layer_normalization()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Layer that computes a dot product between samples in two tensors.
layer_dot(inputs, ..., axes, normalize = FALSE)
layer_dot(inputs, ..., axes, normalize = FALSE)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
axes |
Integer or list of integers, axis or axes along which to take the dot product. |
normalize |
Whether to L2-normalize samples along the dot product axis before taking the dot product. If set to TRUE, then the output of the dot product is the cosine proximity between the two samples. |
If inputs
is supplied: A tensor, the dot product of the samples
from the inputs. If inputs
is missing, a keras layer instance is
returned.
Other merge layers:
layer_average()
,
layer_concatenate()
,
layer_maximum()
,
layer_minimum()
,
layer_multiply()
,
layer_subtract()
Dropout consists in randomly setting a fraction rate
of input units to 0 at
each update during training time, which helps prevent overfitting.
layer_dropout( object, rate, noise_shape = NULL, seed = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_dropout( object, rate, noise_shape = NULL, seed = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
rate |
float between 0 and 1. Fraction of the input units to drop. |
noise_shape |
1D integer tensor representing the shape of the binary
dropout mask that will be multiplied with the input. For instance, if your
inputs have shape |
seed |
integer to use as random seed. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
Other dropout layers:
layer_spatial_dropout_1d()
,
layer_spatial_dropout_2d()
,
layer_spatial_dropout_3d()
Turns positive integers (indexes) into dense vectors of fixed size
layer_embedding( object, input_dim, output_dim, embeddings_initializer = "uniform", embeddings_regularizer = NULL, activity_regularizer = NULL, embeddings_constraint = NULL, mask_zero = FALSE, input_length = NULL, sparse = FALSE, ... )
layer_embedding( object, input_dim, output_dim, embeddings_initializer = "uniform", embeddings_regularizer = NULL, activity_regularizer = NULL, embeddings_constraint = NULL, mask_zero = FALSE, input_length = NULL, sparse = FALSE, ... )
object |
Layer or Model object |
input_dim |
Integer. Size of the vocabulary, i.e. maximum integer index + 1. |
output_dim |
Integer. Dimension of the dense embedding. |
embeddings_initializer |
Initializer for the |
embeddings_regularizer , activity_regularizer
|
Regularizer function applied to
the |
embeddings_constraint |
Constraint function applied to
the |
mask_zero |
Boolean, whether or not the input value 0 is a special
"padding" value that should be masked out. This is useful when using
recurrent layers which may take variable length input. If this is
|
input_length |
Length of input sequences, when it is constant.
This argument is required if you are going to connect
|
sparse |
If TRUE, calling this layer returns a |
... |
standard layer arguments. |
For example, list(4L, 20L) -> list(c(0.25, 0.1), c(0.6, -0.2))
.
This layer can only be used on positive integer inputs of a fixed range. The
layer_text_vectorization()
, layer_string_lookup()
,
and layer_integer_lookup()
preprocessing layers can help prepare
inputs for an Embedding
layer.
This layer accepts tf.Tensor
, tf.RaggedTensor
and tf.SparseTensor
input.
2D tensor with shape: (batch_size, sequence_length)
.
3D tensor with shape: (batch_size, sequence_length, output_dim)
.
Flatten a given input, does not affect the batch size.
layer_flatten( object, data_format = NULL, input_shape = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_flatten( object, data_format = NULL, input_shape = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
data_format |
A string. one of |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
As it is a regularization layer, it is only active at training time.
layer_gaussian_dropout(object, rate, seed = NULL, ...)
layer_gaussian_dropout(object, rate, seed = NULL, ...)
object |
What to compose the new
|
rate |
float, drop probability (as with |
seed |
Integer, optional random seed to enable deterministic behavior. |
... |
standard layer arguments. |
Arbitrary. Use the keyword argument input_shape
(list
of integers, does not include the samples axis) when using this layer as
the first layer in a model.
Same shape as input.
Other noise layers:
layer_alpha_dropout()
,
layer_gaussian_noise()
This is useful to mitigate overfitting (you could see it as a form of random data augmentation). Gaussian Noise (GS) is a natural choice as corruption process for real valued inputs. As it is a regularization layer, it is only active at training time.
layer_gaussian_noise(object, stddev, seed = NULL, ...)
layer_gaussian_noise(object, stddev, seed = NULL, ...)
object |
What to compose the new
|
stddev |
float, standard deviation of the noise distribution. |
seed |
Integer, optional random seed to enable deterministic behavior. |
... |
standard layer arguments. |
Arbitrary. Use the keyword argument input_shape
(list
of integers, does not include the samples axis) when using this layer as
the first layer in a model.
Same shape as input.
Other noise layers:
layer_alpha_dropout()
,
layer_gaussian_dropout()
Global average pooling operation for temporal data.
layer_global_average_pooling_1d( object, data_format = "channels_last", keepdims = FALSE, ... )
layer_global_average_pooling_1d( object, data_format = "channels_last", keepdims = FALSE, ... )
object |
What to compose the new
|
data_format |
One of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
3D tensor with shape: (batch_size, steps, features)
.
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Global average pooling operation for spatial data.
layer_global_average_pooling_2d( object, data_format = NULL, keepdims = FALSE, ... )
layer_global_average_pooling_2d( object, data_format = NULL, keepdims = FALSE, ... )
object |
What to compose the new
|
data_format |
A string, one of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
If data_format='channels_last'
: 4D tensor with shape: (batch_size, rows, cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, rows, cols)
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Global Average pooling operation for 3D data.
layer_global_average_pooling_3d( object, data_format = NULL, keepdims = FALSE, ... )
layer_global_average_pooling_3d( object, data_format = NULL, keepdims = FALSE, ... )
object |
What to compose the new
|
data_format |
A string, one of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
If data_format='channels_last'
: 5D tensor with shape: (batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Global max pooling operation for temporal data.
layer_global_max_pooling_1d( object, data_format = "channels_last", keepdims = FALSE, ... )
layer_global_max_pooling_1d( object, data_format = "channels_last", keepdims = FALSE, ... )
object |
What to compose the new
|
data_format |
One of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
3D tensor with shape: (batch_size, steps, features)
.
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Global max pooling operation for spatial data.
layer_global_max_pooling_2d(object, data_format = NULL, keepdims = FALSE, ...)
layer_global_max_pooling_2d(object, data_format = NULL, keepdims = FALSE, ...)
object |
What to compose the new
|
data_format |
A string, one of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
If data_format='channels_last'
: 4D tensor with shape: (batch_size, rows, cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, rows, cols)
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Global Max pooling operation for 3D data.
layer_global_max_pooling_3d(object, data_format = NULL, keepdims = FALSE, ...)
layer_global_max_pooling_3d(object, data_format = NULL, keepdims = FALSE, ...)
object |
What to compose the new
|
data_format |
A string, one of |
keepdims |
A boolean, whether to keep the spatial dimensions or not. If
|
... |
standard layer arguments. |
If data_format='channels_last'
: 5D tensor with shape: (batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)
2D tensor with shape: (batch_size, channels)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
There are two variants. The default one is based on 1406.1078v3 and has reset gate applied to hidden state before matrix multiplication. The other one is based on original 1406.1078v1 and has the order reversed.
layer_gru( object, units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, time_major = FALSE, reset_after = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
layer_gru( object, units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, time_major = FALSE, reset_after = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
object |
What to compose the new
|
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use. Default: hyperbolic tangent
( |
recurrent_activation |
Activation function to use for the recurrent step. |
use_bias |
Boolean, whether the layer uses a bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. |
return_state |
Boolean (default FALSE). Whether to return the last state in addition to the output. |
go_backwards |
Boolean (default FALSE). If TRUE, process the input sequence backwards and return the reversed sequence. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
unroll |
Boolean (default FALSE). If TRUE, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences. |
time_major |
If True, the inputs and outputs will be in shape
|
reset_after |
GRU convention (whether to apply reset gate after or before matrix multiplication). FALSE = "before" (default), TRUE = "after" (CuDNN compatible). |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the
|
bias_constraint |
Constraint function applied to the bias vector. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
... |
Standard Layer args. |
The second variant is compatible with CuDNNGRU (GPU-only) and allows
inference on CPU. Thus it has separate biases for kernel
and
recurrent_kernel
. Use reset_after = TRUE
and
recurrent_activation = "sigmoid"
.
N-D tensor with shape (batch_size, timesteps, ...)
,
or (timesteps, batch_size, ...)
when time_major = TRUE
.
if return_state
: a list of tensors. The first tensor is
the output. The remaining tensors are the last states,
each with shape (batch_size, state_size)
, where state_size
could be a high dimension tensor shape.
if return_sequences
: N-D tensor with shape [batch_size, timesteps, output_size]
, where output_size
could be a high dimension tensor shape, or
[timesteps, batch_size, output_size]
when time_major
is TRUE
else, N-D tensor with shape [batch_size, output_size]
, where
output_size
could be a high dimension tensor shape.
This layer supports masking for input data with a variable number of
timesteps. To introduce masks to your data, use
layer_embedding()
with the mask_zero
parameter set to TRUE
.
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/
To enable statefulness:
Specify stateful = TRUE
in the layer constructor.
Specify a fixed batch size for your model. For sequential models,
pass batch_input_shape = list(...)
to the first layer in your model.
For functional models with 1 or more Input layers, pass
batch_shape = list(...)
to all the first layers in your model.
This is the expected shape of your inputs including the batch size.
It should be a list of integers, e.g. list(32, 10, 100)
.
For dimensions which can vary (are not known ahead of time),
use NULL
in place of an integer, e.g. list(32, NULL, NULL)
.
Specify shuffle = FALSE
when calling fit()
.
To reset the states of your model, call layer$reset_states()
on either
a specific layer, or on your entire model.
You can specify the initial state of RNN layers symbolically by calling them
with the keyword argument initial_state.
The value of initial_state should
be a tensor or list of tensors representing the initial state of the RNN
layer.
You can specify the initial state of RNN layers numerically by calling
reset_states
with the named argument states.
The value of states
should
be an array or list of arrays representing the initial state of the RNN
layer.
You can pass "external" constants to the cell using the constants
named
argument of RNN$__call__
(as well as RNN$call
) method. This requires that the
cell$call
method accepts the same keyword argument constants
. Such constants
can be used to condition the cell transformation on additional static inputs
(not changing over time), a.k.a. an attention mechanism.
Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation
On the Properties of Neural Machine Translation: Encoder-Decoder Approaches
Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling
A Theoretically Grounded Application of Dropout in Recurrent Neural Networks
Other recurrent layers:
layer_cudnn_gru()
,
layer_cudnn_lstm()
,
layer_lstm()
,
layer_rnn()
,
layer_simple_rnn()
Cell class for the GRU layer
layer_gru_cell( units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, reset_after = TRUE, ... )
layer_gru_cell( units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, reset_after = TRUE, ... )
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use. Default: hyperbolic tangent
( |
recurrent_activation |
Activation function to use for the recurrent step.
Default: sigmoid ( |
use_bias |
Boolean, (default |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. Default: |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. Default:
|
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. Default:
|
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Default: 0. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Default: 0. |
reset_after |
GRU convention (whether to apply reset gate after or before matrix multiplication). FALSE = "before", TRUE = "after" (default and CuDNN compatible). |
... |
standard layer arguments. |
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas
tf.keras.layer.GRU
processes the whole sequence.
For example:
inputs <- k_random_uniform(c(32, 10, 8)) output <- inputs %>% layer_rnn(layer_gru_cell(4)) output$shape # TensorShape([32, 4]) rnn <- layer_rnn(cell = layer_gru_cell(4), return_sequence = TRUE, return_state = TRUE) c(whole_sequence_output, final_state) %<-% rnn(inputs) whole_sequence_output$shape # TensorShape([32, 10, 4]) final_state$shape # TensorShape([32, 4])
Other RNN cell layers:
layer_lstm_cell()
,
layer_simple_rnn_cell()
,
layer_stacked_rnn_cells()
A preprocessing layer which hashes and bins categorical features.
layer_hashing( object, num_bins, mask_value = NULL, salt = NULL, output_mode = "int", sparse = FALSE, ... )
layer_hashing( object, num_bins, mask_value = NULL, salt = NULL, output_mode = "int", sparse = FALSE, ... )
object |
What to compose the new
|
num_bins |
Number of hash bins. Note that this includes the |
mask_value |
A value that represents masked inputs, which are mapped to index 0. Defaults to NULL, meaning no mask term will be added and the hashing will start at index 0. |
salt |
A single unsigned integer or NULL.
If passed, the hash function used will be SipHash64, with these values
used as an additional input (known as a "salt" in cryptography).
These should be non-zero. Defaults to |
output_mode |
Specification for the output of the layer. Defaults to
|
sparse |
Boolean. Only applicable to |
... |
standard layer arguments. |
This layer transforms single or multiple categorical inputs to hashed output.
It converts a sequence of int or string to a sequence of int. The stable hash
function uses tensorflow::ops::Fingerprint
to produce the same output
consistently across all platforms.
This layer uses FarmHash64 by default, which provides a consistent hashed output across different platforms and is stable across invocations, regardless of device and context, by mixing the input bits thoroughly.
If you want to obfuscate the hashed output, you can also pass a random salt
argument in the constructor. In that case, the layer will use the
SipHash64 hash function, with
the salt
value serving as additional input to the hash function.
Example (FarmHash64)
layer <- layer_hashing(num_bins=3) inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([[1], # [0], # [1], # [1], # [2]])>
Example (FarmHash64) with a mask value
layer <- layer_hashing(num_bins=3, mask_value='') inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([[1], # [1], # [0], # [2], # [2]])>
Example (SipHash64)
layer <- layer_hashing(num_bins=3, salt=c(133, 137)) inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([[1], # [2], # [1], # [0], # [2]])>
Example (Siphash64 with a single integer, same as salt=[133, 133]
)
layer <- layer_hashing(num_bins=3, salt=133) inp <- matrix(c('A', 'B', 'C', 'D', 'E')) layer(inp) # <tf.Tensor: shape=(5, 1), dtype=int64, numpy= # array([[0], # [0], # [2], # [1], # [0]])>
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Hashing
https://keras.io/api/layers/preprocessing_layers/categorical/hashing/
Other categorical features preprocessing layers:
layer_category_encoding()
,
layer_integer_lookup()
,
layer_string_lookup()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Layer to be used as an entry point into a graph.
layer_input( shape = NULL, batch_shape = NULL, name = NULL, dtype = NULL, sparse = FALSE, tensor = NULL, ragged = FALSE )
layer_input( shape = NULL, batch_shape = NULL, name = NULL, dtype = NULL, sparse = FALSE, tensor = NULL, ragged = FALSE )
shape |
Shape, not including the batch size. For instance,
|
batch_shape |
Shape, including the batch size. For instance,
|
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
dtype |
The data type expected by the input, as a string ( |
sparse |
Boolean, whether the placeholder created is meant to be sparse. |
tensor |
Existing tensor to wrap into the |
ragged |
A boolean specifying whether the placeholder to be created is
ragged. Only one of 'ragged' and 'sparse' can be |
A tensor
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
A preprocessing layer which maps integer features to contiguous ranges.
layer_integer_lookup( object, max_tokens = NULL, num_oov_indices = 1L, mask_token = NULL, oov_token = -1L, vocabulary = NULL, vocabulary_dtype = "int64", idf_weights = NULL, invert = FALSE, output_mode = "int", sparse = FALSE, pad_to_max_tokens = FALSE, ... )
layer_integer_lookup( object, max_tokens = NULL, num_oov_indices = 1L, mask_token = NULL, oov_token = -1L, vocabulary = NULL, vocabulary_dtype = "int64", idf_weights = NULL, invert = FALSE, output_mode = "int", sparse = FALSE, pad_to_max_tokens = FALSE, ... )
object |
What to compose the new
|
max_tokens |
Maximum size of the vocabulary for this layer. This should
only be specified when adapting the vocabulary or when setting
|
num_oov_indices |
The number of out-of-vocabulary tokens to use. If this value is more than 1, OOV inputs are modulated to determine their OOV value. If this value is 0, OOV inputs will cause an error when calling the layer. Defaults to 1. |
mask_token |
An integer token that represents masked inputs. When
|
oov_token |
Only used when |
vocabulary |
Optional. Either an array of integers or a string path to a
text file. If passing an array, can pass a list, list, 1D numpy array,
or 1D tensor containing the integer vocabulary terms. If passing a file
path, the file should contain one line per term in the vocabulary. If
this argument is set, there is no need to |
vocabulary_dtype |
The dtype of the vocabulary terms, for example
|
idf_weights |
Only valid when |
invert |
Only valid when |
output_mode |
Specification for the output of the layer. Defaults to
|
sparse |
Boolean. Only applicable when |
pad_to_max_tokens |
Only applicable when |
... |
standard layer arguments. |
This layer maps a set of arbitrary integer input tokens into indexed integer
output via a table-based vocabulary lookup. The layer's output indices will
be contiguously arranged up to the maximum vocab size, even if the input
tokens are non-continguous or unbounded. The layer supports multiple options
for encoding the output via output_mode
, and has optional support for
out-of-vocabulary (OOV) tokens and masking.
The vocabulary for the layer must be either supplied on construction or
learned via adapt()
. During adapt()
, the layer will analyze a data set,
determine the frequency of individual integer tokens, and create a
vocabulary from them. If the vocabulary is capped in size, the most frequent
tokens will be used to create the vocabulary and all others will be treated
as OOV.
There are two possible output modes for the layer. When output_mode
is
"int"
, input integers are converted to their index in the vocabulary (an
integer). When output_mode
is "multi_hot"
, "count"
, or "tf_idf"
,
input integers are encoded into an array where each dimension corresponds to
an element in the vocabulary.
The vocabulary can optionally contain a mask token as well as an OOV token
(which can optionally occupy multiple indices in the vocabulary, as set
by num_oov_indices
).
The position of these tokens in the vocabulary is fixed. When output_mode
is "int"
, the vocabulary will begin with the mask token at index 0
,
followed by OOV indices, followed by the rest of the vocabulary. When
output_mode
is "multi_hot"
, "count"
, or "tf_idf"
the vocabulary will
begin with OOV indices and instances of the mask token will be dropped.
For an overview and full list of preprocessing layers, see the preprocessing guide.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/IntegerLookup
https://keras.io/api/layers/preprocessing_layers/categorical/integer_lookup
Other categorical features preprocessing layers:
layer_category_encoding()
,
layer_hashing()
,
layer_string_lookup()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Wraps arbitrary expression as a layer
layer_lambda( object, f, output_shape = NULL, mask = NULL, arguments = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_lambda( object, f, output_shape = NULL, mask = NULL, arguments = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
f |
The function to be evaluated. Takes input tensor as first argument. |
output_shape |
Expected output shape from the function (not required when using TensorFlow back-end). |
mask |
mask |
arguments |
optional named list of keyword arguments to be passed to the function. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Arbitrary. Use the keyword argument input_shape (list of integers, does not include the samples axis) when using this layer as the first layer in a model.
Arbitrary (based on tensor returned from the function)
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
Normalize the activations of the previous layer for each given example in a batch independently, rather than across a batch like Batch Normalization. i.e. applies a transformation that maintains the mean activation within each example close to 0 and the activation standard deviation close to 1.
layer_layer_normalization( object, axis = -1, epsilon = 0.001, center = TRUE, scale = TRUE, beta_initializer = "zeros", gamma_initializer = "ones", beta_regularizer = NULL, gamma_regularizer = NULL, beta_constraint = NULL, gamma_constraint = NULL, trainable = TRUE, name = NULL )
layer_layer_normalization( object, axis = -1, epsilon = 0.001, center = TRUE, scale = TRUE, beta_initializer = "zeros", gamma_initializer = "ones", beta_regularizer = NULL, gamma_regularizer = NULL, beta_constraint = NULL, gamma_constraint = NULL, trainable = TRUE, name = NULL )
object |
What to compose the new
|
axis |
Integer or List/Tuple. The axis or axes to normalize across. Typically this is the features axis/axes. The left-out axes are typically the batch axis/axes. This argument defaults to -1, the last dimension in the input. |
epsilon |
Small float added to variance to avoid dividing by zero. Defaults to 1e-3 |
center |
If True, add offset of beta to normalized tensor. If False, beta is ignored. Defaults to True. |
scale |
If True, multiply by gamma. If False, gamma is not used. Defaults to True. When the next layer is linear (also e.g. nn.relu), this can be disabled since the scaling will be done by the next layer. |
beta_initializer |
Initializer for the beta weight. Defaults to zeros. |
gamma_initializer |
Initializer for the gamma weight. Defaults to ones. |
beta_regularizer |
Optional regularizer for the beta weight. None by default. |
gamma_regularizer |
Optional regularizer for the gamma weight. None by default. |
beta_constraint |
Optional constraint for the beta weight. None by default. |
gamma_constraint |
Optional constraint for the gamma weight. None by default. |
trainable |
Boolean, if True the variables will be marked as trainable. Defaults to True. |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
Given a tensor inputs, moments are calculated and normalization is performed across the axes specified in axis.
layer_locally_connected_1d()
works similarly to layer_conv_1d()
, except
that weights are unshared, that is, a different set of filters is applied at
each different patch of the input.
layer_locally_connected_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = NULL, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, implementation = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_locally_connected_1d( object, filters, kernel_size, strides = 1L, padding = "valid", data_format = NULL, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, implementation = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution). |
kernel_size |
An integer or list of a single integer, specifying the length of the 1D convolution window. |
strides |
An integer or list of a single integer, specifying the stride
length of the convolution. Specifying any stride value != 1 is incompatible
with specifying any |
padding |
Currently only supports |
data_format |
A string, one of |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
implementation |
either 1, 2, or 3. 1 loops over input spatial locations
to perform the forward pass. It is memory-efficient but performs a lot of
(small) ops. 2 stores layer weights in a dense but sparsely-populated 2D
matrix and implements the forward pass as a single matrix-multiply. It uses
a lot of RAM but performs few (large) ops. 3 stores layer weights in a
sparse tensor and implements the forward pass as a single sparse
matrix-multiply. How to choose: 1: large, dense models, 2: small models, 3:
large, sparse models, where "large" stands for large input/output
activations (i.e. many |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch_size, steps, input_dim)
3D tensor with shape: (batch_size, new_steps, filters)
steps
value might have changed due to padding or strides.
Other locally connected layers:
layer_locally_connected_2d()
layer_locally_connected_2d
works similarly to layer_conv_2d()
, except
that weights are unshared, that is, a different set of filters is applied at
each different patch of the input.
layer_locally_connected_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, implementation = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_locally_connected_2d( object, filters, kernel_size, strides = c(1L, 1L), padding = "valid", data_format = NULL, activation = NULL, use_bias = TRUE, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, implementation = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution). |
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
Currently only supports |
data_format |
A string, one of |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
kernel_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
implementation |
either 1, 2, or 3. 1 loops over input spatial locations
to perform the forward pass. It is memory-efficient but performs a lot of
(small) ops. 2 stores layer weights in a dense but sparsely-populated 2D
matrix and implements the forward pass as a single matrix-multiply. It uses
a lot of RAM but performs few (large) ops. 3 stores layer weights in a
sparse tensor and implements the forward pass as a single sparse
matrix-multiply. How to choose: 1: large, dense models, 2: small models, 3:
large, sparse models, where "large" stands for large input/output
activations (i.e. many |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape: (samples, channels, rows, cols)
if data_format='channels_first' or 4D tensor with shape: (samples, rows, cols, channels)
if data_format='channels_last'.
4D tensor with shape: (samples, filters, new_rows, new_cols)
if data_format='channels_first' or 4D tensor with shape:
(samples, new_rows, new_cols, filters)
if data_format='channels_last'.
rows
and cols
values might have changed due to padding.
Other locally connected layers:
layer_locally_connected_1d()
For a step-by-step description of the algorithm, see this tutorial.
layer_lstm( object, units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, time_major = FALSE, unroll = FALSE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
layer_lstm( object, units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, time_major = FALSE, unroll = FALSE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
object |
What to compose the new
|
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use. Default: hyperbolic tangent
( |
recurrent_activation |
Activation function to use for the recurrent step. |
use_bias |
Boolean, whether the layer uses a bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. |
return_state |
Boolean (default FALSE). Whether to return the last state in addition to the output. |
go_backwards |
Boolean (default FALSE). If TRUE, process the input sequence backwards and return the reversed sequence. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
time_major |
If True, the inputs and outputs will be in shape
|
unroll |
Boolean (default FALSE). If TRUE, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences. |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
unit_forget_bias |
Boolean. If TRUE, add 1 to the bias of the forget
gate at initialization. Setting it to true will also force
|
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the
|
bias_constraint |
Constraint function applied to the bias vector. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
... |
Standard Layer args. |
N-D tensor with shape (batch_size, timesteps, ...)
,
or (timesteps, batch_size, ...)
when time_major = TRUE
.
if return_state
: a list of tensors. The first tensor is
the output. The remaining tensors are the last states,
each with shape (batch_size, state_size)
, where state_size
could be a high dimension tensor shape.
if return_sequences
: N-D tensor with shape [batch_size, timesteps, output_size]
, where output_size
could be a high dimension tensor shape, or
[timesteps, batch_size, output_size]
when time_major
is TRUE
else, N-D tensor with shape [batch_size, output_size]
, where
output_size
could be a high dimension tensor shape.
This layer supports masking for input data with a variable number of
timesteps. To introduce masks to your data, use
layer_embedding()
with the mask_zero
parameter set to TRUE
.
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/
To enable statefulness:
Specify stateful = TRUE
in the layer constructor.
Specify a fixed batch size for your model. For sequential models,
pass batch_input_shape = list(...)
to the first layer in your model.
For functional models with 1 or more Input layers, pass
batch_shape = list(...)
to all the first layers in your model.
This is the expected shape of your inputs including the batch size.
It should be a list of integers, e.g. list(32, 10, 100)
.
For dimensions which can vary (are not known ahead of time),
use NULL
in place of an integer, e.g. list(32, NULL, NULL)
.
Specify shuffle = FALSE
when calling fit()
.
To reset the states of your model, call layer$reset_states()
on either
a specific layer, or on your entire model.
You can specify the initial state of RNN layers symbolically by calling them
with the keyword argument initial_state.
The value of initial_state should
be a tensor or list of tensors representing the initial state of the RNN
layer.
You can specify the initial state of RNN layers numerically by calling
reset_states
with the named argument states.
The value of states
should
be an array or list of arrays representing the initial state of the RNN
layer.
You can pass "external" constants to the cell using the constants
named
argument of RNN$__call__
(as well as RNN$call
) method. This requires that the
cell$call
method accepts the same keyword argument constants
. Such constants
can be used to condition the cell transformation on additional static inputs
(not changing over time), a.k.a. an attention mechanism.
Long short-term memory (original 1997 paper)
A Theoretically Grounded Application of Dropout in Recurrent Neural Networks
Other recurrent layers:
layer_cudnn_gru()
,
layer_cudnn_lstm()
,
layer_gru()
,
layer_rnn()
,
layer_simple_rnn()
Other recurrent layers:
layer_cudnn_gru()
,
layer_cudnn_lstm()
,
layer_gru()
,
layer_rnn()
,
layer_simple_rnn()
Cell class for the LSTM layer
layer_lstm_cell( units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
layer_lstm_cell( units, activation = "tanh", recurrent_activation = "sigmoid", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", unit_forget_bias = TRUE, kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use. Default: hyperbolic tangent
( |
recurrent_activation |
Activation function to use for the recurrent step.
Default: sigmoid ( |
use_bias |
Boolean, (default |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. Default: |
unit_forget_bias |
Boolean (default |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to
the |
bias_regularizer |
Regularizer function applied to the bias vector. Default:
|
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. Default:
|
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Default: 0. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Default: 0. |
... |
standard layer arguments. |
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas
tf$keras$layer$LSTM
processes the whole sequence.
For example:
inputs <- k_random_normal(c(32, 10, 8)) rnn <- layer_rnn(cell = layer_lstm_cell(units = 4)) output <- rnn(inputs) dim(output) # (32, 4) rnn <- layer_rnn(cell = layer_lstm_cell(units = 4), return_sequences = TRUE, return_state = TRUE) c(whole_seq_output, final_memory_state, final_carry_state) %<-% rnn(inputs) dim(whole_seq_output) # (32, 10, 4) dim(final_memory_state) # (32, 4) dim(final_carry_state) # (32, 4)
Other RNN cell layers:
layer_gru_cell()
,
layer_simple_rnn_cell()
,
layer_stacked_rnn_cells()
For each timestep in the input tensor (dimension #1 in the tensor), if all
values in the input tensor at that timestep are equal to mask_value
, then
the timestep will be masked (skipped) in all downstream layers (as long as
they support masking). If any downstream layer does not support masking yet
receives such an input mask, an exception will be raised.
layer_masking( object, mask_value = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_masking( object, mask_value = 0, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
mask_value |
float, mask value |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_permute()
,
layer_repeat_vector()
,
layer_reshape()
Max pooling operation for temporal data.
layer_max_pooling_1d( object, pool_size = 2L, strides = NULL, padding = "valid", data_format = "channels_last", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_max_pooling_1d( object, pool_size = 2L, strides = NULL, padding = "valid", data_format = "channels_last", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
Integer, size of the max pooling windows. |
strides |
Integer, or NULL. Factor by which to downscale. E.g. 2 will
halve the input. If NULL, it will default to |
padding |
One of |
data_format |
A string, one of "channels_last" (default) or
"channels_first". The ordering of the dimensions in the inputs.
channels_last corresponds to inputs with shape |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
If data_format='channels_last': 3D tensor with shape (batch_size, steps, features)
.
If data_format='channels_first': 3D tensor with shape (batch_size, features, steps)
.
If data_format='channels_last': 3D tensor with shape (batch_size, downsampled_steps, features)
.
If data_format='channels_first': 3D tensor with shape (batch_size, features, downsampled_steps)
.
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_2d()
,
layer_max_pooling_3d()
Max pooling operation for spatial data.
layer_max_pooling_2d( object, pool_size = c(2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_max_pooling_2d( object, pool_size = c(2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
integer or list of 2 integers, factors by which to downscale (vertical, horizontal). (2, 2) will halve the input in both spatial dimension. If only one integer is specified, the same window length will be used for both dimensions. |
strides |
Integer, list of 2 integers, or NULL. Strides values. If NULL,
it will default to |
padding |
One of |
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
If data_format='channels_last'
: 4D tensor with shape: (batch_size, rows, cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, rows, cols)
If data_format='channels_last'
: 4D tensor with shape: (batch_size, pooled_rows, pooled_cols, channels)
If data_format='channels_first'
: 4D tensor with shape: (batch_size, channels, pooled_rows, pooled_cols)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_3d()
Max pooling operation for 3D data (spatial or spatio-temporal).
layer_max_pooling_3d( object, pool_size = c(2L, 2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_max_pooling_3d( object, pool_size = c(2L, 2L, 2L), strides = NULL, padding = "valid", data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
pool_size |
list of 3 integers, factors by which to downscale (dim1, dim2, dim3). (2, 2, 2) will halve the size of the 3D input in each dimension. |
strides |
list of 3 integers, or NULL. Strides values. |
padding |
One of |
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
If data_format='channels_last'
: 5D tensor with shape: (batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)
If data_format='channels_last'
: 5D tensor with shape: (batch_size, pooled_dim1, pooled_dim2, pooled_dim3, channels)
If data_format='channels_first'
: 5D tensor with shape: (batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)
Other pooling layers:
layer_average_pooling_1d()
,
layer_average_pooling_2d()
,
layer_average_pooling_3d()
,
layer_global_average_pooling_1d()
,
layer_global_average_pooling_2d()
,
layer_global_average_pooling_3d()
,
layer_global_max_pooling_1d()
,
layer_global_max_pooling_2d()
,
layer_global_max_pooling_3d()
,
layer_max_pooling_1d()
,
layer_max_pooling_2d()
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
layer_maximum(inputs, ...)
layer_maximum(inputs, ...)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the element-wise maximum of the inputs. If inputs
is
missing, a keras layer instance is returned.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/maximum
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Maximum
Other merge layers:
layer_average()
,
layer_concatenate()
,
layer_dot()
,
layer_minimum()
,
layer_multiply()
,
layer_subtract()
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
layer_minimum(inputs, ...)
layer_minimum(inputs, ...)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the element-wise maximum of the inputs. If inputs
is
missing, a keras layer instance is returned.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/minimum
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Minimum
Other merge layers:
layer_average()
,
layer_concatenate()
,
layer_dot()
,
layer_maximum()
,
layer_multiply()
,
layer_subtract()
This is an implementation of multi-headed attention based on "Attention is all you Need". If query, key, value are the same, then this is self-attention. Each timestep in query attends to the corresponding sequence in key, and returns a fixed-width vector.
layer_multi_head_attention( inputs, num_heads, key_dim, value_dim = NULL, dropout = 0, use_bias = TRUE, output_shape = NULL, attention_axes = NULL, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, ... )
layer_multi_head_attention( inputs, num_heads, key_dim, value_dim = NULL, dropout = 0, use_bias = TRUE, output_shape = NULL, attention_axes = NULL, kernel_initializer = "glorot_uniform", bias_initializer = "zeros", kernel_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, bias_constraint = NULL, ... )
inputs |
List of the following tensors:
|
num_heads |
Number of attention heads. |
key_dim |
Size of each attention head for query and key. |
value_dim |
Size of each attention head for value. |
dropout |
Dropout probability. |
use_bias |
Boolean, whether the dense layers use bias vectors/matrices. |
output_shape |
The expected shape of an output tensor, besides the batch and sequence dims. If not specified, projects back to the key feature dim. |
attention_axes |
axes over which the attention is applied. None means attention over all axes, but batch, heads, and features. |
kernel_initializer |
Initializer for dense layer kernels. |
bias_initializer |
Initializer for dense layer biases. |
kernel_regularizer |
Regularizer for dense layer kernels. |
bias_regularizer |
Regularizer for dense layer biases. |
activity_regularizer |
Regularizer for dense layer activity. |
kernel_constraint |
Constraint for dense layer kernels. |
bias_constraint |
Constraint for dense layer kernels. |
... |
Other arguments passed to the layer. Eg, |
This layer first projects query, key and value. These are (effectively) a list
of tensors of length num_attention_heads, where the corresponding shapes are
[batch_size, , key_dim]
, [batch_size, , key_dim]
, [batch_size, , value_dim]
.
Then, the query and key tensors are dot-producted and scaled. These are softmaxed to obtain attention probabilities. The value tensors are then interpolated by these probabilities, then concatenated back to a single tensor.
Finally, the result tensor with the last dimension as value_dim can take an linear projection and return.
attention_output: The result of the computation, of shape [B, T, E]
, where
T is for target sequence shapes and E is the query input last dimension if
output_shape is None. Otherwise, the multi-head outputs are project to the
shape specified by output_shape.
attention_scores: (Optional) multi-head attention coeffients over attention axes.
query: Query Tensor of shape [B, T, dim]
.
value: Value Tensor of shape [B, S, dim]
.
key: Optional key Tensor of shape [B, S, dim]
. If not given, will use value
for both key and value, which is the most common case.
attention_mask: a boolean mask of shape [B, T, S]
, that prevents attention
to certain positions.
return_attention_scores: A boolean to indicate whether the output should be attention output if TRUE, or (attention_output, attention_scores) if FALSE. Defaults to FALSE.
training: Python boolean indicating whether the layer should behave in training mode (adding dropout) or in inference mode (no dropout). Defaults to either using the training mode of the parent layer/model, or FALSE (inference) if there is no parent layer.
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
layer_multiply(inputs, ...)
layer_multiply(inputs, ...)
inputs |
A input tensor, or list of input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the element-wise product of the inputs. If inputs
is
missing, a keras layer instance is returned.
Other merge layers:
layer_average()
,
layer_concatenate()
,
layer_dot()
,
layer_maximum()
,
layer_minimum()
,
layer_subtract()
A preprocessing layer which normalizes continuous features.
layer_normalization( object, axis = -1L, mean = NULL, variance = NULL, invert = FALSE, ... )
layer_normalization( object, axis = -1L, mean = NULL, variance = NULL, invert = FALSE, ... )
object |
What to compose the new
|
axis |
Integer, list of integers, or NULL. The axis or axes that should
have a separate mean and variance for each index in the shape. For
example, if shape is |
mean |
The mean value(s) to use during normalization. The passed value(s)
will be broadcast to the shape of the kept axes above; if the value(s)
cannot be broadcast, an error will be raised when this layer's |
variance |
The variance value(s) to use during normalization. The passed
value(s) will be broadcast to the shape of the kept axes above; if the
value(s) cannot be broadcast, an error will be raised when this layer's
|
invert |
If |
... |
standard layer arguments. |
This layer will shift and scale inputs into a distribution centered around 0
with standard deviation 1. It accomplishes this by precomputing the mean and
variance of the data, and calling (input - mean) / sqrt(var)
at runtime.
The mean and variance values for the layer must be either supplied on
construction or learned via adapt()
. adapt()
will compute the mean and
variance of the data and store them as the layer's weights. adapt()
should
be called before fit()
, evaluate()
, or predict()
.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Normalization
https://keras.io/api/layers/preprocessing_layers/numerical/normalization
Other numerical features preprocessing layers:
layer_discretization()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Permute the dimensions of an input according to a given pattern
layer_permute( object, dims, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_permute( object, dims, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
dims |
List of integers. Permutation pattern, does not include the
samples dimension. Indexing starts at 1. For instance, |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Input shape: Arbitrary
Output shape: Same as the input shape, but with the dimensions re-ordered according to the specified pattern.
Useful for e.g. connecting RNNs and convnets together.
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_repeat_vector()
,
layer_reshape()
A preprocessing layer which randomly adjusts brightness during training
layer_random_brightness( object, factor, value_range = c(0, 255), seed = NULL, ... )
layer_random_brightness( object, factor, value_range = c(0, 255), seed = NULL, ... )
object |
What to compose the new
|
factor |
Float or a list of 2 floats between -1.0 and 1.0. The factor is used to determine the lower bound and upper bound of the brightness adjustment. A float value will be chosen randomly between the limits. When -1.0 is chosen, the output image will be black, and when 1.0 is chosen, the image will be fully white. When only one float is provided, eg, 0.2, then -0.2 will be used for lower bound and 0.2 will be used for upper bound. |
value_range |
Optional list of 2 floats for the lower and upper limit
of the values of the input data. Defaults to |
seed |
optional integer, for fixed RNG behavior. |
... |
standard layer arguments. |
This layer will randomly increase/reduce the brightness for the input RGB
images. At inference time, the output will be identical to the input.
Call the layer with training=TRUE
to adjust the brightness of the input.
Note that different brightness adjustment factors will be apply to each the images in the batch.
For an overview and full list of preprocessing layers, see the preprocessing guide.
Other image augmentation layers:
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Adjust the contrast of an image or images by a random factor
layer_random_contrast(object, factor, seed = NULL, ...)
layer_random_contrast(object, factor, seed = NULL, ...)
object |
What to compose the new
|
factor |
a positive float represented as fraction of value, or a list of
size 2 representing lower and upper bound. When represented as a single
float, lower = upper. The contrast factor will be randomly picked between
|
seed |
Integer. Used to create a random seed. |
... |
standard layer arguments. |
Contrast is adjusted independently for each channel of each image during training.
For each channel, this layer computes the mean of the image pixels in the
channel and then adjusts each component x
of each pixel to
(x - mean) * contrast_factor + mean
.
Input shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
Output shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
Other image augmentation layers:
layer_random_brightness()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly crop the images to target height and width
layer_random_crop(object, height, width, seed = NULL, ...)
layer_random_crop(object, height, width, seed = NULL, ...)
object |
What to compose the new
|
height |
Integer, the height of the output shape. |
width |
Integer, the width of the output shape. |
seed |
Integer. Used to create a random seed. |
... |
standard layer arguments. |
This layer will crop all the images in the same batch to the same cropping
location.
By default, random cropping is only applied during training. At inference
time, the images will be first rescaled to preserve the shorter side, and
center cropped. If you need to apply random cropping at inference time,
set training
to TRUE
when calling the layer.
Input shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
Output shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., target_height, target_width, channels)
.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/RandomCrop
https://keras.io/api/layers/preprocessing_layers/image_augmentation/random_crop
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly flip each image horizontally and vertically
layer_random_flip(object, mode = "horizontal_and_vertical", seed = NULL, ...)
layer_random_flip(object, mode = "horizontal_and_vertical", seed = NULL, ...)
object |
What to compose the new
|
mode |
String indicating which flip mode to use. Can be |
seed |
Integer. Used to create a random seed. |
... |
standard layer arguments. |
This layer will flip the images based on the mode
attribute.
During inference time, the output will be identical to input. Call the layer
with training = TRUE
to flip the input.
Input shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
Output shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/RandomFlip
https://keras.io/api/layers/preprocessing_layers/image_augmentation/random_flip
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly vary the height of a batch of images during training
layer_random_height( object, factor, interpolation = "bilinear", seed = NULL, ... )
layer_random_height( object, factor, interpolation = "bilinear", seed = NULL, ... )
object |
What to compose the new
|
factor |
A positive float (fraction of original height), or a list of size 2
representing lower and upper bound for resizing vertically. When
represented as a single float, this value is used for both the upper and
lower bound. For instance, |
interpolation |
String, the interpolation method. Defaults to |
seed |
Integer. Used to create a random seed. |
... |
standard layer arguments. |
Adjusts the height of a batch of images by a random factor. The input
should be a 3D (unbatched) or 4D (batched) tensor in the "channels_last"
image data format.
By default, this layer is inactive during inference.
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly rotate each image
layer_random_rotation( object, factor, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
layer_random_rotation( object, factor, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
object |
What to compose the new
|
factor |
a float represented as fraction of 2 Pi, or a list of size 2
representing lower and upper bound for rotating clockwise and
counter-clockwise. A positive values means rotating counter clock-wise,
while a negative value means clock-wise. When represented as a single
float, this value is used for both the upper and lower bound. For
instance, |
fill_mode |
Points outside the boundaries of the input are filled according
to the given mode (one of
|
interpolation |
Interpolation mode. Supported values: |
seed |
Integer. Used to create a random seed. |
fill_value |
a float represents the value to be filled outside the boundaries
when |
... |
standard layer arguments. |
By default, random rotations are only applied during training.
At inference time, the layer does nothing. If you need to apply random
rotations at inference time, set training
to TRUE when calling the layer.
Input shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format
Output shape:
3D (unbatched) or 4D (batched) tensor with shape:
(..., height, width, channels)
, in "channels_last"
format
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly translate each image during training
layer_random_translation( object, height_factor, width_factor, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
layer_random_translation( object, height_factor, width_factor, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
object |
What to compose the new
|
height_factor |
a float represented as fraction of value, or a list of size
2 representing lower and upper bound for shifting vertically. A negative
value means shifting image up, while a positive value means shifting image
down. When represented as a single positive float, this value is used for
both the upper and lower bound. For instance, |
width_factor |
a float represented as fraction of value, or a list of size 2
representing lower and upper bound for shifting horizontally. A negative
value means shifting image left, while a positive value means shifting
image right. When represented as a single positive float, this value is
used for both the upper and lower bound. For instance,
|
fill_mode |
Points outside the boundaries of the input are filled according
to the given mode (one of
|
interpolation |
Interpolation mode. Supported values: |
seed |
Integer. Used to create a random seed. |
fill_value |
a float represents the value to be filled outside the boundaries
when |
... |
standard layer arguments. |
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_width()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Randomly vary the width of a batch of images during training
layer_random_width( object, factor, interpolation = "bilinear", seed = NULL, ... )
layer_random_width( object, factor, interpolation = "bilinear", seed = NULL, ... )
object |
What to compose the new
|
factor |
A positive float (fraction of original height), or a list of size 2
representing lower and upper bound for resizing vertically. When
represented as a single float, this value is used for both the upper and
lower bound. For instance, |
interpolation |
String, the interpolation method. Defaults to |
seed |
Integer. Used to create a random seed. |
... |
standard layer arguments. |
Adjusts the width of a batch of images by a random factor. The input
should be a 3D (unbatched) or 4D (batched) tensor in the "channels_last"
image data format.
By default, this layer is inactive during inference.
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_zoom()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
This layer will randomly zoom in or out on each axis of an image independently, filling empty space according to fill_mode.
layer_random_zoom( object, height_factor, width_factor = NULL, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
layer_random_zoom( object, height_factor, width_factor = NULL, fill_mode = "reflect", interpolation = "bilinear", seed = NULL, fill_value = 0, ... )
object |
What to compose the new
|
height_factor |
a float represented as fraction of value, or a list of size
2 representing lower and upper bound for zooming vertically. When
represented as a single float, this value is used for both the upper and
lower bound. A positive value means zooming out, while a negative value
means zooming in. For instance, |
width_factor |
a float represented as fraction of value, or a list of size 2
representing lower and upper bound for zooming horizontally. When
represented as a single float, this value is used for both the upper and
lower bound. For instance, |
fill_mode |
Points outside the boundaries of the input are filled according
to the given mode (one of
|
interpolation |
Interpolation mode. Supported values: |
seed |
Integer. Used to create a random seed. |
fill_value |
a float represents the value to be filled outside the boundaries
when |
... |
standard layer arguments. |
Other image augmentation layers:
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Repeats the input n times.
layer_repeat_vector( object, n, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_repeat_vector( object, n, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
n |
integer, repetition factor. |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
2D tensor of shape (num_samples, features)
.
3D tensor of shape (num_samples, n, features)
.
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_reshape()
scale
and adds offset
Multiply inputs by scale
and adds offset
layer_rescaling(object, scale, offset = 0, ...)
layer_rescaling(object, scale, offset = 0, ...)
object |
What to compose the new
|
scale |
Float, the scale to apply to the inputs. |
offset |
Float, the offset to apply to the inputs. |
... |
standard layer arguments. |
For instance:
To rescale an input in the [0, 255]
range
to be in the [0, 1]
range, you would pass scale=1./255
.
To rescale an input in the [0, 255]
range to be in the [-1, 1]
range,
you would pass scale = 1/127.5, offset = -1
.
The rescaling is applied both during training and inference.
Input shape: Arbitrary.
Output shape: Same as input.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Rescaling
https://keras.io/api/layers/preprocessing_layers/image_preprocessing/rescaling
Other image preprocessing layers:
layer_center_crop()
,
layer_resizing()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_resizing()
,
layer_string_lookup()
,
layer_text_vectorization()
Reshapes an output to a certain shape.
layer_reshape( object, target_shape, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_reshape( object, target_shape, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
target_shape |
List of integers, does not include the samples dimension (batch size). |
input_shape |
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
Input shape: Arbitrary, although all dimensions in the input shaped must be fixed.
Output shape: (batch_size,) + target_shape
.
Other core layers:
layer_activation()
,
layer_activity_regularization()
,
layer_attention()
,
layer_dense()
,
layer_dense_features()
,
layer_dropout()
,
layer_flatten()
,
layer_input()
,
layer_lambda()
,
layer_masking()
,
layer_permute()
,
layer_repeat_vector()
Image resizing layer
layer_resizing( object, height, width, interpolation = "bilinear", crop_to_aspect_ratio = FALSE, ... )
layer_resizing( object, height, width, interpolation = "bilinear", crop_to_aspect_ratio = FALSE, ... )
object |
What to compose the new
|
height |
Integer, the height of the output shape. |
width |
Integer, the width of the output shape. |
interpolation |
String, the interpolation method. Defaults to |
crop_to_aspect_ratio |
If TRUE, resize the images without aspect
ratio distortion. When the original aspect ratio differs from the target
aspect ratio, the output image will be cropped so as to return the largest
possible window in the image (of size |
... |
standard layer arguments. |
Resize the batched image input to target height and width. The input should
be a 4D (batched) or 3D (unbatched) tensor in "channels_last"
format.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/Resizing
https://keras.io/api/layers/preprocessing_layers/image_preprocessing/resizing
Other image preprocessing layers:
layer_center_crop()
,
layer_rescaling()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_string_lookup()
,
layer_text_vectorization()
Base class for recurrent layers
layer_rnn( object, cell, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, time_major = FALSE, ..., zero_output_for_mask = FALSE )
layer_rnn( object, cell, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, time_major = FALSE, ..., zero_output_for_mask = FALSE )
object |
What to compose the new
|
cell |
A RNN cell instance or a list of RNN cell instances. A RNN cell is a class that has:
|
return_sequences |
Boolean (default |
return_state |
Boolean (default |
go_backwards |
Boolean (default |
stateful |
Boolean (default |
unroll |
Boolean (default |
time_major |
The shape format of the |
... |
standard layer arguments. |
zero_output_for_mask |
Boolean (default |
See the Keras RNN API guide for details about the usage of RNN API.
inputs
: Input tensor.
mask
: Binary tensor of shape [batch_size, timesteps]
indicating whether
a given timestep should be masked. An individual TRUE
entry indicates
that the corresponding timestep should be utilized, while a FALSE
entry indicates that the corresponding timestep should be ignored.
training
: R or Python Boolean indicating whether the layer should behave in
training mode or in inference mode. This argument is passed to the cell
when calling it. This is for use with cells that use dropout.
initial_state
: List of initial state tensors to be passed to the first
call of the cell.
constants
: List of constant tensors to be passed to the cell at each
timestep.
N-D tensor with shape (batch_size, timesteps, ...)
,
or (timesteps, batch_size, ...)
when time_major = TRUE
.
if return_state
: a list of tensors. The first tensor is
the output. The remaining tensors are the last states,
each with shape (batch_size, state_size)
, where state_size
could be a high dimension tensor shape.
if return_sequences
: N-D tensor with shape [batch_size, timesteps, output_size]
, where output_size
could be a high dimension tensor shape, or
[timesteps, batch_size, output_size]
when time_major
is TRUE
else, N-D tensor with shape [batch_size, output_size]
, where
output_size
could be a high dimension tensor shape.
This layer supports masking for input data with a variable number of
timesteps. To introduce masks to your data, use
layer_embedding()
with the mask_zero
parameter set to TRUE
.
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/
To enable statefulness:
Specify stateful = TRUE
in the layer constructor.
Specify a fixed batch size for your model. For sequential models,
pass batch_input_shape = list(...)
to the first layer in your model.
For functional models with 1 or more Input layers, pass
batch_shape = list(...)
to all the first layers in your model.
This is the expected shape of your inputs including the batch size.
It should be a list of integers, e.g. list(32, 10, 100)
.
For dimensions which can vary (are not known ahead of time),
use NULL
in place of an integer, e.g. list(32, NULL, NULL)
.
Specify shuffle = FALSE
when calling fit()
.
To reset the states of your model, call layer$reset_states()
on either
a specific layer, or on your entire model.
You can specify the initial state of RNN layers symbolically by calling them
with the keyword argument initial_state.
The value of initial_state should
be a tensor or list of tensors representing the initial state of the RNN
layer.
You can specify the initial state of RNN layers numerically by calling
reset_states
with the named argument states.
The value of states
should
be an array or list of arrays representing the initial state of the RNN
layer.
You can pass "external" constants to the cell using the constants
named
argument of RNN$__call__
(as well as RNN$call
) method. This requires that the
cell$call
method accepts the same keyword argument constants
. Such constants
can be used to condition the cell transformation on additional static inputs
(not changing over time), a.k.a. an attention mechanism.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/RNN
reticulate::py_help(keras$layers$RNN)
Other recurrent layers:
layer_cudnn_gru()
,
layer_cudnn_lstm()
,
layer_gru()
,
layer_lstm()
,
layer_simple_rnn()
Separable convolutions consist in first performing a depthwise spatial
convolution (which acts on each input channel separately) followed by a
pointwise convolution which mixes together the resulting output channels. The
depth_multiplier
argument controls how many output channels are generated
per input channel in the depthwise step. Intuitively, separable convolutions
can be understood as a way to factorize a convolution kernel into two smaller
kernels, or as an extreme version of an Inception block.
layer_separable_conv_1d( object, filters, kernel_size, strides = 1, padding = "valid", data_format = "channels_last", dilation_rate = 1, depth_multiplier = 1, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", pointwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, pointwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, pointwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_separable_conv_1d( object, filters, kernel_size, strides = 1, padding = "valid", data_format = "channels_last", dilation_rate = 1, depth_multiplier = 1, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", pointwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, pointwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, pointwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
data_format |
A string, one of |
dilation_rate |
an integer or list of 2 integers, specifying the
dilation rate to use for dilated convolution. Can be a single integer to
specify the same value for all spatial dimensions. Currently, specifying
any |
depth_multiplier |
The number of depthwise convolution output channels
for each input channel. The total number of depthwise convolution output
channels will be equal to |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
depthwise_initializer |
Initializer for the depthwise kernel matrix. |
pointwise_initializer |
Initializer for the pointwise kernel matrix. |
bias_initializer |
Initializer for the bias vector. |
depthwise_regularizer |
Regularizer function applied to the depthwise kernel matrix. |
pointwise_regularizer |
Regularizer function applied to the pointwise kernel matrix. |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
depthwise_constraint |
Constraint function applied to the depthwise kernel matrix. |
pointwise_constraint |
Constraint function applied to the pointwise kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch, channels, steps)
if data_format='channels_first' or 3D tensor with shape: (batch, steps, channels)
if data_format='channels_last'.
3D tensor with shape: (batch, filters, new_steps)
if data_format='channels_first' or 3D tensor with shape:
(batch, new_steps, filters)
if data_format='channels_last'.
new_steps
values might have changed due to padding or strides.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Separable convolutions consist in first performing a depthwise spatial
convolution (which acts on each input channel separately) followed by a
pointwise convolution which mixes together the resulting output channels. The
depth_multiplier
argument controls how many output channels are generated
per input channel in the depthwise step. Intuitively, separable convolutions
can be understood as a way to factorize a convolution kernel into two smaller
kernels, or as an extreme version of an Inception block.
layer_separable_conv_2d( object, filters, kernel_size, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = 1, depth_multiplier = 1, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", pointwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, pointwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, pointwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_separable_conv_2d( object, filters, kernel_size, strides = c(1, 1), padding = "valid", data_format = NULL, dilation_rate = 1, depth_multiplier = 1, activation = NULL, use_bias = TRUE, depthwise_initializer = "glorot_uniform", pointwise_initializer = "glorot_uniform", bias_initializer = "zeros", depthwise_regularizer = NULL, pointwise_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, depthwise_constraint = NULL, pointwise_constraint = NULL, bias_constraint = NULL, input_shape = NULL, batch_input_shape = NULL, batch_size = NULL, dtype = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
filters |
Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution). |
kernel_size |
An integer or list of 2 integers, specifying the width and height of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions. |
strides |
An integer or list of 2 integers, specifying the strides of
the convolution along the width and height. Can be a single integer to
specify the same value for all spatial dimensions. Specifying any stride
value != 1 is incompatible with specifying any |
padding |
one of |
data_format |
A string, one of |
dilation_rate |
an integer or list of 2 integers, specifying the
dilation rate to use for dilated convolution. Can be a single integer to
specify the same value for all spatial dimensions. Currently, specifying
any |
depth_multiplier |
The number of depthwise convolution output channels
for each input channel. The total number of depthwise convolution output
channels will be equal to |
activation |
Activation function to use. If you don't specify anything,
no activation is applied (ie. "linear" activation: |
use_bias |
Boolean, whether the layer uses a bias vector. |
depthwise_initializer |
Initializer for the depthwise kernel matrix. |
pointwise_initializer |
Initializer for the pointwise kernel matrix. |
bias_initializer |
Initializer for the bias vector. |
depthwise_regularizer |
Regularizer function applied to the depthwise kernel matrix. |
pointwise_regularizer |
Regularizer function applied to the pointwise kernel matrix. |
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
depthwise_constraint |
Constraint function applied to the depthwise kernel matrix. |
pointwise_constraint |
Constraint function applied to the pointwise kernel matrix. |
bias_constraint |
Constraint function applied to the bias vector. |
input_shape |
Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. |
batch_input_shape |
Shapes, including the batch size. For instance,
|
batch_size |
Fixed batch size for layer |
dtype |
The data type expected by the input, as a string ( |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape: (batch, channels, rows, cols)
if data_format='channels_first' or 4D tensor with shape: (batch, rows, cols, channels)
if data_format='channels_last'.
4D tensor with shape: (batch, filters, new_rows, new_cols)
if data_format='channels_first' or 4D tensor with shape:
(batch, new_rows, new_cols, filters)
if data_format='channels_last'.
rows
and cols
values might have changed due to padding.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Fully-connected RNN where the output is to be fed back to input.
layer_simple_rnn( object, units, activation = "tanh", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
layer_simple_rnn( object, units, activation = "tanh", use_bias = TRUE, return_sequences = FALSE, return_state = FALSE, go_backwards = FALSE, stateful = FALSE, unroll = FALSE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, activity_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
object |
What to compose the new
|
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use. Default: hyperbolic tangent
( |
use_bias |
Boolean, whether the layer uses a bias vector. |
return_sequences |
Boolean. Whether to return the last output in the output sequence, or the full sequence. |
return_state |
Boolean (default FALSE). Whether to return the last state in addition to the output. |
go_backwards |
Boolean (default FALSE). If TRUE, process the input sequence backwards and return the reversed sequence. |
stateful |
Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
unroll |
Boolean (default FALSE). If TRUE, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences. |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. |
activity_regularizer |
Regularizer function applied to the output of the layer (its "activation").. |
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the
|
bias_constraint |
Constraint function applied to the bias vector. |
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
... |
Standard Layer args. |
N-D tensor with shape (batch_size, timesteps, ...)
,
or (timesteps, batch_size, ...)
when time_major = TRUE
.
if return_state
: a list of tensors. The first tensor is
the output. The remaining tensors are the last states,
each with shape (batch_size, state_size)
, where state_size
could be a high dimension tensor shape.
if return_sequences
: N-D tensor with shape [batch_size, timesteps, output_size]
, where output_size
could be a high dimension tensor shape, or
[timesteps, batch_size, output_size]
when time_major
is TRUE
else, N-D tensor with shape [batch_size, output_size]
, where
output_size
could be a high dimension tensor shape.
This layer supports masking for input data with a variable number of
timesteps. To introduce masks to your data, use
layer_embedding()
with the mask_zero
parameter set to TRUE
.
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.
For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/
To enable statefulness:
Specify stateful = TRUE
in the layer constructor.
Specify a fixed batch size for your model. For sequential models,
pass batch_input_shape = list(...)
to the first layer in your model.
For functional models with 1 or more Input layers, pass
batch_shape = list(...)
to all the first layers in your model.
This is the expected shape of your inputs including the batch size.
It should be a list of integers, e.g. list(32, 10, 100)
.
For dimensions which can vary (are not known ahead of time),
use NULL
in place of an integer, e.g. list(32, NULL, NULL)
.
Specify shuffle = FALSE
when calling fit()
.
To reset the states of your model, call layer$reset_states()
on either
a specific layer, or on your entire model.
You can specify the initial state of RNN layers symbolically by calling them
with the keyword argument initial_state.
The value of initial_state should
be a tensor or list of tensors representing the initial state of the RNN
layer.
You can specify the initial state of RNN layers numerically by calling
reset_states
with the named argument states.
The value of states
should
be an array or list of arrays representing the initial state of the RNN
layer.
You can pass "external" constants to the cell using the constants
named
argument of RNN$__call__
(as well as RNN$call
) method. This requires that the
cell$call
method accepts the same keyword argument constants
. Such constants
can be used to condition the cell transformation on additional static inputs
(not changing over time), a.k.a. an attention mechanism.
Other recurrent layers:
layer_cudnn_gru()
,
layer_cudnn_lstm()
,
layer_gru()
,
layer_lstm()
,
layer_rnn()
Cell class for SimpleRNN
layer_simple_rnn_cell( units, activation = "tanh", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
layer_simple_rnn_cell( units, activation = "tanh", use_bias = TRUE, kernel_initializer = "glorot_uniform", recurrent_initializer = "orthogonal", bias_initializer = "zeros", kernel_regularizer = NULL, recurrent_regularizer = NULL, bias_regularizer = NULL, kernel_constraint = NULL, recurrent_constraint = NULL, bias_constraint = NULL, dropout = 0, recurrent_dropout = 0, ... )
units |
Positive integer, dimensionality of the output space. |
activation |
Activation function to use.
Default: hyperbolic tangent ( |
use_bias |
Boolean, (default |
kernel_initializer |
Initializer for the |
recurrent_initializer |
Initializer for the |
bias_initializer |
Initializer for the bias vector. Default: |
kernel_regularizer |
Regularizer function applied to the |
recurrent_regularizer |
Regularizer function applied to the
|
bias_regularizer |
Regularizer function applied to the bias vector. Default:
|
kernel_constraint |
Constraint function applied to the |
recurrent_constraint |
Constraint function applied to the |
bias_constraint |
Constraint function applied to the bias vector. Default:
|
dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Default: 0. |
recurrent_dropout |
Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Default: 0. |
... |
standard layer arguments. |
See the Keras RNN API guide for details about the usage of RNN API.
This class processes one step within the whole time sequence input, whereas
tf.keras.layer.SimpleRNN
processes the whole sequence.
Other RNN cell layers:
layer_gru_cell()
,
layer_lstm_cell()
,
layer_stacked_rnn_cells()
This version performs the same function as Dropout, however it drops entire
1D feature maps instead of individual elements. If adjacent frames within
feature maps are strongly correlated (as is normally the case in early
convolution layers) then regular dropout will not regularize the activations
and will otherwise just result in an effective learning rate decrease. In
this case, layer_spatial_dropout_1d
will help promote independence between
feature maps and should be used instead.
layer_spatial_dropout_1d( object, rate, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_spatial_dropout_1d( object, rate, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
rate |
float between 0 and 1. Fraction of the input units to drop. |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (samples, timesteps, channels)
Same as input
- Efficient Object Localization Using Convolutional Networks
Other dropout layers:
layer_dropout()
,
layer_spatial_dropout_2d()
,
layer_spatial_dropout_3d()
This version performs the same function as Dropout, however it drops entire
2D feature maps instead of individual elements. If adjacent pixels within
feature maps are strongly correlated (as is normally the case in early
convolution layers) then regular dropout will not regularize the activations
and will otherwise just result in an effective learning rate decrease. In
this case, layer_spatial_dropout_2d
will help promote independence between
feature maps and should be used instead.
layer_spatial_dropout_2d( object, rate, data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_spatial_dropout_2d( object, rate, data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
rate |
float between 0 and 1. Fraction of the input units to drop. |
data_format |
'channels_first' or 'channels_last'. In 'channels_first'
mode, the channels dimension (the depth) is at index 1, in 'channels_last'
mode is it at index 3. It defaults to the |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape: (samples, channels, rows, cols)
if data_format='channels_first' or 4D tensor with shape: (samples, rows, cols, channels)
if data_format='channels_last'.
Same as input
- Efficient Object Localization Using Convolutional Networks
Other dropout layers:
layer_dropout()
,
layer_spatial_dropout_1d()
,
layer_spatial_dropout_3d()
This version performs the same function as Dropout, however it drops entire
3D feature maps instead of individual elements. If adjacent voxels within
feature maps are strongly correlated (as is normally the case in early
convolution layers) then regular dropout will not regularize the activations
and will otherwise just result in an effective learning rate decrease. In
this case, layer_spatial_dropout_3d
will help promote independence between
feature maps and should be used instead.
layer_spatial_dropout_3d( object, rate, data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_spatial_dropout_3d( object, rate, data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
rate |
float between 0 and 1. Fraction of the input units to drop. |
data_format |
'channels_first' or 'channels_last'. In 'channels_first'
mode, the channels dimension (the depth) is at index 1, in 'channels_last'
mode is it at index 4. It defaults to the |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
5D tensor with shape: (samples, channels, dim1, dim2, dim3)
if data_format='channels_first' or 5D tensor with shape: (samples, dim1, dim2, dim3, channels)
if data_format='channels_last'.
Same as input
- Efficient Object Localization Using Convolutional Networks
Other dropout layers:
layer_dropout()
,
layer_spatial_dropout_1d()
,
layer_spatial_dropout_2d()
Used to implement efficient stacked RNNs.
layer_stacked_rnn_cells(cells, ...)
layer_stacked_rnn_cells(cells, ...)
cells |
List of RNN cell instances. |
... |
standard layer arguments. |
Other RNN cell layers:
layer_gru_cell()
,
layer_lstm_cell()
,
layer_simple_rnn_cell()
A preprocessing layer which maps string features to integer indices.
layer_string_lookup( object, max_tokens = NULL, num_oov_indices = 1L, mask_token = NULL, oov_token = "[UNK]", vocabulary = NULL, idf_weights = NULL, encoding = "utf-8", invert = FALSE, output_mode = "int", sparse = FALSE, pad_to_max_tokens = FALSE, ... )
layer_string_lookup( object, max_tokens = NULL, num_oov_indices = 1L, mask_token = NULL, oov_token = "[UNK]", vocabulary = NULL, idf_weights = NULL, encoding = "utf-8", invert = FALSE, output_mode = "int", sparse = FALSE, pad_to_max_tokens = FALSE, ... )
object |
What to compose the new
|
max_tokens |
Maximum size of the vocabulary for this layer. This should
only be specified when adapting the vocabulary or when setting
|
num_oov_indices |
The number of out-of-vocabulary tokens to use. If this value is more than 1, OOV inputs are hashed to determine their OOV value. If this value is 0, OOV inputs will cause an error when calling the layer. Defaults to 1. |
mask_token |
A token that represents masked inputs. When |
oov_token |
Only used when |
vocabulary |
Optional. Either an array of strings or a string path to a
text file. If passing an array, can pass a character vector or
or 1D tensor containing the string vocabulary terms. If passing a file
path, the file should contain one line per term in the vocabulary. If
this argument is set, there is no need to |
idf_weights |
Only valid when |
encoding |
Optional. The text encoding to use to interpret the input
strings. Defaults to |
invert |
Only valid when |
output_mode |
Specification for the output of the layer. Defaults to
|
sparse |
Boolean. Only applicable when |
pad_to_max_tokens |
Only applicable when |
... |
standard layer arguments. |
This layer translates a set of arbitrary strings into integer output via a
table-based vocabulary lookup. This layer will perform no splitting or
transformation of input strings. For a layer than can split and tokenize
natural language, see the layer_text_vectorization()
layer.
The vocabulary for the layer must be either supplied on construction or
learned via adapt()
. During adapt()
, the layer will analyze a data set,
determine the frequency of individual strings tokens, and create a
vocabulary from them. If the vocabulary is capped in size, the most frequent
tokens will be used to create the vocabulary and all others will be treated
as out-of-vocabulary (OOV).
There are two possible output modes for the layer.
When output_mode
is "int"
,
input strings are converted to their index in the vocabulary (an integer).
When output_mode
is "multi_hot"
, "count"
, or "tf_idf"
, input strings
are encoded into an array where each dimension corresponds to an element in
the vocabulary.
The vocabulary can optionally contain a mask token as well as an OOV token
(which can optionally occupy multiple indices in the vocabulary, as set
by num_oov_indices
).
The position of these tokens in the vocabulary is fixed. When output_mode
is "int"
, the vocabulary will begin with the mask token (if set), followed
by OOV indices, followed by the rest of the vocabulary. When output_mode
is "multi_hot"
, "count"
, or "tf_idf"
the vocabulary will begin with
OOV indices and instances of the mask token will be dropped.
For an overview and full list of preprocessing layers, see the preprocessing guide.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/StringLookup
https://keras.io/api/layers/preprocessing_layers/categorical/string_lookup
Other categorical features preprocessing layers:
layer_category_encoding()
,
layer_hashing()
,
layer_integer_lookup()
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_text_vectorization()
It takes as input a list of tensors of size 2, both of the same shape, and
returns a single tensor, (inputs[[1]] - inputs[[2]]
), also of the same
shape.
layer_subtract(inputs, ...)
layer_subtract(inputs, ...)
inputs |
A input tensor, or list of two input tensors. Can be missing. |
... |
Unnamed args are treated as additional |
A tensor, the difference of the inputs. If inputs
is missing, a
keras layer instance is returned.
Other merge layers:
layer_average()
,
layer_concatenate()
,
layer_dot()
,
layer_maximum()
,
layer_minimum()
,
layer_multiply()
A preprocessing layer which maps text features to integer sequences.
layer_text_vectorization( object, max_tokens = NULL, standardize = "lower_and_strip_punctuation", split = "whitespace", ngrams = NULL, output_mode = "int", output_sequence_length = NULL, pad_to_max_tokens = FALSE, vocabulary = NULL, ... ) get_vocabulary(object, include_special_tokens = TRUE) set_vocabulary(object, vocabulary, idf_weights = NULL, ...)
layer_text_vectorization( object, max_tokens = NULL, standardize = "lower_and_strip_punctuation", split = "whitespace", ngrams = NULL, output_mode = "int", output_sequence_length = NULL, pad_to_max_tokens = FALSE, vocabulary = NULL, ... ) get_vocabulary(object, include_special_tokens = TRUE) set_vocabulary(object, vocabulary, idf_weights = NULL, ...)
object |
What to compose the new
|
max_tokens |
The maximum size of the vocabulary for this layer. If NULL,
there is no cap on the size of the vocabulary. Note that this vocabulary
contains 1 OOV token, so the effective number of tokens is |
standardize |
Optional specification for standardization to apply to the
input text. Values can be NULL (no standardization),
|
split |
Optional specification for splitting the input text. Values can be
NULL (no splitting), |
ngrams |
Optional specification for ngrams to create from the possibly-split input text. Values can be NULL, an integer or list of integers; passing an integer will create ngrams up to that integer, and passing a list of integers will create ngrams for the specified values in the list. Passing NULL means that no ngrams will be created. |
output_mode |
Optional specification for the output of the layer. Values can
be
|
output_sequence_length |
Only valid in INT mode. If set, the output will have
its time dimension padded or truncated to exactly |
pad_to_max_tokens |
Only valid in |
vocabulary |
Optional for |
... |
standard layer arguments. |
include_special_tokens |
If True, the returned vocabulary will include the padding and OOV tokens, and a term's index in the vocabulary will equal the term's index when calling the layer. If False, the returned vocabulary will not include any padding or OOV tokens. |
idf_weights |
An R vector, 1D numpy array, or 1D tensor of inverse document frequency weights with equal length to vocabulary. Must be set if output_mode is "tf_idf". Should not be set otherwise. |
This layer has basic options for managing text in a Keras model. It transforms a batch of strings (one example = one string) into either a list of token indices (one example = 1D tensor of integer token indices) or a dense representation (one example = 1D tensor of float values representing data about the example's tokens).
The vocabulary for the layer must be either supplied on construction or
learned via adapt()
. When this layer is adapted, it will analyze the
dataset, determine the frequency of individual string values, and create a
vocabulary from them. This vocabulary can have unlimited size or be capped,
depending on the configuration options for this layer; if there are more
unique values in the input than the maximum vocabulary size, the most
frequent terms will be used to create the vocabulary.
The processing of each example contains the following steps:
Standardize each example (usually lowercasing + punctuation stripping)
Split each example into substrings (usually words)
Recombine substrings into tokens (usually ngrams)
Index tokens (associate a unique int value with each token)
Transform each example using this index, either into a vector of ints or a dense float vector.
Some notes on passing callables to customize splitting and normalization for this layer:
Any callable can be passed to this Layer, but if you want to serialize
this object you should only pass functions that are registered Keras
serializables (see tf$keras$utils$register_keras_serializable
for more details).
When using a custom callable for standardize
, the data received
by the callable will be exactly as passed to this layer. The callable
should return a tensor of the same shape as the input.
When using a custom callable for split
, the data received by the
callable will have the 1st dimension squeezed out - instead of
matrix(c("string to split", "another string to split"))
, the Callable will
see c("string to split", "another string to split")
. The callable should
return a Tensor with the first dimension containing the split tokens -
in this example, we should see something like list(c("string", "to", "split"), c("another", "string", "to", "split"))
. This makes the callable
site natively compatible with tf$strings$split()
.
https://www.tensorflow.org/api_docs/python/tf/keras/layers/TextVectorization
https://keras.io/api/layers/preprocessing_layers/text/text_vectorization
Other preprocessing layers:
layer_category_encoding()
,
layer_center_crop()
,
layer_discretization()
,
layer_hashing()
,
layer_integer_lookup()
,
layer_normalization()
,
layer_random_brightness()
,
layer_random_contrast()
,
layer_random_crop()
,
layer_random_flip()
,
layer_random_height()
,
layer_random_rotation()
,
layer_random_translation()
,
layer_random_width()
,
layer_random_zoom()
,
layer_rescaling()
,
layer_resizing()
,
layer_string_lookup()
Unit normalization layer
layer_unit_normalization(object, axis = -1L, ...)
layer_unit_normalization(object, axis = -1L, ...)
object |
What to compose the new
|
axis |
Integer or list. The axis or axes to normalize across. Typically
this is the features axis or axes. The left-out axes are typically the
batch axis or axes. Defaults to |
... |
standard layer arguments. data <- as_tensor(1:6, shape = c(2, 3), dtype = "float32") normalized_data <- data %>% layer_unit_normalization() for(row in 1:2) normalized_data[row, ] %>% { sum(.^2) } %>% print() # tf.Tensor(0.9999999, shape=(), dtype=float32) # tf.Tensor(1.0, shape=(), dtype=float32) |
Normalize a batch of inputs so that each input in the batch has a L2 norm
equal to 1 (across the axes specified in axis
).
Repeats each temporal step size
times along the time axis.
layer_upsampling_1d( object, size = 2L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_upsampling_1d( object, size = 2L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
size |
integer. Upsampling factor. |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape: (batch, steps, features)
.
3D tensor with shape: (batch, upsampled_steps, features)
.
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Repeats the rows and columns of the data by size[[0]]
and size[[1]]
respectively.
layer_upsampling_2d( object, size = c(2L, 2L), data_format = NULL, interpolation = "nearest", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_upsampling_2d( object, size = c(2L, 2L), data_format = NULL, interpolation = "nearest", batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
size |
int, or list of 2 integers. The upsampling factors for rows and columns. |
data_format |
A string, one of |
interpolation |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape:
If data_format
is "channels_last"
: (batch, rows, cols, channels)
If data_format
is "channels_first"
: (batch, channels, rows, cols)
4D tensor with shape:
If data_format
is "channels_last"
: (batch, upsampled_rows, upsampled_cols, channels)
If data_format
is "channels_first"
: (batch, channels, upsampled_rows, upsampled_cols)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Repeats the 1st, 2nd and 3rd dimensions of the data by size[[0]]
, size[[1]]
and
size[[2]]
respectively.
layer_upsampling_3d( object, size = c(2L, 2L, 2L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_upsampling_3d( object, size = c(2L, 2L, 2L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
size |
int, or list of 3 integers. The upsampling factors for dim1, dim2 and dim3. |
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
5D tensor with shape:
If data_format
is "channels_last"
: (batch, dim1, dim2, dim3, channels)
If data_format
is "channels_first"
: (batch, channels, dim1, dim2, dim3)
5D tensor with shape:
If data_format
is "channels_last"
: (batch, upsampled_dim1, upsampled_dim2, upsampled_dim3, channels)
If data_format
is "channels_first"
: (batch, channels, upsampled_dim1, upsampled_dim2, upsampled_dim3)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
Zero-padding layer for 1D input (e.g. temporal sequence).
layer_zero_padding_1d( object, padding = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_zero_padding_1d( object, padding = 1L, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
padding |
int, or list of int (length 2)
|
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
3D tensor with shape (batch, axis_to_pad, features)
3D tensor with shape (batch, padded_axis, features)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_2d()
,
layer_zero_padding_3d()
This layer can add rows and columns of zeros at the top, bottom, left and right side of an image tensor.
layer_zero_padding_2d( object, padding = c(1L, 1L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_zero_padding_2d( object, padding = c(1L, 1L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
padding |
int, or list of 2 ints, or list of 2 lists of 2 ints.
|
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
4D tensor with shape:
If data_format
is "channels_last"
: (batch, rows, cols, channels)
If data_format
is "channels_first"
: (batch, channels, rows, cols)
4D tensor with shape:
If data_format
is "channels_last"
: (batch, padded_rows, padded_cols, channels)
If data_format
is "channels_first"
: (batch, channels, padded_rows, padded_cols)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_3d()
Zero-padding layer for 3D data (spatial or spatio-temporal).
layer_zero_padding_3d( object, padding = c(1L, 1L, 1L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
layer_zero_padding_3d( object, padding = c(1L, 1L, 1L), data_format = NULL, batch_size = NULL, name = NULL, trainable = NULL, weights = NULL )
object |
What to compose the new
|
padding |
int, or list of 3 ints, or list of 3 lists of 2 ints.
|
data_format |
A string, one of |
batch_size |
Fixed batch size for layer |
name |
An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. |
trainable |
Whether the layer weights will be updated during training. |
weights |
Initial weights for layer. |
5D tensor with shape:
If data_format
is "channels_last"
: (batch, first_axis_to_pad, second_axis_to_pad, third_axis_to_pad, depth)
If data_format
is "channels_first"
: (batch, depth, first_axis_to_pad, second_axis_to_pad, third_axis_to_pad)
5D tensor with shape:
If data_format
is "channels_last"
: (batch, first_padded_axis, second_padded_axis, third_axis_to_pad, depth)
If data_format
is "channels_first"
: (batch, depth, first_padded_axis, second_padded_axis, third_axis_to_pad)
Other convolutional layers:
layer_conv_1d()
,
layer_conv_1d_transpose()
,
layer_conv_2d()
,
layer_conv_2d_transpose()
,
layer_conv_3d()
,
layer_conv_3d_transpose()
,
layer_conv_lstm_2d()
,
layer_cropping_1d()
,
layer_cropping_2d()
,
layer_cropping_3d()
,
layer_depthwise_conv_1d()
,
layer_depthwise_conv_2d()
,
layer_separable_conv_1d()
,
layer_separable_conv_2d()
,
layer_upsampling_1d()
,
layer_upsampling_2d()
,
layer_upsampling_3d()
,
layer_zero_padding_1d()
,
layer_zero_padding_2d()
A LearningRateSchedule that uses a cosine decay schedule
learning_rate_schedule_cosine_decay( initial_learning_rate, decay_steps, alpha = 0, ..., name = NULL )
learning_rate_schedule_cosine_decay( initial_learning_rate, decay_steps, alpha = 0, ..., name = NULL )
initial_learning_rate |
A scalar |
decay_steps |
A scalar |
alpha |
A scalar |
... |
For backwards and forwards compatibility |
name |
String. Optional name of the operation. Defaults to 'CosineDecay'. |
See Loshchilov & Hutter, ICLR2016, SGDR: Stochastic Gradient Descent with Warm Restarts.
When training a model, it is often useful to lower the learning rate as
the training progresses. This schedule applies a cosine decay function
to an optimizer step, given a provided initial learning rate.
It requires a step
value to compute the decayed learning rate. You can
just pass a TensorFlow variable that you increment at each training step.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
decayed_learning_rate <- function(step) { step <- min(step, decay_steps) cosine_decay = <- 0.5 * (1 + cos(pi * step / decay_steps)) decayed <- (1 - alpha) * cosine_decay + alpha initial_learning_rate * decayed }
Example usage:
decay_steps <- 1000 lr_decayed_fn <- learning_rate_schedule_cosine_decay(initial_learning_rate, decay_steps)
You can pass this schedule directly into a keras Optimizer
as the learning_rate
.
A LearningRateSchedule that uses a cosine decay schedule with restarts
learning_rate_schedule_cosine_decay_restarts( initial_learning_rate, first_decay_steps, t_mul = 2, m_mul = 1, alpha = 0, ..., name = NULL )
learning_rate_schedule_cosine_decay_restarts( initial_learning_rate, first_decay_steps, t_mul = 2, m_mul = 1, alpha = 0, ..., name = NULL )
initial_learning_rate |
A scalar |
first_decay_steps |
A scalar |
t_mul |
A scalar |
m_mul |
A scalar |
alpha |
A scalar |
... |
For backwards and forwards compatibility |
name |
String. Optional name of the operation. Defaults to 'SGDRDecay'. |
See Loshchilov & Hutter, ICLR2016, SGDR: Stochastic Gradient Descent with Warm Restarts.
When training a model, it is often useful to lower the learning rate as
the training progresses. This schedule applies a cosine decay function with
restarts to an optimizer step, given a provided initial learning rate.
It requires a step
value to compute the decayed learning rate. You can
just pass a TensorFlow variable that you increment at each training step.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions.
The learning rate multiplier first decays
from 1 to alpha
for first_decay_steps
steps. Then, a warm
restart is performed. Each new warm restart runs for t_mul
times more
steps and with m_mul
times initial learning rate as the new learning rate.
You can pass this schedule directly into a keras Optimizer
as the learning_rate
.
A LearningRateSchedule that uses an exponential decay schedule
learning_rate_schedule_exponential_decay( initial_learning_rate, decay_steps, decay_rate, staircase = FALSE, ..., name = NULL )
learning_rate_schedule_exponential_decay( initial_learning_rate, decay_steps, decay_rate, staircase = FALSE, ..., name = NULL )
initial_learning_rate |
A scalar |
decay_steps |
A scalar |
decay_rate |
A scalar |
staircase |
Boolean. If |
... |
For backwards and forwards compatibility |
name |
String. Optional name of the operation. Defaults to 'ExponentialDecay'. |
When training a model, it is often useful to lower the learning rate as the training progresses. This schedule applies an exponential decay function to an optimizer step, given a provided initial learning rate.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
decayed_learning_rate <- function(step) initial_learning_rate * decay_rate ^ (step / decay_steps)
If the argument staircase
is TRUE
, then step / decay_steps
is
an integer division (%/%
) and the decayed learning rate follows a
staircase function.
You can pass this schedule directly into a optimizer as the learning rate (see example) Example: When fitting a Keras model, decay every 100000 steps with a base of 0.96:
initial_learning_rate <- 0.1 lr_schedule <- learning_rate_schedule_exponential_decay( initial_learning_rate, decay_steps = 100000, decay_rate = 0.96, staircase = TRUE) model %>% compile( optimizer= optimizer_sgd(learning_rate = lr_schedule), loss = 'sparse_categorical_crossentropy', metrics = 'accuracy') model %>% fit(data, labels, epochs = 5)
A LearningRateSchedule that uses an inverse time decay schedule
learning_rate_schedule_inverse_time_decay( initial_learning_rate, decay_steps, decay_rate, staircase = FALSE, ..., name = NULL )
learning_rate_schedule_inverse_time_decay( initial_learning_rate, decay_steps, decay_rate, staircase = FALSE, ..., name = NULL )
initial_learning_rate |
A scalar |
decay_steps |
A scalar |
decay_rate |
An R number. The decay rate. |
staircase |
Boolean. Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. |
... |
For backwards and forwards compatibility |
name |
String. Optional name of the operation. Defaults to 'InverseTimeDecay'. |
When training a model, it is often useful to lower the learning rate as
the training progresses. This schedule applies the inverse decay function
to an optimizer step, given a provided initial learning rate.
It requires a step
value to compute the decayed learning rate. You can
just pass a TensorFlow variable that you increment at each training step.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
decayed_learning_rate <- function(step) { initial_learning_rate / (1 + decay_rate * step / decay_step) }
or, if staircase
is TRUE
, as:
decayed_learning_rate function(step) { initial_learning_rate / (1 + decay_rate * floor(step / decay_step)) }
You can pass this schedule directly into a keras Optimizer
as the learning_rate
.
Example: Fit a Keras model when decaying 1/t
with a rate of 0.5
:
... initial_learning_rate <- 0.1 decay_steps <- 1.0 decay_rate <- 0.5 learning_rate_fn <- learning_rate_schedule_inverse_time_decay( initial_learning_rate, decay_steps, decay_rate) model %>% compile(optimizer = optimizer_sgd(learning_rate = learning_rate_fn), loss = 'sparse_categorical_crossentropy', metrics = 'accuracy') model %>% fit(data, labels, epochs = 5)
A LearningRateSchedule that uses a piecewise constant decay schedule
learning_rate_schedule_piecewise_constant_decay( boundaries, values, ..., name = NULL )
learning_rate_schedule_piecewise_constant_decay( boundaries, values, ..., name = NULL )
boundaries |
A list of |
values |
A list of |
... |
For backwards and forwards compatibility |
name |
A string. Optional name of the operation. Defaults to 'PiecewiseConstant'. |
The function returns a 1-arg callable to compute the piecewise constant when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions.
Example: use a learning rate that's 1.0 for the first 100001 steps, 0.5 for the next 10000 steps, and 0.1 for any additional steps.
step <- tf$Variable(0, trainable=FALSE) boundaries <- as.integer(c(100000, 110000)) values <- c(1.0, 0.5, 0.1) learning_rate_fn <- learning_rate_schedule_piecewise_constant_decay( boundaries, values) # Later, whenever we perform an optimization step, we pass in the step. learning_rate <- learning_rate_fn(step)
You can pass this schedule directly into a keras Optimizer
as the learning_rate
.
A LearningRateSchedule that uses a polynomial decay schedule
learning_rate_schedule_polynomial_decay( initial_learning_rate, decay_steps, end_learning_rate = 1e-04, power = 1, cycle = FALSE, ..., name = NULL )
learning_rate_schedule_polynomial_decay( initial_learning_rate, decay_steps, end_learning_rate = 1e-04, power = 1, cycle = FALSE, ..., name = NULL )
initial_learning_rate |
A scalar |
decay_steps |
A scalar |
end_learning_rate |
A scalar |
power |
A scalar |
cycle |
A boolean, whether or not it should cycle beyond decay_steps. |
... |
For backwards and forwards compatibility |
name |
String. Optional name of the operation. Defaults to 'PolynomialDecay'. |
It is commonly observed that a monotonically decreasing learning rate, whose
degree of change is carefully chosen, results in a better performing model.
This schedule applies a polynomial decay function to an optimizer step,
given a provided initial_learning_rate
, to reach an end_learning_rate
in the given decay_steps
.
It requires a step
value to compute the decayed learning rate. You
can just pass a TensorFlow variable that you increment at each training
step.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
decayed_learning_rate <- function(step) { step <- min(step, decay_steps) ((initial_learning_rate - end_learning_rate) * (1 - step / decay_steps) ^ (power) ) + end_learning_rate }
If cycle
is TRUE
then a multiple of decay_steps
is used, the first one
that is bigger than step
.
decayed_learning_rate <- function(step) { decay_steps <- decay_steps * ceiling(step / decay_steps) ((initial_learning_rate - end_learning_rate) * (1 - step / decay_steps) ^ (power) ) + end_learning_rate }
You can pass this schedule directly into a keras Optimizer
as the learning_rate
.
Example: Fit a model while decaying from 0.1 to 0.01 in 10000 steps using sqrt (i.e. power=0.5):
... starter_learning_rate <- 0.1 end_learning_rate <- 0.01 decay_steps <- 10000 learning_rate_fn <- learning_rate_schedule_polynomial_decay( starter_learning_rate, decay_steps, end_learning_rate, power = 0.5) model %>% compile(optimizer = optimizer_sgd(learning_rate = learning_rate_fn), loss = 'sparse_categorical_crossentropy', metrics = 'accuracy') model %>% fit(data, labels, epochs = 5)
Loss functions
loss_binary_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., reduction = "auto", name = "binary_crossentropy" ) loss_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0L, axis = -1L, ..., reduction = "auto", name = "categorical_crossentropy" ) loss_categorical_hinge( y_true, y_pred, ..., reduction = "auto", name = "categorical_hinge" ) loss_cosine_similarity( y_true, y_pred, axis = -1L, ..., reduction = "auto", name = "cosine_similarity" ) loss_hinge(y_true, y_pred, ..., reduction = "auto", name = "hinge") loss_huber( y_true, y_pred, delta = 1, ..., reduction = "auto", name = "huber_loss" ) loss_kullback_leibler_divergence( y_true, y_pred, ..., reduction = "auto", name = "kl_divergence" ) loss_kl_divergence( y_true, y_pred, ..., reduction = "auto", name = "kl_divergence" ) loss_logcosh(y_true, y_pred, ..., reduction = "auto", name = "log_cosh") loss_mean_absolute_error( y_true, y_pred, ..., reduction = "auto", name = "mean_absolute_error" ) loss_mean_absolute_percentage_error( y_true, y_pred, ..., reduction = "auto", name = "mean_absolute_percentage_error" ) loss_mean_squared_error( y_true, y_pred, ..., reduction = "auto", name = "mean_squared_error" ) loss_mean_squared_logarithmic_error( y_true, y_pred, ..., reduction = "auto", name = "mean_squared_logarithmic_error" ) loss_poisson(y_true, y_pred, ..., reduction = "auto", name = "poisson") loss_sparse_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, axis = -1L, ..., reduction = "auto", name = "sparse_categorical_crossentropy" ) loss_squared_hinge( y_true, y_pred, ..., reduction = "auto", name = "squared_hinge" )
loss_binary_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., reduction = "auto", name = "binary_crossentropy" ) loss_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0L, axis = -1L, ..., reduction = "auto", name = "categorical_crossentropy" ) loss_categorical_hinge( y_true, y_pred, ..., reduction = "auto", name = "categorical_hinge" ) loss_cosine_similarity( y_true, y_pred, axis = -1L, ..., reduction = "auto", name = "cosine_similarity" ) loss_hinge(y_true, y_pred, ..., reduction = "auto", name = "hinge") loss_huber( y_true, y_pred, delta = 1, ..., reduction = "auto", name = "huber_loss" ) loss_kullback_leibler_divergence( y_true, y_pred, ..., reduction = "auto", name = "kl_divergence" ) loss_kl_divergence( y_true, y_pred, ..., reduction = "auto", name = "kl_divergence" ) loss_logcosh(y_true, y_pred, ..., reduction = "auto", name = "log_cosh") loss_mean_absolute_error( y_true, y_pred, ..., reduction = "auto", name = "mean_absolute_error" ) loss_mean_absolute_percentage_error( y_true, y_pred, ..., reduction = "auto", name = "mean_absolute_percentage_error" ) loss_mean_squared_error( y_true, y_pred, ..., reduction = "auto", name = "mean_squared_error" ) loss_mean_squared_logarithmic_error( y_true, y_pred, ..., reduction = "auto", name = "mean_squared_logarithmic_error" ) loss_poisson(y_true, y_pred, ..., reduction = "auto", name = "poisson") loss_sparse_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, axis = -1L, ..., reduction = "auto", name = "sparse_categorical_crossentropy" ) loss_squared_hinge( y_true, y_pred, ..., reduction = "auto", name = "squared_hinge" )
y_true |
Ground truth values. shape = |
y_pred |
The predicted values. shape = |
from_logits |
Whether |
label_smoothing |
Float in |
axis |
The axis along which to compute crossentropy (the features axis).
Axis is 1-based (e.g, first axis is |
... |
Additional arguments passed on to the Python callable (for forward and backwards compatibility). |
reduction |
Only applicable if |
name |
Only applicable if |
delta |
A float, the point where the Huber loss function changes from a quadratic to linear. |
Loss functions for model training. These are typically supplied in
the loss
parameter of the compile.keras.engine.training.Model()
function.
If called with y_true
and y_pred
, then the corresponding loss is
evaluated and the result returned (as a tensor). Alternatively, if y_true
and y_pred
are missing, then a callable is returned that will compute the
loss function and, by default, reduce the loss to a scalar tensor; see the
reduction
parameter for details. (The callable is a typically a class
instance that inherits from keras$losses$Loss
).
Computes the binary crossentropy loss.
label_smoothing
details: Float in [0, 1]
. If > 0
then smooth the labels
by squeezing them towards 0.5 That is, using 1. - 0.5 * label_smoothing
for the target class and 0.5 * label_smoothing
for the non-target class.
Computes the categorical crossentropy loss.
When using the categorical_crossentropy loss, your targets should be in
categorical format (e.g. if you have 10 classes, the target for each sample
should be a 10-dimensional vector that is all-zeros except for a 1 at the
index corresponding to the class of the sample). In order to convert
integer targets into categorical targets, you can use the Keras utility
function to_categorical()
:
categorical_labels <- to_categorical(int_labels, num_classes = NULL)
Computes Huber loss value.
For each value x in error = y_true - y_pred
:
loss = 0.5 * x^2 if |x| <= d loss = d * |x| - 0.5 * d^2 if |x| > d
where d is delta
. See: https://en.wikipedia.org/wiki/Huber_loss
Logarithm of the hyperbolic cosine of the prediction error.
log(cosh(x))
is approximately equal to (x ** 2) / 2
for small x
and
to abs(x) - log(2)
for large x
. This means that 'logcosh' works mostly
like the mean squared error, but will not be so strongly affected by the
occasional wildly incorrect prediction. However, it may return NaNs if the
intermediate value cosh(y_pred - y_true)
is too large to be represented
in the chosen precision.
compile.keras.engine.training.Model()
,
loss_binary_crossentropy()
Generates a word rank-based probabilistic sampling table.
make_sampling_table(size, sampling_factor = 1e-05)
make_sampling_table(size, sampling_factor = 1e-05)
size |
Int, number of possible words to sample. |
sampling_factor |
The sampling factor in the word2vec formula. |
Used for generating the sampling_table
argument for skipgrams()
.
sampling_table[[i]]
is the probability of sampling the word i-th most common
word in a dataset (more common words should be sampled less frequently, for balance).
The sampling probabilities are generated according to the sampling distribution used in word2vec:
p(word) = min(1, sqrt(word_frequency / sampling_factor) / (word_frequency / sampling_factor))
We assume that the word frequencies follow Zipf's law (s=1) to derive a numerical approximation of frequency(rank):
frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank))
where gamma
is the Euler-Mascheroni constant.
An array of length size
where the ith entry is the
probability that a word of rank i should be sampled.
The word2vec formula is: p(word) = min(1, sqrt(word.frequency/sampling_factor) / (word.frequency/sampling_factor))
Other text preprocessing:
pad_sequences()
,
skipgrams()
,
text_hashing_trick()
,
text_one_hot()
,
text_to_word_sequence()
A Metric
object encapsulates metric logic and state that can be used to
track model performance during training. It is what is returned by the family
of metric functions that start with prefix metric_*
.
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
compile
model %>% compile( optimizer = 'sgd', loss = 'mse', metrics = list(metric_SOME_METRIC(), metric_SOME_OTHER_METRIC()) )
m <- metric_SOME_METRIC() for (e in seq(epochs)) { for (i in seq(train_steps)) { c(y_true, y_pred, sample_weight = NULL) %<-% ... m$update_state(y_true, y_pred, sample_weight) } cat('Final epoch result: ', as.numeric(m$result()), "\n") m$reset_state() }
To be implemented by subclasses:
initialize()
: All state variables should be created in this method by calling self$add_weight()
like:
self$var <- self$add_weight(...)
update_state()
: Has all updates to the state variables like:
self$var$assign_add(...)
result()
: Computes and returns a value for the metric from the state variables.
Example custom metric subclass:
metric_binary_true_positives <- new_metric_class( classname = "BinaryTruePositives", initialize = function(name = 'binary_true_positives', ...) { super$initialize(name = name, ...) self$true_positives <- self$add_weight(name = 'tp', initializer = 'zeros') }, update_state = function(y_true, y_pred, sample_weight = NULL) { y_true <- k_cast(y_true, "bool") y_pred <- k_cast(y_pred, "bool") values <- y_true & y_pred values <- k_cast(values, self$dtype) if (!is.null(sample_weight)) { sample_weight <- k_cast(sample_weight, self$dtype) sample_weight <- tf$broadcast_to(sample_weight, values$shape) values <- values * sample_weight } self$true_positives$assign_add(tf$reduce_sum(values)) }, result = function() self$true_positives ) model %>% compile(..., metrics = list(metric_binary_true_positives()))
The same metric_binary_true_positives
could be built with %py_class%
like
this:
metric_binary_true_positives(keras$metrics$Metric) %py_class% { initialize <- <same-as-above>, update_state <- <same-as-above>, result <- <same-as-above> }
Calculates how often predictions equal labels
metric_accuracy(..., name = NULL, dtype = NULL)
metric_accuracy(..., name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This metric creates two local variables, total
and count
that are used to
compute the frequency with which y_pred
matches y_true
. This frequency is
ultimately returned as binary accuracy
: an idempotent operation that simply
divides total
by count
.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Approximates the AUC (Area under the curve) of the ROC or PR curves
metric_auc( ..., num_thresholds = 200L, curve = "ROC", summation_method = "interpolation", thresholds = NULL, multi_label = FALSE, num_labels = NULL, label_weights = NULL, from_logits = FALSE, name = NULL, dtype = NULL )
metric_auc( ..., num_thresholds = 200L, curve = "ROC", summation_method = "interpolation", thresholds = NULL, multi_label = FALSE, num_labels = NULL, label_weights = NULL, from_logits = FALSE, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
num_thresholds |
(Optional) Defaults to 200. The number of thresholds toa use when discretizing the roc curve. Values must be > 1. |
curve |
(Optional) Specifies the name of the curve to be computed, 'ROC' (default) or 'PR' for the Precision-Recall-curve. |
summation_method |
(Optional) Specifies the Riemann summation method used. 'interpolation' (default)
applies mid-point summation scheme for |
thresholds |
(Optional) A list of floating point values to use as the
thresholds for discretizing the curve. If set, the |
multi_label |
boolean indicating whether multilabel data should be treated as such, wherein AUC is computed separately for each label and then averaged across labels, or (when FALSE) if the data should be flattened into a single label before AUC computation. In the latter case, when multilabel data is passed to AUC, each label-prediction pair is treated as an individual data point. Should be set to FALSE for multi-class data. |
num_labels |
(Optional) The number of labels, used when |
label_weights |
(Optional) list, array, or tensor of non-negative
weights used to compute AUCs for multilabel data. When |
from_logits |
boolean indicating whether the predictions ( |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
The AUC (Area under the curve) of the ROC (Receiver operating characteristic; default) or PR (Precision Recall) curves are quality measures of binary classifiers. Unlike the accuracy, and like cross-entropy losses, ROC-AUC and PR-AUC evaluate all the operational points of a model.
This class approximates AUCs using a Riemann sum. During the metric accumulation phrase, predictions are accumulated within predefined buckets by value. The AUC is then computed by interpolating per-bucket averages. These buckets define the evaluated operational points.
This metric creates four local variables, true_positives
, true_negatives
,
false_positives
and false_negatives
that are used to compute the AUC. To
discretize the AUC curve, a linearly spaced set of thresholds is used to
compute pairs of recall and precision values. The area under the ROC-curve is
therefore computed using the height of the recall values by the false
positive rate, while the area under the PR-curve is the computed using the
height of the precision values by the recall.
This value is ultimately returned as auc
, an idempotent operation that
computes the area under a discretized curve of precision versus recall values
(computed using the aforementioned variables). The num_thresholds
variable
controls the degree of discretization with larger numbers of thresholds more
closely approximating the true AUC. The quality of the approximation may vary
dramatically depending on num_thresholds
. The thresholds
parameter can be
used to manually specify thresholds which split the predictions more evenly.
For a best approximation of the real AUC, predictions
should be distributed
approximately uniformly in the range [0, 1]
(if from_logits=FALSE
). The
quality of the AUC approximation may be poor if this is not the case. Setting
summation_method
to 'minoring' or 'majoring' can help quantify the error in
the approximation by providing lower or upper bound estimate of the AUC.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Calculates how often predictions match binary labels
metric_binary_accuracy( y_true, y_pred, threshold = 0.5, ..., name = "binary_accuracy", dtype = NULL )
metric_binary_accuracy( y_true, y_pred, threshold = 0.5, ..., name = "binary_accuracy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
threshold |
(Optional) Float representing the threshold for deciding whether prediction values are 1 or 0. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This metric creates two local variables, total
and count
that are used to
compute the frequency with which y_pred
matches y_true
. This frequency is
ultimately returned as binary accuracy
: an idempotent operation that simply
divides total
by count
.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the crossentropy metric between the labels and predictions
metric_binary_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., name = "binary_crossentropy", dtype = NULL )
metric_binary_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., name = "binary_crossentropy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
from_logits |
(Optional) Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution. |
label_smoothing |
(Optional) Float in |
axis |
(Optional) (1-based) Defaults to -1. The dimension along which the metric is computed. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This is the crossentropy metric class to be used when there are only two label classes (0 and 1).
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Calculates how often predictions match one-hot labels
metric_categorical_accuracy( y_true, y_pred, ..., name = "categorical_accuracy", dtype = NULL )
metric_categorical_accuracy( y_true, y_pred, ..., name = "categorical_accuracy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
You can provide logits of classes as y_pred
, since argmax of
logits and probabilities are same.
This metric creates two local variables, total
and count
that are used to
compute the frequency with which y_pred
matches y_true
. This frequency is
ultimately returned as categorical accuracy
: an idempotent operation that
simply divides total
by count
.
y_pred
and y_true
should be passed in as vectors of probabilities, rather
than as labels. If necessary, use tf.one_hot
to expand y_true
as a vector.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the crossentropy metric between the labels and predictions
metric_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., name = "categorical_crossentropy", dtype = NULL )
metric_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, label_smoothing = 0, axis = -1L, ..., name = "categorical_crossentropy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
from_logits |
(Optional) Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution. |
label_smoothing |
(Optional) Float in |
axis |
(Optional) (1-based) Defaults to -1. The dimension along which the metric is computed. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This is the crossentropy metric class to be used when there are multiple
label classes (2 or more). Here we assume that labels are given as a one_hot
representation. eg., When labels values are c(2, 0, 1)
:
y_true = rbind(c(0, 0, 1), c(1, 0, 0), c(0, 1, 0))`
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
and y_pred
Computes the categorical hinge metric between y_true
and y_pred
metric_categorical_hinge(..., name = NULL, dtype = NULL)
metric_categorical_hinge(..., name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the cosine similarity between the labels and predictions
metric_cosine_similarity( ..., axis = -1L, name = "cosine_similarity", dtype = NULL )
metric_cosine_similarity( ..., axis = -1L, name = "cosine_similarity", dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
axis |
(Optional) (1-based) Defaults to -1. The dimension along which the metric is computed. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
cosine similarity = (a . b) / ||a|| ||b||
See: Cosine Similarity.
This metric keeps the average cosine similarity between predictions
and
labels
over a stream of data.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
If you want to compute the cosine_similarity for each case in a
mini-batch you can use loss_cosine_similarity()
.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Calculates the number of false negatives
metric_false_negatives(..., thresholds = NULL, name = NULL, dtype = NULL)
metric_false_negatives(..., thresholds = NULL, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) Defaults to 0.5. A float value or a
list of float threshold values in |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If sample_weight
is given, calculates the sum of the weights of
false negatives. This metric creates one local variable, accumulator
that is used to keep track of the number of false negatives.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Calculates the number of false positives
metric_false_positives(..., thresholds = NULL, name = NULL, dtype = NULL)
metric_false_positives(..., thresholds = NULL, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) Defaults to 0.5. A float value or a
list of float threshold values in |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If sample_weight
is given, calculates the sum of the weights of
false positives. This metric creates one local variable, accumulator
that is used to keep track of the number of false positives.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
and y_pred
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are
provided we will convert them to -1 or 1.
metric_hinge(y_true, y_pred, ..., name = "hinge", dtype = NULL)
metric_hinge(y_true, y_pred, ..., name = "hinge", dtype = NULL)
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
loss = tf$reduce_mean(tf$maximum(1 - y_true * y_pred, 0L), axis=-1L)
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes Kullback-Leibler divergence
metric_kullback_leibler_divergence( y_true, y_pred, ..., name = "kullback_leibler_divergence", dtype = NULL )
metric_kullback_leibler_divergence( y_true, y_pred, ..., name = "kullback_leibler_divergence", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
metric = y_true * log(y_true / y_pred)
See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
logcosh = log((exp(x) + exp(-x))/2)
, where x is the error (y_pred - y_true
)
metric_logcosh_error(..., name = "logcosh", dtype = NULL)
metric_logcosh_error(..., name = "logcosh", dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the (weighted) mean of the given values
metric_mean(..., name = "mean", dtype = NULL)
metric_mean(..., name = "mean", dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
For example, if values is c(1, 3, 5, 7)
then the mean is 4.
If the weights were specified as c(1, 1, 0, 0)
then the mean would be 2.
This metric creates two variables, total
and count
that are used to
compute the average of values
. This average is ultimately returned as mean
which is an idempotent operation that simply divides total
by count
.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Unlike most other metrics, this only takes a single tensor as input to update state.
Example usage with compile()
:
model$add_metric(metric_mean(name='mean_1')(outputs)) model %>% compile(optimizer='sgd', loss='mse')
Example standalone usage:
m <- metric_mean() m$update_state(c(1, 3, 5, 7)) m$result() m$reset_state() m$update_state(c(1, 3, 5, 7), sample_weight=c(1, 1, 0, 0)) m$result() as.numeric(m$result())
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the mean absolute error between the labels and predictions
metric_mean_absolute_error( y_true, y_pred, ..., name = "mean_absolute_error", dtype = NULL )
metric_mean_absolute_error( y_true, y_pred, ..., name = "mean_absolute_error", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
loss = mean(abs(y_true - y_pred), axis=-1)
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
and y_pred
Computes the mean absolute percentage error between y_true
and y_pred
metric_mean_absolute_percentage_error( y_true, y_pred, ..., name = "mean_absolute_percentage_error", dtype = NULL )
metric_mean_absolute_percentage_error( y_true, y_pred, ..., name = "mean_absolute_percentage_error", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
loss = 100 * mean(abs((y_true - y_pred) / y_true), axis=-1)
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the mean Intersection-Over-Union metric
metric_mean_iou(..., num_classes, name = NULL, dtype = NULL)
metric_mean_iou(..., num_classes, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
num_classes |
The possible number of labels the prediction task can have.
This value must be provided, since a confusion matrix of |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
Mean Intersection-Over-Union is a common evaluation metric for semantic image segmentation, which first computes the IOU for each semantic class and then computes the average over classes. IOU is defined as follows:
IOU = true_positive / (true_positive + false_positive + false_negative)
The predictions are accumulated in a confusion matrix, weighted by
sample_weight
and the metric is then calculated from it.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the mean relative error by normalizing with the given values
metric_mean_relative_error(..., normalizer, name = NULL, dtype = NULL)
metric_mean_relative_error(..., normalizer, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
normalizer |
The normalizer values with same shape as predictions. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This metric creates two local variables, total
and count
that are used to
compute the mean relative error. This is weighted by sample_weight
, and
it is ultimately returned as mean_relative_error
:
an idempotent operation that simply divides total
by count
.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
metric = mean(|y_pred - y_true| / normalizer)
For example:
m = metric_mean_relative_error(normalizer=c(1, 3, 2, 3)) m$update_state(c(1, 3, 2, 3), c(2, 4, 6, 8)) # result = mean(c(1, 1, 4, 5) / c(1, 3, 2, 3)) = mean(c(1, 1/3, 2, 5/3)) # = 5/4 = 1.25 m$result()
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the mean squared error between labels and predictions
metric_mean_squared_error( y_true, y_pred, ..., name = "mean_squared_error", dtype = NULL )
metric_mean_squared_error( y_true, y_pred, ..., name = "mean_squared_error", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
After computing the squared distance between the inputs, the mean value over the last dimension is returned.
loss = mean(square(y_true - y_pred), axis=-1)
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the mean squared logarithmic error
metric_mean_squared_logarithmic_error( y_true, y_pred, ..., name = "mean_squared_logarithmic_error", dtype = NULL )
metric_mean_squared_logarithmic_error( y_true, y_pred, ..., name = "mean_squared_logarithmic_error", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
loss = mean(square(log(y_true + 1) - log(y_pred + 1)), axis=-1)
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the element-wise (weighted) mean of the given tensors
metric_mean_tensor(..., shape = NULL, name = NULL, dtype = NULL)
metric_mean_tensor(..., shape = NULL, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
shape |
(Optional) A list of integers, a list of integers, or a 1-D Tensor of type int32. If not specified, the shape is inferred from the values at the first call of update_state. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
MeanTensor
returns a tensor with the same shape of the input tensors. The
mean value is updated by keeping local variables total
and count
. The
total
tracks the sum of the weighted values, and count
stores the sum of
the weighted counts.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Wraps a stateless metric function with the Mean metric
metric_mean_wrapper(..., fn, name = NULL, dtype = NULL)
metric_mean_wrapper(..., fn, name = NULL, dtype = NULL)
... |
named arguments to pass on to |
fn |
The metric function to wrap, with signature |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
You could use this class to quickly build a mean metric from a function. The
function needs to have the signature fn(y_true, y_pred)
and return a
per-sample loss array. MeanMetricWrapper$result()
will return
the average metric value across all samples seen so far.
For example:
accuracy <- function(y_true, y_pred) k_cast(y_true == y_pred, 'float32') accuracy_metric <- metric_mean_wrapper(fn = accuracy) model %>% compile(..., metrics=accuracy_metric)
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
and y_pred
metric = y_pred - y_true * log(y_pred)
metric_poisson(y_true, y_pred, ..., name = "poisson", dtype = NULL)
metric_poisson(y_true, y_pred, ..., name = "poisson", dtype = NULL)
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the precision of the predictions with respect to the labels
metric_precision( ..., thresholds = NULL, top_k = NULL, class_id = NULL, name = NULL, dtype = NULL )
metric_precision( ..., thresholds = NULL, top_k = NULL, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) A float value or a list of float
threshold values in |
top_k |
(Optional) Unset by default. An int value specifying the top-k predictions to consider when calculating precision. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
The metric creates two local variables, true_positives
and
false_positives
that are used to compute the precision. This value is
ultimately returned as precision
, an idempotent operation that simply
divides true_positives
by the sum of true_positives
and
false_positives
.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
If top_k
is set, we'll calculate precision as how often on average a class
among the top-k classes with the highest predicted values of a batch entry is
correct and can be found in the label for that entry.
If class_id
is specified, we calculate precision by considering only the
entries in the batch for which class_id
is above the threshold and/or in
the top-k highest predictions, and computing the fraction of them for which
class_id
is indeed a correct label.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes best precision where recall is >= specified value
metric_precision_at_recall( ..., recall, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
metric_precision_at_recall( ..., recall, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
recall |
A scalar value in range |
num_thresholds |
(Optional) Defaults to 200. The number of thresholds to use for matching the given recall. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This metric creates four local variables, true_positives
,
true_negatives
, false_positives
and false_negatives
that are used to
compute the precision at the given recall. The threshold for the given recall
value is computed and used to evaluate the corresponding precision.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
If class_id
is specified, we calculate precision by considering only the
entries in the batch for which class_id
is above the threshold predictions,
and computing the fraction of them for which class_id
is indeed a correct
label.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the recall of the predictions with respect to the labels
metric_recall( ..., thresholds = NULL, top_k = NULL, class_id = NULL, name = NULL, dtype = NULL )
metric_recall( ..., thresholds = NULL, top_k = NULL, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) A float value or a list of float
threshold values in |
top_k |
(Optional) Unset by default. An int value specifying the top-k predictions to consider when calculating recall. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
This metric creates two local variables, true_positives
and
false_negatives
, that are used to compute the recall. This value is
ultimately returned as recall
, an idempotent operation that simply divides
true_positives
by the sum of true_positives
and false_negatives
.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
If top_k
is set, recall will be computed as how often on average a class
among the labels of a batch entry is in the top-k predictions.
If class_id
is specified, we calculate recall by considering only the
entries in the batch for which class_id
is in the label, and computing the
fraction of them for which class_id
is above the threshold and/or in the
top-k predictions.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes best recall where precision is >= specified value
metric_recall_at_precision( ..., precision, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
metric_recall_at_precision( ..., precision, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
precision |
A scalar value in range |
num_thresholds |
(Optional) Defaults to 200. The number of thresholds to use for matching the given precision. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
For a given score-label-distribution the required precision might not be achievable, in this case 0.0 is returned as recall.
This metric creates four local variables, true_positives
, true_negatives
,
false_positives
and false_negatives
that are used to compute the recall
at the given precision. The threshold for the given precision value is
computed and used to evaluate the corresponding recall.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
If class_id
is specified, we calculate precision by considering only the
entries in the batch for which class_id
is above the threshold predictions,
and computing the fraction of them for which class_id
is indeed a correct
label.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
and y_pred
Computes root mean squared error metric between y_true
and y_pred
metric_root_mean_squared_error(..., name = NULL, dtype = NULL)
metric_root_mean_squared_error(..., name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
The sensitivity at a given specificity.
metric_sensitivity_at_specificity( ..., specificity, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
metric_sensitivity_at_specificity( ..., specificity, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
specificity |
A scalar value in range |
num_thresholds |
(Optional) Defaults to 200. The number of thresholds to use for matching the given specificity. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
Sensitivity
measures the proportion of actual positives that are correctly
identified as such (tp / (tp + fn))
. Specificity
measures the proportion of
actual negatives that are correctly identified as such (tn / (tn + fp))
.
This metric creates four local variables, true_positives
, true_negatives
,
false_positives
and false_negatives
that are used to compute the
sensitivity at the given specificity. The threshold for the given specificity
value is computed and used to evaluate the corresponding sensitivity.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
If class_id
is specified, we calculate precision by considering only the
entries in the batch for which class_id
is above the threshold predictions,
and computing the fraction of them for which class_id
is indeed a correct
label.
For additional information about specificity and sensitivity, see the following.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Calculates how often predictions match integer labels
metric_sparse_categorical_accuracy( y_true, y_pred, ..., name = "sparse_categorical_accuracy", dtype = NULL )
metric_sparse_categorical_accuracy( y_true, y_pred, ..., name = "sparse_categorical_accuracy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
acc = k_dot(sample_weight, y_true == k_argmax(y_pred, axis=2))
You can provide logits of classes as y_pred
, since argmax of
logits and probabilities are same.
This metric creates two local variables, total
and count
that are used to
compute the frequency with which y_pred
matches y_true
. This frequency is
ultimately returned as sparse categorical accuracy
: an idempotent operation
that simply divides total
by count
.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the crossentropy metric between the labels and predictions
metric_sparse_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, axis = -1L, ..., name = "sparse_categorical_crossentropy", dtype = NULL )
metric_sparse_categorical_crossentropy( y_true, y_pred, from_logits = FALSE, axis = -1L, ..., name = "sparse_categorical_crossentropy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
from_logits |
(Optional) Whether output is expected to be a logits tensor. By default, we consider that output encodes a probability distribution. |
axis |
(Optional) (1-based) Defaults to -1. The dimension along which the metric is computed. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
Use this crossentropy metric when there are two or more label classes.
We expect labels to be provided as integers. If you want to provide labels
using one-hot
representation, please use CategoricalCrossentropy
metric.
There should be # classes
floating point values per feature for y_pred
and a single floating point value per feature for y_true
.
In the snippet below, there is a single floating point value per example for
y_true
and # classes
floating pointing values per example for y_pred
.
The shape of y_true
is [batch_size]
and the shape of y_pred
is
[batch_size, num_classes]
.
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
K
predictionsComputes how often integer targets are in the top K
predictions
metric_sparse_top_k_categorical_accuracy( y_true, y_pred, k = 5L, ..., name = "sparse_top_k_categorical_accuracy", dtype = NULL )
metric_sparse_top_k_categorical_accuracy( y_true, y_pred, k = 5L, ..., name = "sparse_top_k_categorical_accuracy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
k |
(Optional) Number of top elements to look at for computing accuracy. Defaults to 5. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes best specificity where sensitivity is >= specified value
metric_specificity_at_sensitivity( ..., sensitivity, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
metric_specificity_at_sensitivity( ..., sensitivity, num_thresholds = 200L, class_id = NULL, name = NULL, dtype = NULL )
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
sensitivity |
A scalar value in range |
num_thresholds |
(Optional) Defaults to 200. The number of thresholds to use for matching the given sensitivity. |
class_id |
(Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
Sensitivity
measures the proportion of actual positives that are correctly
identified as such (tp / (tp + fn))
.
Specificity
measures the proportion of actual negatives that are correctly
identified as such (tn / (tn + fp))
.
This metric creates four local variables, true_positives
, true_negatives
,
false_positives
and false_negatives
that are used to compute the
specificity at the given sensitivity. The threshold for the given sensitivity
value is computed and used to evaluate the corresponding specificity.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
If class_id
is specified, we calculate precision by considering only the
entries in the batch for which class_id
is above the threshold predictions,
and computing the fraction of them for which class_id
is indeed a correct
label.
For additional information about specificity and sensitivity, see the following.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are
provided we will convert them to -1 or 1.
metric_squared_hinge(y_true, y_pred, ..., name = "squared_hinge", dtype = NULL)
metric_squared_hinge(y_true, y_pred, ..., name = "squared_hinge", dtype = NULL)
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
Computes the (weighted) sum of the given values
metric_sum(..., name = NULL, dtype = NULL)
metric_sum(..., name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
For example, if values is c(1, 3, 5, 7)
then the sum is 16.
If the weights were specified as c(1, 1, 0, 0)
then the sum would be 4.
This metric creates one variable, total
, that is used to compute the sum of
values
. This is ultimately returned as sum
.
If sample_weight
is NULL
, weights default to 1. Use sample_weight
of 0
to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
,
metric_true_positives()
K
predictionsComputes how often targets are in the top K
predictions
metric_top_k_categorical_accuracy( y_true, y_pred, k = 5L, ..., name = "top_k_categorical_accuracy", dtype = NULL )
metric_top_k_categorical_accuracy( y_true, y_pred, k = 5L, ..., name = "top_k_categorical_accuracy", dtype = NULL )
y_true |
Tensor of true targets. |
y_pred |
Tensor of predicted targets. |
k |
(Optional) Number of top elements to look at for computing accuracy. Defaults to 5. |
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If y_true
and y_pred
are missing, a (subclassed) Metric
instance is returned. The Metric
object can be passed directly to
compile(metrics = )
or used as a standalone object. See ?Metric
for
example usage.
Alternatively, if called with y_true
and y_pred
arguments, then the
computed case-wise values for the mini-batch are returned directly.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_true_negatives()
,
metric_true_positives()
Calculates the number of true negatives
metric_true_negatives(..., thresholds = NULL, name = NULL, dtype = NULL)
metric_true_negatives(..., thresholds = NULL, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) Defaults to 0.5. A float value or a
list of float threshold values in |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If sample_weight
is given, calculates the sum of the weights of
true negatives. This metric creates one local variable, accumulator
that is used to keep track of the number of true negatives.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_positives()
Calculates the number of true positives
metric_true_positives(..., thresholds = NULL, name = NULL, dtype = NULL)
metric_true_positives(..., thresholds = NULL, name = NULL, dtype = NULL)
... |
Passed on to the underlying metric. Used for forwards and backwards compatibility. |
thresholds |
(Optional) Defaults to 0.5. A float value or a
list of float threshold values in |
name |
(Optional) string name of the metric instance. |
dtype |
(Optional) data type of the metric result. |
If sample_weight
is given, calculates the sum of the weights of
true positives. This metric creates one local variable, true_positives
that is used to keep track of the number of true positives.
If sample_weight
is NULL
, weights default to 1.
Use sample_weight
of 0 to mask values.
A (subclassed) Metric
instance that can be passed directly to
compile(metrics = )
, or used as a standalone object. See ?Metric
for
example usage.
Other metrics:
custom_metric()
,
metric_accuracy()
,
metric_auc()
,
metric_binary_accuracy()
,
metric_binary_crossentropy()
,
metric_categorical_accuracy()
,
metric_categorical_crossentropy()
,
metric_categorical_hinge()
,
metric_cosine_similarity()
,
metric_false_negatives()
,
metric_false_positives()
,
metric_hinge()
,
metric_kullback_leibler_divergence()
,
metric_logcosh_error()
,
metric_mean()
,
metric_mean_absolute_error()
,
metric_mean_absolute_percentage_error()
,
metric_mean_iou()
,
metric_mean_relative_error()
,
metric_mean_squared_error()
,
metric_mean_squared_logarithmic_error()
,
metric_mean_tensor()
,
metric_mean_wrapper()
,
metric_poisson()
,
metric_precision()
,
metric_precision_at_recall()
,
metric_recall()
,
metric_recall_at_precision()
,
metric_root_mean_squared_error()
,
metric_sensitivity_at_specificity()
,
metric_sparse_categorical_accuracy()
,
metric_sparse_categorical_crossentropy()
,
metric_sparse_top_k_categorical_accuracy()
,
metric_specificity_at_sensitivity()
,
metric_squared_hinge()
,
metric_sum()
,
metric_top_k_categorical_accuracy()
,
metric_true_negatives()
Load a Keras model from the Saved Model format
model_from_saved_model(saved_model_path, custom_objects = NULL)
model_from_saved_model(saved_model_path, custom_objects = NULL)
saved_model_path |
a string specifying the path to the SavedModel directory. |
custom_objects |
Optional dictionary mapping string names to custom classes or functions (e.g. custom loss functions). |
a Keras model.
This functionality is experimental and only works with TensorFlow version >= "2.0".
Other saved_model:
model_to_saved_model()
Save and re-load models configurations as JSON. Note that the representation does not include the weights, only the architecture.
model_to_json(object) model_from_json(json, custom_objects = NULL)
model_to_json(object) model_from_json(json, custom_objects = NULL)
object |
Model object to save |
json |
JSON with model configuration |
custom_objects |
Optional named list mapping names to custom classes or functions to be considered during deserialization. |
Other model persistence:
get_weights()
,
model_to_yaml()
,
save_model_hdf5()
,
save_model_tf()
,
save_model_weights_hdf5()
,
serialize_model()
Save and re-load models configurations as YAML Note that the representation does not include the weights, only the architecture.
model_to_yaml(object) model_from_yaml(yaml, custom_objects = NULL)
model_to_yaml(object) model_from_yaml(yaml, custom_objects = NULL)
object |
Model object to save |
yaml |
YAML with model configuration |
custom_objects |
Optional named list mapping names to custom classes or functions to be considered during deserialization. |
Other model persistence:
get_weights()
,
model_to_json()
,
save_model_hdf5()
,
save_model_tf()
,
save_model_weights_hdf5()
,
serialize_model()
Create a new learning rate schedule type
new_learning_rate_schedule_class( classname, ..., initialize = NULL, call, get_config = NULL )
new_learning_rate_schedule_class( classname, ..., initialize = NULL, call, get_config = NULL )
classname |
string |
... |
methods and properties of the schedule class |
initialize , get_config
|
Additional recommended methods to implement. |
call |
function which takes a step argument (scalar integer tensor, the
current training step count, and returns the new learning rate). For
tracking additional state, objects |
A LearningRateSchedule
class generator.
These functions can be used to make custom objects that fit in the family of
existing keras types. For example, new_layer_class()
will return a class
constructor, an object that behaves like other layer functions such as
layer_dense()
. new_callback_class()
will return an object that behaves
similarly to other callback functions, like
callback_reduce_lr_on_plateau()
, and so on. All arguments with a default
NULL
value are optional methods that can be provided.
new_metric_class(classname, ..., initialize, update_state, result) new_loss_class(classname, ..., call = NULL) new_callback_class( classname, ..., on_epoch_begin = NULL, on_epoch_end = NULL, on_train_begin = NULL, on_train_end = NULL, on_batch_begin = NULL, on_batch_end = NULL, on_predict_batch_begin = NULL, on_predict_batch_end = NULL, on_predict_begin = NULL, on_predict_end = NULL, on_test_batch_begin = NULL, on_test_batch_end = NULL, on_test_begin = NULL, on_test_end = NULL, on_train_batch_begin = NULL, on_train_batch_end = NULL ) new_model_class( classname, ..., initialize = NULL, call = NULL, train_step = NULL, predict_step = NULL, test_step = NULL, compute_loss = NULL, compute_metrics = NULL ) new_layer_class( classname, ..., initialize = NULL, build = NULL, call = NULL, get_config = NULL ) mark_active(x)
new_metric_class(classname, ..., initialize, update_state, result) new_loss_class(classname, ..., call = NULL) new_callback_class( classname, ..., on_epoch_begin = NULL, on_epoch_end = NULL, on_train_begin = NULL, on_train_end = NULL, on_batch_begin = NULL, on_batch_end = NULL, on_predict_batch_begin = NULL, on_predict_batch_end = NULL, on_predict_begin = NULL, on_predict_end = NULL, on_test_batch_begin = NULL, on_test_batch_end = NULL, on_test_begin = NULL, on_test_end = NULL, on_train_batch_begin = NULL, on_train_batch_end = NULL ) new_model_class( classname, ..., initialize = NULL, call = NULL, train_step = NULL, predict_step = NULL, test_step = NULL, compute_loss = NULL, compute_metrics = NULL ) new_layer_class( classname, ..., initialize = NULL, build = NULL, call = NULL, get_config = NULL ) mark_active(x)
classname |
The classname as a string. Convention is for the classname to be a CamelCase version of the constructor. |
... |
Additional fields and methods for the new type. |
initialize , build , call , get_config , on_epoch_begin , on_epoch_end , on_train_begin , on_train_end , on_batch_begin , on_batch_end , on_predict_batch_begin , on_predict_batch_end , on_predict_begin , on_predict_end , on_test_batch_begin , on_test_batch_end , on_test_begin , on_test_end , on_train_batch_begin , on_train_batch_end , update_state , result , train_step , predict_step , test_step , compute_loss , compute_metrics
|
Optional methods that can be overridden. |
x |
A function that should be converted to an active property of the class type. |
mark_active()
is a decorator that can be used to indicate functions that
should become active properties of the class instances.
A new class generator object that inherits from the appropriate Keras base class.
Normalize a matrix or nd-array
normalize(x, axis = -1, order = 2)
normalize(x, axis = -1, order = 2)
x |
Matrix or array to normalize |
axis |
Axis along which to normalize. Axis indexes are 1-based (pass -1 to select the last axis). |
order |
Normalization order (e.g. 2 for L2 norm) |
A normalized copy of the array.
Optimizer that implements the Adadelta algorithm
optimizer_adadelta( learning_rate = 0.001, rho = 0.95, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adadelta", ... )
optimizer_adadelta( learning_rate = 0.001, rho = 0.95, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adadelta", ... )
learning_rate |
Initial value for the learning rate:
either a floating point value,
or a |
rho |
A |
epsilon |
Small floating point value used to maintain numerical stability. Defaults to 1e-7. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Adadelta optimization is a stochastic gradient descent method that is based on adaptive learning rate per dimension to address two drawbacks:
The continual decay of learning rates throughout training.
The need for a manually selected global learning rate.
Adadelta is a more robust extension of Adagrad that adapts learning rates based on a moving window of gradient updates, instead of accumulating all past gradients. This way, Adadelta continues learning even when many updates have been done. Compared to Adagrad, in the original version of Adadelta you don't have to set an initial learning rate. In this version, the initial learning rate can be set, as in most other Keras optimizers.
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the Adagrad algorithm
optimizer_adagrad( learning_rate = 0.001, initial_accumulator_value = 0.1, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adagrad", ... )
optimizer_adagrad( learning_rate = 0.001, initial_accumulator_value = 0.1, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adagrad", ... )
learning_rate |
Initial value for the learning rate:
either a floating point value,
or a |
initial_accumulator_value |
Floating point value. Starting value for the accumulators (per-parameter momentum values). Must be non-negative. |
epsilon |
Small floating point value used to maintain numerical stability. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Adagrad is an optimizer with parameter-specific learning rates, which are adapted relative to how frequently a parameter gets updated during training. The more updates a parameter receives, the smaller the updates.
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the Adam algorithm
optimizer_adam( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, amsgrad = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adam", ... )
optimizer_adam( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, amsgrad = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adam", ... )
learning_rate |
A |
beta_1 |
A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 1st moment estimates. Defaults to 0.9. |
beta_2 |
A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 2nd moment estimates. Defaults to 0.999. |
epsilon |
A small constant for numerical stability. This epsilon is "epsilon hat" in the Kingma and Ba paper (in the formula just before Section 2.1), not the epsilon in Algorithm 1 of the paper. Defaults to 1e-7. |
amsgrad |
Boolean. Whether to apply AMSGrad variant of this algorithm from
the paper "On the Convergence of Adam and beyond". Defaults to |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Adam optimization is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments.
According to Kingma et al., 2014, the method is "computationally efficient, has little memory requirement, invariant to diagonal rescaling of gradients, and is well suited for problems that are large in terms of data/parameters".
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the Adamax algorithm
optimizer_adamax( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adamax", ... )
optimizer_adamax( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Adamax", ... )
learning_rate |
A |
beta_1 |
A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates. |
beta_2 |
A float value or a constant float tensor. The exponential decay rate for the exponentially weighted infinity norm. |
epsilon |
A small constant for numerical stability. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Adamax, a variant of Adam based on the infinity norm, is a first-order gradient-based optimization method. Due to its capability of adjusting the learning rate based on data characteristics, it is suited to learn time-variant process, e.g., speech data with dynamically changed noise conditions. Default parameters follow those provided in the paper (see references below).
Initialization:
m = 0 # Initialize initial 1st moment vector u = 0 # Initialize the exponentially weighted infinity norm t = 0 # Initialize timestep
The update rule for parameter w
with gradient g
is described at the end
of section 7.1 of the paper (see the referenece section):
t += 1 m = beta1 * m + (1 - beta) * g u = max(beta2 * u, abs(g)) current_lr = learning_rate / (1 - beta1 ** t) w = w - current_lr * m / (u + epsilon)
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the FTRL algorithm
optimizer_ftrl( learning_rate = 0.001, learning_rate_power = -0.5, initial_accumulator_value = 0.1, l1_regularization_strength = 0, l2_regularization_strength = 0, l2_shrinkage_regularization_strength = 0, beta = 0, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Ftrl", ... )
optimizer_ftrl( learning_rate = 0.001, learning_rate_power = -0.5, initial_accumulator_value = 0.1, l1_regularization_strength = 0, l2_regularization_strength = 0, l2_shrinkage_regularization_strength = 0, beta = 0, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Ftrl", ... )
learning_rate |
A |
learning_rate_power |
A float value, must be less or equal to zero. Controls how the learning rate decreases during training. Use zero for a fixed learning rate. |
initial_accumulator_value |
The starting value for accumulators. Only zero or positive values are allowed. |
l1_regularization_strength |
A float value, must be greater than or equal to zero. Defaults to 0.0. |
l2_regularization_strength |
A float value, must be greater than or equal to zero. Defaults to 0.0. |
l2_shrinkage_regularization_strength |
A float value, must be greater than or equal to zero. This differs from L2 above in that the L2 above is a stabilization penalty, whereas this L2 shrinkage is a magnitude penalty. When input is sparse shrinkage will only happen on the active weights. |
beta |
A float value, representing the beta value from the paper. Defaults to 0.0. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
"Follow The Regularized Leader" (FTRL) is an optimization algorithm developed at Google for click-through rate prediction in the early 2010s. It is most suitable for shallow models with large and sparse feature spaces. The algorithm is described by McMahan et al., 2013. The Keras version has support for both online L2 regularization (the L2 regularization described in the paper above) and shrinkage-type L2 regularization (which is the addition of an L2 penalty to the loss function).
Initialization:
n = 0 sigma = 0 z = 0
Update rule for one variable w
:
prev_n = n n = n + g ** 2 sigma = (n ** -lr_power - prev_n ** -lr_power) / lr z = z + g - sigma * w if abs(z) < lambda_1: w = 0 else: w = (sgn(z) * lambda_1 - z) / ((beta + sqrt(n)) / alpha + lambda_2)
Notation:
lr
is the learning rate
g
is the gradient for the variable
lambda_1
is the L1 regularization strength
lambda_2
is the L2 regularization strength
lr_power
is the power to scale n.
Check the documentation for the l2_shrinkage_regularization_strength
parameter for more details when shrinkage is enabled, in which case gradient
is replaced with a gradient with shrinkage.
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_nadam()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the Nadam algorithm
optimizer_nadam( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Nadam", ... )
optimizer_nadam( learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-07, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "Nadam", ... )
learning_rate |
A |
beta_1 |
A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 1st moment estimates. Defaults to 0.9. |
beta_2 |
A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 2nd moment estimates. Defaults to 0.999. |
epsilon |
A small constant for numerical stability. This epsilon is "epsilon hat" in the Kingma and Ba paper (in the formula just before Section 2.1), not the epsilon in Algorithm 1 of the paper. Defaults to 1e-7. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Much like Adam is essentially RMSprop with momentum, Nadam is Adam with Nesterov momentum.
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_rmsprop()
,
optimizer_sgd()
Optimizer that implements the RMSprop algorithm
optimizer_rmsprop( learning_rate = 0.001, rho = 0.9, momentum = 0, epsilon = 1e-07, centered = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = 100L, jit_compile = TRUE, name = "RMSprop", ... )
optimizer_rmsprop( learning_rate = 0.001, rho = 0.9, momentum = 0, epsilon = 1e-07, centered = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = 100L, jit_compile = TRUE, name = "RMSprop", ... )
learning_rate |
Initial value for the learning rate:
either a floating point value,
or a |
rho |
float, defaults to 0.9. Discounting factor for the old gradients. |
momentum |
float, defaults to 0.0. If not 0.0., the optimizer tracks the
momentum value, with a decay rate equals to |
epsilon |
A small constant for numerical stability. This epsilon is "epsilon hat" in the Kingma and Ba paper (in the formula just before Section 2.1), not the epsilon in Algorithm 1 of the paper. Defaults to 1e-7. |
centered |
Boolean. If |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
The gist of RMSprop is to:
Maintain a moving (discounted) average of the square of gradients
Divide the gradient by the root of this average
This implementation of RMSprop uses plain momentum, not Nesterov momentum.
The centered version additionally maintains a moving average of the gradients, and uses that average to estimate the variance.
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_sgd()
Gradient descent (with momentum) optimizer
optimizer_sgd( learning_rate = 0.01, momentum = 0, nesterov = FALSE, amsgrad = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "SGD", ... )
optimizer_sgd( learning_rate = 0.01, momentum = 0, nesterov = FALSE, amsgrad = FALSE, weight_decay = NULL, clipnorm = NULL, clipvalue = NULL, global_clipnorm = NULL, use_ema = FALSE, ema_momentum = 0.99, ema_overwrite_frequency = NULL, jit_compile = TRUE, name = "SGD", ... )
learning_rate |
A |
momentum |
float hyperparameter >= 0 that accelerates gradient descent in the relevant direction and dampens oscillations. Defaults to 0, i.e., vanilla gradient descent. |
nesterov |
boolean. Whether to apply Nesterov momentum.
Defaults to |
amsgrad |
ignored. |
weight_decay |
Float, defaults to NULL. If set, weight decay is applied. |
clipnorm |
Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value. |
clipvalue |
Float. If set, the gradient of each weight is clipped to be no higher than this value. |
global_clipnorm |
Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value. |
use_ema |
Boolean, defaults to FALSE. If TRUE, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average. |
ema_momentum |
Float, defaults to 0.99. Only used if |
ema_overwrite_frequency |
Int or NULL, defaults to NULL. Only used if
|
jit_compile |
Boolean, defaults to TRUE. If TRUE, the optimizer will use XLA # noqa: E501 compilation. If no GPU device is found, this flag will be ignored. |
name |
String. The name to use for momentum accumulator weights created by the optimizer. |
... |
Used for backward and forward compatibility |
Update rule for parameter w
with gradient g
when momentum
is 0:
w = w - learning_rate * g
Update rule when momentum
is larger than 0:
velocity = momentum * velocity - learning_rate * g w = w + velocity
When nesterov=TRUE
, this rule becomes:
velocity = momentum * velocity - learning_rate * g w = w + momentum * velocity - learning_rate * g
Optimizer for use with compile.keras.engine.training.Model
.
Other optimizers:
optimizer_adadelta()
,
optimizer_adagrad()
,
optimizer_adam()
,
optimizer_adamax()
,
optimizer_ftrl()
,
optimizer_nadam()
,
optimizer_rmsprop()
Pads sequences to the same length
pad_sequences( sequences, maxlen = NULL, dtype = "int32", padding = "pre", truncating = "pre", value = 0 )
pad_sequences( sequences, maxlen = NULL, dtype = "int32", padding = "pre", truncating = "pre", value = 0 )
sequences |
List of lists where each element is a sequence |
maxlen |
int, maximum length of all sequences |
dtype |
type of the output sequences |
padding |
'pre' or 'post', pad either before or after each sequence. |
truncating |
'pre' or 'post', remove values from sequences larger than maxlen either in the beginning or in the end of the sequence |
value |
float, padding value |
This function transforms a list of num_samples
sequences (lists
of integers) into a matrix of shape (num_samples, num_timesteps)
.
num_timesteps
is either the maxlen
argument if provided, or the length
of the longest sequence otherwise.
Sequences that are shorter than num_timesteps
are padded with value
at
the end.
Sequences longer than num_timesteps
are truncated so that they fit the
desired length. The position where padding or truncation happens is
determined by the arguments padding
and truncating
, respectively.
Pre-padding is the default.
Matrix with dimensions (number_of_sequences, maxlen)
Other text preprocessing:
make_sampling_table()
,
skipgrams()
,
text_hashing_trick()
,
text_one_hot()
,
text_to_word_sequence()
Plots metrics recorded during training.
## S3 method for class 'keras_training_history' plot( x, y, metrics = NULL, method = c("auto", "ggplot2", "base"), smooth = getOption("keras.plot.history.smooth", TRUE), theme_bw = getOption("keras.plot.history.theme_bw", FALSE), ... )
## S3 method for class 'keras_training_history' plot( x, y, metrics = NULL, method = c("auto", "ggplot2", "base"), smooth = getOption("keras.plot.history.smooth", TRUE), theme_bw = getOption("keras.plot.history.theme_bw", FALSE), ... )
x |
Training history object returned from
|
y |
Unused. |
metrics |
One or more metrics to plot (e.g. |
method |
Method to use for plotting. The default "auto" will use ggplot2 if available, and otherwise will use base graphics. |
smooth |
Whether a loess smooth should be added to the plot, only
available for the |
theme_bw |
Use |
... |
Additional parameters to pass to the |
Plot a Keras model
## S3 method for class 'keras.engine.training.Model' plot( x, show_shapes = FALSE, show_dtype = FALSE, show_layer_names = TRUE, ..., rankdir = "TB", expand_nested = FALSE, dpi = 96, layer_range = NULL, show_layer_activations = FALSE, to_file = NULL )
## S3 method for class 'keras.engine.training.Model' plot( x, show_shapes = FALSE, show_dtype = FALSE, show_layer_names = TRUE, ..., rankdir = "TB", expand_nested = FALSE, dpi = 96, layer_range = NULL, show_layer_activations = FALSE, to_file = NULL )
x |
A Keras model instance |
show_shapes |
whether to display shape information. |
show_dtype |
whether to display layer dtypes. |
show_layer_names |
whether to display layer names. |
... |
passed on to |
rankdir |
a string specifying the format of the plot: |
expand_nested |
Whether to expand nested models into clusters. |
dpi |
Dots per inch. Increase this value if the image text appears excessively pixelated. |
layer_range |
|
show_layer_activations |
Display layer activations (only for layers that
have an |
to_file |
File name of the plot image. If |
Nothing, called for it's side effects.
ValueError: if plot_model
is called before the model is
built, unless a input_shape =
argument was supplied to
keras_model_sequential()
.
This function requires pydot and graphviz.
pydot
is by default installed by install_keras()
, but if you installed
tensorflow by other means, you can install pydot directly with :
reticulate::py_install("pydot", pip = TRUE)
In a conda environment, you can install graphviz with:
reticulate::conda_install(packages = "graphviz") # Restart the R session after install.
Otherwise you can install graphviz from here: https://graphviz.gitlab.io/download/
Remove the last layer in a model
pop_layer(object)
pop_layer(object)
object |
Keras model object |
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Returns predictions for a single batch of samples.
predict_on_batch(object, x)
predict_on_batch(object, x)
object |
Keras model object |
x |
Input data (vector, matrix, or array). You can also
pass a |
array of predictions.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
Generates output predictions for the input samples, processing the samples in a batched way.
## S3 method for class 'keras.engine.training.Model' predict( object, x, batch_size = NULL, verbose = "auto", steps = NULL, callbacks = NULL, ... )
## S3 method for class 'keras.engine.training.Model' predict( object, x, batch_size = NULL, verbose = "auto", steps = NULL, callbacks = NULL, ... )
object |
Keras model |
x |
Input data (vector, matrix, or array). You can also
pass a |
batch_size |
Integer. If unspecified, it will default to 32. |
verbose |
Verbosity mode, 0, 1, 2, or "auto". "auto" defaults to 1
for for most cases and defaults to |
steps |
Total number of steps (batches of samples) before declaring the
evaluation round finished. Ignored with the default value of |
callbacks |
List of callbacks to apply during prediction. |
... |
Unused |
vector, matrix, or array of predictions
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
,
train_on_batch()
L1 and L2 regularization
regularizer_l1(l = 0.01) regularizer_l2(l = 0.01) regularizer_l1_l2(l1 = 0.01, l2 = 0.01)
regularizer_l1(l = 0.01) regularizer_l2(l = 0.01) regularizer_l1_l2(l1 = 0.01, l2 = 0.01)
l |
Regularization factor. |
l1 |
L1 regularization factor. |
l2 |
L2 regularization factor. |
A regularizer that encourages input vectors to be orthogonal to each other
regularizer_orthogonal(factor = 0.01, mode = "rows", ...)
regularizer_orthogonal(factor = 0.01, mode = "rows", ...)
factor |
Float. The regularization factor. The regularization penalty will
be proportional to |
mode |
String, one of |
... |
For backwards and forwards compatibility layer <- layer_dense( units = 4, kernel_regularizer = regularizer_orthogonal(factor = 0.01)) |
It can be applied to either the rows of a matrix (mode="rows"
) or its
columns (mode="columns"
). When applied to a Dense
kernel of shape
(input_dim, units)
, rows mode will seek to make the feature vectors
(i.e. the basis of the output space) orthogonal to each other.
Reset the states for a layer
reset_states(object)
reset_states(object)
object |
Model or layer object |
Other layer methods:
count_params()
,
get_config()
,
get_input_at()
,
get_weights()
Save/Load models using HDF5 files
save_model_hdf5(object, filepath, overwrite = TRUE, include_optimizer = TRUE) load_model_hdf5(filepath, custom_objects = NULL, compile = TRUE)
save_model_hdf5(object, filepath, overwrite = TRUE, include_optimizer = TRUE) load_model_hdf5(filepath, custom_objects = NULL, compile = TRUE)
object |
Model object to save |
filepath |
File path |
overwrite |
Overwrite existing file if necessary |
include_optimizer |
If |
custom_objects |
Mapping class names (or function names) of custom (non-Keras) objects to class/functions (for example, custom metrics or custom loss functions). This mapping can be done with the dict() function of reticulate. |
compile |
Whether to compile the model after loading. |
The following components of the model are saved:
The model architecture, allowing to re-instantiate the model.
The model weights.
The state of the optimizer, allowing to resume training exactly where you left off. This allows you to save the entirety of the state of a model in a single file.
Saved models can be reinstantiated via load_model_hdf5()
. The model returned by
load_model_hdf5()
is a compiled model ready to be used (unless the saved model
was never compiled in the first place or compile = FALSE
is specified).
As an alternative to providing the custom_objects
argument, you can
execute the definition and persistence of your model using the
with_custom_object_scope()
function.
The serialize_model()
function enables saving Keras models to
R objects that can be persisted across R sessions.
Other model persistence:
get_weights()
,
model_to_json()
,
model_to_yaml()
,
save_model_tf()
,
save_model_weights_hdf5()
,
serialize_model()
Save/Load models using SavedModel format
save_model_tf( object, filepath, overwrite = TRUE, include_optimizer = TRUE, signatures = NULL, options = NULL ) load_model_tf(filepath, custom_objects = NULL, compile = TRUE)
save_model_tf( object, filepath, overwrite = TRUE, include_optimizer = TRUE, signatures = NULL, options = NULL ) load_model_tf(filepath, custom_objects = NULL, compile = TRUE)
object |
Model object to save |
filepath |
File path |
overwrite |
Overwrite existing file if necessary |
include_optimizer |
If |
signatures |
Signatures to save with the SavedModel. Please see the signatures
argument in |
options |
Optional |
custom_objects |
Mapping class names (or function names) of custom (non-Keras) objects to class/functions (for example, custom metrics or custom loss functions). This mapping can be done with the dict() function of reticulate. |
compile |
Whether to compile the model after loading. |
Other model persistence:
get_weights()
,
model_to_json()
,
model_to_yaml()
,
save_model_hdf5()
,
save_model_weights_hdf5()
,
serialize_model()
Save/Load model weights using HDF5 files
save_model_weights_hdf5(object, filepath, overwrite = TRUE) load_model_weights_hdf5( object, filepath, by_name = FALSE, skip_mismatch = FALSE, reshape = FALSE )
save_model_weights_hdf5(object, filepath, overwrite = TRUE) load_model_weights_hdf5( object, filepath, by_name = FALSE, skip_mismatch = FALSE, reshape = FALSE )
object |
Model object to save/load |
filepath |
Path to the file |
overwrite |
Whether to silently overwrite any existing file at the target location |
by_name |
Whether to load weights by name or by topological order. |
skip_mismatch |
Logical, whether to skip loading of layers
where there is a mismatch in the number of weights, or a mismatch in the
shape of the weight (only valid when |
reshape |
Reshape weights to fit the layer when the correct number of values are present but the shape does not match. |
The weight file has:
layer_names
(attribute), a list of strings (ordered names of model layers).
For every layer, a group
named layer.name
For every such layer group, a group attribute weight_names
, a list of strings
(ordered names of weights tensor of the layer).
For every weight in the layer, a dataset storing the weight value, named after the weight tensor.
For load_model_weights()
, if by_name
is FALSE
(default) weights are
loaded based on the network's topology, meaning the architecture should be
the same as when the weights were saved. Note that layers that don't have
weights are not taken into account in the topological ordering, so adding
or removing layers is fine as long as they don't have weights.
If by_name
is TRUE
, weights are loaded into layers only if they share
the same name. This is useful for fine-tuning or transfer-learning models
where some of the layers have changed.
Other model persistence:
get_weights()
,
model_to_json()
,
model_to_yaml()
,
save_model_hdf5()
,
save_model_tf()
,
serialize_model()
Save model weights in the SavedModel format
save_model_weights_tf(object, filepath, overwrite = TRUE) load_model_weights_tf( object, filepath, by_name = FALSE, skip_mismatch = FALSE, reshape = FALSE )
save_model_weights_tf(object, filepath, overwrite = TRUE) load_model_weights_tf( object, filepath, by_name = FALSE, skip_mismatch = FALSE, reshape = FALSE )
object |
Model object to save/load |
filepath |
Path to the file |
overwrite |
Whether to silently overwrite any existing file at the target location |
by_name |
Whether to load weights by name or by topological order. |
skip_mismatch |
Logical, whether to skip loading of layers
where there is a mismatch in the number of weights, or a mismatch in the
shape of the weight (only valid when |
reshape |
Reshape weights to fit the layer when the correct number of values are present but the shape does not match. |
When saving in TensorFlow format, all objects referenced by the network
are saved in the same format as tf.train.Checkpoint
, including any Layer instances
or Optimizer instances assigned to object attributes. For networks constructed from
inputs and outputs using tf.keras.Model(inputs, outputs)
, Layer instances used by
the network are tracked/saved automatically. For user-defined classes which inherit
from tf.keras.Model
, Layer instances must be assigned to object attributes,
typically in the constructor.
See the documentation of tf.train.Checkpoint
and tf.keras.Model
for details.
Enables persistence of text tokenizers alongside saved models.
save_text_tokenizer(object, filename) load_text_tokenizer(filename)
save_text_tokenizer(object, filename) load_text_tokenizer(filename)
object |
Text tokenizer fit with |
filename |
File to save/load |
You should always use the same text tokenizer for training and
prediction. In many cases however prediction will occur in another
session with a version of the model loaded via load_model_hdf5()
.
In this case you need to save the text tokenizer object after training and then reload it prior to prediction.
Other text tokenization:
fit_text_tokenizer()
,
sequences_to_matrix()
,
text_tokenizer()
,
texts_to_matrix()
,
texts_to_sequences()
,
texts_to_sequences_generator()
## Not run: # vectorize texts then save for use in prediction tokenizer <- text_tokenizer(num_words = 10000) %>% fit_text_tokenizer(tokenizer, texts) save_text_tokenizer(tokenizer, "tokenizer") # (train model, etc.) # ...later in another session tokenizer <- load_text_tokenizer("tokenizer") # (use tokenizer to preprocess data for prediction) ## End(Not run)
## Not run: # vectorize texts then save for use in prediction tokenizer <- text_tokenizer(num_words = 10000) %>% fit_text_tokenizer(tokenizer, texts) save_text_tokenizer(tokenizer, "tokenizer") # (train model, etc.) # ...later in another session tokenizer <- load_text_tokenizer("tokenizer") # (use tokenizer to preprocess data for prediction) ## End(Not run)
Convert a list of sequences into a matrix.
sequences_to_matrix( tokenizer, sequences, mode = c("binary", "count", "tfidf", "freq") )
sequences_to_matrix( tokenizer, sequences, mode = c("binary", "count", "tfidf", "freq") )
tokenizer |
Tokenizer |
sequences |
List of sequences (a sequence is a list of integer word indices). |
mode |
one of "binary", "count", "tfidf", "freq". |
A matrix
Other text tokenization:
fit_text_tokenizer()
,
save_text_tokenizer()
,
text_tokenizer()
,
texts_to_matrix()
,
texts_to_sequences()
,
texts_to_sequences_generator()
sequential_model_input_layer
sequential_model_input_layer( input_shape = NULL, batch_size = NULL, dtype = NULL, input_tensor = NULL, sparse = NULL, name = NULL, ragged = NULL, type_spec = NULL, ..., input_layer_name = NULL )
sequential_model_input_layer( input_shape = NULL, batch_size = NULL, dtype = NULL, input_tensor = NULL, sparse = NULL, name = NULL, ragged = NULL, type_spec = NULL, ..., input_layer_name = NULL )
input_shape |
an integer vector of dimensions (not including the batch
axis), or a |
batch_size |
Optional input batch size (integer or NULL). |
dtype |
Optional datatype of the input. When not provided, the Keras default float type will be used. |
input_tensor |
Optional tensor to use as layer input. If set, the layer
will use the |
sparse |
Boolean, whether the placeholder created is meant to be sparse.
Default to |
ragged |
Boolean, whether the placeholder created is meant to be ragged.
In this case, values of 'NULL' in the 'shape' argument represent ragged
dimensions. For more information about |
type_spec |
A |
... |
additional arguments passed on to |
input_layer_name , name
|
Optional name of the input layer (string). |
Model objects are external references to Keras objects which cannot be saved
and restored across R sessions. The serialize_model()
and
unserialize_model()
functions provide facilities to convert Keras models to
R objects for persistence within R data files.
serialize_model(model, include_optimizer = TRUE) unserialize_model(model, custom_objects = NULL, compile = TRUE)
serialize_model(model, include_optimizer = TRUE) unserialize_model(model, custom_objects = NULL, compile = TRUE)
model |
Keras model or R "raw" object containing serialized Keras model. |
include_optimizer |
If |
custom_objects |
Mapping class names (or function names) of custom (non-Keras) objects to class/functions (for example, custom metrics or custom loss functions). This mapping can be done with the dict() function of reticulate. |
compile |
Whether to compile the model after loading. |
serialize_model()
returns an R "raw" object containing an hdf5
version of the Keras model. unserialize_model()
returns a Keras model.
The save_model_hdf5()
function enables saving Keras models to
external hdf5 files.
Other model persistence:
get_weights()
,
model_to_json()
,
model_to_yaml()
,
save_model_hdf5()
,
save_model_tf()
,
save_model_weights_hdf5()
Generates skipgram word pairs.
skipgrams( sequence, vocabulary_size, window_size = 4, negative_samples = 1, shuffle = TRUE, categorical = FALSE, sampling_table = NULL, seed = NULL )
skipgrams( sequence, vocabulary_size, window_size = 4, negative_samples = 1, shuffle = TRUE, categorical = FALSE, sampling_table = NULL, seed = NULL )
sequence |
A word sequence (sentence), encoded as a list of word indices
(integers). If using a |
vocabulary_size |
Int, maximum possible word index + 1 |
window_size |
Int, size of sampling windows (technically half-window).
The window of a word |
negative_samples |
float >= 0. 0 for no negative (i.e. random) samples. 1 for same number as positive samples. |
shuffle |
whether to shuffle the word couples before returning them. |
categorical |
bool. if |
sampling_table |
1D array of size |
seed |
Random seed |
This function transforms a list of word indexes (lists of integers) into lists of words of the form:
(word, word in the same window), with label 1 (positive samples).
(word, random word from the vocabulary), with label 0 (negative samples).
Read more about Skipgram in this gnomic paper by Mikolov et al.: Efficient Estimation of Word Representations in Vector Space
List of couples
, labels
where:
couples
is a list of 2-element integer vectors: [word_index, other_word_index]
.
labels
is an integer vector of 0 and 1, where 1 indicates that other_word_index
was found in the same window as word_index
, and 0 indicates that other_word_index
was random.
if categorical
is set to TRUE
, the labels are categorical, ie. 1 becomes [0,1]
,
and 0 becomes [1, 0]
.
Other text preprocessing:
make_sampling_table()
,
pad_sequences()
,
text_hashing_trick()
,
text_one_hot()
,
text_to_word_sequence()
Print a summary of a Keras model
## S3 method for class 'keras.engine.training.Model' summary(object, ...) ## S3 method for class 'keras.engine.training.Model' format( x, line_length = width - (11L * show_trainable), positions = NULL, expand_nested = FALSE, show_trainable = x$built && as.logical(length(x$non_trainable_weights)), ..., compact = TRUE, width = getOption("width") ) ## S3 method for class 'keras.engine.training.Model' print(x, ...)
## S3 method for class 'keras.engine.training.Model' summary(object, ...) ## S3 method for class 'keras.engine.training.Model' format( x, line_length = width - (11L * show_trainable), positions = NULL, expand_nested = FALSE, show_trainable = x$built && as.logical(length(x$non_trainable_weights)), ..., compact = TRUE, width = getOption("width") ) ## S3 method for class 'keras.engine.training.Model' print(x, ...)
object , x
|
Keras model instance |
... |
for |
line_length |
Total length of printed lines |
positions |
Relative or absolute positions of log elements in each line.
If not provided, defaults to |
expand_nested |
Whether to expand the nested models. If not provided,
defaults to |
show_trainable |
Whether to show if a layer is trainable. If not
provided, defaults to |
compact |
Whether to remove white-space only lines from the model
summary. (Default |
width |
the column width to use for printing. |
format()
returns a length 1 character vector. print()
returns the
model object invisibly. summary()
returns the output of format()
invisibly after printing it.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
train_on_batch()
tf.data.Dataset
from text files in a directoryGenerate a tf.data.Dataset
from text files in a directory
text_dataset_from_directory( directory, labels = "inferred", label_mode = "int", class_names = NULL, batch_size = 32L, max_length = NULL, shuffle = TRUE, seed = NULL, validation_split = NULL, subset = NULL, follow_links = FALSE, ... )
text_dataset_from_directory( directory, labels = "inferred", label_mode = "int", class_names = NULL, batch_size = 32L, max_length = NULL, shuffle = TRUE, seed = NULL, validation_split = NULL, subset = NULL, follow_links = FALSE, ... )
directory |
Directory where the data is located.
If |
labels |
Either "inferred"
(labels are generated from the directory structure),
NULL (no labels),
or a list of integer labels of the same size as the number of
text files found in the directory. Labels should be sorted according
to the alphanumeric order of the text file paths
(obtained via |
label_mode |
|
class_names |
Only valid if |
batch_size |
Size of the batches of data. Default: |
max_length |
Maximum size of a text string. Texts longer than this will
be truncated to |
shuffle |
Whether to shuffle the data. Default: |
seed |
Optional random seed for shuffling and transformations. |
validation_split |
Optional float between 0 and 1, fraction of data to reserve for validation. |
subset |
One of "training" or "validation".
Only used if |
follow_links |
Whether to visits subdirectories pointed to by symlinks.
Defaults to |
... |
For future compatibility (unused presently). |
If your directory structure is:
main_directory/ ...class_a/ ......a_text_1.txt ......a_text_2.txt ...class_b/ ......b_text_1.txt ......b_text_2.txt
Then calling text_dataset_from_directory(main_directory, labels = 'inferred')
will return a tf.data.Dataset
that yields batches of texts from
the subdirectories class_a
and class_b
, together with labels
0 and 1 (0 corresponding to class_a
and 1 corresponding to class_b
).
Only .txt
files are supported at this time.
Converts a text to a sequence of indexes in a fixed-size hashing space.
text_hashing_trick( text, n, hash_function = NULL, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " " )
text_hashing_trick( text, n, hash_function = NULL, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " " )
text |
Input text (string). |
n |
Dimension of the hashing space. |
hash_function |
if |
filters |
Sequence of characters to filter out such as punctuation. Default includes basic punctuation, tabs, and newlines. |
lower |
Whether to convert the input to lowercase. |
split |
Sentence split marker (string). |
Two or more words may be assigned to the same index, due to possible collisions by the hashing function.
A list of integer word indices (unicity non-guaranteed).
Other text preprocessing:
make_sampling_table()
,
pad_sequences()
,
skipgrams()
,
text_one_hot()
,
text_to_word_sequence()
One-hot encode a text into a list of word indexes in a vocabulary of size n.
text_one_hot( input_text, n, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " ", text = NULL )
text_one_hot( input_text, n, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " ", text = NULL )
input_text |
Input text (string). |
n |
Size of vocabulary (integer) |
filters |
Sequence of characters to filter out such as punctuation. Default includes basic punctuation, tabs, and newlines. |
lower |
Whether to convert the input to lowercase. |
split |
Sentence split marker (string). |
text |
for compatibility purpose. use |
List of integers in [1, n]
. Each integer encodes a word (unicity
non-guaranteed).
Other text preprocessing:
make_sampling_table()
,
pad_sequences()
,
skipgrams()
,
text_hashing_trick()
,
text_to_word_sequence()
Convert text to a sequence of words (or tokens).
text_to_word_sequence( text, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " " )
text_to_word_sequence( text, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " " )
text |
Input text (string). |
filters |
Sequence of characters to filter out such as punctuation. Default includes basic punctuation, tabs, and newlines. |
lower |
Whether to convert the input to lowercase. |
split |
Sentence split marker (string). |
Words (or tokens)
Other text preprocessing:
make_sampling_table()
,
pad_sequences()
,
skipgrams()
,
text_hashing_trick()
,
text_one_hot()
Vectorize a text corpus, by turning each text into either a sequence of integers (each integer being the index of a token in a dictionary) or into a vector where the coefficient for each token could be binary, based on word count, based on tf-idf...
text_tokenizer( num_words = NULL, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " ", char_level = FALSE, oov_token = NULL )
text_tokenizer( num_words = NULL, filters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n", lower = TRUE, split = " ", char_level = FALSE, oov_token = NULL )
num_words |
the maximum number of words to keep, based on word
frequency. Only the most common |
filters |
a string where each element is a character that will be filtered from the texts. The default is all punctuation, plus tabs and line breaks, minus the ' character. |
lower |
boolean. Whether to convert the texts to lowercase. |
split |
character or string to use for token splitting. |
char_level |
if |
oov_token |
|
By default, all punctuation is removed, turning the texts into
space-separated sequences of words (words maybe include the ' character).
These sequences are then split into lists of tokens. They will then be
indexed or vectorized. 0
is a reserved index that won't be assigned to any
word.
The tokenizer object has the following attributes:
word_counts
— named list mapping words to the number of times they appeared
on during fit. Only set after fit_text_tokenizer()
is called on the tokenizer.
word_docs
— named list mapping words to the number of documents/texts they
appeared on during fit. Only set after fit_text_tokenizer()
is called on the tokenizer.
word_index
— named list mapping words to their rank/index (int). Only set
after fit_text_tokenizer()
is called on the tokenizer.
document_count
— int. Number of documents (texts/sequences) the tokenizer
was trained on. Only set after fit_text_tokenizer()
is called on the tokenizer.
Other text tokenization:
fit_text_tokenizer()
,
save_text_tokenizer()
,
sequences_to_matrix()
,
texts_to_matrix()
,
texts_to_sequences()
,
texts_to_sequences_generator()
Convert a list of texts to a matrix.
texts_to_matrix(tokenizer, texts, mode = c("binary", "count", "tfidf", "freq"))
texts_to_matrix(tokenizer, texts, mode = c("binary", "count", "tfidf", "freq"))
tokenizer |
Tokenizer |
texts |
Vector/list of texts (strings). |
mode |
one of "binary", "count", "tfidf", "freq". |
A matrix
Other text tokenization:
fit_text_tokenizer()
,
save_text_tokenizer()
,
sequences_to_matrix()
,
text_tokenizer()
,
texts_to_sequences()
,
texts_to_sequences_generator()
Only top "num_words" most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
texts_to_sequences(tokenizer, texts)
texts_to_sequences(tokenizer, texts)
tokenizer |
Tokenizer |
texts |
Vector/list of texts (strings). |
Other text tokenization:
fit_text_tokenizer()
,
save_text_tokenizer()
,
sequences_to_matrix()
,
text_tokenizer()
,
texts_to_matrix()
,
texts_to_sequences_generator()
Only top "num_words" most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
texts_to_sequences_generator(tokenizer, texts)
texts_to_sequences_generator(tokenizer, texts)
tokenizer |
Tokenizer |
texts |
Vector/list of texts (strings). |
Generator which yields individual sequences
Other text tokenization:
fit_text_tokenizer()
,
save_text_tokenizer()
,
sequences_to_matrix()
,
text_tokenizer()
,
texts_to_matrix()
,
texts_to_sequences()
This layer wrapper allows to apply a layer to every temporal slice of an input
time_distributed(object, layer, ...)
time_distributed(object, layer, ...)
object |
What to compose the new
|
layer |
a |
... |
standard layer arguments. |
Every input should be at least 3D, and the dimension of index one of the first input will be considered to be the temporal dimension.
Consider a batch of 32 video samples, where each sample is a 128x128 RGB image
with channels_last
data format, across 10 timesteps.
The batch input shape is (32, 10, 128, 128, 3)
.
You can then use TimeDistributed
to apply the same Conv2D
layer to each
of the 10 timesteps, independently:
input <- layer_input(c(10, 128, 128, 3)) conv_layer <- layer_conv_2d(filters = 64, kernel_size = c(3, 3)) output <- input %>% time_distributed(conv_layer) output$shape # TensorShape([None, 10, 126, 126, 64])
Because TimeDistributed
applies the same instance of Conv2D
to each of the
timestamps, the same set of weights are used at each timestamp.
Other layer wrappers:
bidirectional()
Creates a dataset of sliding windows over a timeseries provided as array
timeseries_dataset_from_array( data, targets, sequence_length, sequence_stride = 1L, sampling_rate = 1L, batch_size = 128L, shuffle = FALSE, ..., seed = NULL, start_index = NULL, end_index = NULL )
timeseries_dataset_from_array( data, targets, sequence_length, sequence_stride = 1L, sampling_rate = 1L, batch_size = 128L, shuffle = FALSE, ..., seed = NULL, start_index = NULL, end_index = NULL )
data |
array or eager tensor containing consecutive data points (timesteps). The first axis is expected to be the time dimension. |
targets |
Targets corresponding to timesteps in |
sequence_length |
Length of the output sequences (in number of timesteps). |
sequence_stride |
Period between successive output sequences.
For stride |
sampling_rate |
Period between successive individual timesteps
within sequences. For rate |
batch_size |
Number of timeseries samples in each batch (except maybe the last one). |
shuffle |
Whether to shuffle output samples, or instead draw them in chronological order. |
... |
For backwards and forwards compatibility, ignored presently. |
seed |
Optional int; random seed for shuffling. |
start_index , end_index
|
Optional int (1 based); data points earlier
than |
This function takes in a sequence of data-points gathered at equal intervals, along with time series parameters such as length of the sequences/windows, spacing between two sequence/windows, etc., to produce batches of timeseries inputs and targets.
A tf.data.Dataset
instance. If targets
was passed, the
dataset yields batches of two items: (batch_of_sequences, batch_of_targets)
. If not, the dataset yields only
batch_of_sequences
.
Consider indices 0:99
. With sequence_length=10
, sampling_rate=2
,
sequence_stride=3
, shuffle=FALSE
, the dataset will yield batches of
sequences composed of the following indices:
First sequence: 0 2 4 6 8 10 12 14 16 18 Second sequence: 3 5 7 9 11 13 15 17 19 21 Third sequence: 6 8 10 12 14 16 18 20 22 24 ... Last sequence: 78 80 82 84 86 88 90 92 94 96
In this case the last 3 data points are discarded since no full sequence can be generated to include them (the next sequence would have started at index 81, and thus its last step would have gone over 99).
Temporal regression.
Consider an array data
of scalar values, of shape (steps)
.
To generate a dataset that uses the past 10
timesteps to predict the next timestep, you would use:
steps <- 100 # data is integer seq with some noise data <- array(1:steps + abs(rnorm(steps, sd = .25))) inputs_data <- head(data, -10) # drop last 10 targets <- tail(data, -10) # drop first 10 dataset <- timeseries_dataset_from_array( inputs_data, targets, sequence_length=10) library(tfdatasets) dataset_iterator <- as_iterator(dataset) repeat { batch <- iter_next(dataset_iterator) if(is.null(batch)) break c(input, target) %<-% batch stopifnot(exprs = { # First sequence: steps [1-10] # Corresponding target: step 11 all.equal(as.array(input[1, ]), data[1:10]) all.equal(as.array(target[1]), data[11]) all.equal(as.array(input[2, ]), data[2:11]) all.equal(as.array(target[2]), data[12]) all.equal(as.array(input[3, ]), data[3:12]) all.equal(as.array(target[3]), data[13]) }) }
Temporal regression for many-to-many architectures.
Consider two arrays of scalar values X
and Y
,
both of shape (100)
. The resulting dataset should consist of samples with
20 timestamps each. The samples should not overlap.
To generate a dataset that uses the current timestamp
to predict the corresponding target timestep, you would use:
X <- seq(100) Y <- X*2 sample_length <- 20 input_dataset <- timeseries_dataset_from_array( X, NULL, sequence_length=sample_length, sequence_stride=sample_length) target_dataset <- timeseries_dataset_from_array( Y, NULL, sequence_length=sample_length, sequence_stride=sample_length) library(tfdatasets) dataset_iterator <- zip_datasets(input_dataset, target_dataset) %>% as_array_iterator() while(!is.null(batch <- iter_next(dataset_iterator))) { c(inputs, targets) %<-% batch stopifnot( all.equal(inputs[1,], X[1:sample_length]), all.equal(targets[1,], Y[1:sample_length]), # second sample equals output timestamps 20-40 all.equal(inputs[2,], X[(1:sample_length) + sample_length]), all.equal(targets[2,], Y[(1:sample_length) + sample_length]) ) }
int_sequence <- seq(20) dummy_dataset <- timeseries_dataset_from_array( data = head(int_sequence, -3), # drop last 3 targets = tail(int_sequence, -3), # drop first 3 sequence_length = 3, start_index = 3, end_index = 9, batch_size = 2 ) library(tfdatasets) dummy_dataset_iterator <- as_array_iterator(dummy_dataset) repeat { batch <- iter_next(dummy_dataset_iterator) if (is.null(batch)) # iterator exhausted break c(inputs, targets) %<-% batch for (r in 1:nrow(inputs)) cat(sprintf("input: [ %s ] target: %s\n", paste(inputs[r,], collapse = " "), targets[r])) cat("---------------------------\n") # demark batchs }
Will give output like:
input: [ 3 4 5 ] target: 6 input: [ 4 5 6 ] target: 7 --------------------------- input: [ 5 6 7 ] target: 8 input: [ 6 7 8 ] target: 9 --------------------------- input: [ 7 8 9 ] target: 10
Utility function for generating batches of temporal data.
timeseries_generator( data, targets, length, sampling_rate = 1, stride = 1, start_index = 0, end_index = NULL, shuffle = FALSE, reverse = FALSE, batch_size = 128 )
timeseries_generator( data, targets, length, sampling_rate = 1, stride = 1, start_index = 0, end_index = NULL, shuffle = FALSE, reverse = FALSE, batch_size = 128 )
data |
Object containing consecutive data points (timesteps). The data should be 2D, and axis 1 is expected to be the time dimension. |
targets |
Targets corresponding to timesteps in |
length |
Length of the output sequences (in number of timesteps). |
sampling_rate |
Period between successive individual timesteps
within sequences. For rate |
stride |
Period between successive output sequences.
For stride |
start_index , end_index
|
Data points earlier than |
shuffle |
Whether to shuffle output samples, or instead draw them in chronological order. |
reverse |
Boolean: if |
batch_size |
Number of timeseries samples in each batch (except maybe the last one). |
An object that can be passed to generator based training
functions (e.g. fit_generator()
).ma
Converts a class vector (integers) to binary class matrix.
to_categorical(y, num_classes = NULL, dtype = "float32")
to_categorical(y, num_classes = NULL, dtype = "float32")
y |
Class vector to be converted into a matrix (integers from 0 to num_classes). |
num_classes |
Total number of classes. |
dtype |
The data type expected by the input, as a string |
E.g. for use with loss_categorical_crossentropy()
.
A binary matrix representation of the input.
Single gradient update or model evaluation over one batch of samples.
train_on_batch(object, x, y, class_weight = NULL, sample_weight = NULL) test_on_batch(object, x, y, sample_weight = NULL)
train_on_batch(object, x, y, class_weight = NULL, sample_weight = NULL) test_on_batch(object, x, y, sample_weight = NULL)
object |
Keras model object |
x |
input data, as an array or list of arrays (if the model has multiple inputs). |
y |
labels, as an array. |
class_weight |
named list mapping classes to a weight value, used for scaling the loss function (during training only). |
sample_weight |
sample weights, as an array. |
Scalar training or test loss (if the model has no metrics) or list of scalars
(if the model computes other metrics). The property model$metrics_names
will give you the display labels for the scalar outputs.
Other model functions:
compile.keras.engine.training.Model()
,
evaluate.keras.engine.training.Model()
,
evaluate_generator()
,
fit.keras.engine.training.Model()
,
fit_generator()
,
get_config()
,
get_layer()
,
keras_model()
,
keras_model_sequential()
,
multi_gpu_model()
,
pop_layer()
,
predict.keras.engine.training.Model()
,
predict_generator()
,
predict_on_batch()
,
predict_proba()
,
summary.keras.engine.training.Model()
Select a Keras implementation and backend
use_implementation(implementation = c("keras", "tensorflow")) use_backend(backend = c("tensorflow", "cntk", "theano", "plaidml"))
use_implementation(implementation = c("keras", "tensorflow")) use_backend(backend = c("tensorflow", "cntk", "theano", "plaidml"))
implementation |
One of "keras" or "tensorflow" (defaults to "keras"). |
backend |
One of "tensorflow", "cntk", or "theano" (defaults to "tensorflow") |
Keras has multiple implementations (the original keras implementation and the implementation native to TensorFlow) and supports multiple backends ("tensorflow", "cntk", "theano", and "plaidml"). These functions allow switching between the various implementations and backends.
The functions should be called after library(keras)
and before calling
other functions within the package (see below for an example).
The default implementation and backend should be suitable for most use cases. The "tensorflow" implementation is useful when using Keras in conjunction with TensorFlow Estimators (the tfestimators R package).
## Not run: # use the tensorflow implementation library(keras) use_implementation("tensorflow") # use the cntk backend library(keras) use_backend("theano") ## End(Not run)
## Not run: # use the tensorflow implementation library(keras) use_implementation("tensorflow") # use the cntk backend library(keras) use_backend("theano") ## End(Not run)
Provide a scope with mappings of names to custom objects
with_custom_object_scope(objects, expr)
with_custom_object_scope(objects, expr)
objects |
Named list of objects |
expr |
Expression to evaluate |
There are many elements of Keras models that can be customized with
user objects (e.g. losses, metrics, regularizers, etc.). When
loading saved models that use these functions you typically
need to explicitily map names to user objects via the custom_objects
parmaeter.
The with_custom_object_scope()
function provides an alternative that
lets you create a named alias for a user object that applies to an entire
block of code, and is automatically recognized when loading saved models.
## Not run: # define custom metric metric_top_3_categorical_accuracy <- custom_metric("top_3_categorical_accuracy", function(y_true, y_pred) { metric_top_k_categorical_accuracy(y_true, y_pred, k = 3) }) with_custom_object_scope(c(top_k_acc = sparse_top_k_cat_acc), { # ...define model... # compile model (refer to "top_k_acc" by name) model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_nadam(), metrics = c("top_k_acc") ) # save the model save_model_hdf5("my_model.h5") # loading the model within the custom object scope doesn't # require explicitly providing the custom_object load_model_hdf5("my_model.h5") }) ## End(Not run)
## Not run: # define custom metric metric_top_3_categorical_accuracy <- custom_metric("top_3_categorical_accuracy", function(y_true, y_pred) { metric_top_k_categorical_accuracy(y_true, y_pred, k = 3) }) with_custom_object_scope(c(top_k_acc = sparse_top_k_cat_acc), { # ...define model... # compile model (refer to "top_k_acc" by name) model %>% compile( loss = "binary_crossentropy", optimizer = optimizer_nadam(), metrics = c("top_k_acc") ) # save the model save_model_hdf5("my_model.h5") # loading the model within the custom object scope doesn't # require explicitly providing the custom_object load_model_hdf5("my_model.h5") }) ## End(Not run)
This is conceptually similar to zip()
in Python, or R functions
purrr::transpose()
and data.table::transpose()
(albeit, accepting
elements in ...
instead of a single list), with one crucial difference: if
the provided objects are named, then matching is done by names, not
positions.
zip_lists(...)
zip_lists(...)
... |
R lists or atomic vectors, optionally named. |
All arguments supplied must be of the same length. If positional matching is required, then all arguments provided must be unnamed. If matching by names, then all arguments must have the same set of names, but they can be in different orders.
A inverted list
gradients <- list("grad_for_wt_1", "grad_for_wt_2", "grad_for_wt_3") weights <- list("weight_1", "weight_2", "weight_3") str(zip_lists(gradients, weights)) str(zip_lists(gradient = gradients, weight = weights)) names(gradients) <- names(weights) <- paste0("layer_", 1:3) str(zip_lists(gradients, weights[c(3, 1, 2)])) names(gradients) <- paste0("gradient_", 1:3) try(zip_lists(gradients, weights)) # error, names don't match # call unname directly for positional matching str(zip_lists(unname(gradients), unname(weights)))
gradients <- list("grad_for_wt_1", "grad_for_wt_2", "grad_for_wt_3") weights <- list("weight_1", "weight_2", "weight_3") str(zip_lists(gradients, weights)) str(zip_lists(gradient = gradients, weight = weights)) names(gradients) <- names(weights) <- paste0("layer_", 1:3) str(zip_lists(gradients, weights[c(3, 1, 2)])) names(gradients) <- paste0("gradient_", 1:3) try(zip_lists(gradients, weights)) # error, names don't match # call unname directly for positional matching str(zip_lists(unname(gradients), unname(weights)))