Hello all, good day. i have tried to explore DGL since last year and just start again after leaving it 7 months. Right now i am begin to work on it again and struggling with GAT, with very little knowledge/experience in programming, i aiming to learn it on the go but it is really a steep curve. Right now i try the best i can in the last two weeks working on simplest model i can imagine, but so far still no result.
I try manually to go with only 3 nodes and 3 edges (including the train_mask tensor etc), replacing only data from citation_graph from example written in GAT.
I define the node data with 5x5 tensor, and the edge data for message passing with 1x3 tensor. after i run no matter what the size is mismatch :
(e.g : RuntimeError: size mismatch, m1: [30 x 8], m2: [16 x 1] at C:\cb\pytorch_1000000000000\work\aten\src\TH/generic/THTensorMath.cpp:41)
i figure maybe that is caused by improper definition of tensor in train_mask etc, eventhough i dont believe it.
I try to figure out what the generate_mask_tensor do in the util but i just got lost in many functions. i do get a gist by reading the explanation from 4 fundamental formula. but the coding is bothering me so much, i admit my insufficiency but i really want to get this done for now.
Here is the code that is used for defining the features etc.
from dgl import DGLGraph
#from dgl.data import citation_graph as citegrh
import networkx as nx
import pandas as pd
import numpy as np
import torch
import dgl
import torch as th
import time
#Let's begin with only 3 nodes and 3 edges as the simplest example possible.
#Definition stated in tutorial not worked for me, let me use GCN example
#def load_cora_data():
#data = citegrh.load_cora()
#features = torch.FloatTensor(data.features)
#labels = torch.LongTensor(data.labels)
#mask = torch.BoolTensor(data.train_mask) --> train_mask = g.ndata['train_mask'], this is for the built in module, how i make one for myself?
#g = DGLGraph(data.graph)
#return g, features, labels, mask
#How to address and determine non uniformity of layer?
u, v = th.tensor([0, 1, 0]), th.tensor([1, 2, 2])
g = dgl.graph((u, v))
#only 2 m depth interval
n_weights_1 = th.tensor([[x, y, z, aa, ab],
[.., .., .., .., ..],
[.., .., .., .., ..],
[.., .., .., .., ..],
[.., .., .., .., ..]])
n_weights_2 = ...
n_weights_3 = ...
n_weights = th.stack([n_weights_1, n_weights_2, n_weights_3])
g.ndata['z'][th.tensor([0,1,2])] = n_weights
#(time_lapse(hr), dx_1, dx_2)
e_weights_1 = th.tensor([0.4, 3, 3])
e_weights_2 = th.tensor([0.08, -3, 0])
e_weights_3 = th.tensor([0.75, -3, -3])
e_weights = th.stack([e_weights_1, e_weights_2, e_weights_3])
features = n_weights
#mask = g.ndata['z'][th.tensor([1, 0, 0])] #Masking matrix (at least the dimension) need to be revised, in the example the dim is 'N' nodes (rows) x 1 column
mask = th.tensor([1, 1, 0])
#labels = g.ndata['z'][th.tensor([0, 1, 1])]
labels = th.tensor([0, 1, 1])
#From https://docs.dgl.ai/en/0.5.x/_modules/dgl/data/citation_graph.html
# @property
# def labels(self):
# deprecate_property('dataset.label', 'g.ndata[\'label\']')
# return F.asnumpy(self._g.ndata['label'])
g.edata['e'][th.tensor([0,1,2])] = e_weights
# create the model, 2 heads, each head has hidden size 8
net = GAT(g,
in_dim=features.size()[1],
hidden_dim=3,
out_dim=7,
num_heads=1)
# create optimizer
optimizer = torch.optim.Adam(net.parameters(), lr=1e-4)
# main loop
dur = []
for epoch in range(50):
if epoch >= 3:
t0 = time.time()
logits = net(features) #how is should modify the 'features'?HOW IS ONLY 'FEATURES' NEEDED?
logp = F.log_softmax(logits, 1)
loss = F.nll_loss(logp[mask], labels[mask]) #how i should modify the 'message'?
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch >= 3:
dur.append(time.time() - t0)
print("Epoch {:05d} | Loss {:.4f} | Time(s) {:.4f}".format(
epoch, loss.item(), np.mean(dur)))
Is the wrong train_mask splitting (due to manual way) the problem? or there is other problem i should know, i can’t find how to split train_mask and others easily too, and looking in this post it doesnt get any clearer.
Best Regards