Composing Modules

Open In Colab

In PyTorch, a forward method passes tensors from one module to the next by hand. A DeepLog module declares what its inputs and outputs are, as SymTensor shapes, so DeepLog can do that wiring by name: Sequential chains modules and compose_modules wires a graph of them.

import torch
from torch import nn

from deeplog import Sequential, SymTensor, WrappedModule, compose_modules

Sequential: a chain

Sequential works like nn.Sequential, and checks that each module’s output shape is the next module’s input shape.

raw = SymTensor([f"raw{i}" for i in range(4)])
hidden = SymTensor([f"hidden{i}" for i in range(8)])
classes = SymTensor([f"class{i}" for i in range(2)])

extractor = WrappedModule(nn.Linear(4, 8), raw, hidden)
classifier = WrappedModule(nn.Linear(8, 2), hidden, classes)

model = Sequential(extractor, classifier)
batch = torch.randn(16, 4)

print("input :", model.get_input_shape())
print("output:", model.get_output_shape())
print("result:", model(batch).shape)
input : [raw0 ... raw3]
output: [class0 class1]
result: torch.Size([16, 2])

compose_modules: a graph

compose_modules looks symbols up the way reshape does in the deeplogmodule notebook, across a graph. It takes modules in any order and the outputs you want, and feeds each module from the module that produces what it reads, or the part of it the module reads. A result that several modules read is computed once: below, both heads read the extractor’s output, the second only its first four columns.

The composed module’s inputs are the symbols no module produces, here raw0 to raw3. Without input_shape, each is an input of its own; input_shape lays them out, here as the one tensor a batch comes in.

first_half = SymTensor([f"hidden{i}" for i in range(4)])
score1 = SymTensor(["score1"])
score2 = SymTensor(["score2"])
head1 = WrappedModule(nn.Linear(8, 1), hidden, score1)
head2 = WrappedModule(nn.Linear(4, 1), first_half, score2)

composed = compose_modules(
    [head2, extractor, head1], output_shape=(score1, score2), input_shape=raw
)

out1, out2 = composed(batch)
print("outputs:", out1.shape, out2.shape)
outputs: torch.Size([16, 1]) torch.Size([16, 1])

Inputs no module produces

A module may read a symbol that no module produces. This head reads the extractor’s output and a prior beside it, in one tensor, so prior becomes an input of the composed module:

score3 = SymTensor(["score3"])
head3 = WrappedModule(
    nn.Linear(9, 1), SymTensor([f"hidden{i}" for i in range(8)] + ["prior"]), score3
)

composed = compose_modules([extractor, head3], output_shape=score3)
print("inputs:", composed.get_input_shape())
inputs: ([raw0], [raw1], [raw2], [raw3], [prior])

input_shape must hold every such symbol, and a layout that leaves one out is refused. Here the prior comes as a second tensor beside the batch:

prior = SymTensor(["prior"])
composed = compose_modules(
    [extractor, head3], output_shape=score3, input_shape=(raw, prior)
)

print("inputs:", composed.get_input_shape())
print("result:", composed(batch, torch.ones(16, 1)).shape)
inputs: ([raw0 raw1 raw2 raw3], [prior])
result: torch.Size([16, 1])