David's stuff!

Drawing graphs

split[source]

split(path, sep='/')

make_dot_graph[source]

make_dot_graph(nodes, edges, direction='LR', **kwargs)

to_dict[source]

to_dict(inputs)

class DotGraph[source]

DotGraph(graph, size=None, direction='LR')

Let's define a compact way of representing directed graphs (suitable for describing neural networks) as python dictionaries. Each node has a name which should be a string, a value which can be anything and an (ordered) list of inputs representing incoming edges from other nodes. Inputs are ordered as they typically represent function args. We can alternatively provide a dict of inputs to represent named args. Here is our graph format:

Graph := {name: (value, [input])}

or

Graph := {name: (value, {input_name: input}}

Here is an example of such a Graph using both kinds of inputs:

#define some node values - just strings for now we'll use more interesting values later
A,B,C,D = 'Aa Bb Cc Dd'.split()

graph = {
    'a': (A, []),
    'b': (B, ['a']),
    'c': (C, {'x': 'a', 'y': 'c'}),
    'd': (D, ['b'])
}

It will be useful to be able to display Graphs. Here is a function that does that:

draw_graph[source]

draw_graph(graph)

draw_graph(graph)
G a a b b a->b c c a->c d d b->d c->c

Exploding graphs

iter_nodes[source]

iter_nodes(graph)

bindings[source]

bindings(graph, inputs)

to_graph[source]

to_graph(value)

maybe_graph[source]

maybe_graph(x)

explode[source]

explode(graph, max_levels=-1, convert='maybe_graph')

Creating Networks

class ColorMap[source]

ColorMap() :: dict

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

class Network[source]

Network(graph, cache_activations=False) :: Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:to, etc.

to_network[source]

to_network(module, max_levels=-1)

from nbdev.export import notebook2script
notebook2script()
Converted 00_core.ipynb.
Converted index.ipynb.