forked from Diego999/pyGAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Diego999#13 from sh0416/master
[Bug fix] No backpropagation in sparse GAT
- Loading branch information
Showing
5 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from graphviz import Digraph | ||
|
||
import torch | ||
import models | ||
|
||
def make_dot(var, params): | ||
""" Produces Graphviz representation of PyTorch autograd graph | ||
Blue nodes are the Variables that require grad, orange are Tensors | ||
saved for backward in torch.autograd.Function | ||
Args: | ||
var: output Variable | ||
params: dict of (name, Variable) to add names to node that | ||
require grad (TODO: make optional) | ||
""" | ||
param_map = {id(v): k for k, v in params.items()} | ||
print(param_map) | ||
|
||
node_attr = dict(style='filled', | ||
shape='box', | ||
align='left', | ||
fontsize='12', | ||
ranksep='0.1', | ||
height='0.2') | ||
dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12")) | ||
seen = set() | ||
|
||
def size_to_str(size): | ||
return '('+(', ').join(['%d'% v for v in size])+')' | ||
|
||
def add_nodes(var): | ||
if var not in seen: | ||
if torch.is_tensor(var): | ||
dot.node(str(id(var)), size_to_str(var.size()), fillcolor='orange') | ||
elif hasattr(var, 'variable'): | ||
u = var.variable | ||
node_name = '%s\n %s' % (param_map.get(id(u)), size_to_str(u.size())) | ||
dot.node(str(id(var)), node_name, fillcolor='lightblue') | ||
else: | ||
dot.node(str(id(var)), str(type(var).__name__)) | ||
seen.add(var) | ||
if hasattr(var, 'next_functions'): | ||
for u in var.next_functions: | ||
if u[0] is not None: | ||
dot.edge(str(id(u[0])), str(id(var))) | ||
add_nodes(u[0]) | ||
if hasattr(var, 'saved_tensors'): | ||
for t in var.saved_tensors: | ||
dot.edge(str(id(t)), str(id(var))) | ||
add_nodes(t) | ||
add_nodes(var.grad_fn) | ||
return dot | ||
|
||
inputs = torch.randn(100, 50).cuda() | ||
adj = torch.randn(100, 100).cuda() | ||
model = models.SpGAT(50, 8, 7, 0.5, 0.01, 3) | ||
model = model.cuda() | ||
y = model(inputs, adj) | ||
|
||
g = make_dot(y, model.state_dict()) | ||
g.view() |