예제 #1
0
Petar Veličković, Guillem Cucurull, Arantxa Casanova, Adriana Romero, Pietro Liò, Yoshua Bengio
"""

from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers import Input, Dropout
from keras.models import Model
from keras.optimizers import Adam
from keras.regularizers import l2

from spektral.datasets import citation
from spektral.layers import GraphAttention
from spektral.utils.misc import add_eye

# Load data
dataset = 'cora'
adj, node_features, y_train, y_val, y_test, train_mask, val_mask, test_mask = citation.load_data(
    dataset)

# Parameters
gat_channels = 8  # Output size of first GraphAttention layer
n_attn_heads = 8  # Number of attention heads in first GAT layer
N = node_features.shape[0]  # Number of nodes in the graph
F = node_features.shape[1]  # Original feature dimensionality
n_classes = y_train.shape[1]  # Number of classes
dropout_rate = 0.25  # Dropout rate applied to the input of GAT layers
l2_reg = 5e-4  # Regularization rate for l2
learning_rate = 1e-2  # Learning rate for SGD
epochs = 20000  # Number of training epochs
es_patience = 200  # Patience fot early stopping

# Preprocessing operations
adj = add_eye(adj).toarray()  # Add self-loops
예제 #2
0
    with tf.GradientTape() as tape:
        _, S_pool = model(inputs, training=True)
        loss = sum(model.losses)
    gradients = tape.gradient(loss, model.trainable_variables)
    opt.apply_gradients(zip(gradients, model.trainable_variables))
    return model.losses[0], model.losses[1], S_pool


np.random.seed(1)
epochs = 5000  # Training iterations
lr = 5e-4  # Learning rate

################################################################################
# LOAD DATASET
################################################################################
A, X, y, _, _, _ = citation.load_data('cora')
A_norm = normalized_adjacency(A)
X = X.todense()
F = X.shape[-1]
y = np.argmax(y, axis=-1)
n_clusters = y.max() + 1

################################################################################
# MODEL
################################################################################
X_in = Input(shape=(F, ), name='X_in')
A_in = Input(shape=(None, ), name='A_in', sparse=True)

X_1 = GraphConvSkip(16, activation='elu')([X_in, A_in])
X_1, A_1, S = MinCutPool(n_clusters, return_mask=True)([X_1, A_in])
예제 #3
0
Thomas N. Kipf, Max Welling
"""

from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Input, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l2

from spektral.datasets import citation
from spektral.layers import GraphConv

# Load data
dataset = 'cora'
print('load data start ...')
A, X, y, train_mask, val_mask, test_mask = citation.load_data(dataset)
print('load data finish ...')
print(X.shape, y.shape, train_mask.shape)
# Parameters
channels = 16  # Number of channels in the first layer
N = X.shape[0]  # Number of nodes in the graph
print('Number of nodes in the graph N = ', N)
F = X.shape[1]  # Original size of node features
print('Original size of node features F = ', F)
n_classes = y.shape[1]  # Number of classes
dropout = 0.5  # Dropout rate for the features
l2_reg = 5e-4 / 2  # L2 regularization rate
learning_rate = 1e-2  # Learning rate
epochs = 10  # Number of training epochs 200
es_patience = 10  # Patience for early stopping
예제 #4
0
def test_citation():
    for dataset_name in ['cora', 'citeseer', 'pubmed']:
        citation.load_data(dataset_name)
# vn_batch_length = vn_batch.service_length
# print(vn_batch_state)
# vnf_state = [[vn_batch_state[bid][aid][0] for aid in range(3)] for bid in range(batch_size)]
# print(vnf_state)
'''args_parse'''
# from config import get_config
# args, _ = get_config()
# print(args.batch_size)
'''spektral'''
from spektral.datasets import citation
from spektral.layers import GraphConv
from spektral.utils.data import numpy_to_disjoint
from tensorflow.keras.layers import Input

from spektral.datasets import citation
A, X, y, train_mask, val_mask, test_mask = citation.load_data('cora')

N = A.shape[0]
F = X.shape[-1]
n_classes = y.shape[-1]

print('A', A.shape)
print('X', X[0])
# print('y', y)
# print('N', N)

from spektral.layers import GraphConv
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dropout
from tensorflow.keras.regularizers import l2
예제 #6
0
def test_citation():
    for dataset_name in ['cora', 'citeseer', 'pubmed']:
        citation.load_data(dataset_name)
        citation.load_data(dataset_name, random_split=True)