Yeah, now i found what make make it doesn’t match, the writing isn’t valid like you said, the following code is still crude. I set the node and edges features still in individual manner. I copy the loss function exactly like in 5.3 except chaging ‘h’ and etc, but the following error occur, although it is clearly in definition of DotProductPredictor
class in 5.3 :
NameError Traceback (most recent call last)
<ipython-input-28-f9fc5552814f> in <module>
148 opt = th.optim.Adam(model.parameters())
149 for epoch in range(10):
--> 150 negative_graph = construct_negative_graph(GX, k) #NameError: name 'construct_negative_graph' is not defined
151 pos_score, neg_score = model(GX, negative_graph, node_features)
152 loss = compute_loss(pos_score, neg_score)
NameError: name 'construct_negative_graph' is not defined
I don’t pretend that i understand everything. here is the code that include 9 nodes and 22 edges, with bottom zero-padding features to all the node features so the tensor dimension right now is 26columns x 487 rows.
import dgl
import numpy as np
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import pandas as pd
from dgl.nn import SAGEConv
class SAGE(nn.Module):
def __init__(self, in_feats, hid_feats, out_feats):
super().__init__()
self.conv1 = dgl.nn.SAGEConv(
in_feats=in_feats, out_feats=hid_feats, aggregator_type='mean')
self.conv2 = dgl.nn.SAGEConv(
in_feats=hid_feats, out_feats=out_feats, aggregator_type='mean')
def forward(self, graph, inputs):
# inputs are features of nodes
XX = self.conv1(graph, inputs)
XX = F.relu(XX)
XX = self.conv2(graph, XX)
return XX
class DotProductPredictor(nn.Module):
def forward(self, graph, XX):
# h contains the node representations computed from the GNN defined
# in the node classification section (Section 5.1).
with graph.local_scope():
graph.ndata['XX'] = XX
graph.apply_edges(fn.u_dot_v('XX', 'XX', 'score'))
return graph.edata['score']
def construct_negative_graph(graph, k):
src, dst = graph.edges()
neg_src = src.repeat_interleave(k)
neg_dst = th.randint(0, graph.number_of_nodes(), (len(src) * k,))
return dgl.graph((neg_src, neg_dst), num_nodes=graph.number_of_nodes())
class Model(nn.Module):
def __init__(self, in_features, hidden_features, out_features):
super().__init__()
self.sage = SAGE(in_features, hidden_features, out_features)
self.pred = DotProductPredictor()
def forward(self, graph, neg_g, x):
h = self.sage(graph, x)
return self.pred(graph, XX), self.pred(neg_g, XX)
def compute_loss(pos_score, neg_score):
# Margin loss
n_edges = pos_score.shape[0]
return (1 - neg_score.view(n_edges, -1) + pos_score.unsqueeze(1)).clamp(min=0).mean()
#FOR TYPE 1
print("Loading CSV...")
Area1_A1 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A1.xlsx')
Area1_A2 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A2.xlsx')
Area1_A3 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A3.xlsx')
Area1_A4 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A4.xlsx')
Area1_A5 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A5.xlsx')
Area1_A6 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A6.xlsx')
Area1_A7 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A7.xlsx')
Area1_A8 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A8.xlsx')
Area1_A9 = pd.read_excel('C:/Users/Acer/DGLCONDA05/Data Type 1/Area1-A9.xlsx')
#Is it possible for data type to be float32?as the guide suggest.
print("Converting to Tensor...")
Area1_A1 = th.tensor(Area1_A1.values, dtype=th.float32)
Area1_A2 = th.tensor(Area1_A2.values, dtype=th.float32)
Area1_A3 = th.tensor(Area1_A3.values, dtype=th.float32)
Area1_A4 = th.tensor(Area1_A4.values, dtype=th.float32)
Area1_A5 = th.tensor(Area1_A5.values, dtype=th.float32)
Area1_A6 = th.tensor(Area1_A6.values, dtype=th.float32)
Area1_A7 = th.tensor(Area1_A7.values, dtype=th.float32)
Area1_A8 = th.tensor(Area1_A8.values, dtype=th.float32)
Area1_A9 = th.tensor(Area1_A9.values, dtype=th.float32)
th.save(Area1_A1, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A1.pt')
th.save(Area1_A2, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A2.pt')
th.save(Area1_A3, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A3.pt')
th.save(Area1_A4, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A4.pt')
th.save(Area1_A5, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A5.pt')
th.save(Area1_A6, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A6.pt')
th.save(Area1_A7, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A7.pt')
th.save(Area1_A8, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A8.pt')
th.save(Area1_A9, 'C:/Users/Acer/DGLCONDA05/Data Type 1/Area1_A9.pt')
u = th.tensor([0,1,0,1,2,0,0,2,0,3,1,2,1,3,0,1,2,3,4,5,6,7])
v = th.tensor([1,2,2,3,3,3,4,4,5,5,6,6,7,7,8,8,8,8,8,8,8,8])
graph_data_type1 = (u, v)
G = dgl.graph(graph_data_type1)
G.edata['linkdata'] = th.tensor([[0,-2,0],
[5,1,1],
[5,-1,1],
[5,1,-1],
[0,0,-2],
[5,-1,-1],
[7,0,1],
[2,1,0],
[7,0,-1],
[2,1,-1],
[7,0,1],
[2,-1,0],
[7,0,-1],
[2,-1,0],
[9,-1,0],
[9,1,0],
[4,0,-1],
[4,0,1],
[2,-1,-1],
[2,-1,1],
[2,1,-1],
[2,1,1]], dtype=th.float32)
ai = nn.ZeroPad2d((0,0,0,34))
bi = nn.ZeroPad2d((0,0,0,56))
ci = nn.ZeroPad2d((0,0,0,20))
di = nn.ZeroPad2d((0,0,0,75))
ei = nn.ZeroPad2d((0,0,0,49))
fi = nn.ZeroPad2d((0,0,0,10))
gi = nn.ZeroPad2d((0,0,0,39))
hi = nn.ZeroPad2d((0,0,0,192))
ii = nn.ZeroPad2d((0,0,0,0))
#All of this now matrices in 26x487 size.
a = ai(Area1_A1)
b = bi(Area1_A2)
c = ci(Area1_A3)
d = di(Area1_A4)
e = ei(Area1_A5)
f = fi(Area1_A6)
g = gi(Area1_A7)
h = hi(Area1_A8)
i = ii(Area1_A9)
G.ndata['gabungan'] = th.stack([a, b, c, d, e, f, g, h, i])
GX = dgl.add_self_loop(G)
node_features = GX.ndata['gabungan']
n_features = node_features.shape[1]
k = 5
model = Model(n_features, 100, 100)
opt = th.optim.Adam(model.parameters())
for epoch in range(10):
negative_graph = construct_negative_graph(GX, k) #NameError: name 'construct_negative_graph' is not defined
pos_score, neg_score = model(GX, negative_graph, node_features)
loss = compute_loss(pos_score, neg_score)
opt.zero_grad()
loss.backward()
opt.step()
print(loss.item())