GraphSAGE 代码解析 - minibatch.py
class EdgeMinibatchIterator
""" This minibatch iterator iterates over batches of sampled edges or
random pairs of co-occuring edges. G -- networkx graph
id2idx -- dict mapping node ids to index in feature tensor
placeholders -- tensorflow placeholders object
context_pairs -- if not none, then a list of co-occuring node pairs (from random walks)
batch_size -- size of the minibatches
max_degree -- maximum size of the downsampled adjacency lists
n2v_retrain -- signals that the iterator is being used to add new embeddings to a n2v model
fixed_n2v -- signals that the iterator is being used to retrain n2v with only existing nodes as context
"""
def __init__(self, G, id2idx, placeholders, context_pairs=None, batch_size=100, max_degree=25,
n2v_retrain=False, fixed_n2v=False, **kwargs) 中具体介绍以下:
1 self.nodes = np.random.permutation(G.nodes())
2 # 函数shuffle与permutation都是对原来的数组进行重新洗牌,即随机打乱原来的元素顺序
3 # shuffle直接在原来的数组上进行操作,改变原来数组的顺序,无返回值
4 # permutation不直接在原来的数组上进行操作,而是返回一个新的打乱顺序的数组,并不改变原来的数组。
1 self.adj, self.deg = self.construct_adj()
这里重点看construct_adj()函数。
def construct_adj(self):
adj = len(self.id2idx) * \
np.ones((len(self.id2idx) + 1, self.max_degree))
# 该矩阵记录训练数据中各节点的邻居节点的编号
# 采样只取max_degree个邻居节点,采样方法见下
# 同样进行了行数加一操作 deg = np.zeros((len(self.id2idx),))
# 该矩阵记录了每个节点的度数 for nodeid in self.G.nodes():
if self.G.node[nodeid]['test'] or self.G.node[nodeid]['val']:
continue
neighbors = np.array([self.id2idx[neighbor]
for neighbor in self.G.neighbors(nodeid)
if (not self.G[nodeid][neighbor]['train_removed'])])
# Graph.neighbors() Return a list of the nodes connected to the node n.
# 在选取邻居节点时进行了筛选,对于G.neighbors(nodeid) 点node的邻居,
# 只取该node与neighbor相连的边的train_removed = False的neighbor
# 也就是只取不是val, test的节点。
# neighbors得到了邻居节点编号数列。 deg[self.id2idx[nodeid]] = len(neighbors)
# deg各位取值为该位对应nodeid的节点的度数,
# 也即经过上面筛选后得到的邻居数 if len(neighbors) == 0:
continue
if len(neighbors) > self.max_degree:
neighbors = np.random.choice(
neighbors, self.max_degree, replace=False)
# range: neighbors; size = max_degree; replace: replace the origin matrix or not
# np.random.choice为选取size大小的数列 elif len(neighbors) < self.max_degree:
neighbors = np.random.choice(
neighbors, self.max_degree, replace=True)
# 经过choice随机选取,得到了固定大小max_degree = 25的直接相连的邻居数列 adj[self.id2idx[nodeid], :] = neighbors
# 把该node的邻居数列,赋值给adj矩阵中对应nodeid位的向量。
return adj, deg
在construct_test_adj() 函数中,与上不同之处在于,可以直接得到邻居而无需根据val/test/train_removed筛选.
neighbors = np.array([self.id2idx[neighbor]
for neighbor in self.G.neighbors(nodeid)])
GraphSAGE 代码解析 - minibatch.py的更多相关文章
- GraphSAGE 代码解析(一) - unsupervised_train.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(二) - layers.py GraphSAGE 代码解析(三) - aggregators.py GraphSA ...
- GraphSAGE 代码解析(四) - models.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(二) - layers.py ...
- GraphSAGE 代码解析(三) - aggregators.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(二) - layers.py ...
- GraphSAGE 代码解析(二) - layers.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(三) - aggregator ...
- py-faster-rcnn代码阅读2-config.py
简介 该文件指定了用于fast rcnn训练的默认config选项,不能随意更改,如需更改,应当用yaml再写一个config_file,然后使用cfg_from_file(filename)导入以 ...
- 用 TensorFlow 实现 k-means 聚类代码解析
k-means 是聚类中比较简单的一种.用这个例子说一下感受一下 TensorFlow 的强大功能和语法. 一. TensorFlow 的安装 按照官网上的步骤一步一步来即可,我使用的是 virtua ...
- OpenStack之虚机热迁移代码解析
OpenStack之虚机热迁移代码解析 话说虚机迁移分为冷迁移以及热迁移,所谓热迁移用度娘的话说即是:热迁移(Live Migration,又叫动态迁移.实时迁移),即虚机保存/恢复(Save/Res ...
- Faster RCNN算法demo代码解析
一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...
- pointnet.pytorch代码解析
pointnet.pytorch代码解析 代码运行 Training cd utils python train_classification.py --dataset <dataset pat ...
随机推荐
- Laravel5 打印SQL
在src/Illuminate/Database/Connection.php里打印SQL默认是关闭的,见https://github.com/laravel/framework/commit/e0a ...
- Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)
原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...
- 制作npm插件vue-toast-m实例练习
制作npm插件vue-toast-m实例练习(消息弹窗) 一.使用npm插件 import VueToast from 'vue-toast-demo-cc' Vue.use(VueToast) th ...
- mysql忘记密码重置
一.更改my.cnf配置文件 0.MySQL 版本查看 mysql --version 1.用命令编辑/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.c ...
- 打造开源GIS方案
现在GIS用途较多,最近要有所接触,所以决定自己打造一个已经又的方案.均以Java作为开发 二位地图:客户端:geotools,swing,geoserver; web:openlayer,geose ...
- 揭开redux,react-redux的神秘面纱
16年开始使用react-redux,迄今也已两年多.这时候再来阅读和读懂redux/react-redux源码,虽已没有当初的新鲜感,但依然觉得略有收获.把要点简单写下来,一方面供感兴趣的读者参考, ...
- 安装mysqlclient报OSError: mysql_config not found
输入命令: :~$ pip install mysqlclient 报错: Collecting mysqlclient Using cached https://files.pythonhosted ...
- MHA实现mysql高可用复制集群
MHA简述 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在My ...
- cent os 上安装 matlab
下载和安装可以参考,这个链接: https://lanseyujie.com/post/matlab-download-and-activate.html 上面这链接,在创建桌面快捷键时,未能创建,c ...
- angularjs1+requirejs+ bootstrap+ jQuery低版本配合兼容ie8+浏览器
angularjs兼容低版本IE浏览器(IE8)angularjs在1.3之后的版本都是选择放弃对IE8及更低IE版本的支持,但是就目前的开发形式来看,IE8的使用客户还是蛮多的,最近有个项目要求尽量 ...