def test_model(num_sensors, batch_size=1, lr=3e-4, is_robot=0): z_input = load_input(num_sensors=num_sensor, select_case=test_case, select_env=test_env) z_model = load_model_gcn(num_sensors=num_sensor) z_model.load_weights(model_path) z_model.compile(optimizer=Adam(learning_rate=lr, clipnorm=1), loss='mse') if is_robot: z_admatrix = np.ones((batch_size, num_sensors + 1, num_sensors + 1)) else: z_admatrix = np.ones((batch_size, num_sensors, num_sensors)) input_admatrix = gcn_filter(z_admatrix) input_data = [] for i in range(num_sensors): input_data.append(np.expand_dims(z_input[0][i], axis=0)) input_data.append(input_admatrix) z_res = z_model.predict(input_data) return z_res # test_model(4)
def load_adjacency_matrix(self): # Load adjacency matrix for attributes adj_matrix_attr_df = pd.read_csv( self.config.graph_adjacency_matrix_attributes_file, sep=';', index_col=0) col_values = adj_matrix_attr_df.columns.values index_values = adj_matrix_attr_df.index.values if not np.array_equal(col_values, self.feature_names_all): raise ValueError( 'Ordering of features in the adjacency matrix (columns) does not match the one in the dataset.' ) if not np.array_equal(index_values, self.feature_names_all): raise ValueError( 'Ordering of features in the adjacency matrix (index) does not match the one in the dataset.' ) self.graph_adjacency_matrix_attributes = adj_matrix_attr_df.values.astype( dtype=np.float) #Add self loop: if self.config.add_selfloop_to_adj_matrix: self.graph_adjacency_matrix_attributes = self.graph_adjacency_matrix_attributes + np.eye( 61) # Preprocess the adj matrix for using a GCN if self.config.use_GCN_adj_matrix_preprocessing: self.graph_adjacency_matrix_attributes_preprocessed = utils.gcn_filter( self.graph_adjacency_matrix_attributes, symmetric=self.config.use_GCN_adj_matrix_preprocessing_sym) # Load adjacency matrix for workstations adj_matrix_df = pd.read_csv(self.config.graph_adjacency_matrix_ws_file, sep=';', index_col=0) self.graph_adjacency_matrix_ws = adj_matrix_df.values.astype( dtype=np.float) # Preprocess the adj matrix for using a GCN if self.config.use_GCN_adj_matrix_preprocessing: self.graph_adjacency_matrix_ws_preprocessed = utils.gcn_filter( self.graph_adjacency_matrix_ws, symmetric=self.config.use_GCN_adj_matrix_preprocessing_sym)
def train_model(num_epoch, num_batch, num_maps=10, lr_decay=True, is_robot=0, is_gcn=0): cur_lr = init_lr for ep in range(num_epoch): print('starting training epoch:', ep) if lr_decay: cur_lr = init_lr / np.sqrt(ep + 1) select_env = np.random.randint(10) num_sensors = sensor_per_map[select_env] select_case = np.arange(2, 33) np.random.shuffle(select_case) select_case = select_case[:32] input_image, image_index = load_input(num_sensors, select_case, select_env) if is_robot: z_admatrix = np.ones( (len(select_case), num_sensors + 1, num_sensors + 1)) else: z_admatrix = np.ones((len(select_case), num_sensors, num_sensors)) if is_gcn: z_admatrix = gcn_filter(z_admatrix) #input_admatrix = gcn_filter(z_admatrix) input_admatrix = z_admatrix.copy() input_data = [] for i in range(num_sensors): input_data.append(input_image[:, i]) input_data.append(input_admatrix) input_label = load_label(select_env, num_sensors, image_index) model = load_model_gcn(num_sensors) model.load_weights('training/model_gcn_s1.h5') model.compile(optimizer=Adam(learning_rate=cur_lr), loss='mse') train_his = model.fit(input_data, np.asarray(input_label), batch_size=num_batch, epochs=int(len(input_label) / num_batch)) hist_df = pd.DataFrame(train_his.history) hist_csv_file = 'training/history.csv' with open(hist_csv_file, mode='a') as f: hist_df.to_csv(f, index=False) model.save('training/model_gcn_s1.h5')
def get_adj_matrix(self, class_label): adj_matrix_input = np.zeros((61, 61, 3)) if self.config.adj_matrix_preprocessing == 3: adj_matrix_input = np.zeros((61, 61, 3)) else: # Use masking vectors to generated adj. matrix relevant to a class label masking = self.get_masking( class_label, self.config.use_additional_strict_masking_for_attribute_sim) if self.config.use_additional_strict_masking_for_attribute_sim == False: strict_mask = masking context_mask = masking else: strict_mask = masking[0] context_mask = masking[1] adj_mat = self.graph_adjacency_matrix_attributes_preprocessed # + np.identity(61) adj_matrix_input[:, :, 0] = adj_mat # Mask Adj Mat according context attributes: adj_mat_context = np.multiply( self.graph_adjacency_matrix_attributes, context_mask) adj_mat_context = np.multiply(adj_mat_context.T, context_mask).T adj_mat_context = utils.gcn_filter( adj_mat_context, symmetric=self.config.use_GCN_adj_matrix_preprocessing_sym) adj_matrix_input[:, :, 1] = adj_mat_context # Mask Adj Mat according strict attributes: adj_mat_strict = np.multiply( self.graph_adjacency_matrix_attributes, strict_mask) adj_mat_strict = np.multiply(adj_mat_strict.T, strict_mask).T adj_mat_strict = utils.gcn_filter( adj_mat_strict, symmetric=self.config.use_GCN_adj_matrix_preprocessing_sym) adj_matrix_input[:, :, 2] = adj_mat_strict #print("adj_matrix_input:", adj_matrix_input.shape) return adj_matrix_input
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()
def __call__(self, graph): if graph.a is not None: graph.a = gcn_filter(graph.a, self.symmetric) return graph
def preprocess(a): return gcn_filter(a)