0Pricing
R Academy · Lesson

Building Sequential Models

Stack Dense layers, apply activations, and compile with loss and optimizer.

The Sequential Model

A sequential model is a linear stack of layers where each layer has exactly one input tensor and one output tensor. It is the simplest Keras model type and covers the majority of practical use cases: feedforward classifiers, regressors, and basic CNNs/RNNs.

library(keras)

# An empty sequential model
model <- keras_model_sequential()
print(model)

# Or specify the input shape upfront
model <- keras_model_sequential(input_shape = c(20))
print(model)

layer_dense() — Fully Connected Layer

layer_dense(units, activation) adds a fully connected (dense) layer where every input neuron connects to every output neuron. The units argument sets the output dimensionality. activation applies a nonlinearity: 'relu' is standard for hidden layers.

library(keras)

model <- keras_model_sequential(input_shape = c(30)) |>
  layer_dense(units = 128, activation = 'relu') |>
  layer_dense(units = 64,  activation = 'relu') |>
  layer_dense(units = 1,   activation = 'sigmoid')

# Print architecture
summary(model)

All lessons in this course

  1. Setting Up Keras and TensorFlow in R
  2. Building Sequential Models
  3. Convolutional Neural Networks Basics
  4. Training, Validation, and Preventing Overfitting
← Back to R Academy