liveBook · Manning

Call()

As explained in Deep Learning with R, Third Edition

A call method defines the forward computation performed when a neural-network layer or model receives inputs.

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

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains implementation through custom dense and diffusion examples, while deferring detailed build() behavior.
Examples
Uses SimpleDense, shape inference, diffusion denoising, and PyTorch linear models to demonstrate forward computation.
What it assumes
Assumes familiarity with input tensors and layer composition while introducing state creation and forward computation.
What you can do after
Build custom Keras layers that transform inputs and return outputs through a call() method.
How it connects
Connects call() with Keras Layer, build(), weights, forward passes, tensor shapes, and PyTorch Module.__call__().

Also explained in

Compare how 2 books explain call() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.