這是我的data shape:
然後我將我的dataset轉變為以下這樣:
from torch_geometric_temporal.signal import DynamicGraphTemporalSignal
dataset = DynamicGraphTemporalSignal(
edge_indices, edge_features, xs, ys, y_indices=y_indices
)
我的模型設置:
import torch.nn.functional as F
from torch_geometric_temporal.nn.recurrent import A3TGCN
class TemporalGNN(torch.nn.Module):
def __init__(self, node_features, periods):
super(TemporalGNN, self).__init__()
# Attention Temporal Graph Convolutional Cell
self.tgnn = A3TGCN(in_channels=node_features,
out_channels=32,
periods=periods)
# Equals single-shot prediction
self.linear = torch.nn.Linear(32, periods)
def forward(self, x, edge_index):
"""
x = Node features for T time steps
edge_index = Graph edge indices
"""
h = self.tgnn(x, edge_index)
h = F.relu(h)
h = self.linear(h)
return h
TemporalGNN(node_features=1, periods=1)
我的訓練程式碼:
device = torch.device('cpu') # cuda
subset = 2000
# Create model and optimizers
model = TemporalGNN(node_features=1, periods=12).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
model.train()
print("Running training...")
for epoch in range(200):
loss = 0
step = 0
for snapshot in train_dataset:
snapshot = snapshot.to(device)
# Get model predictions
y_hat = model(snapshot.x, snapshot.edge_index)
# Mean squared error
loss = loss + torch.mean((y_hat-snapshot.y)**2)
step += 1
if step > subset:
break
loss = loss / (step + 1)
loss.backward()
optimizer.step()
optimizer.zero_grad()
print("Epoch {} train MSE: {:.4f}".format(epoch, loss.item()))
但當我開始訓練我的模型時,卻出現以下錯誤: