-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_citation_cheb.py
128 lines (93 loc) · 3.36 KB
/
example_citation_cheb.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
import logging
from tensorflow.keras.callbacks import EarlyStopping # noqa
from tensorflow.keras.layers import Dropout, Input # noqa
from tensorflow.keras.losses import CategoricalCrossentropy # noqa
from tensorflow.keras.models import Model # noqa
from tensorflow.keras.optimizers import Adam # noqa
from tensorflow.keras.regularizers import l2 # noqa
from gns.config.settings import settings_fabric
from gns.dataset.citation import citation_fabric
from gns.layer.cheb import ChebyshevConvolutionalLayer
from gns.loaders.single_loader import single_loader_fabric
from gns.transformation.layer_process import layer_process_fabric
from gns.utils.mask_to_weights import mask_to_simple_weights
settings = settings_fabric()
logger = logging.getLogger(__name__)
logger.info(
"Test example for the Cora dataset for the Chebyshev Convolutional layer (analysis of the citation graph of social network messages)."
)
logger.info("We will use convolutional layer")
logger.info("Download dataset...")
dataset = citation_fabric(
"cora", transforms=[layer_process_fabric(ChebyshevConvolutionalLayer)]
)
logger.info("Calculate weights...")
weights_tr, weights_va, weights_te = (
mask_to_simple_weights(mask)
for mask in (dataset.mask_tr, dataset.mask_va, dataset.mask_te)
)
logger.info("Define parameters...")
# Number of channels for the first layer
channels = 16
# The maximum power of the Chebyshev polynomial
K = 2
# Dropout percentage for functions
dropout = 0.5
# Regularization parameter
l2_reg = 2.5e-4
# Traning ration
learning_rate = 1e-2
# Training epoches
epochs = 200
# Limit for early stopping of training
patience = 10
logger.info("Определим модель...")
# The number of graph nodes
N = dataset.n_nodes
# The original size of the node features
F = dataset.n_node_features
# Labels quantity
n_out = dataset.n_labels
a_dtype = dataset[0].a.dtype
x_in = Input(shape=(F,))
a_in = Input((N,), sparse=True, dtype=a_dtype)
logger.info("Let's define the layers of a graph neural network...")
do_1 = Dropout(dropout)(x_in)
gc_1 = ChebyshevConvolutionalLayer( # noqa
channels,
K=K,
activation=settings.activations.relu,
kernel_regularizer=l2(l2_reg),
use_bias=False,
)([do_1, a_in])
do_2 = Dropout(dropout)(gc_1)
gc_2 = ChebyshevConvolutionalLayer( # noqa
n_out, K=K, activation=settings.activations.softmax, use_bias=False
)([do_2, a_in])
logger.info("Build model...")
model = Model(inputs=[x_in, a_in], outputs=gc_2)
model.compile(
optimizer=Adam(lr=learning_rate),
loss=CategoricalCrossentropy(
reduction=settings.aggregation_methods.sum
), # To calculate the average
weighted_metrics=["acc"],
)
model.summary()
logger.info("Train model...")
loader_tr = single_loader_fabric(dataset, sample_weights=weights_tr)
loader_va = single_loader_fabric(dataset, sample_weights=weights_va)
model.fit(
loader_tr.load(),
steps_per_epoch=loader_tr.steps_per_epoch,
validation_data=loader_va.load(),
validation_steps=loader_va.steps_per_epoch,
epochs=epochs,
callbacks=[EarlyStopping(patience=patience, restore_best_weights=True)],
)
# Run model
logger.info("Run model...")
loader_te = single_loader_fabric(dataset, sample_weights=weights_te)
eval_results = model.evaluate(loader_te.load(), steps=loader_te.steps_per_epoch)
logger.info("Completed...")
logger.info("Loss: {}\n" "Test accuracy: {}".format(*eval_results))