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

Can't add graph with pytorch v0.2 #9

Closed
miguelvr opened this issue Aug 12, 2017 · 8 comments
Closed

Can't add graph with pytorch v0.2 #9

miguelvr opened this issue Aug 12, 2017 · 8 comments

Comments

@miguelvr
Copy link

I tried to run this snippet:

import torch
import torch.nn as nn
from torch.autograd import Variable
from datetime import datetime
from tensorboard import SummaryWriter


x = Variable(torch.rand(10, 10), requires_grad=True)

model = nn.Linear(10, 10)

h = model(x)

writer = SummaryWriter('runs/'+datetime.now().strftime('%B%d  %H:%M:%S'))

writer.add_graph(model, h)
writer.close()

but I get this error message:

Traceback (most recent call last):
  File "/Users/Miguel/Documents/Unbabel/pytorch-tools/tests/test_tensorboard.py", line 16, in <module>
    writer.add_graph(model, h)
  File "/Users/Miguel/anaconda/envs/venv/lib/python2.7/site-packages/tensorboard/writer.py", line 259, in add_graph
    self.file_writer.add_graph(graph(model, lastVar))
  File "/Users/Miguel/anaconda/envs/venv/lib/python2.7/site-packages/tensorboard/graph.py", line 39, in graph
    make_list_of_nodes(lastVar.grad_fn)
  File "/Users/Miguel/anaconda/envs/venv/lib/python2.7/site-packages/tensorboard/graph.py", line 23, in make_list_of_nodes
    inputs.append(make_name(next_fn))
  File "/Users/Miguel/anaconda/envs/venv/lib/python2.7/site-packages/tensorboard/graph.py", line 12, in make_name
    return id2name[id(obj.variable)]+'_'+str(id(obj.variable))
KeyError: 4654832816
@lanpa
Copy link
Owner

lanpa commented Aug 13, 2017

Passing the variable directly to the model solved the problem. But I don't know why. XD
Does this bug happen only in v0.2?

import torch
import torch.nn as nn
from torch.autograd import Variable
from datetime import datetime
from tensorboard import SummaryWriter


model = nn.Linear(10, 10)

h = model(Variable(torch.rand(10, 10), requires_grad=True))

writer = SummaryWriter('runs/'+datetime.now().strftime('%B%d  %H:%M:%S'))

writer.add_graph(model, h)
writer.close()

@miguelvr
Copy link
Author

in theory it shouldn't be possible to add a graph in v0.1.12, right?

I tried with a nn.Sequential model and the result was the same.

@lanpa
Copy link
Owner

lanpa commented Aug 16, 2017

If your 0.1.12 is installed via pip then this function wont work. But if you build by yourself (0.1.12+xxxx) then it should work. Have you tried my solution?

@miguelvr
Copy link
Author

your solution works, but that shouldn't make any difference lol

@lucabergamini
Copy link
Contributor

I found something..
if you write

x = Variable(torch.rand(10, 10), requires_grad=True)

model = nn.Linear(10, 10)

h = model(x)

writer = SummaryWriter('runs/'+datetime.now().strftime('%B%d  %H:%M:%S'))

writer.add_graph(model,h)
writer.close()

and check using the debug the lastVar.grad_fn.next_functions you will find the weight,the bias (as expected) but also the input, filled with the variable field, holding the matrix.
If you write

#x = Variable(torch.rand(10, 10), requires_grad=True)

model = nn.Linear(10, 10)

#h = model(x)

writer = SummaryWriter('runs/'+datetime.now().strftime('%B%d  %H:%M:%S'))

writer.add_graph(model,model(Variable(torch.rand(10, 10), requires_grad=True)))
writer.close()

The input is still an element of the tuple, but it has a variable field empty.Since we create a binding dict id2name with the id of the Parameters of the network, the input is not included. As such, in the first case the input is still evaluated in the make_name function as it hold the variable field, but there is no association with its id, producing the above error.
An easy way to fix it would be to check before insert using the id from id2name in the function make_name

@zhangmozhe
Copy link

zhangmozhe commented Dec 13, 2017

I update my pytorch to version 0.3, but I got the same error after my update. Previously there is no problem. Here is my code:

output_sample = model(Variable(torch.Tensor(1, 2, patch_size, patch_size).type(dtype), requires_grad=True), Variable(torch.Tensor(1, 1, patch_size, patch_size).type(dtype), requires_grad=True))
writer = SummaryWriter()
writer.add_graph(model, output_sample)

Even using the simple code I still get the error:

model = nn.Linear(10, 10)
h = model(Variable(torch.rand(10, 10), requires_grad=True))
writer = SummaryWriter()
writer.add_graph(model, h)
writer.close()

@lanpa
Copy link
Owner

lanpa commented Dec 13, 2017

That's expected behavior on v0.3. Please have a try on the onnx version: https://github.com/lanpa/tensorboard-pytorch/blob/master/onnx_graph.py
(you may need to install onnx as well)

@lanpa lanpa closed this as completed in 13e2970 Dec 29, 2017
@lanpa
Copy link
Owner

lanpa commented Dec 29, 2017

Since v0.2 is legacy, closing this.

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

4 participants