This is my SVR model that I’m using. I need to add a GWO optimiser to it but am a noob. What code do I include?
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
from scipy import stats
# Define SVR model using PyTorch
class SVRModel(nn.Module):
def __init__(self):
super(SVRModel, self).__init__()
self.fc1 = nn.Linear(in_features=7, out_features=64) # Adjust input and output features if needed
self.fc2 = nn.Linear(in_features=64, out_features=32)
self.fc3 = nn.Linear(in_features=32, out_features=1) # Output layer
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# Convert data to PyTorch tensors
X_train_tensor = torch.tensor(scaled_X_train, dtype=torch.float32)
Y_train_tensor = torch.tensor(Y_train.reshape(-1, 1), dtype=torch.float32)
X_test_tensor = torch.tensor(scaled_X_test, dtype=torch.float32)
# Define hyperparameters
C = 1
epsilon = 0.1
lr = 0.01
epochs = 1000
# Instantiate SVR model
model = SVRModel()
# Define loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=lr)
# Define learning rate scheduler
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5, verbose=True)
# Training loop
for epoch in range(epochs):
model.train()
optimizer.zero_grad()
outputs = model(X_train_tensor)
loss = criterion(outputs, Y_train_tensor)
loss.backward()
optimizer.step()
# Step scheduler
scheduler.step(loss)
print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')
# Evaluation
model.eval()
with torch.no_grad():
Y_pred_tensor = model(X_test_tensor)
Y_pred = Y_pred_tensor.numpy().flatten()
# Calculate correlation
corr = stats.pearsonr(Y_test, Y_pred)[0]
print(f'Correlation: {corr}')
# Create result DataFrame
result_df = pd.DataFrame({'svr_predicted': Y_pred, 'true': Y_test})
result_df['svr_error'] = abs(result_df['true'] - result_df['svr_predicted'])
I just need to improve the accuracy of my model by a little and want to try a GWO to impress my instructor. Again, I am a noobie at this. I tried to watch some YT videos to do it myself and nothing is working. If someone can just write the code for me, you’d be saving my life!
New contributor
Ibrahim AW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.