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. 获取订单的product_id 和订单的数量

    php 获取订单的product_id 和相对id的数量 <?php foreach ($val->groupResults(2) as $key2=>$val2):?> &l ...

  2. 自定义AngularJS中的services服务

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. js面向对象编程——创建对象

    JavaScript对每个创建的对象都会设置一个原型,指向它的原型对象. 当我们用obj.xxx访问一个对象的属性时,JavaScript引擎先在当前对象上查找该属性,如果没有找到,就到其原型对象上找 ...

  4. SpringBoot非官方教程 | 第十三篇:springboot集成spring cache

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot13-springcache/ 本文出自方志朋的博 ...

  5. 菜鸟笔记 -- Chapter 6.2.6 内部类

    6.2.6  内部类 在权限修饰符中,我们已经见过内部类了,但我们看到的只是冰山一角,这节我们详细介绍一下内部类,内部类可以分为成员内部类,局部内部类,匿名内部类,静态内部类.下面我们来讲解一下,在讲 ...

  6. 关于利用HashSet,split,deleteCharAt等方法详解

    1.首先了解一下HashSet的原理: Set接口  Set是对数学上集的抽象,Set中不包含重复的元素.如何界定是否是重复元素?Set最多可含一个null元素;对于任意的非null元素e1和e2,都 ...

  7. shiro框架 4种授权方式 说明

    1. shiro的配置文件(applicationContext-shiro.xml)中使用filterChain过滤url的方式 详细配置看注释 <?xml version="1.0 ...

  8. #leetcode刷题之路3-无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...

  9. 微信小程序navigator无法跳转情况

    情况有三种 跳转的页面没有在app.json中注册 跳转的路径不正确 以上两种在命令行(console)中都会提示 跳转的页面在TabBar中,需要将open-type属性是设置为switchTab

  10. 【解决】venv 的名字在 zsh prompt 中不显示

    venv 的名字在 zsh prompt 中不显示 ➜ liyongjiandeMBP.lan [/Users/liyongjian/lyj] python3 -m venv lyj_venv ➜ l ...