Skip to contents

Keras Model composed of a linear stack of layers

Usage

keras_model_sequential(
  input_shape = NULL,
  name = NULL,
  ...,
  input_dtype = NULL,
  input_batch_size = NULL,
  input_sparse = NULL,
  input_batch_shape = NULL,
  input_name = NULL,
  input_tensor = NULL,
  input_optional = FALSE,
  trainable = TRUE,
  layers = list()
)

Arguments

input_shape

A shape integer vector, not including the batch size. For instance, shape=c(32) indicates that the expected input will be batches of 32-dimensional vectors. Elements of this shape can be NA; NA elements represent dimensions where the shape is not known and may vary (e.g. sequence length).

name

Name of model

...

additional arguments passed on to keras.layers.InputLayer.

input_dtype

The data type expected by the input, as a string (e.g. "float32", "int32"...)

input_batch_size

Optional static batch size (integer).

input_sparse

A boolean specifying whether the expected input will be sparse tensors. Note that, if sparse is FALSE, sparse tensors can still be passed into the input - they will be densified with a default value of 0. This feature is only supported with the TensorFlow backend. Defaults to FALSE.

input_batch_shape

An optional way to specify batch_size and input_shape as one argument.

input_name

Optional name string for the input layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided.

input_tensor

Optional existing tensor to wrap into the InputLayer. If set, the layer will use this tensor rather than creating a new placeholder tensor.

input_optional

Boolean, whether the input is optional or not. An optional input can accept NULL values.

trainable

Boolean, whether the model's variables should be trainable. You can also change the trainable status of a model/layer with freeze_weights() and unfreeze_weights().

layers

List of layers to add to the model.

Value

A Sequential model instance.

Note

If input_shape is omitted, then the model layer shapes, including the final model output shape, will not be known until the model is built, either by calling the model with an input tensor/array like model(input), (possibly via fit()/evaluate()/predict()), or by explicitly calling model$build(input_shape).

Examples

model <- keras_model_sequential(input_shape = c(784))
model |>
  layer_dense(units = 32) |>
  layer_activation('relu') |>
  layer_dense(units = 10) |>
  layer_activation('softmax')

model |> compile(
  optimizer = 'rmsprop',
  loss = 'categorical_crossentropy',
  metrics = c('accuracy')
)

model

## Model: "sequential"
## +---------------------------------+------------------------+---------------+
## | Layer (type)                    | Output Shape           |       Param # |
## +=================================+========================+===============+
## | dense (Dense)                   | (None, 32)             |        25,120 |
## +---------------------------------+------------------------+---------------+
## | activation (Activation)         | (None, 32)             |             0 |
## +---------------------------------+------------------------+---------------+
## | dense_1 (Dense)                 | (None, 10)             |           330 |
## +---------------------------------+------------------------+---------------+
## | activation_1 (Activation)       | (None, 10)             |             0 |
## +---------------------------------+------------------------+---------------+
##  Total params: 25,450 (99.41 KB)
##  Trainable params: 25,450 (99.41 KB)
##  Non-trainable params: 0 (0.00 B)

See also

Other model functions:
get_config()
get_layer()
keras_model()
pop_layer()
summary.keras.src.models.model.Model()

Other model creation:
keras_input()
keras_model()