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):
26 from torch.nn import functional as fn
27 from torch import Tensor
28 from torch.autograd import Variable
29 from torch.nn import Module
34 def __init__(self, input_dim, hidden_dim, output_dim):
35 super(MLP, self).__init__()
36 self.fc1 = nn.Linear(input_dim, hidden_dim)
37 self.fc2 = nn.Linear(hidden_dim, output_dim)
46 input = Variable(Tensor(100, 10).normal_())
47 target = Variable(Tensor(100).normal_())
49 criterion = nn.MSELoss()
50 loss = criterion(output, target)
52 agtree2dot.save_dot(loss,
57 mlp.fc1.weight: 'weight1',
58 mlp.fc1.bias: 'bias1',
59 mlp.fc2.weight: 'weight2',
60 mlp.fc2.bias: 'bias2',
62 open('./mlp.dot', 'w'))
64 print('Generated mlp.dot')
67 subprocess.check_call(["dot", "mlp.dot", "-Lg", "-T", "pdf", "-o", "mlp.pdf" ])
68 except subprocess.CalledProcessError:
69 print('Calling the dot command failed. Is Graphviz installed?')
72 print('Generated mlp.pdf')
75 which would generate a file mlp.dot and try to generate
76 [mlp.pdf](https://fleuret.org/git-extract/agtree2dot/mlp.pdf) from it
77 with [Graphviz tools.](http://www.graphviz.org/)