Training with Graph Mini-Batches #6355
-
Hello, I would like to perform graph-classification where each graph is of the following format: As such, my whole dataset has the following format where I have n=108221 such graphs: Given the size of my dataset, the code which I used previously for smaller tasks no longer work on my single RTX3070 GPU, as I run out of memory. As such, I would like to train with PyTorch Mini-Batches, but wanted to ask the community for your opinions to see how I can achieve it using best practices. Before implementing Mini-Batches, my working code was the following (although in bigger datasets I do not have enough memory to run it): %%time
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GATConvModel(in_features=graph_batch.x.shape[1], hidden_features=8, num_classes=len(classes), pool_type="mean").to(device)
data = graph_batch.to(device)
train_percentage = 0.8
train_count = ceil(train_percentage * graph_batch.num_graphs)
test_count = graph_batch.num_graphs - train_count
data.train_mask = torch.tensor([True] * train_count + [False] * test_count)
# Randomly shuffle the train_mask
data.train_mask = data.train_mask[torch.randperm(data.train_mask.size(0))]
data.test_mask = ~data.train_mask
data.y = torch.tensor(y_dataset).type(torch.LongTensor)
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=5e-4)
model.train()
losses = []
for epoch in range(1000):
optimizer.zero_grad()
out = model(data.x, data.edge_index, data.batch)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
if epoch%10 == 0:
print(f"Epoch {epoch}, Loss: {loss.item():.4f}")
losses.append(loss.item())
loss.backward()
optimizer.step()
# Plot the loss over the epochs
plt.plot(losses)
plt.show() How can I fix the following code to achieve what I am looking for? %%time
device = torch.device('cuda' if torch.cuda.is_available() and args.use_cuda else 'cpu')
model = GATConvModel(in_features=graph_batch.x.shape[1],
hidden_features=8, num_classes=len(classes), pool_type="mean").to(device)
train_percentage = 0.8
train_count = ceil(train_percentage * graph_batch.num_graphs)
test_count = graph_batch.num_graphs - train_count
graph_batch.train_mask = torch.tensor([True] * train_count + [False] * test_count)
# Randomly shuffle the train_mask
graph_batch.train_mask = graph_batch.train_mask[torch.randperm(graph_batch.train_mask.size(0))]
graph_batch.test_mask = ~graph_batch.train_mask
graph_batch.y = torch.tensor(y_dataset).type(torch.LongTensor)
graph_batch.to(device)
dataloader = DataLoader(graph_batch, batch_size=args.batch_size)
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight_decay=5e-4)
model.train()
losses = []
for epoch in range(1000):
optimizer.zero_grad()
for data in dataloader:
out = model(data.x, data.edge_index, data.batch)
# Here what should the line look like?
loss = F.nll_loss(out[graph_batch.train_mask], graph_batch.y[graph_batch.train_mask])
if epoch%10 == 0:
print(f"Epoch {epoch}, Loss: {loss.item():.4f}")
losses.append(loss.item())
loss.backward()
optimizer.step()
# Plot the loss over the epochs
plt.plot(losses)
plt.show() Thanks in advance for your inputs :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 17 replies
-
Firstly don't combine your list of
You can directly use these data loaders in the above code. |
Beta Was this translation helpful? Give feedback.
Firstly don't combine your list of
Data
objects into aData
object.DataLoader
can work with a list ofData
objects.You can directly use these data loaders in the above code.
Refer to graph classifications examples in pyg for more information. https://github.com/pyg-team/pytorch_geometric/blob/master/examples/proteins_topk_pool.py, https://github.com/pyg-team/pytorch_geometric/blob/master/examples/mem_pool.py etc.