fighterlyl / graphneuralnetwork Goto Github PK
View Code? Open in Web Editor NEW《深入浅出图神经网络:GNN原理解析》配套代码
《深入浅出图神经网络:GNN原理解析》配套代码
92页:5.21公式里面的h0, h1这些系数,则么计算?
93页:5.23公式里面的hk 的k应该是下标而不是上标。
94页:x(1) = Hx,x(2)=Hx(1), 根据93页的公式,不对吧。应该是x(1)=Lx, x(2)=Lx(1)。而且在已知H=A的情况,系数向量h则么计算得到。根据5.21公式,先算出L,把书上的系数向量h代入公式,计算得到H 和书上写的 H 不一致。
公式中i和j的求和范围是否都应该是-floor(k/2)到floor(k/2),而不是floor(2/k)?
P47倒数第二行H(3)∈R272796,应该为2727256
如题
The download url for Cora data (chapter 5), i.e., https://raw.githubusercontent.com/kimiyoung/planetoid/master/data, is unavailable now. When I try this url, it leads me to the 404 not found page.
something seems wrong in the file of main.py in chapter07
# line 31 - 32
train_index = np.where(data.train_mask)[0]
train_label = data.y[train_index]
# line 45 - 46
batch_src_index = np.random.choice(train_index, size=(BTACH_SIZE,))
batch_src_label = torch.from_numpy(train_label[batch_src_index]).long().to(DEVICE)
here, train_label
should be data.y
but not data.y[train_index]
"x:节点特征,维度为 2808×1433" 这里应该是 2708*1433
是否应该为:第l层的误差与第l+1层的误差有关?
原书中2.2.2多层感知机中第一小节【单隐层感知器】中最后阐述的“W^(0)的每一列表示从输入连接到隐藏层一个神经元的权值向量”是否应为“W^(0)的每一行”?
那个cora的网址显示——404: Not Found
可以给的可以用的吗?不太清楚数据集放置位置(新手发盐)
作者你好,感谢这个详细的代码库。
请问第九章运行示例什么时候可以更新?
您好 我想问一下为什么第九章的代码最后我得到的结果特别低啊 LOSS1.26 RSME0.92
E:\深度学习开源项目\GraphNeuralNetwork-master\chapter8>python self_attn_pool.py
Traceback (most recent call last):
File "self_attn_pool.py", line 11, in
from sklearn.model_selection import train_test_split
ModuleNotFoundError: No module named 'sklearn'
但是我已经安装过sklearn了,请问该如何解决?
另外该程序用cpu可以跑的吧?
谢谢!
cora数据集下载保存到本地D:\GraphNeuralNetwork-master\chapter5\cora\raw,当运行第五章代码# In[7]:# 加载数据,并转换为torch.Tensor时出现下列问题:
UnpicklingError Traceback (most recent call last)
in
----> 1 dataset = CoraData().data
2 x = dataset.x / dataset.x.sum(1, keepdims=True) # 归一化数据,使得每一行和为1
3 tensor_x = torch.from_numpy(x).to(device)
4 tensor_y = torch.from_numpy(dataset.y).to(device)
5 tensor_train_mask = torch.from_numpy(dataset.train_mask).to(device)
in init(self, data_root, rebuild)
16 else:
17 self.maybe_download()
---> 18 self._data = self.process_data()
19 with open(save_file, "wb") as f:
20 pickle.dump(self.data, f)
in process_data(self)
32 print("Process data ...")
33 _, tx, allx, y, ty, ally, graph, test_index = [self.read_data(
---> 34 osp.join(self.data_root, "raw", name)) for name in self.filenames]
35 train_index = np.arange(y.shape[0])
36 val_index = np.arange(y.shape[0], y.shape[0] + 500)
in (.0)
32 print("Process data ...")
33 _, tx, allx, y, ty, ally, graph, test_index = [self.read_data(
---> 34 osp.join(self.data_root, "raw", name)) for name in self.filenames]
35 train_index = np.arange(y.shape[0])
36 val_index = np.arange(y.shape[0], y.shape[0] + 500)
in read_data(path)
92 return out
93 else:
---> 94 out = pickle.load(open(path, "rb"), encoding="latin1")
95 out = out.toarray() if hasattr(out, "toarray") else out
96 return out
UnpicklingError: invalid load key, '\x0a'.
GraphNeuralNetwork/chapter5/GCN_Cora.py
Line 131 in 7bad933
第54页
对于大小为3x3的输出区域,上述三种卷积核对应的感知野是如何计算的?
通过以下代码发现结果和书上的不一样
import numpy as np
l = np.array([[2, -1, -1, 0, 0], [-1, 3, -1, -1, 0], [-1, -1, 3, -1, 0], [0, -1, -1, 3, -1], [0, 0, 0, -1, 1]])
print(l)
vals, vecs = np.linalg.eig(l)
sorted_indices = np.argsort(vals)
rank_vals = vals[sorted_indices]
rank_vecs = vecs[:, sorted_indices]
np.set_printoptions(
infstr='inf',
nanstr='nan',
formatter=None,
precision=3, # 精度,保留小数点后几位
threshold=500,
# 最多可显示的Array元素个数
edgeitems=3,
# 在缩率显示时在起始和默认显示的元素个数
linewidth=150, # 每行最多显示的字符数,默认80,超过则换行显示
suppress=True # 浮点显示(不用科学计数法)
)
print(rank_vals)
print(rank_vecs)
算出的结果如下
[0. 0.83 2.689 4. 4.481]
[[ 0.447 0.438 -0.703 -0. -0.338]
[ 0.447 0.256 0.242 0.707 0.419]
[ 0.447 0.256 0.242 -0.707 0.419]
[ 0.447 -0.138 0.536 -0. -0.702]
[ 0.447 -0.811 -0.318 0. 0.202]]
请问这里的data是什么数据?公共数据集吗?
首先感谢作者写了这本书,作为入门书我觉得很不错。由于理解还不够,想问作者几个问题:
买了亚马逊kindle版本的电子书,感觉是第一版的,能不能把勘误的错误在电子书中改正。不然得边看电子书边看勘误pdf。谢谢
作者您好,我感觉第五页K阶子图定义有些问题。这里面写的E‘={eij|任意 vj,d(vi,vj)<=k}
这里面如果eij图中根本不存在这条边,但是存在这个路径所以说这可能会有些问题。所以我认为E‘={elm|任意 vj,d(vi,vj)<=k,elm属于Pij}
希望得到您的回复。目前正在认真阅读图卷积知识。
Hello @FighterLYL
Thank you for sharing the code of the book!
In chapter 5's Jupyter Notebook, I found a small problem in its cell 9.
For those who use CUDA device to run this cell.
def test(mask):
model.eval()
with torch.no_grad():
logits = model(tensor_adjacency, tensor_x)
test_mask_logits = logits[mask]
predict_y = test_mask_logits.max(1)[1]
accuarcy = torch.eq(predict_y, tensor_y[mask]).float().mean()
return accuarcy, test_mask_logits.numpy(), tensor_y[mask].numpy()
This is your original code. And I run it on a CUDA device, then it returns such error.
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
It shows that the tensor is still in CUDA device. and use .cpu()
method can solve this.
So, I modified the code:
def test(mask):
model.eval()
with torch.no_grad():
logits = model(tensor_adjacency, tensor_x)
test_mask_logits = logits[mask]
predict_y = test_mask_logits.max(1)[1]
accuarcy = torch.eq(predict_y, tensor_y[mask]).float().mean()
# return accuarcy, test_mask_logits.numpy(), tensor_y[mask].numpy()
# NOTE(WMF): if you want to use CUDA device, and... do not detach them?
if device == 'cpu':
return accuarcy, test_mask_logits.detach().numpy(), tensor_y[mask].detach().numpy()
else:
return accuarcy, test_mask_logits.detach().cpu().numpy(), tensor_y[mask].detach().cpu().numpy()
I added a .detach()
to make sure the result is detached from PyTorch's computation map. (I tried the code without .detach()
, it still works, so .detach()
can be an optional.
yours sincerely,
@WMF1997
当步长s=2时,卷积之后的输出是否应该为3x3?
如题: https://raw.githubusercontent.com/kimiyoung/planetoid/master/data 这个可能失效了。请提供下新的下载地址。
跑了一下里面的jupyter文件,报错RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target',自己将target改成long类型也会报错RuntimeError: multi-target not supported at C:/w/1/s/tmp_conda_3.6_045031/conda/conda-bld/pytorch_1565412750030/work/aten/src\THCUNN/generic/ClassNLLCriterion.cu:15
4-10与4-11都是算KL距离,一个是单竖线,一个是双竖线,请问这两个有什么区别吗?上下文中没有说明,百度和CSDN里都是用的双竖线,这里不明白,请教一下,谢谢
你好,我尝试在R8数据集上使用GraphSAGE进行分类预测
由于你实验用的.x,.y,.alllx一类的文件都是直接下载的,所以我用了别人的代码来处理数据并生成这一类文件。但在实验中遇到了loss一直为NAN的原因。网上说可能是输入数据有问题,但是我比较之后,发现两者似乎没有明显差别,现在就有点懵。不知道你有没有在别的数据集上用过GraphSAGE?
另外,请问你知道哪里可以下到R8,MR,20ng这一类数据集的.x,.y,.allx这一类文件吗?
P44页的公式3.6是否应该对通道数求一次和?
2020年11月第一版第5次印刷, P88, P89内容。
起因是图5.4(a)的显示问题,这里第一列显示全负,但是P88给的V却不是全负的...所以如下重新计算了一遍:
In [77]: L = np.array([[2, -1, -1, 0, 0], [-1, 3, -1, -1, 0], [-1, -1, 3, -1, 0]
...: , [0, -1, -1, 3, -1], [0, 0, 0, -1, 1]])
In [78]: vals, vecs = np.linalg.eig(L)
In [79]: vals
Out[79]: array([ 0. , 0.82991351, 2.68889218, 4.4811943 , 4. ])
In [80]: vecs
Out[80]:
array([[ 4.47213595e-01, 4.37531395e-01, -7.03081478e-01,
-3.37998097e-01, -1.24491566e-16],
[ 4.47213595e-01, 2.55974786e-01, 2.42173667e-01,
4.19319477e-01, 7.07106781e-01],
[ 4.47213595e-01, 2.55974786e-01, 2.42173667e-01,
4.19319477e-01, -7.07106781e-01],
[ 4.47213595e-01, -1.38018756e-01, 5.36249932e-01,
-7.02415001e-01, -8.17563909e-16],
[ 4.47213595e-01, -8.11462211e-01, -3.17515788e-01,
2.01774144e-01, 3.46536171e-16]])
In [81]: vecs[:, 0]
Out[81]: array([ 0.4472136, 0.4472136, 0.4472136, 0.4472136, 0.4472136])
In [82]: vecs[:, 1]
Out[82]: array([ 0.43753139, 0.25597479, 0.25597479, -0.13801876, -0.81146221])
In [83]: vecs[:, 3]
Out[83]: array([-0.3379981 , 0.41931948, 0.41931948, -0.702415 , 0.20177414])
In [84]: vals
Out[84]: array([ 0. , 0.82991351, 2.68889218, 4.4811943 , 4. ])
好像都对不太上...不知道我是不是哪里搞错了Orz
Traceback (most recent call last):
File "main.py", line 71, in
train()
File "main.py", line 47, in train
batch_sampling_result = multihop_sampling(batch_src_index, NUM_NEIGHBORS_LIST, data.adjacency_dict)
File "E:\GraphNeuralNetwork-master\chapter7\sampling.py", line 37, in multihop_sampling
hopk_result = sampling(sampling_result[k], hopk_num, neighbor_table)
File "E:\GraphNeuralNetwork-master\chapter7\sampling.py", line 19, in sampling
res = np.random.choice(neighbor_table[sid], size=(sample_num, ))
TypeError: 'coo_matrix' object is not subscriptable
你好,Chapter5中Cora数据集的下载链接https://raw.githubusercontent.com/kimiyoung/planetoid/master/data打不开,请问有其他方式下载该数据集吗?
有个疑问,似乎用于训练的数据是按顺序取的?并且实际上用于训练的数据并不是对应于tx?
作者您好,你在解释最初如何设计图卷积神经网络的过程中,你提到可以对频率响应矩阵进行参数化,之后解释他的不足的原因解释为:
“引入的学习参数过多,需要学习的参数量与图中的节点数一致,这在大规模图数据,比如上亿节点数规模
的图中,极易发生过拟合问题。”
可是按照我的理解,在现在GCN的设计中,参数量应该是大于节点个数的吧?所以我不是很懂这个部分的理解。另外,我不知道这句话是不是有个前提,那就是参数量大这个情况只会发生在大规模图数据。
作者你好,我想问一下如果我想在第五章代码的基础上改一下,在新的数据集上测试,但是我的输入数据维度是3D的,这个时候会遇到输入特征维度出错,因为Cora数据集输入特征维度就是int类型的,这种情况应该怎么改呢
Traceback (most recent call last):
File "D:/GraphNeuralNetwork-master/GraphNeuralNetwork-master/chapter7/main.py", line 71, in
train()
File "D:/GraphNeuralNetwork-master/GraphNeuralNetwork-master/chapter7/main.py", line 47, in train
batch_sampling_result = multihop_sampling(batch_src_index, NUM_NEIGHBORS_LIST, data.adjacency_dict)
File "D:\GraphNeuralNetwork-master\GraphNeuralNetwork-master\chapter7\sampling.py", line 37, in multihop_sampling
hopk_result = sampling(sampling_result[0], hopk_num, neighbor_table)
File "D:\GraphNeuralNetwork-master\GraphNeuralNetwork-master\chapter7\sampling.py", line 19, in sampling
res = np.random.choice(neighbor_table[sid], size=(sample_num, ))
TypeError: 'coo_matrix' object does not support indexing
基于公式z(l+1) = W(l+1)a(l) + b(l),这里应该是b(l+1)吧
数据加载是否要VPN,5章程序训练不了代码显示[WinError 10054] 远程主机强迫关闭了一个现有的连接。>,打开数据集连接显示404
书中:
如何证明是图位移算子??这应该和x1自身有关系吧,不一定是图移位算子。
@FighterLYL
您好,我在运行第八章关于SAGPool的代码时,在运行dataset = DDDatataset()时出现以下问题:
Loading DD_A.txt
Loading DD_node_labels.txt
Loading DD_graph_indicator.txt
Loading DD_graph_labels.txt
Number of nodes: 334925
Traceback (most recent call last):
File "self_attn_pool.py", line 347, in
dataset = DDDataset()
File "self_attn_pool.py", line 83, in init
self.train_index, self.test_index = self.split_data(train_size)
File "self_attn_pool.py", line 91, in split_data
random_state=1234)
File "/home/zyf/anaconda3/envs/pt36/lib/python3.6/site-packages/sklearn/model_selection/_split.py", line 2096, in train_test_split
arrays = indexable(*arrays)
File "/home/zyf/anaconda3/envs/pt36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 230, in indexable
check_consistent_length(*result)
File "/home/zyf/anaconda3/envs/pt36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 201, in check_consistent_length
lengths = [_num_samples(X) for X in arrays if X is not None]
File "/home/zyf/anaconda3/envs/pt36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 201, in
lengths = [_num_samples(X) for X in arrays if X is not None]
File "/home/zyf/anaconda3/envs/pt36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 146, in _num_samples
" a valid collection." % x)
TypeError: Singleton array array({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,(中间省略),1167, 1168, 1169, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177},
dtype=object) cannot be considered a valid collection.
不知道是数据集的问题还是其他问题,请您帮我解决一下,谢谢。
我登录https://raw.githubusercontent.com/kimiyoung/planetoid/master/data这个网站不能直接下载数据。代码按您的粘过来报错如下:
Traceback (most recent call last):
File "D:\练习\learning\GCN_KEY.py", line 257, in
dataset = CoraData().data
File "D:\练习\learning\GCN_KEY.py", line 70, in init
self.maybe_download()
File "D:\练习\learning\GCN_KEY.py", line 122, in maybe_download
"{}/{}".format(self.download_url, name), save_path)
File "D:\练习\learning\GCN_KEY.py", line 157, in download_data
data = urllib.request.urlopen(url)
File "D:\anaconda3\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "D:\anaconda3\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "D:\anaconda3\lib\urllib\request.py", line 543, in _open
'_open', req)
File "D:\anaconda3\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "D:\anaconda3\lib\urllib\request.py", line 1362, in https_open
context=self._context, check_hostname=self._check_hostname)
File "D:\anaconda3\lib\urllib\request.py", line 1321, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 11004] getaddrinfo failed>
我将main函数中的train部分改为
def train():
model.train()
for e in range(EPOCHS):
for batch in range(NUM_BATCH_PER_EPOCH):
batch_src_index = np.random.choice(train_index, size=(BTACH_SIZE,))
r=batch_src_index
#t=len(r)
print("batch_src_index ")
print(r)#添加部分,显示1---------------------------------------------------显示1
batch_src_label = torch.from_numpy(train_label[batch_src_index]).long().to(DEVICE)
batch_sampling_result = multihop_sampling(batch_src_index, NUM_NEIGHBORS_LIST, data.adjacency_dict)
batch_sampling_x = [torch.from_numpy(x[idx]).float().to(DEVICE) for idx in batch_sampling_result]
# r=batch_sampling_x[0],x=3个16的tensor
# t=len(r)
# print(t)
batch_train_logits = model(batch_sampling_x)
loss = criterion(batch_train_logits, batch_src_label)
optimizer.zero_grad()
loss.backward() # 反向传播计算参数的梯度
optimizer.step() # 使用优化方法进行梯度更新
print("Epoch {:03d} Batch {:03d} Loss: {:.4f}".format(e, batch, loss.item()))
test()
在multihop_sampling函数中添加显示
def multihop_sampling(src_nodes, sample_nums, neighbor_table):
sampling_result = [src_nodes]
print(src_nodes)# 显示2------------------------------------------------------显示2
for k, hopk_num in enumerate(sample_nums):
r=hopk_num
print(k)# 显示3------------------------------------------------------显示3
print(r)# 显示4--------------------------------------------------- 显示4
q=sampling_result[k]
print(q)# 显示5---------------------------------------------------显示5
hopk_result = sampling(sampling_result[k], hopk_num, neighbor_table)
sampling_result.append(hopk_result)
return sampling_result
在sampling函数中添加显示
def sampling(src_nodes, sample_num, neighbor_table):
results = []
print(src_nodes)# 显示6---------------------------------------------------显示6
对应的multihop_sampling中的循环结果为:
[107 95 10 100 24 118 108 50 83 68 33 19 90 3 92 83]#显示1
[107 95 10 100 24 118 108 50 83 68 33 19 90 3 92 83]#显示2
0#显示3
10#显示4
[107 95 10 100 24 118 108 50 83 68 33 19 90 3 92 83]#显示5
[107 95 10 100 24 118 108 50 83 68 33 19 90 3 92 83]#显示6
1#二轮显示3
10#二轮显示4
[ 541 1650 541 1113 1650 1113 541 541 971 1113 334 1303 456 2182
1580 1580 2199 2200 1628 861 2545 476 476 2545 2545 476 476 2545
476 476 2056 2056 2056 1602 2056 1602 1602 2056 2056 1602 1701 2141
1701 17 17 201 2141 2139 1636 17 1343 842 2165 1690 1507 842
2166 1616 2165 554 2209 1647 2157 1647 2209 2209 2209 1647 2157 2157
1441 1441 1441 1441 1441 1441 1441 1441 1441 1441 2581 2581 2581 2581
2581 2581 2581 2581 1520 2581 391 1986 1358 1986 1358 1358 391 1986
1358 1358 2040 1051 286 2121 1051 2119 286 2120 911 2119 1939 1939
1939 1939 1939 1939 1939 1939 1939 1939 1358 155 155 156 155 1358
155 1358 156 1358 2544 2544 2544 2544 2544 2544 2544 2544 2544 2544
1836 898 1836 898 898 898 1836 1836 1836 1836 1520 2581 2581 2581
1520 1520 1520 1520 2581 2581]#二轮显示5,总共1000维
[ 541 1650 541 1113 1650 1113 541 541 971 1113 334 1303 456 2182
1580 1580 2199 2200 1628 861 2545 476 476 2545 2545 476 476 2545
476 476 2056 2056 2056 1602 2056 1602 1602 2056 2056 1602 1701 2141
1701 17 17 201 2141 2139 1636 17 1343 842 2165 1690 1507 842
2166 1616 2165 554 2209 1647 2157 1647 2209 2209 2209 1647 2157 2157
1441 1441 1441 1441 1441 1441 1441 1441 1441 1441 2581 2581 2581 2581
2581 2581 2581 2581 1520 2581 391 1986 1358 1986 1358 1358 391 1986
1358 1358 2040 1051 286 2121 1051 2119 286 2120 911 2119 1939 1939
1939 1939 1939 1939 1939 1939 1939 1939 1358 155 155 156 155 1358
155 1358 156 1358 2544 2544 2544 2544 2544 2544 2544 2544 2544 2544
1836 898 1836 898 898 898 1836 1836 1836 1836 1520 2581 2581 2581
1520 1520 1520 1520 2581 2581]#二轮显示6
问题1:二轮的数据怎么生成的?
问题2:显示5中的sampling_result[k]不应该为sampling_result的第k个数据,不应是一个数据吗?怎么显示的是一个张量?
一、
P133页GraphSAGE的伪代码第9行应该是for k = 1...K do 吧,这个循环不应该从src_node作聚合了,而是从远处的node聚合,原论文也是写的for k = 1...K do
二、
不是很理解p152页GraphSAGE代码实现的forward部分,为什么有两个分别遍历num_layers和num_layers - 1的for循环,我理解的算法似乎做一次从1到K的聚合遍历就行了,希望能解释一下
P137页 图7-4softmax输出的节点应为αij而非eij
P138页 公式7.8 αij的计算公式,分母中的Whj,应为Whk,与前面的求和函数的第k个节点应保持一致
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.