deepr.layers.DAG

class deepr.layers.DAG(*layers)[source]

Class to easily compose layers in a deep learning network.

A Deep Learning Network is a Directed Acyclic Graph (DAG) of layers. The easiest way to define a DAG is by stacking layers on top of each others. For example:

@deepr.layers.layer(n_in=1, n_out=1)
def OffsetLayer(tensors, mode, offset):
    return tensors + offset

layer = deepr.layers.DAG(
    OffsetLayer(offset=1, inputs="x"),
    OffsetLayer(offset=2, outputs="y")
)
layer(1)  # (1 + 1) + 2 = 4
layer({"x": 1})  # {"y": 4}

Because in some cases your model is more complicated (branches etc.) you can exploit the inputs / outputs naming capability of the base Layer class. For example:

@deepr.layers.layer(n_in=2, n_out=1)
def Add(tensors, mode):
    x, y = tensors
    return x + y

layer = deepr.layers.DAG(
    OffsetLayer(offset=2, inputs="x", outputs="y"),
    OffsetLayer(offset=2, inputs="x", outputs="z"),
    Add(inputs="y, z", outputs="total"),
)
layer(1)  # (1 + 2) + (1 + 2) = 6
layer({"x": 1})  # {"total": 6}

As always, the resulting layer can be operated on Tensors or dictionaries of Tensors. The inputs / outputs of the DAG layer corresponds to the inputs of the first layer and the outputs of the last layer in the stack (intermediary nodes that are not returned by the last layer will not be returned).

An easy way to define arbitrary inputs / outputs nodes is to use the Select class. For example:

layer = deepr.layers.DAG(
    deepr.layers.Select("x1, x2"),
    OffsetLayer(offset=2, inputs="x1", outputs="y1"),
    OffsetLayer(offset=2, inputs="x2", outputs="y2"),
    Add(inputs="y1, y2", outputs="y3"),
    deepr.layers.Select("y1, y2, y3"),
)
layer((1, 2))  # (3, 4, 7)
layer({"x1": 1, "x2": 2})  # {"y1": 3, "y2": 4, "y3": 7}

Note that default naming still applies, so it won’t raise an error if you try stacking layers with incoherent shapes, as long as the correctly named nodes are defined.

layer = deepr.layers.DAG(
    deepr.layers.Select(n_in=2),  # Defines "t_0" and "t_1" nodes
    OffsetLayer(offset=2),  # Replace "t_0" <- "t_0" + 2
    Add(),  # Returns "t_0" + "t_1"
)
result = layer((tf.constant(2), tf.constant(2)))
with tf.Session() as sess:
    assert sess.run(result) == 6
__init__(*layers)[source]

Methods

__init__(*layers)

forward(tensors[, mode])

Forward method on one Tensor or a tuple of Tensors.

forward_as_dict(tensors[, mode])

Forward method of the layer