How to visualize model in Pytorch

hs99: <p>Just like we have plot_model in keras, is there some way in pytorch by which model can be visualized?<br>
I tried make_dot using:</p>
<pre><code class="lang-auto">batch = next(iter(dataloader_train))
yhat = model(batch.text) # Give dummy batch to forward().
</code></pre>
<pre><code class="lang-auto">from torchviz import make_dot

make_dot(yhat, params=dict(list(model.named_parameters()))).render("rnn_torchviz", format="png")
</code></pre>
<p>but this gives output that batch has no attribute ‘text’<br>
Help?</p>

AlphaBetaGamma96: <p>replace <code>yhat</code> with <code>model(batch.text)</code> in the <code>make_dot</code> call?</p>
<p>From what I remember <code>make_dot</code> takes a model instance with an input and returns an object which you can render into a .pdf.</p>
<p>The error you get says that the variable <code>batch</code> has no attribute <code>text</code>. Try printing <code>batch.text</code> and see what it returns?</p>

hs99: <p><a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>  Thanks!<br>
But this is what I get: (THE ERROR AFTER replacing)<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/3/d35ff0f7c52bb201088805f7c2a782cf11fa662d.png" data-download-href="https://discuss.pytorch.org/uploads/default/d35ff0f7c52bb201088805f7c2a782cf11fa662d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d35ff0f7c52bb201088805f7c2a782cf11fa662d_2_690x344.png" alt="image" data-base62-sha1="u9UizjL809jSgl4HfTBi7r306v3" width="690" height="344" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d35ff0f7c52bb201088805f7c2a782cf11fa662d_2_690x344.png, https://discuss.pytorch.org/uploads/default/original/3X/d/3/d35ff0f7c52bb201088805f7c2a782cf11fa662d.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/3/d35ff0f7c52bb201088805f7c2a782cf11fa662d.png 2x" data-dominant-color="787A7D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">856×427 33.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

AlphaBetaGamma96: <p>So, the error is due to the fact that the variable <code>batch</code> which is a <code>list</code> has no attribute <code>text</code>. Shouldn’t <code>batch</code> be of type <code>Tensor</code>(if it’s being passed through a model?) Do the elements within the list have <code>.text</code> as an attribute?</p>

hs99: <p><a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>  This is what batch is:<br>
batch = next(iter(train_loader_1))</p>
<p>I am new to pytorch…so am unable to fully understand!Any help will be appreciated!</p>

AlphaBetaGamma96: <p>Yes, but it’s returning a variable of type <code>list</code>. Does your model take a <code>list</code> as input or a <code>Tensor</code>? And regardless of what type it should be, <code>batch</code> doesn’t have <code>text</code> as an attribute. (That’s what’s causing the error “list object has no attribute text”). Do the elements <em>within</em> batch have the attribute <code>.text</code>?</p>

hs99: <p>The input is a tensor<br>
Also batch doesn’t have text attribute</p>
<p>Still what else i can do/replace this code with to plot my model…just as we do in keras (plot-model) …is there some easy way!!<img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

AlphaBetaGamma96: <p>Can you print what is returned by <code>print(type(batch))</code>? I’m pretty sure <code>batch</code> is a <code>list</code> because your error states that a list object doesn’t have the attribute <code>text</code> and you call <code>batch.text</code> which would call the <code>text</code> attribute of batch… which doesn’t exist</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="hs99" data-post="7" data-topic="116091">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hs99/48/36177_2.png" class="avatar"> hs99:</div>
<blockquote>
<p>Also batch doesn’t have text attribute</p>
</blockquote>
</aside>
<p>This is the source of your error. Do the elements within the list have text as an attribute?</p>

hs99: <p><a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a><br>
print(type(batch))  gives O/p as:</p>
<p>&lt;class ‘list’&gt;</p>
<p>No ,elements within the list do not have text as an attribute</p>

AlphaBetaGamma96: <p>So, if you were trying to do inference with your model. i.e. output = m1(input). What exactly would your <code>input</code> variable be? If it’s just <code>batch</code> try removing the <code>.text</code> from <code>batch.text</code> and see if that fixes your issue.</p>

hs99: <p>When I print batch , I get:<br>
batch</p>
<p>[tensor([[[-0.0579,  0.0439,  0.0658,  …, -0.0565,  0.0413, -0.0023]],</p>
<pre><code>     [[ 0.9421,  0.8119,  0.6808,  ...,  0.0039,  0.0252,  0.0431]],

     [[ 0.7007,  0.5435,  0.2290,  ..., -0.1226,  0.0346, -0.0850]],

     ...,

     [[ 0.9834,  0.4084,  0.5638,  ...,  0.0261, -0.0626,  0.0173]],

     [[ 0.8450,  1.0068,  0.9290,  ..., -0.0277,  0.0607,  0.0565]],

     [[ 1.0653,  0.6558,  0.4438,  ...,  0.0670,  0.1114, -0.0131]]]),
</code></pre>
<p>tensor([2, 3, 1, 1, 2, 3, 2, 4, 3, 1, 0, 2, 3, 0, 1, 2, 4, 2, 1, 2, 4, 1, 2, 0, 3, 1, 4, 2, 1, 4, 2, 3])]</p>
<p>Also on removing .text part:</p>
<p>from torchviz import make_dot</p>
<p>make_dot(m1(batch), params=dict(list(m1.named_parameters()))).render(“cnn_torchviz”, format=“png”)</p>
<h2>I GET ERROR:</h2>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
1 from torchviz import make_dot<br>
2<br>
----&gt; 3 make_dot(m1(batch), params=dict(list(m1.named_parameters()))).render(“cnn_torchviz”, format=“png”)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, input)<br>
36         <span class="hashtag">#input</span> = input.unsqueeze(1)<br>
37         <span class="hashtag">#input</span> = input.unsqueeze(0)<br>
—&gt; 38         x = self.conv1(input)<br>
39         x = self.conv2(x)<br>
40         x = self.conv3(x)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, input)<br>
52         <span class="hashtag">#print</span>(“INPUT SHAPE SKIP”)<br>
53         <span class="hashtag">#print</span>(input.shape)<br>
—&gt; 54         conv1 = self.conv_1(input)<br>
55         x = self.normalization_1(conv1)<br>
56         x = self.swish_1(x)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
257                             _single(0), self.dilation, self.groups)<br>
258         return F.conv1d(input, self.weight, self.bias, self.stride,<br>
 → 259                         self.padding, self.dilation, self.groups)<br>
260<br>
261</p>
<p>TypeError: conv1d(): argument ‘input’ (position 1) must be Tensor, not list</p>

AlphaBetaGamma96: <p>Ok, it seems that <code>batch</code> is a list of 2 tensors. I assume the first tensor is your input and the 2nd tensor is your target/label?</p>
<p>Try passing <code>batch[0]</code> as your input! That might work! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Like this</p>
<p>make_dot(m1(batch[0]), params=dict(list(m1.named_parameters()))).render(“cnn_torchviz”, format=“png”)</p>

hs99: <p>I get this error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/a/7aaf8f2877e1ec929d5517d34e5bc0aa703404ee.png" data-download-href="https://discuss.pytorch.org/uploads/default/7aaf8f2877e1ec929d5517d34e5bc0aa703404ee" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/a/7aaf8f2877e1ec929d5517d34e5bc0aa703404ee.png" alt="image" data-base62-sha1="hvknSYFusu8RC5vSN5sTLymQ10G" width="690" height="125" data-dominant-color="292E33"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">890×162 16.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>However when i remove the render portion,it works fine!!Could u plz help how to do the render operation to save this large image <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>

AlphaBetaGamma96: <p>perhaps just try <code>.render("cnn_torchviz.png")</code>? <code>format</code> might be an argument from an older version of <code>make_dot</code>?</p>

hs99: <p>Thank you so much <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a><br>
Amazing guidance and support thoroughly!!<br>
I have another query REGARDING MODEL SAVING AND LOADING IN pytorch and I hope you can also help me with that also…Shall I ask here itsef or raise a new topic?</p>

AlphaBetaGamma96: <p>Not a problem <a class="mention" href="/u/hs99">@hs99</a>! I’d suggest reading the tutorial first <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" class="inline-onebox" rel="noopener nofollow ugc">Saving and Loading Models — PyTorch Tutorials 1.8.1+cu102 documentation</a> and if there are still problems, raise a new topic!</p>