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的更多相关文章

  1. GraphSAGE 代码解析(一) - unsupervised_train.py

    原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(二) - layers.py GraphSAGE 代码解析(三) - aggregators.py GraphSA ...

  2. GraphSAGE 代码解析(四) - models.py

    原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(二) - layers.py ...

  3. GraphSAGE 代码解析(三) - aggregators.py

    原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(二) - layers.py ...

  4. GraphSAGE 代码解析(二) - layers.py

    原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(三) - aggregator ...

  5. py-faster-rcnn代码阅读2-config.py

    简介  该文件指定了用于fast rcnn训练的默认config选项,不能随意更改,如需更改,应当用yaml再写一个config_file,然后使用cfg_from_file(filename)导入以 ...

  6. 用 TensorFlow 实现 k-means 聚类代码解析

    k-means 是聚类中比较简单的一种.用这个例子说一下感受一下 TensorFlow 的强大功能和语法. 一. TensorFlow 的安装 按照官网上的步骤一步一步来即可,我使用的是 virtua ...

  7. OpenStack之虚机热迁移代码解析

    OpenStack之虚机热迁移代码解析 话说虚机迁移分为冷迁移以及热迁移,所谓热迁移用度娘的话说即是:热迁移(Live Migration,又叫动态迁移.实时迁移),即虚机保存/恢复(Save/Res ...

  8. Faster RCNN算法demo代码解析

    一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...

  9. pointnet.pytorch代码解析

    pointnet.pytorch代码解析 代码运行 Training cd utils python train_classification.py --dataset <dataset pat ...

随机推荐

  1. 微信小程序【消息推送服务器认证C# WebAPI】

    参考微信开发文档: https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/callback_help.html 代码可用 /// ...

  2. c的三个内存分配函数(malloc,realloc,calloc)

    //内存分配_malloc int main(){ int *p; char *p1; p=(int *)malloc(sizeof(*p)*size);//size为需要存储的数量 p1=();// ...

  3. vue2.0移除或更改的一些东西

    一.vue2.0移除了$index和$key 虽然说现在很多文章说他们的代码是vue2.0版本的,但是有一些仔细一看,发现并不全是2.0版本,有些语法还是1.0的版本,比如这个$index,$key, ...

  4. springboot2.04+mybatis-plus+swagger2+CodeGenerator

    @author zhangyh SpringBoot技术栈搭建个人博客[项目准备]  RESTful API就是一套协议来规范多种形式的前端和同一个后台的交互方式 原型设计 事实上,我是直接先去找的原 ...

  5. swift计算label动态宽度和高度

    swift计算label动态宽度和高度 func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let ...

  6. meclipse6.5破解

    package com.test.ssh.common;   import java.text.DecimalFormat; import java.text.NumberFormat; import ...

  7. 【TOJ 4475】The Coolest Sub-matrix(对角线前缀和)

    描述 Given an N*N matrix, find the coolest square sub-matrix.We define the cool value of the square ma ...

  8. SQL:检索数据-基本检索

    检索数据 1.select语句 增删改查四大操作之"查",即检索: 一般包括:what,where:查什么,从哪里选择 2.检索单个列 例:想从products表中检索名为prod ...

  9. Python协程中使用上下文

    在Python 3.7中,asyncio 协程加入了对上下文的支持.使用上下文就可以在一些场景下隐式地传递变量,比如数据库连接session等,而不需要在所有方法调用显示地传递这些变量.使用得当的话, ...

  10. Python学习手册之Python介绍、基本语法(二)

    在上一篇文章中,我们介绍了Python的一些基本语法,现在我们继续介绍剩下的Python基本语法.查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/987193 ...