Update.
[agtree2dot.git] / README.md
1 # Introduction #
2
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.
6
7 # Usage #
8
9 ## Functions ##
10
11 ### agtree2dot.save_dot(variable, variable_labels, result_file) ###
12
13 Saves into `result_file` a dot file corresponding to the autograd graph for `variable`, which can be either a single `Variable` or a set of `Variable`s. The dictionary `variable_labels` associates strings to some variables, which will be used in the resulting graph.
14
15 ## Example ##
16
17 A typical use would be:
18
19 ```python
20 import torch
21
22 from torch import nn
23 from torch.nn import functional as fn
24 from torch import Tensor
25 from torch.autograd import Variable
26 from torch.nn import Module
27
28 import agtree2dot
29
30 class MLP(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)
35
36     def forward(self, x):
37         x = self.fc1(x)
38         x = fn.tanh(x)
39         x = self.fc2(x)
40         return x
41
42 mlp = MLP(10, 20, 1)
43 input = Variable(Tensor(100, 10).normal_())
44 target = Variable(Tensor(100).normal_())
45 output = mlp(input)
46 criterion = nn.MSELoss()
47 loss = criterion(output, target)
48
49 agtree2dot.save_dot(loss,
50                     { input: 'input', target: 'target', loss: 'loss' },
51                     open('./mlp.dot', 'w'))
52 ```
53
54 which would generate a file mlp.dot, which can then be translated to
55 pdf using the [Graphviz tools](http://www.graphviz.org/)
56
57 ```
58 dot mlp.dot -Lg -T pdf -o mlp.pdf
59 ```
60
61 to produce [mlp.pdf.](https://fleuret.org/git-extract/agtree2dot/mlp.pdf)