Initial commit.
[agtree2dot.git] / README.md
1 # Introduction #
2
3 This package provides a function that generates a dot file from the
4 auto-grad graph.
5
6 # Usage #
7
8 ## Functions ##
9
10 ### agtree2dot.save_dot(variable, variable_labels, result_file) ###
11
12 Saves into `result_file` a dot file corresponding to the auto-grad 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.
13
14 ## Example ##
15
16 A typical use would be:
17
18 ```python
19 import torch
20
21 from torch import nn
22 from torch.nn import functional as fn
23 from torch import Tensor
24 from torch.autograd import Variable
25 from torch.nn import Module
26
27 import agtree2dot
28
29 class MLP(Module):
30     def __init__(self, input_dim, hidden_dim, output_dim):
31         super(MLP, self).__init__()
32         self.fc1 = nn.Linear(input_dim, hidden_dim)
33         self.fc2 = nn.Linear(hidden_dim, output_dim)
34
35     def forward(self, x):
36         x = self.fc1(x)
37         x = fn.tanh(x)
38         x = self.fc2(x)
39         return x
40
41 mlp = MLP(10, 20, 1)
42 input = Variable(Tensor(100, 10).normal_())
43 target = Variable(Tensor(100).normal_())
44 output = mlp(input)
45 criterion = nn.MSELoss()
46 loss = criterion(output, target)
47
48 agtree2dot.save_dot(loss,
49                     { input: 'input', loss: 'loss' },
50                     open('./mlp.dot', 'w'))
51 ```
52
53 which would generate a file mlp.dot, which can then be translated to pdf with
54
55 ```
56 dot mlp.dot -Lg -T pdf -o mlp.pdf
57 ```
58
59 to produce [mlp.pdf](https://fleuret.org/git-extract/agtree2dot/mlp.pdf).