/
models.py
142 lines (117 loc) · 4.52 KB
/
models.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
from .. import utils as U
from ..imports import *
GRAPHSAGE = "graphsage"
NODE_CLASSIFIERS = {GRAPHSAGE: "GraphSAGE: https://arxiv.org/pdf/1706.02216.pdf"}
LINK_PREDICTORS = {GRAPHSAGE: "GraphSAGE: https://arxiv.org/pdf/1706.02216.pdf"}
def print_node_classifiers():
for k, v in NODE_CLASSIFIERS.items():
print("%s: %s" % (k, v))
def print_link_predictors():
for k, v in LINK_PREDICTORS.items():
print("%s: %s" % (k, v))
def graph_node_classifier(name, train_data, layer_sizes=[32, 32], verbose=1):
"""
```
Build and return a neural node classification model.
Notes: Only mutually-exclusive class labels are supported.
Args:
name (string): one of:
- 'graphsage' for GraphSAGE model
(only GraphSAGE currently supported)
train_data (NodeSequenceWrapper): a ktrain.graph.sg_wrappers.NodeSequenceWrapper object
verbose (boolean): verbosity of output
Return:
model (Model): A Keras Model instance
```
"""
from .sg_wrappers import NodeSequenceWrapper
# check argument
if not isinstance(train_data, NodeSequenceWrapper):
err = """
train_data must be a ktrain.graph.sg_wrappers.NodeSequenceWrapper object
"""
raise Exception(err)
if len(layer_sizes) != 2:
raise ValueError("layer_sizes must be of length 2")
num_classes = U.nclasses_from_data(train_data)
# determine multilabel
multilabel = U.is_multilabel(train_data)
if multilabel:
raise ValueError(
"Multi-label classification not currently supported for graphs."
)
U.vprint("Is Multi-Label? %s" % (multilabel), verbose=verbose)
# set loss and activations
loss_func = "categorical_crossentropy"
activation = "softmax"
# import stellargraph
try:
import stellargraph as sg
from stellargraph.layer import GraphSAGE
except:
raise Exception(SG_ERRMSG)
if version.parse(sg.__version__) < version.parse("0.8"):
raise Exception(SG_ERRMSG)
# build a GraphSAGE node classification model
graphsage_model = GraphSAGE(
layer_sizes=layer_sizes,
generator=train_data,
bias=True,
dropout=0.5,
)
# x_inp, x_out = graphsage_model.default_model(flatten_output=True)
x_inp, x_out = graphsage_model.build()
prediction = keras.layers.Dense(units=num_classes, activation=activation)(x_out)
model = keras.Model(inputs=x_inp, outputs=prediction)
model.compile(optimizer=U.DEFAULT_OPT, loss=loss_func, metrics=["accuracy"])
U.vprint("done", verbose=verbose)
return model
def graph_link_predictor(name, train_data, preproc, layer_sizes=[20, 20], verbose=1):
"""
```
Build and return a neural link prediction model.
Args:
name (string): one of:
- 'graphsage' for GraphSAGE model
(only GraphSAGE currently supported)
train_data (LinkSequenceWrapper): a ktrain.graph.sg_wrappers.LinkSequenceWrapper object
preproc(LinkPreprocessor): a LinkPreprocessor instance
verbose (boolean): verbosity of output
Return:
model (Model): A Keras Model instance
```
"""
from .sg_wrappers import LinkSequenceWrapper
# check argument
if not isinstance(train_data, LinkSequenceWrapper):
err = """
train_data must be a ktrain.graph.sg_wrappers.LinkSequenceWrapper object
"""
raise Exception(err)
if len(layer_sizes) != len(preproc.sample_sizes):
raise ValueError("number of layer_sizes must match len(preproc.sample_sizes)")
num_classes = U.nclasses_from_data(train_data)
# set loss and activations
loss_func = "categorical_crossentropy"
activation = "softmax"
# import stellargraph
try:
import stellargraph as sg
from stellargraph.layer import GraphSAGE, link_classification
except:
raise Exception(SG_ERRMSG)
if version.parse(sg.__version__) < version.parse("0.8"):
raise Exception(SG_ERRMSG)
# build a GraphSAGE link prediction model
graphsage = GraphSAGE(
layer_sizes=layer_sizes, generator=train_data, bias=True, dropout=0.3
)
x_inp, x_out = graphsage.build()
prediction = link_classification(
output_dim=1, output_act="relu", edge_embedding_method="ip"
)(x_out)
model = keras.Model(inputs=x_inp, outputs=prediction)
model.compile(
optimizer=U.DEFAULT_OPT, loss="binary_crossentropy", metrics=["accuracy"]
)
return model