diff options
-rw-r--r-- | power_predictor.py | 82 |
1 files changed, 62 insertions, 20 deletions
diff --git a/power_predictor.py b/power_predictor.py index ca38498..cc2a86d 100644 --- a/power_predictor.py +++ b/power_predictor.py @@ -82,27 +82,43 @@ def analyze_feature_importance(X, y): class PowerEstimator(nn.Module): def __init__(self, input_size): super(PowerEstimator, self).__init__() - self.model = nn.Sequential( - # nn.Linear(input_size, 128), - # nn.ReLU(), - # nn.Dropout(0.3), - # nn.Linear(128, 64), - # nn.ReLU(), - # nn.Dropout(0.2), - # nn.Linear(64, 32), - # nn.ReLU(), - # nn.Linear(32, 1) - nn.Linear(input_size, 64), - nn.ReLU(), - # // leaky relu - nn.Dropout(0.2), - nn.Linear(64, 32), - nn.ReLU(), - nn.Linear(32, 1) - ) + self.input_lin = nn.Linear(input_size, 16) + self.relu1 = nn.ReLU() + self.lin2 = nn.Linear(16, 4) + self.relu2 = nn.ReLU() + self.lin3 = nn.Linear(4, 1) def forward(self, x): - return self.model(x) + x = self.input_lin(x) + x = self.relu1(x) + x = self.lin2(x) + x = self.relu2(x) + x = self.lin3(x) + return x +# class PowerEstimator(nn.Module): +# def __init__(self, input_size): +# super(PowerEstimator, self).__init__() +# self.model = nn.Sequential( +# # nn.Linear(input_size, 128), +# # nn.ReLU(), +# # nn.Dropout(0.3), +# # nn.Linear(128, 64), +# # nn.ReLU(), +# # nn.Dropout(0.2), +# # nn.Linear(64, 32), +# # nn.ReLU(), +# # nn.Linear(32, 1) +# nn.Linear(input_size, 16), +# nn.ReLU(), +# # // leaky relu +# # nn.Dropout(0.2), +# nn.Linear(16, 4), +# nn.ReLU(), +# nn.Linear(4, 1) +# ) + +# def forward(self, x): +# return self.model(x) # Step 5: Train the model def train_model(X, y, batch_size=32, epochs=100, lr=0.001, early_stopping_patience=10): @@ -114,7 +130,9 @@ def train_model(X, y, batch_size=32, epochs=100, lr=0.001, early_stopping_patien # Scale the features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) + # X_train_scaled = X_train X_val_scaled = scaler.transform(X_val) + # X_val_scaled = X_val # Convert to PyTorch tensors X_train_tensor = torch.tensor(X_train_scaled, dtype=torch.float32) @@ -132,6 +150,7 @@ def train_model(X, y, batch_size=32, epochs=100, lr=0.001, early_stopping_patien model = PowerEstimator(X_train.shape[1]) criterion = nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=lr) + print(model.state_dict().keys()); # Training loop train_losses = [] @@ -280,10 +299,33 @@ def main(): # Train the model print("\nTraining model...") model, scaler = train_model(X_imputed, y, batch_size=8, epochs=100) - + # Evaluate the model print("\nEvaluating model...") evaluate_model(model, X_imputed, y, scaler) + + # Save model + first_layer_weight = model.input_lin.weight.data # Original weight matrix + first_layer_bias = model.input_lin.bias.data # Original bias vector + + # Apply scaling to weights and bias + for i in range(first_layer_weight.shape[1]): # For each input feature + scale_factor = scaler.scale_[i] # Get std for this feature + mean_value = scaler.mean_[i] # Get mean for this feature + + # Modify weights: w_new = w_old / scale + first_layer_weight[:, i] = first_layer_weight[:, i] / scale_factor + + # Add the mean adjustment to the bias: b_new = b_old - (w_new * mean) + first_layer_bias[:] = first_layer_bias[:] - (first_layer_weight[:, i] * mean_value).sum(dim=0) + + # Update the model parameters + model.input_lin.weight.data = first_layer_weight + model.input_lin.bias.data = first_layer_bias + + model_weights = model.state_dict() + torch.save(model_weights, "perf.pt") + print("\nDone!") |