-
Notifications
You must be signed in to change notification settings - Fork 1
/
GAN_product_demand
97 lines (79 loc) · 3.69 KB
/
GAN_product_demand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.data import Data, DataLoader
from torch_geometric.nn import GCNConv
# Sample data for the product graph (Replace this with your actual data)
# Each node represents a product, and the features include prices, number of product ordered,
# number of product manufactured, and product inventory.
product_data = {
'prices': [10.0, 12.0, 15.0],
'ordered': [100, 120, 130],
'manufactured': [1000, 1200, 1300],
'inventory': [200, 300, 400]
}
# Sample data for external features (Replace this with your actual data)
# These features include weather, holidays, events, and macroeconomic data.
external_data = {
'weather': [25.0, 28.0, 30.0],
'holidays': [0, 1, 0],
'events': [2, 1, 3],
'macroeconomic': [100, 110, 95]
}
# Convert the data to tensors
prices = torch.tensor(product_data['prices'], dtype=torch.float)
ordered = torch.tensor(product_data['ordered'], dtype=torch.float)
manufactured = torch.tensor(product_data['manufactured'], dtype=torch.float)
inventory = torch.tensor(product_data['inventory'], dtype=torch.float)
weather = torch.tensor(external_data['weather'], dtype=torch.float)
holidays = torch.tensor(external_data['holidays'], dtype=torch.float)
events = torch.tensor(external_data['events'], dtype=torch.float)
macroeconomic = torch.tensor(external_data['macroeconomic'], dtype=torch.float)
# Combine the features into a node feature matrix
node_features = torch.stack([prices, ordered, manufactured, inventory, weather, holidays, events, macroeconomic], dim=1)
# Sample graph structure (Replace this with your actual graph structure)
# Here, we assume a fully connected graph, where each node is connected to all other nodes.
# In practice, you may have a different graph structure based on the relationships between products.
num_nodes = node_features.size(0)
edge_index = torch.tensor([(i, j) for i in range(num_nodes) for j in range(num_nodes) if i != j], dtype=torch.long).t()
# Create a PyTorch Geometric Data object
data = Data(x=node_features, edge_index=edge_index)
# Define the GNN model
class GNNModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(GNNModel, self).__init__()
self.conv1 = GCNConv(input_dim, hidden_dim)
self.conv2 = GCNConv(hidden_dim, output_dim)
def forward(self, data):
x, edge_index = data.x, data.edge_index
# GNN layer 1
x = self.conv1(x, edge_index)
x = F.relu(x)
# GNN layer 2
x = self.conv2(x, edge_index)
return x
# Initialize the GNN model
input_dim = node_features.size(1)
hidden_dim = 64
output_dim = 1 # Demand forecasting output dimension
gnn_model = GNNModel(input_dim, hidden_dim, output_dim)
# Define the loss function and optimizer
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(gnn_model.parameters(), lr=0.01)
# Sample demand data (Replace this with your actual demand data)
demand_data = torch.tensor([500, 600, 700], dtype=torch.float)
# Training loop (Replace this with your actual data loading and training loop)
num_epochs = 1000
for epoch in range(num_epochs):
optimizer.zero_grad()
predictions = gnn_model(data)
loss = loss_function(predictions.view(-1), demand_data)
loss.backward()
optimizer.step()
if epoch % 100 == 0:
print(f"Epoch {epoch}/{num_epochs}, Loss: {loss.item()}")
# Once the model is trained, you can use it to predict demand for new products
new_product_features = torch.tensor([[16.0, 110, 1150, 250, 28.0, 1, 1, 115]], dtype=torch.float)
new_data = Data(x=new_product_features, edge_index=edge_index)
predicted_demand = gnn_model(new_data)
print("Predicted Demand:", predicted_demand.item())