3 This package provides a function that generates a
4 [dot file](https://en.wikipedia.org/wiki/DOT_(graph_description_language))
5 from a [PyTorch](http://pytorch.org) autograd graph.
11 ### agtree2dot.save_dot(variable, variable_labels, result_file) ###
13 Saves into `result_file` a dot file corresponding to the autograd
14 graph for the `Variable` `variable`. The dictionary `variable_labels`
15 associates strings to some variables, which will be used in the
20 A typical use is provided in [mlp.py](https://fleuret.org/git-extract/agtree2dot/mlp.py):
24 from torch.nn import functional as fn
25 from torch import Tensor
26 from torch.nn import Module
31 def __init__(self, input_dim, hidden_dim, output_dim):
32 super(MLP, self).__init__()
33 self.fc1 = nn.Linear(input_dim, hidden_dim)
34 self.fc2 = nn.Linear(hidden_dim, output_dim)
43 input = Tensor(100, 10).normal_()
44 target = Tensor(100, 1).normal_()
46 criterion = nn.MSELoss()
47 loss = criterion(output, target)
49 agtree2dot.save_dot(loss,
54 mlp.fc1.weight: 'weight1',
55 mlp.fc1.bias: 'bias1',
56 mlp.fc2.weight: 'weight2',
57 mlp.fc2.bias: 'bias2',
59 open('./mlp.dot', 'w'))
61 print('Generated mlp.dot')
64 subprocess.check_call(['dot', 'mlp.dot', '-Lg', '-T', 'pdf', '-o', 'mlp.pdf' ])
66 except subprocess.CalledProcessError:
67 print('Calling the dot command failed. Is Graphviz installed?')
70 print('Generated mlp.pdf')
73 which would generate a file mlp.dot and try to generate
74 [mlp.pdf](https://fleuret.org/git-extract/agtree2dot/mlp.pdf) from it
75 with [Graphviz tools.](http://www.graphviz.org/)