3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation. #
8 # This program is distributed in the hope that it will be useful, but #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
11 # General Public License for more details. #
13 # You should have received a copy of the GNU General Public License #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports #
18 #########################################################################
24 from torch.nn import Module
29 def __init__(self, input_dim, hidden_dim, output_dim):
31 self.fc1 = nn.Linear(input_dim, hidden_dim)
32 self.fc2 = nn.Linear(hidden_dim, output_dim)
41 criterion = nn.MSELoss()
43 input = torch.randn(100, 10)
44 target = torch.randn(100, 1)
48 loss = criterion(output, target)
50 agtree2dot.save_dot(loss,
55 mlp.fc1.weight: 'weight1',
56 mlp.fc1.bias: 'bias1',
57 mlp.fc2.weight: 'weight2',
58 mlp.fc2.bias: 'bias2',
60 open('./mlp.dot', 'w'))
62 print('Generated mlp.dot')
66 fontname = 'Computer Modern'
68 subprocess.check_call(['dot', 'mlp.dot',
71 '-Efontname=' + fontname, '-Efontsize=' + str(fontsize),
72 '-Nfontname=' + fontname, '-Nfontsize=' + str(fontsize),
75 except subprocess.CalledProcessError:
77 print('Calling the dot command failed. Is Graphviz installed?')
80 print('Generated mlp.pdf')