Пример #1
0
 def __init__(self, n_out=4):
     super().__init__()
     # Define layers of the model
     self.att1 = GATConv(hidden_states,
                         attn_heads=2,
                         dropout_rate=0.4,
                         activation="relu",
                         return_attn_coef=False
                         )  #required keywords is channels/hidden states
     self.att2 = GATConv(
         hidden_states // 2,
         attn_heads=3,
         dropout_rate=0.1,
         activation="relu"
     )  # attn heads are the time limiting key_word, watch out with it
     self.att3 = GATConv(
         hidden_states * 2,
         attn_heads=4,
         dropout_rate=0.7,
         activation="relu")  # hiddenstates has to be pretty low as well
     self.Pool1 = GlobalAvgPool()  #good results with all three
     self.Pool2 = GlobalSumPool()
     self.Pool3 = GlobalMaxPool()  #important for angle fitting
     self.decode = [Dense(size * hidden_states) for size in [16, 8, 4]]
     self.norm_layers = [
         BatchNormalization() for i in range(len(self.decode))
     ]
     self.d2 = Dense(n_out)
Пример #2
0
def graph_net(arglist):
    I1 = Input(shape=(no_agents, no_features), name="graph_input")
    Adj = Input(shape=(no_agents, no_agents), name="adj")
    gat = GATConv(
        arglist.no_neurons,
        activation='relu',
        attn_heads=4,
        concat_heads=True,
    )([I1, Adj])
    concat = tf.keras.layers.Concatenate(axis=2)([I1, gat])

    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"

    tf.keras.utils.plot_model(model, show_shapes=True)
    return model
Пример #3
0
def graph_net(arglist):
    I1 = Input(shape=(no_agents, no_features), name="gcn_input")
    Adj = Input(shape=(no_agents, no_agents), name="adj")
    gat = GATConv(
        arglist.no_neurons,
        activation='relu',
        attn_heads=4,
        concat_heads=True,
    )([I1, Adj])
    concat = tf.keras.layers.Concatenate(axis=2)([I1, gat])

    outputs = []
    dense = Dense(arglist.no_neurons,
                  kernel_initializer=tf.keras.initializers.he_uniform(),
                  activation=tf.keras.layers.LeakyReLU(alpha=0.1),
                  name="dense_layer")

    dense2 = Dense(arglist.no_neurons / 2,
                   kernel_initializer=tf.keras.initializers.he_uniform(),
                   activation=tf.keras.layers.LeakyReLU(alpha=0.1),
                   name="sec_dense_layer")

    state_value = Dense(1,
                        kernel_initializer='he_uniform',
                        name="value_output")
    state_value_lambda = Lambda(lambda s: K.expand_dims(s[:, 0], -1),
                                output_shape=(no_actions, ))

    action_advantage = Dense(no_actions,
                             name="advantage_output",
                             kernel_initializer='he_uniform')
    action_advantage_lambda = Lambda(
        lambda a: a[:, :] - K.mean(a[:, :], keepdims=True),
        output_shape=(no_actions, ))

    split = Lambda(lambda x: tf.squeeze(
        tf.split(x, num_or_size_splits=no_agents, axis=1), axis=2))(concat)
    for j in list(range(no_agents)):
        V = dense(split[j])
        V2 = dense2(V)
        if arglist.dueling:
            state_value_dense = state_value(V2)
            state_value_n = state_value_lambda(state_value_dense)
            action_adj_dense = action_advantage(V2)
            action_adj_n = action_advantage_lambda(action_adj_dense)
            output = Add()([state_value_n, action_adj_n])
            output = tf.keras.activations.tanh(output)
            outputs.append(output)
        else:
            outputs.append(V2)

    V = tf.stack(outputs, axis=1)
    model = Model([I1, Adj], V)
    model._name = "final_network"
    tf.keras.utils.plot_model(model, show_shapes=True)
    return model
    def __init__(self, no_neighbors, num_hidden_layers, units_per_layer, lr,
                 obs_n_shape, act_shape_n, act_type, 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.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")
        # (2, (None, 15))
        self.gat = GATConv(
            units_per_layer,
            activation='elu',
            attn_heads=2,
            concat_heads=True,
        )([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')
        self.flatten = tf.keras.layers.Flatten()(self.gat)
        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')
Пример #5
0
def build_model(N, F):
    n_out = 1

    # Model definition
    x_in = Input(shape=(F, ))
    a_in = Input((N, ), sparse=True)
    e_in = Input(shape=(1, ))

    do_1 = Dropout(dropout)(x_in)
    gc_1 = GATConv(
        channels,
        attn_heads=n_attn_heads,
        concat_heads=True,
        dropout_rate=dropout,
        activation="elu",
        kernel_regularizer=l2(l2_reg),
        attn_kernel_regularizer=l2(l2_reg),
        bias_regularizer=l2(l2_reg),
    )([do_1, a_in])
    do_2 = Dropout(dropout)(gc_1)
    gc_2 = GATConv(
        n_out,
        attn_heads=1,
        concat_heads=False,
        dropout_rate=dropout,
        activation="sigmoid",
        kernel_regularizer=l2(l2_reg),
        attn_kernel_regularizer=l2(l2_reg),
        bias_regularizer=l2(l2_reg),
    )([do_2, a_in])

    # Build model
    model = Model(inputs=[x_in, a_in, e_in], outputs=gc_2)
    optimizer = Adam(lr=learning_rate)
    model.compile(
        optimizer=optimizer,
        loss=BinaryCrossentropy(reduction="sum"),
        weighted_metrics=["acc"],
        metrics=METRICS,
    )
    logging.info("Model summary: %s", model.summary())
    return model
Пример #6
0
    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 = GATConv.preprocess(adj).astype('f4')
        return adj
Пример #7
0
               transforms=[LayerPreprocess(GATConv),
                           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

l2_reg = 2.5e-4
# Define model
x_in = Input(shape=(dataset.n_node_features, ))
a_in = Input(shape=(None, ), sparse=True)
x_1 = Dropout(0.6)(x_in)
x_1 = GATConv(
    8,
    attn_heads=8,
    concat_heads=True,
    dropout_rate=0.6,
    activation="elu",
    kernel_regularizer=l2(l2_reg),
    attn_kernel_regularizer=l2(l2_reg),
    bias_regularizer=l2(l2_reg),
)([x_1, a_in])
x_2 = Dropout(0.6)(x_1)
x_2 = GATConv(
    dataset.n_labels,
    attn_heads=1,
    concat_heads=False,
    dropout_rate=0.6,
    activation="softmax",
    kernel_regularizer=l2(l2_reg),
    attn_kernel_regularizer=l2(l2_reg),
    bias_regularizer=l2(l2_reg),
)([x_2, a_in])
Пример #8
0
patience = 100  # Patience for early stopping

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)

do_1 = Dropout(dropout)(x_in)
gc_1 = GATConv(
    channels,
    attn_heads=n_attn_heads,
    concat_heads=True,
    dropout_rate=dropout,
    activation="elu",
    kernel_regularizer=l2(l2_reg),
    attn_kernel_regularizer=l2(l2_reg),
    bias_regularizer=l2(l2_reg),
)([do_1, a_in])
do_2 = Dropout(dropout)(gc_1)
gc_2 = GATConv(
    n_out,
    attn_heads=1,
    concat_heads=False,
    dropout_rate=dropout,
    activation="softmax",
    kernel_regularizer=l2(l2_reg),
    attn_kernel_regularizer=l2(l2_reg),
    bias_regularizer=l2(l2_reg),
)([do_2, a_in])
Пример #9
0
from spektral.utils import tic, toc

# Load data
dataset = Cora(transforms=[LayerPreprocess(GATConv), 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(shape=(None, ), sparse=True)
x_1 = Dropout(0.6)(x_in)
x_1 = GATConv(8,
              attn_heads=8,
              concat_heads=True,
              dropout_rate=0.6,
              activation='elu',
              kernel_regularizer=l2(5e-4),
              attn_kernel_regularizer=l2(5e-4),
              bias_regularizer=l2(5e-4))([x_1, a_in])
x_2 = Dropout(0.6)(x_1)
x_2 = GATConv(dataset.n_labels,
              attn_heads=1,
              concat_heads=True,
              dropout_rate=0.6,
              activation='softmax',
              kernel_regularizer=l2(5e-4),
              attn_kernel_regularizer=l2(5e-4),
              bias_regularizer=l2(5e-4))([x_2, a_in])

# Build model
model = Model(inputs=[x_in, a_in], outputs=x_2)