# Understanding deep network: visualize weights

**URL:** <https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060>\
**Category:** Uncategorized\
**Created:** [April 19, 2017, 8:54pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060 "2017-04-19T20:54:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [April 19, 2017, 8:55pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/1 "2017-04-19T20:55:00Z")

</div>

Are there any exiting implementations of understanding what it is learning in the networks. For example, weight visualization, “deconvolution” for visualization, gradient ascent, etc?

Thanks!

---

<div class="post-metadata">

**Author:** ![bodokaiser](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bodokaiser/32/218_2.png) [@bodokaiser](https://discuss.pytorch.org/u/bodokaiser)\
**Post date:** [April 20, 2017, 8:49am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/2 "2017-04-20T08:49:30Z")

</div>

I dont think there exists a reliable all-in-one solution. For me I found [visdom](https://github.com/facebookresearch/visdom) to be a good building block for visualization.

You can access model weights via:

```
for m in model.modules():
    if isinstance(m, nn.Conv2d):
        print(m.weights.data)

```

However you still need to convert `m.weights.data` to numpy and maybe even do some type casting so that you can pass it to `vis.image`.

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [April 21, 2017, 6:16pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/3 "2017-04-21T18:16:22Z")

</div>

Great, I have heard about visdom too.

I guess it will be a good further pull request of pytorch. 🙂 @Soumith_Chintala

---

<div class="post-metadata">

**Author:** ![lakehanne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lakehanne](https://discuss.pytorch.org/u/lakehanne)\
**Post date:** [May 3, 2017, 6:38am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/4 "2017-05-03T06:38:40Z")

</div>

Did you implement this already in visdom?

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [May 3, 2017, 5:02pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/5 "2017-05-03T17:02:31Z")

</div>

Not yet, planning too. Did you have any implementation?

---

<div class="post-metadata">

**Author:** ![lakehanne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lakehanne](https://discuss.pytorch.org/u/lakehanne)\
**Post date:** [May 3, 2017, 5:24pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/6 "2017-05-03T17:24:30Z")

</div>

@bodokaiser, are you sure the `model.modules()``nn.Conv2d` instance has weights attribute?  
Cause I tried to access the weights using your example and it returns

```auto
  self.weights = m.weights#.data
  File "/home/user/anaconda2/envs/py27/lib/python2.7/site-packages/torch/nn/modules/module.py", line 237, in __getattr__
    return object. __getattr__ (self, name)
AttributeError: type object 'object' has no attribute ' __getattr__'

```

---

<div class="post-metadata">

**Author:** ![lakehanne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lakehanne](https://discuss.pytorch.org/u/lakehanne)\
**Post date:** [May 3, 2017, 5:43pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/7 "2017-05-03T17:43:15Z")

</div>

The weights can be found via `model.state_dict()` and the values for layer weights can be extracted from the dictionary using `model.state_dict()['name of key']`

---

<div class="post-metadata">

**Author:** ![Fchaubard](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@Fchaubard](https://discuss.pytorch.org/u/Fchaubard)\
**Post date:** [May 3, 2017, 11:09pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/8 "2017-05-03T23:09:11Z")

</div>

Not using visdom, but something I hacked together for this purpose (with an example for VGG):

```
import torch
import torchvision.models as models
from matplotlib import pyplot as plt

def plot_kernels(tensor, num_cols=6):
    if not tensor.ndim==4:
        raise Exception("assumes a 4D tensor")
    if not tensor.shape[-1]==3:
        raise Exception("last dim needs to be 3 to plot")
    num_kernels = tensor.shape[0]
    num_rows = 1+ num_kernels // num_cols
    fig = plt.figure(figsize=(num_cols,num_rows))
    for i in range(tensor.shape[0]):
        ax1 = fig.add_subplot(num_rows,num_cols,i+1)
        ax1.imshow(tensor[i])
        ax1.axis('off')
        ax1.set_xticklabels([])
        ax1.set_yticklabels([])

    plt.subplots_adjust(wspace=0.1, hspace=0.1)
    plt.show()
    
  
vgg = models.vgg16(pretrained=True)
mm = vgg.double()
filters = mm.modules
body_model = [i for i in mm.children()][0]
layer1 = body_model[0]
tensor = layer1.weight.data.numpy()
plot_kernels(tensor)
```

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [May 3, 2017, 11:42pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/9 "2017-05-03T23:42:03Z")

</div>

Do you have the output of it?

---

<div class="post-metadata">

**Author:** ![Fchaubard](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@Fchaubard](https://discuss.pytorch.org/u/Fchaubard)\
**Post date:** [May 4, 2017, 5:27am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/10 "2017-05-04T05:27:58Z")

</div>

Here ya go!

 ![](https://discuss.pytorch.org/uploads/default/original/1X/34dbfb7174515502893b6963e21c20e08514eae8.png)

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [May 4, 2017, 5:32pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/11 "2017-05-04T17:32:15Z")

</div>

@Fchaubard Interesting, so they are 3x3 filters?

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 5, 2017, 2:11am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/12 "2017-05-05T02:11:23Z")

</div>

yes vgg has 3x3 filters all over.

---

<div class="post-metadata">

**Author:** ![zym1010](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zym1010/32/3443_2.png) [@zym1010](https://discuss.pytorch.org/u/zym1010)\
**Post date:** [May 25, 2017, 6:24pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/13 "2017-05-25T18:24:17Z")

</div>

How can I implement visualization techniques like deconvolution, guided backprop?

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [May 25, 2017, 6:59pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/14 "2017-05-25T18:59:57Z")

</div>

I am working on it. May release some gist snippets in sometime. Would this be useful?

---

<div class="post-metadata">

**Author:** ![zym1010](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zym1010/32/3443_2.png) [@zym1010](https://discuss.pytorch.org/u/zym1010)\
**Post date:** [May 25, 2017, 7:02pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/15 "2017-05-25T19:02:18Z")

</div>

Yes. How are you going to do it? I’m planning to implement it using backward hooks. However, seems that, backward hook only takes the gradient as input, and for many visualization techniques, the original input and output are also needed.

One thing I can think of, is using both forward and backward hooks, and keeping all the input/output in some external dictionary.

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [May 30, 2017, 7:55pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/16 "2017-05-30T19:55:08Z")

</div>

@zym1010 Sorry for late reply. Oh, for salency map and class model optimization. It should be pretty easy. Guided backprop I have not invested into it yet. Backprop is just taking the gradient out, so I guess it’s fine.

I am going to create a repo (probably) to have some examples on saliency map and other. You want to work on it together?

---

<div class="post-metadata">

**Author:** ![zym1010](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zym1010/32/3443_2.png) [@zym1010](https://discuss.pytorch.org/u/zym1010)\
**Post date:** [May 30, 2017, 8:10pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/17 "2017-05-30T20:10:50Z")

</div>

Yeah. sure. Let’s work on this together.

My goal is to have something in PyTorch that is similar to [https://github.com/yosinski/deep-visualization-toolbox](https://github.com/yosinski/deep-visualization-toolbox), yet more flexible and powerful. The visualization code for custom networks, in the deep visualization toolbox is painful.

One reference I can think of, on saliency map stuffs, is [https://github.com/Lasagne/Recipes/blob/master/examples/Saliency%20Maps%20and%20Guided%20Backpropagation.ipynb](https://github.com/Lasagne/Recipes/blob/master/examples/Saliency%20Maps%20and%20Guided%20Backpropagation.ipynb)

---

<div class="post-metadata">

**Author:** ![johnny5550822](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johnny5550822/32/497_2.png) [@johnny5550822](https://discuss.pytorch.org/u/johnny5550822)\
**Post date:** [June 1, 2017, 7:29pm UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/18 "2017-06-01T19:29:57Z")

</div>

@zym1010 Cool. I will open up a github repo in a new couple of weeks (probably). My goal at the beginning is to first show examples of how to implement several visualization algorithm such as saliency map, and eventually may develop a tool like the deep-visualization-toolbox (if there are enough time).

---

<div class="post-metadata">

**Author:** ![zym1010](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zym1010/32/3443_2.png) [@zym1010](https://discuss.pytorch.org/u/zym1010)\
**Post date:** [June 14, 2017, 4:00am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/19 "2017-06-14T04:00:25Z")

</div>

I’ve finished a rough re-implementation of that Lasagne example I’m talking about.

Check [https://github.com/leelabcnbc/cnnvis-pytorch/blob/master/test.ipynb](https://github.com/leelabcnbc/cnnvis-pytorch/blob/master/test.ipynb)

I’ve double checked it against the Lasagne example, and they are exactly the same, except for some bug on the Lasagne/Theano side on my system, for deconv.

---

<div class="post-metadata">

**Author:** ![herleeyandi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/herleeyandi/32/948_2.png) [@herleeyandi](https://discuss.pytorch.org/u/herleeyandi)\
**Post date:** [August 29, 2017, 8:49am UTC](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060/20 "2017-08-29T08:49:26Z")

</div>

@johnny5550822 @zym1010 How to visualize of the image as the result of every convolution?, are you able to make like [this](https://github.com/yosinski/deep-visualization-toolbox) . Or can we copy the Pytorch weight to caffe and produce the result?, do you have any suggestion?  
-Thank you-

[Next page](https://discuss.pytorch.org/t/understanding-deep-network-visualize-weights/2060.md?page=2)
