Hello,
I am trying to draw the graph of the model. But it seems the input_to_model parameter only takes tensor. However, there are some other types of input (like String, List etc.) in my model. I am wondering whether tensorboard can handle this scenario. Or is there any workaround?
Another confusion is that when I tried to drew a simple network which takes multiple tensors as input, the graph only shows the first one, others are shown as "unused". But I am sure these tensors are passed to through layers all the way to the output.
Thanks for any help in advance!
Objects labeled "unused" should be the Long() operator, which does not take any input nor been fed as input (You can check that by turning verbose on). I am not sure whether it's a bug or it's intended so I just let it stay there :)
I haven't consider the special input you mentioned. If you can provide a minimal sample, I an happy to try on that.
I haven't consider the special input you mentioned. If you can provide a minimal sample, I an happy to try on that.
Here is a piece of sample code modified from demo_graph.py.
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(
n_categories +
input_size +
hidden_size,
hidden_size)
self.i2o = nn.Linear(
n_categories +
input_size +
hidden_size,
output_size)
self.o2o = nn.Linear(hidden_size + output_size, output_size)
self.dropout = nn.Dropout(0.1)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, category, input, hidden, input_text):
# might do sth with input_text somewhere
input_combined = torch.cat((category, input, hidden), 1)
hidden = self.i2h(input_combined)
output = self.i2o(input_combined)
output_combined = torch.cat((hidden, output), 1)
output = self.o2o(output_combined)
output = self.dropout(output)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return torch.zeros(1, self.hidden_size)
n_letters = 100
n_hidden = 128
n_categories = 10
rnn = RNN(n_letters, n_hidden, n_categories)
cat = torch.Tensor(1, n_categories)
dummy_input = torch.Tensor(1, n_letters)
hidden = torch.Tensor(1, n_hidden)
input_text = ["Welcome"]*len(dummy_input)
out, hidden = rnn(cat, dummy_input, hidden, input_text)
with SummaryWriter(comment='RNN') as w:
w.add_graph(rnn, (cat, dummy_input, hidden, input_text), verbose=True)
I didn't wrote down the specific usage of input_text. In my code I just made a plot and used it as the axis labels.
And it will have some errors like:
Error occurs, No graph saved
Traceback (most recent call last):
File "/Users/Qiu/anaconda3/envs/telescope/lib/python3.6/site-packages/tensorboardX-1.4-py3.6.egg/tensorboardX/pytorch_graph.py", line 116, in graph
trace, _ = torch.jit.get_trace_graph(model, args)
File "/Users/Qiu/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 77, in get_trace_graph
return LegacyTracedModule(f)(*args, **kwargs)
File "/Users/Qiu/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/Users/Qiu/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 102, in forward
in_vars, in_desc = _flatten(args)
RuntimeError: Only tuples, lists and Variables supported as JIT inputs, but got str
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/Qiu/Desktop/demo_graph.py", line 179, in <module>
w.add_graph(rnn, (cat, dummy_input, hidden, input_text), verbose=True)
File "/Users/Qiu/anaconda3/envs/telescope/lib/python3.6/site-packages/tensorboardX-1.4-py3.6.egg/tensorboardX/writer.py", line 536, in add_graph
self.file_writer.add_graph(graph(model, input_to_model, verbose))
File "/Users/Qiu/anaconda3/envs/telescope/lib/python3.6/site-packages/tensorboardX-1.4-py3.6.egg/tensorboardX/pytorch_graph.py", line 119, in graph
_ = model(args) # don't catch, just print the error message
File "/Users/Qiu/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
TypeError: forward() missing 3 required positional arguments: 'input', 'hidden', and 'input_text'
Objects labeled "unused" should be the
Long()operator, which does not take any input nor been fed as input (You can check that by turningverboseon). I am not sure whether it's a bug or it's intended so I just let it stay there :)
As for the "unused" input, here is another simple example:
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(
input_size,
hidden_size)
self.i2o = nn.Linear(
hidden_size,
output_size)
self.o2o = nn.Linear(hidden_size + output_size, output_size)
self.dropout = nn.Dropout(0.1)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden):
hidden = self.i2h(input)
output = self.i2o(hidden)
output_combined = torch.cat((hidden, output), 1)
output = self.o2o(output_combined)
output = self.dropout(output)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return torch.zeros(1, self.hidden_size)
n_letters = 100
n_hidden = 128
n_categories = 10
rnn = RNN(n_letters, n_hidden, n_categories)
cat = torch.Tensor(1, n_categories)
dummy_input = torch.Tensor(1, n_letters)
hidden = torch.Tensor(1, n_hidden)
out, hidden = rnn(dummy_input, hidden)
with SummaryWriter(comment='RNN') as w:
w.add_graph(rnn, (dummy_input, hidden), verbose=True)

Here I believe input and hidden are passed to different layers parallelly (i2h and i2o). But the latter tensor appeared to be "unused" and the structure seems not correct as well (i2h and i2o part at least).
I swapped input and hidden but it seems weirder...
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.i2h = nn.Linear(
input_size,
hidden_size)
self.i2o = nn.Linear(
hidden_size,
output_size)
self.o2o = nn.Linear(hidden_size + output_size, output_size)
self.dropout = nn.Dropout(0.1)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, hidden, input):
hidden = self.i2h(input)
output = self.i2o(hidden)
output_combined = torch.cat((hidden, output), 1)
output = self.o2o(output_combined)
output = self.dropout(output)
output = self.softmax(output)
return output, hidden
def initHidden(self):
return torch.zeros(1, self.hidden_size)
n_letters = 100
n_hidden = 128
n_categories = 10
rnn = RNN(n_letters, n_hidden, n_categories)
cat = torch.Tensor(1, n_categories)
dummy_input = torch.Tensor(1, n_letters)
hidden = torch.Tensor(1, n_hidden)
out, hidden = rnn(hidden, dummy_input)
with SummaryWriter(comment='RNN') as w:
w.add_graph(rnn, (hidden, dummy_input), verbose=True)

Did I use this function right or am I missing anything? I am not quite understand the logic behind this phenomenon.
Anybody help馃槪
Have the same problem. Use list as input to my model and have TypeError: forward() missing X required positional arguments.
@Hui-Li I will handle this case this weekend.
Most helpful comment
Have the same problem. Use list as input to my model and have
TypeError: forward() missing X required positional arguments.