Zeroing out some weights doesn't seem to affect output tensor

username: BaruchG
cooked: <p>I’m bumping into some strange behavior and was hoping someone could explain it to me or show me where I’m making a mistake.<br>
I’m trying to figure out how certain types of changes to the weights of a pretrained network will affect the output tensor compared to an “untampered” network.<br>
With that in mind I have two identical networks:</p>
<pre><code class="lang-auto">base_model = models.vgg16(pretrained=True)
modified_model = models.vgg16(pretrained=True)
</code></pre>
<p>and then I modify a given layer.  <code>config['layer']</code> is equal to one of the values returned from <code>layers</code> below.</p>
<pre><code class="lang-auto">layers = list(models.vgg16().state_dict().keys())
layers = [layer for layer in layers if layer.split('.')[-1] != 'bias']
</code></pre>
<pre><code class="lang-auto">sh = modified_model.state_dict()[config['layer']].shape 

for i,b in range(798):
    dim = i // sh[0]
    modified_model.state_dict()[config['layer']][i - dim*sh[0]][dim] = 0.0
</code></pre>
<p>I then take an image and transform,batch etc. and feed it through the two networks.</p>
<pre><code class="lang-auto">    transform = transforms.Compose([            
        transforms.Resize(256),                    
        transforms.CenterCrop(224),                
        transforms.ToTensor(),                     
        transforms.Normalize(                      
        mean=[0.485, 0.456, 0.406],                
        std=[0.229, 0.224, 0.225]                  
        )])
    url = """
    data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUSEhIVFhUXFxUVFRUVFxUVFRcVFRUXFxUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OFxAQGi0lHR0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLS0tLS0tLS0tLS0rLS0tLS0tLf/AABEIALMBGgMBIgACEQEDEQH/xAAcAAACAwEBAQEAAAAAAAAAAAACAwABBAUGBwj/xABBEAACAQIDBQQGBwcCBwAAAAAAAQIDEQQSIQUxQVFhBhNxgSIykaHR8AcUQlKSscEVYnKCsuHxM3MjJIOTosLS/8QAGgEAAwEBAQEAAAAAAAAAAAAAAAECAwQGBf/EACYRAAICAQQDAAEFAQAAAAAAAAABAhESAyExQ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aJn6oQGBTRMyM0ZdRkUdAi6m4Wq1hrRmryBA9jbSxKHd4cGNVtm+nNrwKaJTbNTl7QFLUVOsiQrJiKNakhNT552/QKlLmipAFGeD+WMhX1+AjEMmFp3GT/CNua/ERVXNhxpSWl7kdNiGJTt/cNz5L8x0KHtHQiFjoxOg3q9xtoU1bhbyYxUwcjTJbGkZ6+BTe4Qtl/NjqNjaevAMmDijn0dnpcL+41QodEaXEliWx0DGgDOnb46e5Ds3hcCUriChMp8woCK6d9F5lQkIRr7pFZFyuDTxHBjs42hi72CkuoEnfgAtCQKcOi/MzzptP4mq4qavyEKgO8tuY+FZPeYKisUqjADoS8wMq6e0RSr8Hf58RuZBQzhUZM1pkIdDIiS5mq72QgwYEY6m+nuIQGEeQ3BPeg4UkloiEEUSX6kmiEAo59V6mzCxIQb4M1yakNjFEISzQtIKG8hBAMfxFTLIIQukw6b1j1bT/DL4IsgAPiy7EIIBd9RtPcQghoXUir+wxV95ZAYgUzRTm+ZCDAOcnouvxFzehCEsAISve/MIhBMBNVaHPlJ5mQghBp6XJ3j5kIWgP//Z

    """
    img = Image.fromarray(url_to_image(url))
    img_t = transform(img)
    
    batch_t = torch.unsqueeze(img_t, 0)
    
    base_model.eval();
    modified_model.eval();
    
    res_orig = base_model(img_t[None, ...])
    res_new = modified_model(img_t[None, ...])
    
    res_orig = res_orig.detach()
    res_new = res_new.detach()
</code></pre>
<p>and then print out L-norms and the <code>equals</code> value.</p>
<pre><code class="lang-auto">metrics = {"L1-Norm":np.linalg.norm(res_orig-res_new, ord=1), "L2-Norm":np.linalg.norm(res_orig-res_new, ord=2), "LInf-Norm":np.linalg.norm(res_orig-res_new, ord=np.inf)}
print(metrics)
print(torch.equal(res_orig,res_new))
</code></pre>
<p>I discoved that if <code>config['layer']</code> is <code>'classifier.6.weight'</code> or <code>'classifier.0.weight</code> then the output of <code>metrics</code> and <code>equals</code> at the end is</p>
<pre><code class="lang-auto">{'L1-Norm': 0.0, 'L2-Norm': 0.0, 'LInf-Norm': 0.0}
True
</code></pre>
<p>But if it is any other weight layer then the outputs are different, for example <code>classifier.3.weight</code> is</p>
<pre><code class="lang-auto">{'L1-Norm': 2.3841858e-07, 'L2-Norm': 4.7497085e-07, 'LInf-Norm': 1.3113022e-06}
False
</code></pre>
<p>and <code>features.26.weight</code> is</p>
<pre><code class="lang-auto">{'L1-Norm': 1.9073486e-06, 'L2-Norm': 7.4874556e-06, 'LInf-Norm': 0.00014960021}
False
</code></pre>
<p>and so on for every other layer besides the 0th and 6th classifier layers.</p>
<p>Can someone please explain why this is happening?</p>

username: BaruchG
cooked: <p>Is this possible that this is a image specific issue, that somehow certain images wouldn’t activate certain nodes of the fully connected layers?</p>

username: ptrblck
cooked: <p>You might get “unlucky”, e.g. if the input activation were zeroed out through the last <code>ReLU</code> in the <code>features</code> submodule.<br>
To verify it, you could e.g. also zero out the <code>bias</code> of the selected layer, which you would like to manipulate.</p>

username: BaruchG
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Thanks for the reply.  If that is the case that the input activations were zeroed out then we would not expect this behavior to hold on a dataset of images, correct?</p>

username: ptrblck
cooked: <p>Why would this not be the case for image inputs?</p>

username: BaruchG
cooked: <p>Sorry, I meant to emphasize a <em>dataset</em> of images as opposed to a single image, not that their is something special about images in general.</p>

username: ptrblck
cooked: <p>That seems plausible. You could verify it by checking the input activation (e.g. via forward hooks) and check the <code>weights</code> as well as the <code>bias</code> of the following layer, which you’ve manipulated.</p>