-
Notifications
You must be signed in to change notification settings - Fork 425
/
sampled_node_generators.py
563 lines (448 loc) · 19.8 KB
/
sampled_node_generators.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# -*- coding: utf-8 -*-
#
# Copyright 2018-2020 Data61, CSIRO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Mappers to provide input data for the graph models in layers.
"""
__all__ = [
"GraphSAGENodeGenerator",
"HinSAGENodeGenerator",
"Attri2VecNodeGenerator",
"DirectedGraphSAGENodeGenerator",
]
import warnings
import operator
import random
import abc
import warnings
import numpy as np
import itertools as it
import networkx as nx
import scipy.sparse as sps
from tensorflow.keras import backend as K
from functools import reduce
from tensorflow.keras.utils import Sequence
from ..data import (
SampledBreadthFirstWalk,
SampledHeterogeneousBreadthFirstWalk,
DirectedBreadthFirstNeighbours,
)
from ..core.graph import StellarGraph, GraphSchema
from ..core.utils import is_real_iterable
from . import NodeSequence
from ..random import SeededPerBatch
class BatchedNodeGenerator(abc.ABC):
"""
Abstract base class for graph data generators.
The supplied graph should be a StellarGraph object that is ready for
machine learning. Currently the model requires node features for all
nodes in the graph.
Do not use this base class: use a subclass specific to the method.
Args:
G (StellarGraph): The machine-learning ready graph.
batch_size (int): Size of batch to return.
schema (GraphSchema): [Optional] Schema for the graph, for heterogeneous graphs.
"""
def __init__(self, G, batch_size, schema=None):
if not isinstance(G, StellarGraph):
raise TypeError("Graph must be a StellarGraph or StellarDiGraph object.")
self.graph = G
self.batch_size = batch_size
# This is a node generator and requries a model with one root nodes per query
self.multiplicity = 1
# Check if the graph has features
G.check_graph_for_ml()
# We need a schema for compatibility with HinSAGE
if schema is None:
self.schema = G.create_graph_schema()
elif isinstance(schema, GraphSchema):
self.schema = schema
else:
raise TypeError("Schema must be a GraphSchema object")
# We will need real node types here
self.head_node_types = None
# Create sampler for GraphSAGE
self.sampler = None
@abc.abstractmethod
def sample_features(self, head_nodes, batch_num):
pass
def flow(self, node_ids, targets=None, shuffle=False, seed=None):
"""
Creates a generator/sequence object for training or evaluation
with the supplied node ids and numeric targets.
The node IDs are the nodes to train or inference on: the embeddings
calculated for these nodes are passed to the downstream task. These
are a subset of the nodes in the graph.
The targets are an array of numeric targets corresponding to the
supplied node_ids to be used by the downstream task. They should
be given in the same order as the list of node IDs.
If they are not specified (for example, for use in prediction),
the targets will not be available to the downstream task.
Note that the shuffle argument should be True for training and
False for prediction.
Args:
node_ids: an iterable of node IDs
targets: a 2D array of numeric targets with shape
`(len(node_ids), target_size)`
shuffle (bool): If True the node_ids will be shuffled at each
epoch, if False the node_ids will be processed in order.
Returns:
A NodeSequence object to use with with StellarGraph models
in Keras methods ``fit_generator``, ``evaluate_generator``,
and ``predict_generator``
"""
if self.head_node_types is not None:
expected_node_type = self.head_node_types[0]
else:
expected_node_type = None
# Check all IDs are actually in the graph and are of expected type
for n in node_ids:
try:
node_type = self.graph.node_type(n)
except KeyError:
raise KeyError(f"Node ID {n} supplied to generator not found in graph")
if expected_node_type is not None and (node_type != expected_node_type):
raise ValueError(
f"Node ID {n} not of expected type {expected_node_type}"
)
return NodeSequence(
self.sample_features,
self.batch_size,
node_ids,
targets,
shuffle=shuffle,
seed=seed,
)
def flow_from_dataframe(self, node_targets, shuffle=False):
"""
Creates a generator/sequence object for training or evaluation
with the supplied node ids and numeric targets.
Args:
node_targets: a Pandas DataFrame of numeric targets indexed
by the node ID for that target.
shuffle (bool): If True the node_ids will be shuffled at each
epoch, if False the node_ids will be processed in order.
Returns:
A NodeSequence object to use with with StellarGraph models
in Keras methods ``fit_generator``, ``evaluate_generator``,
and ``predict_generator``
"""
return self.flow(node_targets.index, node_targets.values, shuffle=shuffle)
class GraphSAGENodeGenerator(BatchedNodeGenerator):
"""
A data generator for node prediction with Homogeneous GraphSAGE models
At minimum, supply the StellarGraph, the batch size, and the number of
node samples for each layer of the GraphSAGE model.
The supplied graph should be a StellarGraph object that is ready for
machine learning. Currently the model requires node features for all
nodes in the graph.
Use the :meth:`flow` method supplying the nodes and (optionally) targets
to get an object that can be used as a Keras data generator.
Example::
G_generator = GraphSAGENodeGenerator(G, 50, [10,10])
train_data_gen = G_generator.flow(train_node_ids, train_node_labels)
test_data_gen = G_generator.flow(test_node_ids)
Args:
G (StellarGraph): The machine-learning ready graph.
batch_size (int): Size of batch to return.
num_samples (list): The number of samples per layer (hop) to take.
seed (int): [Optional] Random seed for the node sampler.
"""
def __init__(self, G, batch_size, num_samples, seed=None, name=None):
super().__init__(G, batch_size)
self.num_samples = num_samples
self.head_node_types = self.schema.node_types
self.name = name
# Check that there is only a single node type for GraphSAGE
if len(self.head_node_types) > 1:
warnings.warn(
"running homogeneous GraphSAGE on a graph with multiple node types",
RuntimeWarning,
)
# Create sampler for GraphSAGE
self._samplers = SeededPerBatch(
lambda s: SampledBreadthFirstWalk(G, graph_schema=self.schema, seed=s),
seed=seed,
)
def sample_features(self, head_nodes, batch_num):
"""
Sample neighbours recursively from the head nodes, collect the features of the
sampled nodes, and return these as a list of feature arrays for the GraphSAGE
algorithm.
Args:
head_nodes: An iterable of head nodes to perform sampling on.
batch_num (int): Batch number
Returns:
A list of the same length as ``num_samples`` of collected features from
the sampled nodes of shape:
``(len(head_nodes), num_sampled_at_layer, feature_size)``
where num_sampled_at_layer is the cumulative product of `num_samples`
for that layer.
"""
node_samples = self._samplers[batch_num].run(
nodes=head_nodes, n=1, n_size=self.num_samples
)
# The number of samples for each head node (not including itself)
num_full_samples = np.sum(np.cumprod(self.num_samples))
# Reshape node samples to sensible format
def get_levels(loc, lsize, samples_per_hop, walks):
end_loc = loc + lsize
walks_at_level = list(it.chain(*[w[loc:end_loc] for w in walks]))
if len(samples_per_hop) < 1:
return [walks_at_level]
return [walks_at_level] + get_levels(
end_loc, lsize * samples_per_hop[0], samples_per_hop[1:], walks
)
nodes_per_hop = get_levels(0, 1, self.num_samples, node_samples)
node_type = self.head_node_types[0]
# Get features for sampled nodes
batch_feats = [
self.graph.node_features(layer_nodes, node_type)
for layer_nodes in nodes_per_hop
]
# Resize features to (batch_size, n_neighbours, feature_size)
batch_feats = [
np.reshape(a, (len(head_nodes), -1 if np.size(a) > 0 else 0, a.shape[1]))
for a in batch_feats
]
return batch_feats
class DirectedGraphSAGENodeGenerator(BatchedNodeGenerator):
"""
A data generator for node prediction with homogeneous GraphSAGE models
on directed graphs.
At minimum, supply the StellarDiGraph, the batch size, and the number of
node samples (separately for in-nodes and out-nodes)
for each layer of the GraphSAGE model.
The supplied graph should be a StellarDiGraph object that is ready for
machine learning. Currently the model requires node features for all
nodes in the graph.
Use the :meth:`flow` method supplying the nodes and (optionally) targets
to get an object that can be used as a Keras data generator.
Example::
G_generator = DirectedGraphSAGENodeGenerator(G, 50, [10,5], [5,1])
train_data_gen = G_generator.flow(train_node_ids, train_node_labels)
test_data_gen = G_generator.flow(test_node_ids)
Args:
G (StellarDiGraph): The machine-learning ready graph.
batch_size (int): Size of batch to return.
in_samples (list): The number of in-node samples per layer (hop) to take.
out_samples (list): The number of out-node samples per layer (hop) to take.
seed (int): [Optional] Random seed for the node sampler.
"""
def __init__(self, G, batch_size, in_samples, out_samples, seed=None, name=None):
super().__init__(G, batch_size)
# TODO Add checks for in- and out-nodes sizes
self.in_samples = in_samples
self.out_samples = out_samples
self.head_node_types = self.schema.node_types
self.name = name
# Check that there is only a single node type for GraphSAGE
if len(self.head_node_types) > 1:
warnings.warn(
"running homogeneous GraphSAGE on a graph with multiple node types",
RuntimeWarning,
)
# Create sampler for GraphSAGE
self.sampler = DirectedBreadthFirstNeighbours(
G, graph_schema=self.schema, seed=seed
)
def sample_features(self, head_nodes, batch_num):
"""
Sample neighbours recursively from the head nodes, collect the features of the
sampled nodes, and return these as a list of feature arrays for the GraphSAGE
algorithm.
Args:
head_nodes: An iterable of head nodes to perform sampling on.
batch_num (int): Batch number
Returns:
A list of feature tensors from the sampled nodes at each layer, each of shape:
``(len(head_nodes), num_sampled_at_layer, feature_size)``
where num_sampled_at_layer is the total number (cumulative product)
of nodes sampled at the given number of hops from each head node,
given the sequence of in/out directions.
"""
node_samples = self.sampler.run(
nodes=head_nodes, n=1, in_size=self.in_samples, out_size=self.out_samples
)
# Reshape node samples to sensible format
# Each 'slot' represents the list of nodes sampled from some neighbourhood, and will have a corresponding
# NN input layer. Every hop potentially generates both in-nodes and out-nodes, held separately,
# and thus the slot (or directed hop sequence) structure forms a binary tree.
node_type = self.head_node_types[0]
max_hops = len(self.in_samples)
max_slots = 2 ** (max_hops + 1) - 1
features = [None] * max_slots # flattened binary tree
for slot in range(max_slots):
nodes_in_slot = list(it.chain(*[sample[slot] for sample in node_samples]))
features_for_slot = self.graph.node_features(nodes_in_slot, node_type)
resize = -1 if np.size(features_for_slot) > 0 else 0
features[slot] = np.reshape(
features_for_slot, (len(head_nodes), resize, features_for_slot.shape[1])
)
return features
class HinSAGENodeGenerator(BatchedNodeGenerator):
"""Keras-compatible data mapper for Heterogeneous GraphSAGE (HinSAGE)
At minimum, supply the StellarGraph, the batch size, and the number of
node samples for each layer of the HinSAGE model.
The supplied graph should be a StellarGraph object that is ready for
machine learning. Currently the model requires node features for all
nodes in the graph.
Use the :meth:`flow` method supplying the nodes and (optionally) targets
to get an object that can be used as a Keras data generator.
Note that the shuffle argument should be True for training and
False for prediction.
Args:
G (StellarGraph): The machine-learning ready graph
batch_size (int): Size of batch to return
num_samples (list): The number of samples per layer (hop) to take
head_node_type (str): The node type that will be given to the generator
using the `flow` method, the model will expect this node type.
schema (GraphSchema, optional): Graph schema for G.
seed (int, optional): Random seed for the node sampler
Example::
G_generator = HinSAGENodeGenerator(G, 50, [10,10])
train_data_gen = G_generator.flow(train_node_ids, train_node_labels)
test_data_gen = G_generator.flow(test_node_ids)
"""
def __init__(
self,
G,
batch_size,
num_samples,
head_node_type,
schema=None,
seed=None,
name=None,
):
super().__init__(G, batch_size, schema=schema)
self.num_samples = num_samples
self.name = name
# The head node type
if head_node_type not in self.schema.node_types:
raise KeyError("Supplied head node type must exist in the graph")
self.head_node_types = [head_node_type]
# Create sampling schema
self._sampling_schema = self.schema.sampling_layout(
self.head_node_types, self.num_samples
)
self._type_adjacency_list = self.schema.type_adjacency_list(
self.head_node_types, len(self.num_samples)
)
# Create sampler for HinSAGE
self.sampler = SampledHeterogeneousBreadthFirstWalk(
G, graph_schema=self.schema, seed=seed
)
def sample_features(self, head_nodes, batch_num):
"""
Sample neighbours recursively from the head nodes, collect the features of the
sampled nodes, and return these as a list of feature arrays for the GraphSAGE
algorithm.
Args:
head_nodes: An iterable of head nodes to perform sampling on.
batch_num (int): Batch number
Returns:
A list of the same length as ``num_samples`` of collected features from
the sampled nodes of shape:
``(len(head_nodes), num_sampled_at_layer, feature_size)``
where num_sampled_at_layer is the cumulative product of `num_samples`
for that layer.
"""
# Get sampled nodes
node_samples = self.sampler.run(nodes=head_nodes, n=1, n_size=self.num_samples)
# Reshape node samples to the required format for the HinSAGE model
# This requires grouping the sampled nodes by edge type and in order
nodes_by_type = [
(
nt,
reduce(
operator.concat,
(samples[ks] for samples in node_samples for ks in indices),
[],
),
)
for nt, indices in self._sampling_schema[0]
]
# Get features
batch_feats = [
self.graph.node_features(layer_nodes, nt)
for nt, layer_nodes in nodes_by_type
]
# Resize features to (batch_size, n_neighbours, feature_size)
batch_feats = [
np.reshape(a, (len(head_nodes), -1 if np.size(a) > 0 else 0, a.shape[1]))
for a in batch_feats
]
return batch_feats
class Attri2VecNodeGenerator(BatchedNodeGenerator):
"""
A node feature generator for node representation prediction with the
attri2vec model.
At minimum, supply the StellarGraph and the batch size.
The supplied graph should be a StellarGraph object that is ready for
machine learning. Currently the model requires node features for all
nodes in the graph.
Use the :meth:`flow` method supplying the nodes to get an object
that can be used as a Keras data generator.
Example::
G_generator = Attri2VecNodeGenerator(G, 50)
data_gen = G_generator.flow(node_ids)
Args:
G (StellarGraph): The machine-learning ready graph.
batch_size (int): Size of batch to return.
name (str or None): Name of the generator (optional).
"""
def __init__(self, G, batch_size, name=None):
super().__init__(G, batch_size)
self.name = name
def sample_features(self, head_nodes, batch_num):
"""
Sample content features of the head nodes, and return these as a list of feature
arrays for the attri2vec algorithm.
Args:
head_nodes: An iterable of head nodes to perform sampling on.
batch_num (int): Batch number
Returns:
A list of feature arrays, with each element being the feature of a
head node.
"""
batch_feats = self.graph.node_features(head_nodes)
return batch_feats
def flow(self, node_ids):
"""
Creates a generator/sequence object for node representation prediction
with the supplied node ids.
The node IDs are the nodes to inference on: the embeddings
calculated for these nodes are passed to the downstream task. These
are a subset/all of the nodes in the graph.
Args:
node_ids: an iterable of node IDs.
Returns:
A NodeSequence object to use with the Attri2Vec model
in the Keras method ``predict_generator``.
"""
return NodeSequence(
self.sample_features, self.batch_size, node_ids, shuffle=False
)
def flow_from_dataframe(self, node_ids):
"""
Creates a generator/sequence object for node representation prediction
with the supplied node ids.
Args:
node_ids: a Pandas DataFrame of node_ids.
Returns:
A NodeSequence object to use with the Attri2Vec model
in the Keras method ``predict_generator``.
"""
return NodeSequence(self.sample_features, self.batch_size, node_ids.index)