def gen_SpektralGNN_emb(n_classes, n_components, max_n_nodes, n_attributes): learning_rate = 0.001 l2_reg = 5e-4 X_in = Input(shape=(max_n_nodes, n_attributes)) filter_in = Input((max_n_nodes, max_n_nodes)) emb_GNN = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))([X_in, filter_in]) emb_GNN = GraphAttention(n_components, activation='relu', kernel_regularizer=l2(l2_reg))( [emb_GNN, filter_in]) emb_GNN = GlobalAttentionPool(n_components)(emb_GNN) cla_GNN = Dense(n_classes, activation='softmax')(emb_GNN) optimizer = Adam(lr=learning_rate) classificator = Model(inputs=[X_in, filter_in], outputs=cla_GNN) embedder = Model(inputs=[X_in, filter_in], outputs=emb_GNN) classificator.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) return (classificator, embedder)
def get_graph_model(): base_inputs = get_base_inputs() adjacency_matrix_input = Input(shape=(None, None), name='adjacency_matrix') inputs = base_inputs + [adjacency_matrix_input] stacked_base_inputs = Concatenate(axis=2, name='input_stacking_layer')(base_inputs) # ACTUAL MODEL # Stack inputs # Embedding vectors_sequence = TimeDistributed(Dense(128), name='vectors_expander')(stacked_base_inputs) # Graph block graph_layer_1 = GraphAttention(512, activation='relu') x = graph_layer_1([vectors_sequence, adjacency_matrix_input]) graph_layer_2 = GraphAttention(512, activation='relu') x = graph_layer_2([x, adjacency_matrix_input]) graph_layer_3 = GraphAttention(256, activation='relu') x = graph_layer_3([x, adjacency_matrix_input]) graph_layer_4 = GraphAttention(256, activation='relu') graph_output = graph_layer_4([x, adjacency_matrix_input]) # Encoder decoder block # Encoder encoder_LSTM = LSTM(256, return_state=True, name='encoder_LSTM') _, state_h, state_c = encoder_LSTM(vectors_sequence) # Decoder decoder_LSTM = LSTM(256, name='decoder_LSTM', return_sequences=True) decoder_outputs = decoder_LSTM(vectors_sequence, initial_state=[state_h, state_c]) # Dense layers x = TimeDistributed(Dense(256, activation='relu', name='dense_1'))(decoder_outputs) enc_dec_output = TimeDistributed(Dense(256, activation='relu', name='dense_2'))(x) # Branch merging concat_outputs = Concatenate(axis=2, name='branch_merger')([graph_output, enc_dec_output]) reactivity_pred = TimeDistributed(Dense(1), name='reactivity')(concat_outputs) deg_Mg_pH10_pred = TimeDistributed(Dense(1), name='deg_Mg_pH10')(concat_outputs) deg_Mg_50C_pred = TimeDistributed(Dense(1), name='deg_Mg_50C')(concat_outputs) # Outputs scored_outputs = [reactivity_pred, deg_Mg_pH10_pred, deg_Mg_50C_pred] stacked_outputs = Concatenate(axis=2, name='stacked_outputs')(scored_outputs) # TESTING MODEL testing_model = Model(inputs=inputs, outputs={'stacked_scored_labels': stacked_outputs}, name='testing_model') # TRAINING MODEL trimmed_stacked_outputs = Lambda(lambda x: x[:, :TRAINING_SCORED_SEQ_LEN], name='trimming_layer')(stacked_outputs) training_model = Model(inputs=inputs, outputs={'stacked_scored_labels': trimmed_stacked_outputs}, name='training_model') return training_model, testing_model
def generate_GNN(max_n_nodes, n_attributes, n_classes, batch_size=32, validation_split=0.1, epochs=100, verbose=0, plot=False): learning_rate = 0.001 l2_reg = 5e-4 ##### DEFINISCI MODELLO ORIGINALE X_in_1_1 = Input(shape=(max_n_nodes, n_attributes)) filter_in_1_1 = Input((max_n_nodes, max_n_nodes)) gc1_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_1_1, filter_in_1_1]) gc2_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [gc1_1_1, filter_in_1_1]) pool_1_1 = GlobalAttentionPool(128)(gc2_1_1) output_1_1 = Dense(n_classes, activation='softmax')(pool_1_1) model_1_1 = Model(inputs=[X_in_1_1, filter_in_1_1], outputs=output_1_1) optimizer = Adam(lr=learning_rate) model_1_1.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) ##### CREA IL SECONDO MODELLO X_in_1_2 = Input(shape=(max_n_nodes, n_attributes)) filter_in_1_2 = Input((max_n_nodes, max_n_nodes)) gc1_1_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_1_2, filter_in_1_2]) gc2_1_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [gc1_1_2, filter_in_1_2]) pool_1_2 = GlobalAttentionPool(128)(gc2_1_2) model_1_2 = Model(inputs=[X_in_1_2, filter_in_1_2], outputs=pool_1_2) model_1_2.compile(optimizer=Adam(lr=learning_rate), loss='categorical_crossentropy', metrics=['acc']) my_GNN_1 = Transformer_GNN(original_model=model_1_1, new_model=model_1_2, batch_size=batch_size, validation_split=validation_split, epochs=epochs, verbose=verbose, plot=plot) return (my_GNN_1)
def Model_treeGAT_softmax_1(node_count, wordvocabsize, w2v_k, word_W, l2_reg=5e-4): X_word_in = Input(shape=(node_count, ), dtype='int32') fltr_in = Input(shape=(node_count, node_count), dtype='float32') # fltr_in1 = Input(tensor=sparse_tensor_to_dense(sp_matrix_to_sp_tensor(fltr))) word_embedding_layer = Embedding(input_dim=wordvocabsize + 1, output_dim=w2v_k, input_length=node_count, mask_zero=True, trainable=True, weights=[word_W]) word_embedding_x = word_embedding_layer(X_word_in) word_embedding_x = Dropout(0.25)(word_embedding_x) graph_conv_1 = GraphAttention(200, attn_heads=3, activation='relu', kernel_regularizer=l2(l2_reg), dropout_rate=0.5, use_bias=True)([word_embedding_x, fltr_in]) graph_conv_1 = Dropout(0.5)(graph_conv_1) graph_conv_2 = GraphAttention(200, attn_heads=3, activation='relu', kernel_regularizer=l2(l2_reg), dropout_rate=0.5, use_bias=True)([graph_conv_1, fltr_in]) graph_conv_2 = Dropout(0.5)(graph_conv_2) feature_node0 = Lambda(lambda x: x[:, 0])(graph_conv_2) pool = GlobalAttentionPool(200)(graph_conv_2) flatten = Flatten()(graph_conv_2) flatten = Dense(512, activation='relu')(flatten) fc = Dropout(0.5)(flatten) # LSTM_backward = LSTM(200, activation='tanh', return_sequences=False, # go_backwards=True, dropout=0.5)(dropout_2) # present_node0 = concatenate([feature_node0, LSTM_backward], axis=-1) class_output = Dense(120)(fc) class_output = Activation('softmax', name='CLASS')(class_output) # Build model model = Model(inputs=[X_word_in, fltr_in], outputs=class_output) optimizer = Adam(lr=0.001) model.compile(optimizer=optimizer, loss='categorical_crossentropy', weighted_metrics=['acc']) return model
def gen_small(max_n_nodes, n_attributes, n_components): n_classes = 2 learning_rate = 0.001 l2_reg = 5e-4 ##### DEFINISCI MODELLO ORIGINALE X_in_1_1 = Input(shape=(max_n_nodes, n_attributes)) filter_in_1_1 = Input((max_n_nodes, max_n_nodes)) gc1_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_1_1, filter_in_1_1]) gc2_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [gc1_1_1, filter_in_1_1]) pool_1_1 = GlobalAttentionPool(128)(gc2_1_1) dense_x = Dense(n_components)(pool_1_1) output_1_1 = Dense(n_classes, activation='softmax')(dense_x) model_1_1 = Model(inputs=[X_in_1_1, filter_in_1_1], outputs=output_1_1) optimizer = Adam(lr=learning_rate) model_1_1.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) ##### CREA IL SECONDO MODELLO X_in_1_2 = Input(shape=(max_n_nodes, n_attributes)) filter_in_1_2 = Input((max_n_nodes, max_n_nodes)) gc1_1_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_1_2, filter_in_1_2]) gc2_1_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [gc1_1_2, filter_in_1_2]) pool_1_2 = GlobalAttentionPool(128)(gc2_1_2) dense2_x = Dense(n_components)(pool_1_2) model_1_2 = Model(inputs=[X_in_1_2, filter_in_1_2], outputs=dense2_x) model_1_2.compile(optimizer=Adam(lr=learning_rate), loss='categorical_crossentropy', metrics=['acc']) return (model_1_1, model_1_2)
total_acc = 0 n_splits = 10 skf = StratifiedKFold(n_splits=n_splits, shuffle=True) for train_idx, test_idx in skf.split(data[0], labels): x_train, y_train = [d[train_idx] for d in data], labels[train_idx] x_test, y_test = [d[test_idx] for d in data], labels[test_idx] y_train_cat = to_categorical(y_train) y_test_cat = to_categorical(y_test) gans = [] for _ in range(sub): input_a = Input((N, F)) input_b = Input((N, N)) x = GraphAttention(5)([input_a, input_b]) x = Flatten()(x) x = Model(inputs=[input_a, input_b], outputs=x) gans.append(x) combine = Concatenate()([x.output for x in gans]) reshape = Reshape((len(gans), gans[0].output_shape[1]))(combine) lstm = LSTM(32)(reshape) z = Dense(classes, activation='softmax')(lstm) model = Model(inputs=[i for x in gans for i in x.input], outputs=z) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train_cat,
def gen_dense(n_components, max_n_nodes, n_attributes): layers = [64, 32, 16, 8, 5, 3, 2] learning_rate = 0.001 l2_reg = 5e-4 n_classes = 2 # origlinale X_in_1_1 = Input(shape=(max_n_nodes, n_attributes)) filter_in_1_1 = Input((max_n_nodes, max_n_nodes)) gc1_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_1_1, filter_in_1_1]) gc2_1_1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [gc1_1_1, filter_in_1_1]) pool_1_1 = GlobalAttentionPool(128)(gc2_1_1) index = (layers.index(n_components)) + 1 input_layer = pool_1_1 for i in range(0, index): layer = add_layer(input_layer, layers[i]) input_layer = layer output_1_1 = Dense(n_classes, activation='softmax')(input_layer) model_1_1 = Model(inputs=[X_in_1_1, filter_in_1_1], outputs=output_1_1) optimizer = Adam(lr=learning_rate) model_1_1.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) # embedder X_in_2 = Input(shape=(max_n_nodes, n_attributes)) filter_in_2 = Input((max_n_nodes, max_n_nodes)) gc1_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))( [X_in_2, filter_in_2]) gc2_2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))([gc1_2, filter_in_2]) pool_2 = GlobalAttentionPool(128)(gc2_2) index = (layers.index(n_components)) + 1 input_layer = pool_2 for i in range(0, index): layer = add_layer(input_layer, layers[i]) input_layer = layer model_2 = Model(inputs=[X_in_2, filter_in_2], outputs=input_layer) optimizer = Adam(lr=learning_rate) model_2.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc']) return (model_1_1, model_2)
def att_model_distribute(num_sensors=10): # input/output = num of sensors gnn_unit = 128 sensor_matrix1 = Input(shape=(num_sensors, num_sensors)) sensor_matrix2 = Input(shape=(num_sensors, num_sensors)) sensor_matrix3 = Input(shape=(num_sensors, num_sensors)) sensor_matrix4 = Input(shape=(num_sensors, num_sensors)) #sensor_matrix3 = Input(shape=(num_sensors, num_sensors)) s_input1 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input2 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input3 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input4 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input5 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input6 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input7 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input8 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input9 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_input10 = Input(shape=(input_shape[0], input_shape[1], input_shape[2])) s_cnn = sensor_cnn(input_shape, repetitions=[2, 2, 2, 2]) extract_cnn1 = s_cnn(s_input1) extract_cnn2 = s_cnn(s_input2) extract_cnn3 = s_cnn(s_input3) extract_cnn4 = s_cnn(s_input4) extract_cnn5 = s_cnn(s_input5) extract_cnn6 = s_cnn(s_input6) extract_cnn7 = s_cnn(s_input7) extract_cnn8 = s_cnn(s_input8) extract_cnn9 = s_cnn(s_input9) extract_cnn10 = s_cnn(s_input10) extract_cnn = Concatenate(axis=1)([ extract_cnn1, extract_cnn2, extract_cnn3, extract_cnn4, extract_cnn5, extract_cnn6, extract_cnn7, extract_cnn8, extract_cnn9, extract_cnn10 ]) #extract_cnn = np.reshape(extract_cnn, (-1,)) G_h1 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([extract_cnn, sensor_matrix1]) G_h2 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([extract_cnn, sensor_matrix2]) G_h3 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([extract_cnn, sensor_matrix3]) G_h4 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([extract_cnn, sensor_matrix4]) G_1 = Concatenate(axis=-1)([G_h1, G_h2, G_h3, G_h4]) G_2h1 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_1, sensor_matrix1]) G_2h2 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_1, sensor_matrix2]) G_2h3 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_1, sensor_matrix3]) G_2h4 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_1, sensor_matrix4]) G_2 = Concatenate(axis=-1)([G_2h1, G_2h2, G_2h3, G_2h4]) #G_3h1 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_2, sensor_matrix1]) #G_3h2 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_2, sensor_matrix2]) #G_3h3 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_2, sensor_matrix3]) #G_3h4 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_2, sensor_matrix4]) #G_3 = Concatenate(axis=-1)([G_3h1, G_3h2, G_3h3, G_3h4]) #G_4h1 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_3, sensor_matrix1]) #G_4h2 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_3, sensor_matrix2]) #G_4h3 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_3, sensor_matrix3]) #G_4h4 = GraphAttention(gnn_unit, activation='selu', dropout_rate=0)([G_3, sensor_matrix4]) #G_4 = Concatenate(axis=-1)([G_4h1, G_4h2, G_4h3, G_4h4]) gnn_output = tf.split(G_2, num_sensors, 1) mlp_input1 = Concatenate(axis=-1)([gnn_output[0], extract_cnn1]) mlp_input2 = Concatenate(axis=-1)([gnn_output[1], extract_cnn2]) mlp_input3 = Concatenate(axis=-1)([gnn_output[2], extract_cnn3]) mlp_input4 = Concatenate(axis=-1)([gnn_output[3], extract_cnn4]) mlp_input5 = Concatenate(axis=-1)([gnn_output[4], extract_cnn5]) mlp_input6 = Concatenate(axis=-1)([gnn_output[5], extract_cnn6]) mlp_input7 = Concatenate(axis=-1)([gnn_output[6], extract_cnn7]) mlp_input8 = Concatenate(axis=-1)([gnn_output[7], extract_cnn8]) mlp_input9 = Concatenate(axis=-1)([gnn_output[8], extract_cnn9]) mlp_input10 = Concatenate(axis=-1)([gnn_output[9], extract_cnn10]) mlp_layer = mlp_model() output1 = mlp_layer(Flatten()(mlp_input1)) output2 = mlp_layer(Flatten()(mlp_input2)) output3 = mlp_layer(Flatten()(mlp_input3)) output4 = mlp_layer(Flatten()(mlp_input4)) output5 = mlp_layer(Flatten()(mlp_input5)) output6 = mlp_layer(Flatten()(mlp_input6)) output7 = mlp_layer(Flatten()(mlp_input7)) output8 = mlp_layer(Flatten()(mlp_input8)) output9 = mlp_layer(Flatten()(mlp_input9)) output10 = mlp_layer(Flatten()(mlp_input10)) model = Model(inputs=[ s_input1, s_input2, s_input3, s_input4, s_input5, s_input6, s_input7, s_input8, s_input9, s_input10, sensor_matrix1, sensor_matrix2, sensor_matrix3, sensor_matrix4 ], outputs=[ output1, output2, output3, output4, output5, output6, output7, output8, output9, output10 ]) return model
F = train_X.shape[-1] # Original size of node features n_classes = train_y.shape[-1] # Number of classes l2_reg = 5e-4 # L2 regularization rate learning_rate = 5e-3 # Learning rate epochs = args.epochs es_patience = 100 # Patience for early stopping # Preprocessing operations A = A.astype('f4') #X = X.toarray() # Model definition X_in = Input(shape=(F,)) A_in = Input(shape=(N,)) gc1 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))([X_in, A_in]) gc2 = GraphAttention(32, activation='relu', kernel_regularizer=l2(l2_reg))([gc1, A_in]) pool = GlobalAttentionPool(128)(gc2) output = Dense(n_classes, activation='softmax')(Flatten()(pool)) # 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 unknowns = train_X.argmin(2)
def __init__(self, config, name=None, scope=None): self.config = config super(OmniAnomaly, self).__init__(name=name, scope=scope) with reopen_variable_scope(self.variable_scope): if config.posterior_flow_type == 'nf': self._posterior_flow = spt.layers.planar_normalizing_flows( config.nf_layers, name='posterior_flow') else: self._posterior_flow = None self._window_length = config.window_length self._x_dims = config.x_dim self._z_dims = config.z_dim self._gcn_FO = GraphAttention(config.window_length) self._gcn_TO = GraphAttention(config.x_dim) self._vae = VAE( p_z=TfpDistribution( LinearGaussianStateSpaceModel( num_timesteps=config.window_length, transition_matrix=LinearOperatorIdentity(config.z_dim), transition_noise=MultivariateNormalDiag( scale_diag=tf.ones([config.z_dim])), observation_matrix=LinearOperatorIdentity( config.z_dim), observation_noise=MultivariateNormalDiag( scale_diag=tf.ones([config.z_dim])), initial_state_prior=MultivariateNormalDiag( scale_diag=tf.ones([config.z_dim])))) if config.use_connected_z_p else Normal(mean=tf.zeros([config.z_dim]), std=tf.ones([config.z_dim])), p_x_given_z=Normal, q_z_given_x=partial(RecurrentDistribution, mean_q_mlp=partial(tf.layers.dense, units=config.z_dim, name='z_mean', reuse=tf.AUTO_REUSE), std_q_mlp=partial( softplus_std, units=config.z_dim, epsilon=config.std_epsilon, name='z_std'), z_dim=config.z_dim, window_length=config.window_length) if config.use_connected_z_q else Normal, h_for_p_x=Lambda( partial(wrap_params_net, h_for_dist=lambda x: rnn(x=x, window_length=config.window_length, rnn_num_hidden=config.rnn_num_hidden, rnn_cell=config.rnn_cell, hidden_dense=2, dense_dim=config.dense_dim, name='rnn_p_x'), mean_layer=partial(tf.layers.dense, units=config.x_dim, name='x_mean', reuse=tf.AUTO_REUSE), std_layer=partial(softplus_std, units=config.x_dim, epsilon=config.std_epsilon, name='x_std')), name='p_x_given_z'), h_for_q_z=Lambda( lambda x: { 'input_q': rnn( x=x, window_length=config.window_length, # 窗口长度 rnn_num_hidden=config.rnn_num_hidden, rnn_cell=config.rnn_cell, hidden_dense=2, #层数 dense_dim=config.dense_dim, #输出维度 name="rnn_q_z") }, name='q_z_given_x') if config.use_connected_z_q else Lambda(partial(wrap_params_net, h_for_dist=lambda x: rnn(x=x, window_length=config.window_length, rnn_num_hidden=config.rnn_num_hidden, hidden_dense=2, dense_dim=config.dense_dim, name="rnn_q_z"), mean_layer=partial(tf.layers.dense, units=config.z_dim, name='z_mean', reuse=tf.AUTO_REUSE), std_layer=partial(softplus_std, units=config.z_dim, epsilon=config.std_epsilon, name='z_std')), name='q_z_given_x'), with_conditional=config.with_conditional)
def execute(self): # create graph graph, index_list = create_graph(self.data, only_main_pages=True) # create adjacency matrix A = graph.get_adjacency_sparse() # create masks to select training, validation and test samples random_mask = np.random.random(self.x.shape[0]) train_mask = random_mask < 0.8 val_mask = (random_mask >= 0.8) * (random_mask < 0.9) test_mask = random_mask >= 0.9 # encode y encoded_y = [] for code in self.y: if code == 1: encoded_y.append([1, 0, 0]) elif code == 0: encoded_y.append([0, 1, 0]) y_uncoded = self.y self.y = np.array(encoded_y) # Parameters # channels = 4 # Number of channel in each head of the first GAT layer # n_attn_heads = 4 # Number of attention heads in first GAT layer # N = X.shape[0] # Number of nodes in the graph N = self.x.shape[0] # F = X.shape[1] # Original size of node features F = self.x.shape[1] # n_classes = y.shape[1] # Number of classes n_classes = 3 dropout = 0.6 # Dropout rate for the features and adjacency matrix l2_reg = 5e-6 # L2 regularization rate learning_rate = 5e-3 # Learning rate epochs = 200 # Number of training epochs es_patience = 100 # Patience for early stopping # Preprocessing operations A = A.astype('f4') X = self.x.toarray() # Model definition X_in = Input(shape=(F, )) A_in = Input(shape=(N, ), sparse=True) graph_attention_2 = GraphAttention(n_classes, attn_heads=3, concat_heads=False, dropout_rate=dropout, activation='softmax', kernel_regularizer=l2(l2_reg), attn_kernel_regularizer=l2(l2_reg) )([X_in, A_in]) # Build model model = Model(inputs=[X_in, A_in], outputs=graph_attention_2) optimizer = Adam(lr=learning_rate) metrics = [ keras.metrics.TruePositives(name='tp'), keras.metrics.FalsePositives(name='fp'), keras.metrics.TrueNegatives(name='tn'), keras.metrics.FalseNegatives(name='fn'), keras.metrics.BinaryAccuracy(name='accuracy'), keras.metrics.Precision(name='precision'), keras.metrics.Recall(name='recall'), keras.metrics.AUC(name='auc'), ] model.compile(optimizer=optimizer, loss='categorical_crossentropy', weighted_metrics=metrics) # model.summary() # Train model validation_data = ([X, A], self.y, val_mask) model.fit([X, A], # y, y=self.y, sample_weight=train_mask, epochs=epochs, batch_size=N, verbose=0, validation_data=validation_data, shuffle=False, # Shuffling data means shuffling the whole graph callbacks=[ EarlyStopping(patience=es_patience, restore_best_weights=True) ]) # Evaluate model # print('Evaluating model.') # eval_results = model.evaluate([X, A], # y=self.y, # sample_weight=test_mask, # batch_size=N) test_data = ([X, A]) self.pred = model.predict(test_data, batch_size=N) # print('Done.\n' # 'Test loss: {}\n' # 'Test accuracy: {}'.format(*eval_results)) # coding predictions predictions = [] for row in self.pred[test_mask]: if row[0] >= 0.50: predictions.append(1) else: predictions.append(0) predictions = np.array(predictions) return predictions, y_uncoded[test_mask]
from spektral.utils import tic, toc # Load data A, X, y, train_mask, val_mask, test_mask = citation.load_data('cora') fltr = A.astype('f4') fltr = ops.sp_matrix_to_sp_tensor(fltr) X = X.toarray() # Define model X_in = Input(shape=(X.shape[1], )) fltr_in = Input(shape=(X.shape[0], ), sparse=True) X_1 = Dropout(0.6)(X_in) X_1 = GraphAttention(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, fltr_in]) X_2 = Dropout(0.6)(X_1) X_2 = GraphAttention(y.shape[1], 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, fltr_in]) # Build model model = Model(inputs=[X_in, fltr_in], outputs=X_2)
def __init__(self, units, name): super().__init__(name=name) self.conv_graph_layer = GraphAttention(units) self.pool_graph_layer = GlobalAttentionPool(units)
def __init__(self, X, adj, adj_n, hidden_dim=128, latent_dim=10, dec_dim=None, adj_dim=32, decA="DBL", layer_enc="GAT"): super(SCGAE, self).__init__() if dec_dim is None: dec_dim = [64, 256, 512] self.latent_dim = latent_dim self.X = X self.adj = np.float32(adj) self.adj_n = np.float32(adj_n) self.n_sample = X.shape[0] self.in_dim = X.shape[1] self.sparse = False initializer = GlorotUniform(seed=7) # Encoder X_input = Input(shape=self.in_dim) h = Dropout(0.2)(X_input) if layer_enc == "GAT": A_in = Input(shape=self.n_sample) h = GraphAttention(channels=hidden_dim, attn_heads=1, kernel_initializer=initializer, activation="relu")([h, A_in]) z_mean = GraphAttention(channels=latent_dim, kernel_initializer=initializer, attn_heads=1)([h, A_in]) elif layer_enc == "GCN": A_in = Input(shape=self.n_sample) h = GraphConvSkip(channels=hidden_dim, kernel_initializer=initializer, activation="relu")([h, A_in]) z_mean = GraphConvSkip(channels=latent_dim, kernel_initializer=initializer)([h, A_in]) elif layer_enc == "TAG": self.sparse = True A_in = Input(shape=self.n_sample, sparse=True) h = TAGConv(channels=hidden_dim, kernel_initializer=initializer, activation="relu")([h, A_in]) z_mean = TAGConv(channels=latent_dim, kernel_initializer=initializer)([h, A_in]) self.encoder = Model(inputs=[X_input, A_in], outputs=z_mean, name="encoder") clustering_layer = ClusteringLayer(name='clustering')(z_mean) self.cluster_model = Model(inputs=[X_input, A_in], outputs=clustering_layer, name="cluster_encoder") # Adjacency matrix decoder if decA == "DBL": dec_in = Input(shape=latent_dim) h = Dense(units=adj_dim, activation=None)(dec_in) h = Bilinear()(h) dec_out = Lambda(lambda z: tf.nn.sigmoid(z))(h) self.decoderA = Model(inputs=dec_in, outputs=dec_out, name="decoder1") elif decA == "BL": dec_in = Input(shape=latent_dim) h = Bilinear()(dec_in) dec_out = Lambda(lambda z: tf.nn.sigmoid(z))(h) self.decoderA = Model(inputs=dec_in, outputs=dec_out, name="decoder1") elif decA == "IP": dec_in = Input(shape=latent_dim) dec_out = Lambda( lambda z: tf.nn.sigmoid(tf.matmul(z, tf.transpose(z))))(dec_in) self.decoderA = Model(inputs=dec_in, outputs=dec_out, name="decoder1") else: self.decoderA = None # Expression matrix decoder decx_in = Input(shape=latent_dim) h = Dense(units=dec_dim[0], activation="relu")(decx_in) h = Dense(units=dec_dim[1], activation="relu")(h) h = Dense(units=dec_dim[2], activation="relu")(h) decx_out = Dense(units=self.in_dim)(h) self.decoderX = Model(inputs=decx_in, outputs=decx_out, name="decoderX")
es_patience = 100 # Patience for early stopping # Preprocessing operations A = A.astype('f4') #X = X.toarray() # Model definition X_in = Input(shape=(F, )) A_in = Input(shape=(N, )) dropout_1 = Dropout(dropout)(X_in) graph_attention_1 = GraphAttention( 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), )([dropout_1, A_in]) dropout_2 = Dropout(dropout)(graph_attention_1) graph_attention_2 = GraphAttention( n_classes, attn_heads=1, concat_heads=False, dropout_rate=dropout, activation='softmax', kernel_regularizer=l2(l2_reg), attn_kernel_regularizer=l2(l2_reg), )([dropout_2, A_in])
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 # Model definition X_in = Input(shape=(F, )) A_in = Input(shape=(N, )) dropout_1 = Dropout(dropout_rate)(X_in) graph_attention_1 = GraphAttention( gat_channels, attn_heads=n_attn_heads, attn_heads_reduction='concat', dropout_rate=dropout_rate, activation='elu', kernel_regularizer=l2(l2_reg), attn_kernel_regularizer=l2(l2_reg))([dropout_1, A_in]) dropout_2 = Dropout(dropout_rate)(graph_attention_1) graph_attention_2 = GraphAttention( n_classes, attn_heads=1, attn_heads_reduction='average', dropout_rate=dropout_rate, activation='softmax', kernel_regularizer=l2(l2_reg), attn_kernel_regularizer=l2(l2_reg))([dropout_2, A_in]) # Build model model = Model(inputs=[X_in, A_in], outputs=graph_attention_2)
def __init__(self, units, **kwargs): super().__init__(**kwargs) self.units = units self.conv_graph_layer = GraphAttention(units) self.pool_graph_layer = GlobalAttentionPool(units)