Also this ai-generated code gets a similar error: RuntimeError: running_mean should contain 49 elements not 51
import torch
import torch.nn as nn
Define your model architecture
class YourModel(nn.Module):
def init(self):
super(YourModel, self).init()
self.conv1 = nn.Conv1d(in_channels=3, out_channels=16, kernel_size=3)
self.conv2 = nn.Conv1d(in_channels=16, out_channels=32, kernel_size=3)
self.batch_norm1 = nn.BatchNorm1d(num_features=16) # Adjust num_features as needed
self.batch_norm2 = nn.BatchNorm1d(num_features=32) # Adjust num_features as needed
self.fc1 = nn.Linear(32 * 6, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 2)
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
model = YourModel()
model = nn.DataParallel(model, device_ids=[0])
saved_model = torch.save(model,‘./models/convnet2-pso_multi_arch.pth’)
loaded_model = torch.load(‘./models/convnet2-pso_multi_arch.pth’)
Adjust running_mean, running_var, weight, and bias of BatchNorm layers
for name, module in loaded_model.named_modules():
if isinstance(module, nn.BatchNorm1d):
# Set running_mean, running_var, weight, and bias to have 51 elements
current_num_elements = module.running_mean.size(0)
if current_num_elements != 51:
new_running_mean = torch.full((51,), 0.0, dtype=module.running_mean.dtype, device=module.running_mean.device)
new_running_mean[:current_num_elements] = module.running_mean
module.running_mean = nn.Parameter(new_running_mean)
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
Make predictions
ecg_tensor = torch.randn(3, 53) # Example input tensor
ecg_pred = loaded_model(ecg_tensor)