Running_mean should contain 27 elements not 32

PerfectionistAF: <p><strong>This model fully works and saves the model, but when I try to load it then get predictions via:</strong><br>
loaded_model_2 = torch.load(‘./models/convnet-pso_multi_arch.pth’)<br>
#<span class="hashtag-raw">#ecg_tensor</span> shape is: torch.Size([3, 53])<br>
ecg_pred = loaded_model_2(ecg_tensor)</p>
<p><strong>I get the error: RuntimeError: running_mean should contain 27 elements not 32</strong></p>
<p><strong>The model:</strong><br>
<a href="https://github.com/PerfectionistAF/ECG_Processing_Classification/blob/main/jupyter%20notebooks/4_Architecture/convnetquake-pso.ipynb" rel="noopener nofollow ugc">ECG_Processing_Classification/jupyter notebooks/4_Architecture/convnetquake-pso.ipynb at main · PerfectionistAF/ECG_Processing_Classification (github.com)</a></p>

PerfectionistAF: <p><strong>Also this ai-generated code gets a similar error: RuntimeError: running_mean should contain 49 elements not 51</strong></p>
<p>import torch<br>
import torch.nn as nn</p>
<h1><a name="define-your-model-architecture-1" class="anchor" href="#define-your-model-architecture-1"></a>Define your model architecture</h1>
<p>class YourModel(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(YourModel, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv1d(in_channels=3, out_channels=16, kernel_size=3)<br>
self.conv2 = nn.Conv1d(in_channels=16, out_channels=32, kernel_size=3)<br>
self.batch_norm1 = nn.BatchNorm1d(num_features=16)  # Adjust num_features as needed<br>
self.batch_norm2 = nn.BatchNorm1d(num_features=32)  # Adjust num_features as needed<br>
self.fc1 = nn.Linear(32 * 6, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 2)</p>
<pre><code>def forward(self, x):
    x = self.conv1(x)
    x = torch.relu(x)
    x = self.batch_norm1(x)
    x = self.conv2(x)
    x = torch.relu(x)
    x = self.batch_norm2(x)
    x = torch.max_pool1d(x, 2)
    x = torch.flatten(x, 1)
    x = self.fc1(x)
    x = torch.relu(x)
    x = self.fc2(x)
    x = torch.relu(x)
    x = self.fc3(x)
    return x
</code></pre>
<p>model = YourModel()<br>
model = nn.DataParallel(model, device_ids=[0])</p>
<p>saved_model = torch.save(model,‘./models/convnet2-pso_multi_arch.pth’)<br>
loaded_model = torch.load(‘./models/convnet2-pso_multi_arch.pth’)</p>
<h1><a name="adjust-running_mean-running_var-weight-and-bias-of-batchnorm-layers-2" class="anchor" href="#adjust-running_mean-running_var-weight-and-bias-of-batchnorm-layers-2"></a>Adjust running_mean, running_var, weight, and bias of BatchNorm layers</h1>
<p>for name, module in loaded_model.named_modules():<br>
if isinstance(module, nn.BatchNorm1d):<br>
# Set running_mean, running_var, weight, and bias to have 51 elements<br>
current_num_elements = module.running_mean.size(0)<br>
if current_num_elements != 51:<br>
new_running_mean = torch.full((51,), 0.0, dtype=module.running_mean.dtype, device=module.running_mean.device)<br>
new_running_mean[:current_num_elements] = module.running_mean<br>
module.running_mean = nn.Parameter(new_running_mean)</p>
<pre><code>        new_running_var = torch.full((51,), 1.0, dtype=module.running_var.dtype, device=module.running_var.device)
        new_running_var[:current_num_elements] = module.running_var
        module.running_var = nn.Parameter(new_running_var)
        
        new_weight = torch.full((51,), 1.0, dtype=module.weight.dtype, device=module.weight.device)
        new_weight[:current_num_elements] = module.weight
        module.weight = nn.Parameter(new_weight)
        
        new_bias = torch.full((51,), 0.0, dtype=module.bias.dtype, device=module.bias.device)
        new_bias[:current_num_elements] = module.bias
        module.bias = nn.Parameter(new_bias)
        
        module.num_features = 51  # Adjust num_features as needed
        module.running_mean.requires_grad = False  # Freeze running_mean
        module.running_var.requires_grad = False  # Freeze running_var
        module.weight.requires_grad = False  # Freeze weight
        module.bias.requires_grad = False  # Freeze bias
</code></pre>
<h1><a name="make-predictions-3" class="anchor" href="#make-predictions-3"></a>Make predictions</h1>
<p>ecg_tensor = torch.randn(3, 53)  # Example input tensor<br>
ecg_pred = loaded_model(ecg_tensor)</p>

ptrblck: <aside class="quote no-group" data-username="PerfectionistAF" data-post="2" data-topic="203466">
<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/perfectionistaf/48/70292_2.png" class="avatar"> PerfectionistAF:</div>
<blockquote>
<p>Also this ai-generated code gets a similar error: RuntimeError: running_mean should contain 49 elements not 51</p>
</blockquote>
</aside>
<p>Could you explain why you are posting AI-generated code which is broken? Is it representing your use case? If not, post a minimal and executable code snippet representing your use case which reproduces the error.</p>