I have a very basic machine learning application I’m trying to make, basically I want the model to try and predict each month of the next year based on the dummy data I’ve provided but I’m getting huge loss Loss: 5206342.5000
and the predicated values are so off from what I would expect Predicted values for each month of next year: [-0.043424129486083984, 0.041442010551691055, -0.16847632825374603, -0.18227830529212952, 0.4619046151638031, 1.128817081451416, 0.05658513307571411, 0.1113058477640152, 0.2900705337524414, 0.21394489705562592, 0.2618725001811981, 0.3829265236854553]
I’m very new to machine learning, why am I seeing such massive loss and such wrong values predicted?
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
import numpy as np
# Example historical data (replace with your actual data loading)
data = {
'ProjectId': ['666d60f2604698f1383d94e4', '666d60e2604698f1383d9495', '666d5fb1604698f1383d8e8f'],
'Year': ['2023/2024', '2023/2024', '2023/2024'],
'July': [0, 0, 0],
'August': [0, 0, 0],
'September': [0, 0, 0],
'October': [0, 0, 0],
'November': [0, 0, 0],
'December': [10000, 5000, 100000],
'January': [0, 0, 120000],
'February': [0, 0, 0],
'March': [0, 0, 0],
'April': [0, 0, 0],
'May': [0, 0, 0],
'June': [0, 0, 0]
}
df = pd.DataFrame(data)
# Example: Extract X (input features) and y (target values)
X = df.drop(['ProjectId', 'Year'], axis=1).values.astype(np.float32) # Convert to float32
y = df.drop(['ProjectId', 'Year'], axis=1).values.astype(np.float32) # Assuming y includes next year's forecast
# Example: Splitting into training and validation sets
# Assume last row for validation, rest for training
X_train = torch.tensor(X[:-1], dtype=torch.float32).unsqueeze(0) # Add batch dimension (1 sample)
y_train = torch.tensor(y[:-1], dtype=torch.float32).unsqueeze(0) # Add batch dimension (1 sample)
X_val = torch.tensor(X[-1:], dtype=torch.float32).unsqueeze(0) # Example single project for validation
# Define LSTM model for forecasting
class LSTMForecast(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LSTMForecast, self).__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
lstm_out, _ = self.lstm(x)
out = self.fc(lstm_out[:, -1, :]) # Predict based on the last LSTM output
return out
# Initialize the model, loss function, and optimizer
input_size = X_train.shape[-1]
hidden_size = 64
output_size = y_train.shape[-1]
model = LSTMForecast(input_size, hidden_size, output_size)
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
num_epochs = 100
for epoch in range(num_epochs):
optimizer.zero_grad()
outputs = model(X_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
if (epoch+1) % 10 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
# Example: Making predictions for next year's forecast
model.eval()
with torch.no_grad():
forecast = model(X_val)
forecast_values = forecast.numpy().tolist()[0] # Convert to numpy and list
print(f'Predicted values for each month of next year: {forecast_values}')