Work with tutorial on temporal graphs. When I execute the code in Google Colab everything works fine without any error, but when I run same notebook on laptops (Macbook Air 2020 and Macbook Pro 2019) I meet the exception – RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed).
The error appears in training part during call of loss.backward() in first iteration of both loops.
It seems to me quite strange because I use almost the same versions of pytorch 2.4.0 on laptop and 2.4.0+cu121 in Google Colab.
import torch
from torch_geometric_temporal.nn.recurrent import EvolveGCNH
from torch_geometric_temporal.dataset import WikiMathsDatasetLoader
from torch_geometric_temporal.signal import temporal_signal_split, StaticGraphTemporalSignal
class TemporalGNN(torch.nn.Module):
def __init__(self, node_count, dim_in):
super().__init__()
self.recurrent = EvolveGCNH(node_count, dim_in)
self.linear = torch.nn.Linear(dim_in, 1)
def forward(self, x, edge_index, edge_weight):
h = self.recurrent(x, edge_index, edge_weight).relu()
h = self.linear(h)
return h
model = TemporalGNN(dataset[0].x.shape[0], dataset[0].x.shape[1])
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
model.train()
for epoch in tqdm(range(50)):
for i, snapshot in enumerate(train_dataset):
y_pred = model(snapshot.x, snapshot.edge_index, snapshot.edge_attr)
loss = torch.mean((y_pred-snapshot.y)**2)
loss.backward()
optimizer.step()
optimizer.zero_grad()
Why it works so different? Is there any way to fix this error on my laptops?
I tried to use enviroments with different package versions of pytorch and relative libraries and also versions of python from 3.6 to 3.10. In all cases I have the same issue on Mac.
Tried to use loss.detach() as I saw in other advices for this error, but it didn’t work. Parameter retain_graph=True in backward method also didn’t help in my case.
2