Exemple #1
0
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self.conv1 = GCNConv(32,
                          activation='elu',
                          kernel_regularizer=l2(l2_reg))
     self.conv2 = GCNConv(32,
                          activation='elu',
                          kernel_regularizer=l2(l2_reg))
     self.flatten = Flatten()
     self.fc1 = Dense(512, activation='relu')
     self.fc2 = Dense(10, activation='softmax')  # MNIST has 10 classes
 def __init__(self, **kwargs):
     super().__init__(**kwargs)
     self.conv1 = GCNConv(32,
                          activation="elu",
                          kernel_regularizer=l2(l2_reg))
     self.conv2 = GCNConv(32,
                          activation="elu",
                          kernel_regularizer=l2(l2_reg))
     self.flatten = GlobalSumPool()
     self.fc1 = Dense(512, activation="relu")
     self.fc2 = Dense(10, activation="softmax")  # MNIST has 10 classes
Exemple #3
0
def GCN_net():
    I1 = Input(shape=(no_agents, no_features), name="gcn_input")
    Adj = Input(shape=(no_agents, no_agents), name="adj")
    gcn = GCNConv(arglist.no_neurons,
                  kernel_initializer=tf.keras.initializers.he_uniform(),
                  activation=tf.keras.layers.LeakyReLU(alpha=0.1),
                  use_bias=False,
                  name="Gcn")([I1, Adj])
    concat = tf.keras.layers.Concatenate(axis=2)([I1, gcn])
    dense = Dense(arglist.no_neurons,
                  kernel_initializer=tf.keras.initializers.he_uniform(),
                  activation=tf.keras.layers.LeakyReLU(alpha=0.1),
                  name="dense_layer")

    last_dense = Dense(no_actions,
                       kernel_initializer=tf.keras.initializers.he_uniform(),
                       name="last_dense_layer")
    split = Lambda(lambda x: tf.squeeze(
        tf.split(x, num_or_size_splits=no_agents, axis=1), axis=2))(concat)
    outputs = []
    for j in list(range(no_agents)):
        output = last_dense(dense(split[j]))
        output = tf.keras.activations.tanh(output)
        outputs.append(output)

    V = tf.stack(outputs, axis=1)
    model = Model([I1, Adj], V)
    model._name = "final_network"
    return model
    def __init__(self, no_neighbors, num_hidden_layers, units_per_layer, lr,
                 obs_n_shape, act_shape_n, act_type, wd, agent_index):
        """
        Implementation of a critic to represent the Q-Values. Basically just a fully-connected
        regression ANN.
        """
        self.num_layers = num_hidden_layers
        self.lr = lr
        self.obs_shape_n = obs_n_shape
        self.act_shape_n = act_shape_n
        self.act_type = act_type

        self.clip_norm = 0.5
        # self.optimizer = tf.keras.optimizers.Adam(lr=self.lr)
        self.optimizer = AdamW(learning_rate=lr, weight_decay=wd)
        self.no_neighbors = no_neighbors
        self.no_agents = len(self.obs_shape_n)
        self.no_features = self.obs_shape_n[0][0]
        self.no_actions = self.act_shape_n[0][0]
        # GAT
        self.k_lst = list(range(self.no_neighbors + 2))[2:]

        self.graph_input = tf.keras.layers.Input(
            (self.no_agents, self.no_features + self.no_actions),
            name="graph_input")
        self.adj = tf.keras.layers.Input(shape=(self.no_agents,
                                                self.no_agents),
                                         name="adj")

        self.gcn = GCNConv(
            units_per_layer,
            kernel_initializer=tf.keras.initializers.he_uniform(),
            activation=tf.keras.layers.LeakyReLU(alpha=0.1),
            use_bias=False)([self.graph_input, self.adj])

        self.hidden_layers = []
        for idx in range(2):
            layer = tf.keras.layers.Dense(units_per_layer, activation='relu')
            self.hidden_layers.append(layer)

        self.output_layer = tf.keras.layers.Dense(1, activation='linear')

        # Try ResNet Alternative
        # self.flatten = tf.keras.layers.Flatten()(self.gat)
        self.concat = tf.keras.layers.Concatenate(axis=2)(
            [self.graph_input, self.gcn])
        self.flatten = tf.keras.layers.Flatten()(self.concat)

        x = self.flatten
        for idx in range(2):
            x = self.hidden_layers[idx](x)
        x = self.output_layer(x)

        # connect layers
        self.model = tf.keras.Model(
            inputs=[self.graph_input, self.adj],  # list concatenation
            outputs=[x])

        # tf.keras.utils.plot_model(self.model, show_shapes=True)
        self.model.compile(self.optimizer, loss='mse')
Exemple #5
0
def model1():
    x_in = Input(shape=(F, ))
    a_in = Input((N, ), sparse=True, dtype=a_dtype)
    output = GCNConv(n_out,
                     activation="softmax",
                     kernel_regularizer=l2(l2_reg),
                     use_bias=False)([x_in, a_in])
    return x_in, a_in, output
    def __init__(self,
                 input_tensor_spec,
                 action_spec,
                 graph,
                 num_actions=30,
                 name=None):
        super(GATNetwork, self).__init__(input_tensor_spec=input_tensor_spec,
                                         state_spec=(),
                                         name=name)
        #features = pd.DataFrame(
        #	{'cluster': [i // 30 for i in graph.nodes], 'controller': [0 for i in graph.nodes]}, index=graph.nodes)
        #sg_graph = sg.StellarGraph.from_networkx(graph, node_features=features)

        #print(sg_graph.info())
        #generator = FullBatchNodeGenerator(sg_graph, 'gat')
        #print(generator.Aadj)
        #self.tf_clusters = tf.constant(generator.features[:, 0][np.newaxis, ...], dtype=tf.int32)
        #self.net = GAT(
        #	layer_sizes=[64, 30],
        #	activations=['relu', 'softmax'],
        #	attn_heads=8,
        #	generator=generator,
        #	in_dropout=0.5,
        #	attn_dropout=0.5,
        #	normalize=None
        #)
        #self.inp, self.out = self.net.in_out_tensors()
        self.graph_conv_1 = GCNConv(64)
        #self.graph_conv_2 = GCNConv(64)
        self.graph_conv_3 = GCNConv(1)
        #self.graph_conv_1.build(((1, 180, 1), (1, 180, 180)))
        #self.dropout_1 = tf.keras.layers.Dropout(0.5)
        self.dense_1 = dense(180)
        #self.graph_gat_1 = GATConv(16, 6)
        #self.graph_gat_2 = GATConv(1)
        adj_matrix = nx.to_numpy_array(graph)
        idn_matrix = np.identity(adj_matrix.shape[0])
        adj_loop_matrix = adj_matrix + idn_matrix
        self.adjacency_matrix = adj_loop_matrix[np.newaxis, ...]
        self.gcn_adj = gcn_filter(self.adjacency_matrix)
        self.tf_adj = tf.constant(self.adjacency_matrix)
        self.tf_gcn_adj = tf.constant(self.gcn_adj)
        self.flatten = tf.keras.layers.Flatten()
Exemple #7
0
    def get_neighborhood_encoder(self):
        app_input = self.appearance_encoder.input
        morph_input = self.morphology_encoder.input
        centroid_input = self.centroid_encoder.input

        adj_input = Input(shape=(None, None), name='encoder_adj_input')

        app_features = self.appearance_encoder.output
        morph_features = self.morphology_encoder.output
        centroid_features = self.centroid_encoder.output

        adj = adj_input

        # Concatenate features
        node_features = Concatenate(axis=-1)(
            [app_features, morph_features, centroid_features])
        node_features = Dense(self.n_filters, name='dense_ne0')(node_features)
        node_features = BatchNormalization(axis=-1,
                                           name='bn_ne0')(node_features)
        node_features = Activation('relu', name='relu_ne0')(node_features)

        # Apply graph convolution
        for i in range(self.n_layers):
            name = '{}{}'.format(self.graph_layer, i)
            if self.graph_layer == 'gcn':
                graph_layer = GCNConv(self.n_filters,
                                      activation=None,
                                      name=name)
            elif self.graph_layer == 'gcs':
                graph_layer = GCSConv(self.n_filters,
                                      activation=None,
                                      name=name)
            else:
                raise ValueError('Unexpected graph_layer: {}'.format(
                    self.graph_layer))

            node_features = graph_layer([node_features, adj])
            node_features = BatchNormalization(
                axis=-1, name='bn_ne{}'.format(i + 1))(node_features)
            node_features = Activation(
                'relu', name='relu_ne{}'.format(i + 1))(node_features)

        concat = Concatenate(axis=-1)(
            [app_features, morph_features, node_features])
        node_features = Dense(self.embedding_dim, name='dense_nef')(concat)
        node_features = BatchNormalization(axis=-1,
                                           name='bn_nef')(node_features)
        node_features = Activation('relu', name='relu_nef')(node_features)

        inputs = [app_input, morph_input, centroid_input, adj_input]
        outputs = [node_features, centroid_input]

        return Model(inputs=inputs,
                     outputs=outputs,
                     name='neighborhood_encoder')
    def get_adj(arr, k_lst, no_agents):
        """
        Take as input the new obs. In position 4 to k, there are the x and y coordinates of each agent
        Make an adjacency matrix, where each agent communicates with the k closest ones
        """
        points = [i[2:4] for i in arr]
        adj = np.zeros((no_agents, no_agents), dtype=float)
        # construct a kd-tree
        tree = cKDTree(points)
        for cnt, row in enumerate(points):
            # find k nearest neighbors for each element of data, squeezing out the zero result (the first nearest
            # neighbor is always itself)
            dd, ii = tree.query(row, k=k_lst)
            # apply an index filter on data to get the nearest neighbor elements
            adj[cnt][ii] = 1
            # adjacency[cnt, ii] = 1.0

        # add self-loops and symmetric normalization
        adj = GCNConv.preprocess(adj).astype('f4')
        return adj
Exemple #9
0
    def getdata(self):
        # Load data
        self.data
        adj = self.data.a

        # The adjacency matrix is stored as an attribute of the dataset.
        # Create filter for GCN and convert to sparse tensor.

        self.data.a = GCNConv.preprocess(self.data.a)
        self.data.a = sp_matrix_to_sp_tensor(self.data.a)

        # Train/valid/test split
        data_tr, data_te = self.data[:-10000], self.data[-10000:]
        np.random.shuffle(data_tr)
        data_tr, data_va = data_tr[:-10000], data_tr[-10000:]

        # We use a MixedLoader since the dataset is in mixed mode
        loader_tr = MixedLoader(data_tr, batch_size=batch_size, epochs=epochs)
        loader_va = MixedLoader(data_va, batch_size=batch_size)
        loader_te = MixedLoader(data_te, batch_size=batch_size)
        return adj, loader_tr, loader_va, loader_te
Exemple #10
0
# Data splits
idx = ogb_dataset.get_idx_split()
idx_tr, idx_va, idx_te = idx["train"], idx["valid"], idx["test"]
mask_tr = np.zeros(N, dtype=bool)
mask_va = np.zeros(N, dtype=bool)
mask_te = np.zeros(N, dtype=bool)
mask_tr[idx_tr] = True
mask_va[idx_va] = True
mask_te[idx_te] = True
masks = [mask_tr, mask_va, mask_te]

# Model definition
x_in = Input(shape=(F, ))
a_in = Input((N, ), sparse=True)
x_1 = GCNConv(channels, activation="relu")([x_in, a_in])
x_1 = BatchNormalization()(x_1)
x_1 = Dropout(dropout)(x_1)
x_2 = GCNConv(channels, activation="relu")([x_1, a_in])
x_2 = BatchNormalization()(x_2)
x_2 = Dropout(dropout)(x_2)
x_3 = GCNConv(n_out, activation="softmax")([x_2, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=x_3)
optimizer = Adam(lr=learning_rate)
loss_fn = SparseCategoricalCrossentropy()
model.summary()


# Training function
Exemple #11
0
# Data splits
idx = ogb_dataset.get_idx_split()
idx_tr, idx_va, idx_te = idx["train"], idx["valid"], idx["test"]
mask_tr = np.zeros(N, dtype=bool)
mask_va = np.zeros(N, dtype=bool)
mask_te = np.zeros(N, dtype=bool)
mask_tr[idx_tr] = True
mask_va[idx_va] = True
mask_te[idx_te] = True
masks = [mask_tr, mask_va, mask_te]

# Model definition
x_in = Input(shape=(F, ))
a_in = Input((N, ), sparse=True)
x_1 = GCNConv(channels, activation='relu')([x_in, a_in])
x_1 = BatchNormalization()(x_1)
x_1 = Dropout(dropout)(x_1)
x_2 = GCNConv(channels, activation='relu')([x_1, a_in])
x_2 = BatchNormalization()(x_2)
x_2 = Dropout(dropout)(x_2)
x_3 = GCNConv(n_out, activation='softmax')([x_2, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=x_3)
optimizer = Adam(lr=learning_rate)
loss_fn = SparseCategoricalCrossentropy()
model.summary()


# Training function
Exemple #12
0
#we then build a graph using NetworkX(Adjacency Matrix) usingt he obtained nodes and edges list
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edge_list)
A = nx.adjacency_matrix(G)
print('Graph info: ', nx.info(G))

#We now build and train the Graph Convolution Networks
#initializing the parameters
channels = 16 #number of channels in the first layer
dropout = 0.5 
l2_reg = 5e-4
learning_rate = 1e-2 
epochs = 200
es_patience = 10 
A = GCNConv.preprocess(A).astype('f4')

#defining the model
X_in = Input(shape = (F, ))
fltr_in = Input((N, ), sparse=True)
dropout_1= Dropout(dropout)(X_in)
graph_conv_1 = GCNConv(channels, activation='relu', 
        kernel_regularizer = l2(l2_reg), 
        use_bias=False)([dropout_1, fltr_in])
dropout_2 = Dropout(dropout)(graph_conv_1)
graph_conv_2 = GCNConv(num_classes, activation='softmax', 
        use_bias=False)([dropout_2, fltr_in])
#we then build the mode as follows:
model = Model(inputs=[X_in, fltr_in], outputs = graph_conv_2)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer, loss = 'categorical_crossentropy', weighted_metrics=['acc'])
from spektral.layers.ops import sp_matrix_to_sp_tensor

tf.config.experimental_run_functions_eagerly(True)

# Parameters
batch_size = 32  # Batch size
epochs = 1000  # Number of training epochs
patience = 10  # Patience for early stopping
l2_reg = 5e-4  # Regularization rate for l2

# Load data
data = MNIST()

# The adjacency matrix is stored as an attribute of the dataset.
# Create filter for GCN and convert to sparse tensor.
data.a = GCNConv.preprocess(data.a)
data.a = sp_matrix_to_sp_tensor(data.a)

# Train/valid/test split
data_tr, data_te = data[:-10000], data[-10000:]
np.random.shuffle(data_tr)
data_tr, data_va = data_tr[:-10000], data_tr[-10000:]

# We use a MixedLoader since the dataset is in mixed mode
loader_tr = MixedLoader(data_tr, batch_size=batch_size, epochs=epochs)
loader_va = MixedLoader(data_va, batch_size=batch_size)
loader_te = MixedLoader(data_te, batch_size=batch_size)


# Build model
class Net(Model):
Exemple #14
0
learning_rate = 1e-2  # Learning rate
epochs = 200  # Number of training epochs
patience = 10  # Patience for early stopping
a_dtype = dataset[0].a.dtype  # Only needed for TF 2.1

N = dataset.n_nodes  # Number of nodes in the graph
F = dataset.n_node_features  # Original size of node features
n_out = dataset.n_labels  # Number of classes

# Model definition
x_in = Input(shape=(F, ))
a_in = Input((N, ), sparse=True, dtype=a_dtype)

do_1 = Dropout(dropout)(x_in)
gc_1 = GCNConv(channels,
               activation='relu',
               kernel_regularizer=l2(l2_reg),
               use_bias=False)([do_1, a_in])
do_2 = Dropout(dropout)(gc_1)
gc_2 = GCNConv(n_out, activation='softmax', use_bias=False)([do_2, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=gc_2)
optimizer = Adam(lr=learning_rate)
model.compile(
    optimizer=optimizer,
    loss=CategoricalCrossentropy(reduction='sum'),  # To compute mean
    weighted_metrics=['acc'])
model.summary()

# Train model
loader_tr = SingleLoader(dataset, sample_weights=weights_tr)
from spektral.layers import GCNConv
from spektral.layers.ops import sp_matrix_to_sp_tensor

# Parameters
batch_size = 32  # Batch size
epochs = 1000  # Number of training epochs
patience = 10  # Patience for early stopping
l2_reg = 5e-4  # Regularization rate for l2

# Load data
data = MNIST()

# The adjacency matrix is stored as an attribute of the dataset.
# Create filter for GCN and convert to sparse tensor.
adj = data.a
adj = GCNConv.preprocess(adj)
adj = sp_matrix_to_sp_tensor(adj)

# Train/valid/test split
data_tr, data_te = data[:-10000], data[-10000:]
np.random.shuffle(data_tr)
data_tr, data_va = data_tr[:-10000], data_tr[-10000:]


# Build model
class Net(Model):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.conv1 = GCNConv(32,
                             activation='elu',
                             kernel_regularizer=l2(l2_reg))
Exemple #16
0
l2_reg = 5e-6  # L2 regularization rate
learning_rate = 0.2  # Learning rate
epochs = 20000  # Number of training epochs
patience = 200  # Patience for early stopping
a_dtype = dataset[0].a.dtype  # Only needed for TF 2.1

N = dataset.n_nodes  # Number of nodes in the graph
F = dataset.n_node_features  # Original size of node features
n_out = dataset.n_labels  # Number of classes

# Model definition
x_in = Input(shape=(F, ))
a_in = Input((N, ), sparse=True, dtype=a_dtype)

output = GCNConv(n_out,
                 activation="softmax",
                 kernel_regularizer=l2(l2_reg),
                 use_bias=False)([x_in, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=output)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer,
              loss="categorical_crossentropy",
              weighted_metrics=["acc"])
model.summary()

# Train model
loader_tr = SingleLoader(dataset, sample_weights=mask_tr)
loader_va = SingleLoader(dataset, sample_weights=mask_va)
model.fit(
    loader_tr.load(),
Exemple #17
0
from spektral.datasets.citation import Cora
from spektral.layers import GCNConv
from spektral.transforms import LayerPreprocess, AdjToSpTensor
from spektral.utils import tic, toc

# Load data
dataset = Cora(transforms=[LayerPreprocess(GCNConv), AdjToSpTensor()])
graph = dataset[0]
x, a, y = graph.x, graph.a, graph.y
mask_tr, mask_va, mask_te = dataset.mask_tr, dataset.mask_va, dataset.mask_te

# Define model
x_in = Input(shape=(dataset.n_node_features, ))
a_in = Input((dataset.n_nodes, ), sparse=True)
x_1 = GCNConv(16, 'relu', True, kernel_regularizer=l2(5e-4))([x_in, a_in])
x_1 = Dropout(0.5)(x_1)
x_2 = GCNConv(y.shape[1], 'softmax', True)([x_1, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=x_2)
optimizer = Adam(lr=1e-2)
loss_fn = CategoricalCrossentropy()


# Training step
@tf.function
def train():
    with tf.GradientTape() as tape:
        predictions = model([x, a], training=True)
        loss = loss_fn(y[mask_tr], predictions[mask_tr])
Exemple #18
0
idx_tr, idx_va, idx_te = np.split(idxs, [split_va, split_te])
dataset_tr = dataset[idx_tr]
dataset_va = dataset[idx_va]
dataset_te = dataset[idx_te]

loader_tr = BatchLoader(dataset_tr, batch_size=batch_size)
loader_va = BatchLoader(dataset_va, batch_size=batch_size)
loader_te = BatchLoader(dataset_te, batch_size=batch_size)

################################################################################
# BUILD MODEL
################################################################################
X_in = Input(shape=(None, F))
A_in = Input(shape=(None, None))

X_1 = GCNConv(32, activation="relu")([X_in, A_in])
X_1, A_1 = MinCutPool(N // 2)([X_1, A_in])
X_2 = GCNConv(32, activation="relu")([X_1, A_1])
X_3 = GlobalSumPool()(X_2)
output = Dense(n_out)(X_3)

# Build model
model = Model(inputs=[X_in, A_in], outputs=output)
opt = Adam(lr=learning_rate)
model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["acc"])
model.summary()

################################################################################
# FIT MODEL
################################################################################
model.fit(
n_out = dataset.n_labels  # Dimension of the target

# Train/test split
idx = ogb_dataset.get_idx_split()
idx_tr, idx_va, idx_te = idx["train"], idx["valid"], idx["test"]
dataset_tr = dataset[idx_tr]
dataset_va = dataset[idx_va]
dataset_te = dataset[idx_te]

################################################################################
# BUILD MODEL
################################################################################
X_in = Input(shape=(None, F))
A_in = Input(shape=(None, None))

X_1 = GCNConv(32, activation='relu')([X_in, A_in])
X_1, A_1 = MinCutPool(N // 2)([X_1, A_in])
X_2 = GCNConv(32, activation='relu')([X_1, A_1])
X_3 = GlobalSumPool()(X_2)
output = Dense(n_out)(X_3)

# Build model
model = Model(inputs=[X_in, A_in], outputs=output)
opt = Adam(lr=learning_rate)
model.compile(optimizer=opt, loss='mse')
model.summary()

################################################################################
# FIT MODEL
################################################################################
loader_tr = BatchLoader(dataset_tr, batch_size=batch_size)