Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError in name = param_map[id(u)] #21

Open
hughperkins opened this issue Oct 4, 2018 · 6 comments
Open

KeyError in name = param_map[id(u)] #21

hughperkins opened this issue Oct 4, 2018 · 6 comments

Comments

@hughperkins
Copy link

When I run:

import torch
from torch import nn
import torchviz

h1 = nn.Linear(1, 1)

inputs = torch.zeros(1, 1, requires_grad=True)
inputs.data = torch.rand(1, 1)
print('inputs', inputs)
outputs = h1(inputs)
print('outputs', outputs)
torchviz.make_dot(outputs, params={'inputs': inputs})

I get:

inputs tensor([[0.3559]], requires_grad=True)
outputs tensor([[0.4822]], grad_fn=<ThAddmmBackward>)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-a36a1735c26c> in <module>()
     10 outputs = h1(inputs)
     11 print('outputs', outputs)
---> 12 torchviz.make_dot(outputs, params={'inputs': inputs})

/toknas/hugh/condap4/lib/python3.6/site-packages/torchviz/dot.py in make_dot(var, params)
     68             add_nodes(v.grad_fn)
     69     else:
---> 70         add_nodes(var.grad_fn)
     71 
     72     resize_graph(dot)

/toknas/hugh/condap4/lib/python3.6/site-packages/torchviz/dot.py in add_nodes(var)
     57                     if u[0] is not None:
     58                         dot.edge(str(id(u[0])), str(id(var)))
---> 59                         add_nodes(u[0])
     60             if hasattr(var, 'saved_tensors'):
     61                 for t in var.saved_tensors:

/toknas/hugh/condap4/lib/python3.6/site-packages/torchviz/dot.py in add_nodes(var)
     57                     if u[0] is not None:
     58                         dot.edge(str(id(u[0])), str(id(var)))
---> 59                         add_nodes(u[0])
     60             if hasattr(var, 'saved_tensors'):
     61                 for t in var.saved_tensors:

/toknas/hugh/condap4/lib/python3.6/site-packages/torchviz/dot.py in add_nodes(var)
     45             elif hasattr(var, 'variable'):
     46                 u = var.variable
---> 47                 name = param_map[id(u)] if params is not None else ''
     48                 node_name = '%s\n %s' % (name, size_to_str(u.size()))
     49                 dot.node(str(id(var)), node_name, fillcolor='lightblue')

KeyError: 140051140404712

collectenv output:

Collecting environment information...
PyTorch version: 0.4.1
Is debug build: No
CUDA used to build PyTorch: 9.0.176

OS: Ubuntu 16.04.5 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
CMake version: version 3.5.1

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 9.0.176
GPU models and configuration: GPU 0: Tesla M60
Nvidia driver version: 384.130
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.5.1.10
/usr/lib/x86_64-linux-gnu/libcudnn.so.6.0.21
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.0.3
/usr/lib/x86_64-linux-gnu/libcudnn_static_v5.a
/usr/lib/x86_64-linux-gnu/libcudnn_static_v6.a
/usr/lib/x86_64-linux-gnu/libcudnn_static_v7.a

Versions of relevant libraries:
[pip] numpy (1.14.3)
[pip] pytorchviz (0.0.1)
[pip] torch (0.4.1)
[pip] torchtext (0.2.3)
[pip] torchvision (0.2.1)
[conda] pytorch                   0.4.1           py36_cuda9.0.176_cudnn7.1.2_1    pytorch
[conda] pytorchviz                0.0.1                     <pip>
[conda] torchtext                 0.2.3                     <pip>
[conda] torchvision               0.2.1                    py36_1    pytorch
@szagoruyko
Copy link
Owner

Hi @hughperkins , it's been a while! you need to provide either None parameters, or * all * tensors that require grad in the graph, this will fix your example:

torchviz.make_dot(outputs, params={**{'inputs': inputs}, **dict(h1.named_parameters())})

we should be either giving a better error message, or allow missing named nodes.

@hughperkins
Copy link
Author

it's been a while!

Yes :)

you need to provide either None parameters, or * all * tensors that require grad in the graph

Ah, ok. Awesome, thanks! Will give this a try :)

@aaronlelevier
Copy link

I am getting this issue as well. Here is the final stacktrace line:

~/Documents/github/ssdmultibox/venv/lib/python3.7/site-packages/torchviz/dot.py in add_nodes(var)
     45             elif hasattr(var, 'variable'):
     46                 u = var.variable
---> 47                 name = param_map[id(u)] if params is not None else ''
     48                 node_name = '%s\n %s' % (name, size_to_str(u.size()))
     49                 dot.node(str(id(var)), node_name, fillcolor='lightblue')

KeyError: 4871725920

I tried:

torchviz.make_dot(loss, params={**{'inputs': inputs}, **dict(model.named_parameters())})

And also:

torchviz.make_dot(loss, params=dict(model.named_parameters()))

But, I still got the same error. Is this user error on my part? My model is an SSD Multibox model where the predictions are a list of tensors of different sizes. I am wondering if that might be the issue and it doesn't work with make_dot. Thanks

@Jacoobr
Copy link

Jacoobr commented Dec 1, 2018

Hi @szagoruyko ,I met the same error when i train my network using 'make_dot' function to visulize my nerual network structure. There are the errors as follow:

output = self.lstmlayer(output)[0]
Traceback (most recent call last):
  File "/data/xiaojianli/sorftWare/pycharm-2017.3.1/helpers/pydev/pydevd.py", line 1668, in <module>
    main()
  File "/data/xiaojianli/sorftWare/pycharm-2017.3.1/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/data/xiaojianli/sorftWare/pycharm-2017.3.1/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/mnt/data/polygonRNN_data/pytorch-polygon-rnn-master-pmri/train.py", line 113, in <module>
    make_dot(r, params=dict(list(PolygonNet().named_parameters())+[('x',x),('x1',x1),('x2',x2),('x3',x3)]))
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 70, in make_dot
    add_nodes(var.grad_fn)
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 59, in add_nodes
    add_nodes(u[0])
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 59, in add_nodes
    add_nodes(u[0])
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 59, in add_nodes
    add_nodes(u[0])
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 59, in add_nodes
    add_nodes(u[0])
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 59, in add_nodes
    add_nodes(u[0])
  File "/usr/local/lib/python2.7/dist-packages/torchviz/dot.py", line 47, in add_nodes
    name = param_map[id(u)] if params is not None else ''
KeyError: 140707794969536

there are the 'make_dot' function in my code:

        x = Variable(data[0].type(dtype), requires_grad=True)
        x1 = Variable(data[1].type(dtype), requires_grad=True)
        x2 = Variable(data[2].type(dtype), requires_grad=True)
        x3 = Variable(data[3].type(dtype), requires_grad=True)
        ta = Variable(data[4].type(dtype_t))
        optimizer.zero_grad()   #make the grad be zero
        r = net(x,x1,x2,x3)     # go to forward func in model.py script
        make_dot(r, params=dict(list(PolygonNet().named_parameters())+[('x',x),('x1',x1),('x2',x2),('x3',x3)]))

when i debuge my code, i found this line code output_o = self.lstmlayer(output_i)[0] that call lstmlayer casued this Error. Why when i call lstmlayer() the error occured? What's more, when i uncomment make_dot(r, params=dict(list(PolygonNet().named_parameters())+[('x',x),('x1',x1),('x2',x2),('x3',x3)])) code, the training code goes well. So the error only occured when i try to visulize the network structure. Waiting for your appreciative apply. Thank U~

@chensonglu
Copy link

@szagoruyko set None parameters works for me, thanks

@vcharvet
Copy link

vcharvet commented Mar 4, 2020

There's a hacky way to fix it
Change line 47 in torchviz/dot.py to
name = param_map.get(id(u), ' ') if params is not None else ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants