call() defines the forward computation performed by a Keras layer or model when it receives input tensors. A layer combines state, such as weights, with this computation; build() typically creates the state, while call() transforms the inputs. section 3.6.2
layer_simple_dense <- new_layer_class(
classname = "SimpleDense",
initialize = function(units, activation = NULL) {
super$initialize()
self$units <- units
self$activation <- activation
},
build = function(input_shape) { #1
.[batch_dim, input_dim] <- input_shape
self$W <- self$add_weight(shape(input_dim, self$units), #2
initializer = "random_normal")
self$b <- self$add_weight(shape(self$units), initializer = "zeros")
},
call = function(inputs) { #3
y <- op_matmul(inputs, self$W) + self$b
if (!is.null(self$activation)) {
y <- self$activation(y)
}
y
}
)
A custom SimpleDense layer implements call() with matrix multiplication, bias addition, and an optional activation. section 3.6.2
Applying a custom layer
After creating a SimpleDense layer with 32 units, passing a tensor shaped (2, 784) through the layer produces an output shaped (2, 32). The layer instance is called like a function; Keras uses the incoming shape to build compatible state when needed. section 3.6.2
A call() implementation can:
- Compute a layer’s basic transformation, such as matrix multiplication followed by bias addition. section 3.6.2
- Apply optional operations such as an activation function before returning the result. section 3.6.2
- Coordinate a complete model pass, including normalization, random noise generation, diffusion scheduling, denoising, and returning intermediate quantities. section 17.2.4
- Serve as the forward-pass method invoked indirectly by a model object; PyTorch uses forward() behind __call__(). section 3.4.2

