Evaluation Model After weight errors

takis17: <p>Hello everyone,<br>
Hope you are doing well.</p>
<p>So I have modified the weights of a given pre-trained model.<br>
Passing though my evaluation dataset, I cannot detect any changed from before I have change the weights and after with the error induced, even when I changed the weights to extreme cases causing some weights to appear as NaN.</p>
<p>I have stored the corrupted weights back to a specific layer in the model as such</p>
<pre><code class="lang-auto"># set manipulated weight to conv layer
with torch.no_grad():
    conv.weight.copy_(weight_float32)
print(conv.weight)
</code></pre>
<p>My question is that:</p>
<ol>
<li>Do I need to re-train the model with the corrupted errors and pass the evaluation dataset<br>
Since the model’s weight are corrupted  - I double-check the parameters before and after</li>
</ol>

ptrblck: <p>I’m not sure I understand the issue completely, so please correct me if I miss something.<br>
Based on your description you are manually manipulating the weights (to e.g. invalid values) which is not reflected in the output and thus you think the weights were never updated or are never used.<br>
I don’t know if these parameters are ever used, but your code should work as seen here:</p>
<pre><code class="lang-python">conv = nn.Conv2d(1, 3, 3)
x = torch.randn(1, 1, 24, 24)
out = conv(x)
print(out.abs().sum())
#tensor(664.0641, grad_fn=&lt;SumBackward0&gt;)

with torch.no_grad():
    conv.weight.copy_(float("nan"))
print(conv.weight)
# Parameter containing:
# tensor([[[[nan, nan, nan],
#           [nan, nan, nan],
#           [nan, nan, nan]]],


#         [[[nan, nan, nan],
#           [nan, nan, nan],
#           [nan, nan, nan]]],


#         [[[nan, nan, nan],
#           [nan, nan, nan],
#           [nan, nan, nan]]]], requires_grad=True)

out = conv(x)
print(out.abs().sum())
# tensor(nan, grad_fn=&lt;SumBackward0&gt;)
</code></pre>

takis17: <p><a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>Thank you, really appreciate your help.<br>
Correct, that is what I am doing, except for the only difference that not all the values are nan - I randomly shuffle values and change them to one.<br>
My code follows the same logic as the one you provided, but I wanted to verify that I am actually correctly copying the weights back to my model - since at the end a tensor is immutable, that is why I converted it to a numpy array.<br>
If that is true ; then<br>
Does it make sense re-training the model and then pass the evaluation dataset (I imagine by doing that the weights will adapt but will still be different from the “original” for example if I train it for 1 epoch) hence might detect some changes<br>
Or just pass it through after model.eval()</p>

ptrblck: <p>I think it depends on your actual use case if a re-training is desired or not.<br>
The main issue I saw from your original post was that it seemed as if the weight changes were not used at all, which should not be the case as seen in my code snippet. Also setting a single value would already reflect the changes without any re-training:</p>
<pre><code class="lang-python">conv = nn.Conv2d(1, 3, 3)
x = torch.randn(1, 1, 24, 24)
out = conv(x)
print(out.abs().sum())
#tensor(664.0641, grad_fn=&lt;SumBackward0&gt;)

with torch.no_grad():
    conv.weight[0, 0, 0, 0].copy_(float("nan"))
print(conv.weight)
# Parameter containing:
# tensor([[[[    nan,  0.0670,  0.3143],
#           [-0.3256, -0.1418, -0.2889],
#           [ 0.2058, -0.0386, -0.0661]]],


#         [[[ 0.1033, -0.0968, -0.1645],
#           [-0.2478,  0.0022, -0.1859],
#           [ 0.1836, -0.2671,  0.1425]]],


#         [[[-0.1508, -0.3039, -0.3118],
#           [ 0.2929, -0.2692, -0.1859],
#           [ 0.1304, -0.0377,  0.0886]]]], requires_grad=True)

out = conv(x)
print(out.abs().sum())
# tensor(nan, grad_fn=&lt;SumBackward0&gt;)
</code></pre>
<p>I don’t know where the new values come from, but assuming you are trying to use other pre-trained weights a re-training might not be necessary.</p>

takis17: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
Thank you for your reply.<br>
Just to clarify, I am using a pre-trained model weights<br>
Here is some related pieces of my code.</p>
<pre><code class="lang-auto"># Copying weights of model to conv_1
conv_1 = test_model.conv.weight
print(conv_1)
print(conv_1.size()) # SIZE
print(conv_1.dtype) # TYPE
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">Parameter containing:
tensor([[ 0.0013, -0.0214, -0.0093,  ...,  0.0155,  0.0124, -0.0157],
        [-0.0005,  0.0098,  0.0049,  ...,  0.0025, -0.0032, -0.0067],
        [ 0.0001, -0.0011,  0.0112,  ..., -0.0214, -0.0035, -0.0176],
        ...,
        [ 0.0195, -0.0015, -0.0102,  ...,  0.0002, -0.0010, -0.0098],
        [-0.0070,  0.0201,  0.0172,  ..., -0.0128, -0.0091, -0.0014],
        [-0.0203, -0.0179, -0.0067,  ..., -0.0168, -0.0150,  0.0099]],
       device='cuda:0', requires_grad=True)
torch.Size([100, 2048])
torch.float32
</code></pre>
<pre><code class="lang-auto">print(conv_1.abs().sum())
# tensor(2260.9946, device='cuda:0', grad_fn=&lt;SumBackward0&gt;)
</code></pre>
<p>Then I manipulated some values</p>
<pre><code class="lang-auto"># tensor(nan, grad_fn=&lt;SumBackward0&gt;)
</code></pre>
<p>So I believe my I do follow the same logic as your code snippet.<br>
So retraining the model is not necessary, what other things can I consider?</p>

ptrblck: <aside class="quote no-group" data-username="takis17" data-post="5" data-topic="174842">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/ba9def/48.png" class="avatar"> takis17:</div>
<blockquote>
<p>So I believe my I do follow the same logic as your code snippet.</p>
</blockquote>
</aside>
<p>Just to clarify: are you setting (some) values in the conv weights to <code>NaN</code> and the output is not showing any invalid values?</p>

takis17: <p>No, it does show NaN values.<br>
I now think I have found my mistake.<br>
For some reason, wasn’t comparing the same things - my mistake.</p>