def evaluate(A_list, X_list, y_list, mask_list, ops, batch_size): batches_ = batch_iterator([A_list, X_list, y_list, mask_list], batch_size=batch_size) output_ = [] y_ = [] for b_ in batches_: batch_ = Batch(b_[0], b_[1]) X__, A__, _ = batch_.get('XAI') y__ = np.vstack(b_[2]) mask__ = np.concatenate(b_[3], axis=0) feed_dict_ = { X_in: X__, A_in: sp_matrix_to_sp_tensor_value(A__), mask_in: mask__, target: y__, SW_KEY: np.ones((1, )) } outs_ = sess.run(ops, feed_dict=feed_dict_) output_.append(outs_[1][mask__.astype(np.bool)]) y_.append(y__[mask__.astype(np.bool)]) output_ = np.concatenate(output_, axis=0) y_ = np.concatenate(y_, axis=0) mse = (output_[:, 0] - y_[:, 0])**2 return mse.mean(), np.std(mse) / np.sqrt(mse.shape[0])
# Build model model = Model(inputs=[X_in, A_in, I_in], outputs=output) optimizer = Adam(lr=learning_rate) model.compile(optimizer=optimizer, loss='mse', target_tensors=target) model.summary() # Training setup sess = K.get_session() batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size, epochs=epochs) loss = 0 batch_index = 0 batches_in_epoch = np.ceil(len(A_train) / batch_size) # Training loop for b in batches_train: batch = Batch(b[0], b[1]) y_ = b[2] sess.run(target_iter.initializer, feed_dict={target_ph: y_}) loss += model.train_on_batch(list(batch.get('XAI')), None) batch_index += 1 if batch_index == batches_in_epoch: print('Loss: {}'.format(loss / batches_in_epoch)) loss = 0 batch_index = 0 # Test setup batches_test = batch_iterator([A_test, X_test, y_test], batch_size=batch_size) loss = 0 batches_in_epoch = np.ceil(len(A_test) / batch_size)
def run_kcn_sage(): def evaluate(A_list, X_list, y_list, mask_list, ops, batch_size): batches_ = batch_iterator([A_list, X_list, y_list, mask_list], batch_size=batch_size) output_ = [] y_ = [] for b_ in batches_: batch_ = Batch(b_[0], b_[1]) X__, A__, _ = batch_.get('XAI') y__ = np.vstack(b_[2]) mask__ = np.concatenate(b_[3], axis=0) feed_dict_ = { X_in: X__, A_in: sp_matrix_to_sp_tensor_value(A__), mask_in: mask__, target: y__, SW_KEY: np.ones((1, )) } outs_ = sess.run(ops, feed_dict=feed_dict_) output_.append(outs_[1][mask__.astype(np.bool)]) y_.append(y__[mask__.astype(np.bool)]) output_ = np.concatenate(output_, axis=0) y_ = np.concatenate(y_, axis=0) mse = (output_[:, 0] - y_[:, 0])**2 return mse.mean(), np.std(mse) / np.sqrt(mse.shape[0]) ################################################################################ # LOAD DATA ################################################################################ coords, features, y, y_train_val, nbs, Ntrain, train_mask, val_mask, test_mask = load_kriging_data( FLAGS.dataset, FLAGS.n_neighbors) y_f_train = y * train_mask[:, np.newaxis].astype(np.float) X_train, A_train, mask_train, y_train = get_sub_graph( coords, features, y, y_f_train, nbs, train_mask) X_val, A_val, mask_val, y_val = get_sub_graph(coords, features, y, y_f_train, nbs, val_mask) X_test, A_test, mask_test, y_test = get_sub_graph(coords, features, y, y_train_val, nbs, test_mask) # Parameters F = X_train[0].shape[-1] # Dimension of node features n_out = y_train[0].shape[-1] # Dimension of the target ################################################################################ # BUILD MODEL ################################################################################ X_in = Input( tensor=tf.placeholder(tf.float32, shape=(None, F), name='X_in')) A_in = Input(tensor=tf.sparse_placeholder(tf.float32, shape=(None, None)), sparse=True, name='A_in') mask_in = Input(tensor=tf.placeholder(tf.float32), shape=(None, ), name='mask_in') target = Input( tensor=tf.placeholder(tf.float32, shape=(None, n_out), name='target')) # Block 1 gc1 = GraphSageConv(FLAGS.hidden1, aggregate_method='max', activation=keras.activations.relu, use_bias=True)([X_in, A_in]) gc1 = Dropout(FLAGS.dropout)(gc1) if FLAGS.hidden2 != -1: # Block 2 gc2 = GraphSageConv(FLAGS.hidden2, aggregate_method='max', activation=keras.activations.relu, use_bias=True)([gc1, A_in]) gc2 = Dropout(FLAGS.dropout)(gc2) else: gc2 = gc1 # Output block output = Dense(n_out, activation=FLAGS.last_activation, use_bias=True)(gc2) # Build model model = Model([X_in, A_in], output) model.compile(optimizer='adam', loss='mse', loss_weights=[mask_in], target_tensors=[target]) # Training setup sess = K.get_session() loss = model.total_loss opt = tf.train.AdamOptimizer(learning_rate=FLAGS.lr) train_step = opt.minimize(loss) # Initialize all variables init_op = tf.global_variables_initializer() sess.run(init_op) ################################################################################ # FIT MODEL ################################################################################ # Run training loop current_batch = 0 model_loss = 0 model_acc = 0 best_val_loss = np.inf patience = FLAGS.es_patience batches_in_epoch = np.ceil(len(y_train) / FLAGS.batch_size) print('Fitting model') batches = batch_iterator([A_train, X_train, y_train, mask_train], batch_size=FLAGS.batch_size, epochs=FLAGS.epochs) for b in batches: batch = Batch(b[0], b[1]) X_, A_, _ = batch.get('XAI') y_ = np.vstack(b[2]) mask_ = np.concatenate(b[3], axis=0) tr_feed_dict = { X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), mask_in: mask_, target: y_, SW_KEY: np.ones((1, )) } outs = sess.run([train_step, loss], feed_dict=tr_feed_dict) model_loss += np.sum(outs[1] * mask_) current_batch += 1 if current_batch % batches_in_epoch == 0: model_loss /= np.sum(train_mask) # Compute validation loss and accuracy val_loss, val_loss_std = evaluate(A_val, X_val, y_val, mask_val, [loss, output], batch_size=FLAGS.batch_size) ep = int(current_batch / batches_in_epoch) print('Ep: {:d} - Train loss: {:.5f} - Val mse: {:.5f}'.format( ep, model_loss, val_loss)) # Check if loss improved for early stopping if val_loss < best_val_loss: best_val_loss = val_loss patience = FLAGS.es_patience else: patience -= 1 if patience == 0: print('Early stopping (best val_loss: {})'.format( best_val_loss)) break model_loss = 0 ################################################################################ # EVALUATE MODEL ################################################################################ # Test model test_loss, test_loss_std = evaluate(A_test, X_test, y_test, mask_test, [loss, output], batch_size=FLAGS.batch_size) print('Test mse: {:.5f}'.format(test_loss))
loss = model.total_loss opt = tf.train.AdamOptimizer(learning_rate=learning_rate) train_step = opt.minimize(loss) # Initialize all variables init_op = tf.global_variables_initializer() sess.run(init_op) batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size, epochs=epochs) model_loss = 0 batch_index = 0 batches_in_epoch = np.ceil(len(A_train) / batch_size) # Training loop for b in batches_train: batch = Batch(b[0], b[1]) X_, A_, I_ = batch.get('XAI') y_ = b[2] tr_feed_dict = {X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), I_in: I_, target: y_, SW_KEY: np.ones((1,))} outs = sess.run([train_step, loss], feed_dict=tr_feed_dict) model_loss += outs[-1] batch_index += 1 if batch_index == batches_in_epoch: print('Loss: {}'.format(model_loss / batches_in_epoch)) model_loss = 0 batch_index = 0
loss = model.total_loss opt = tf.train.AdamOptimizer(learning_rate=learning_rate) train_step = opt.minimize(loss) init_op = tf.global_variables_initializer() sess.run(init_op) batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size, epochs=epochs) model_loss = 0 batch_index = 0 batches_in_epoch = np.ceil(len(A_train) / batch_size) # Training loop for b in batches_train: X_, A_, I_ = Batch(b[0], b[1]).get('XAI') y_ = b[2] tr_feed_dict = { X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), I_in: I_, target: y_, SW_KEY: np.ones((1, )) } outs = sess.run([train_step, loss], feed_dict=tr_feed_dict) model_loss += outs[-1] batch_index += 1 if batch_index == batches_in_epoch: print('Loss: {}'.format(model_loss / batches_in_epoch)) model_loss = 0
loss = model.total_loss opt = tf.train.AdamOptimizer(learning_rate=learning_rate) train_step = opt.minimize(loss) init_op = tf.global_variables_initializer() sess.run(init_op) batches_train = batch_iterator([A_train, X_train, E_train, y_train], batch_size=batch_size, epochs=epochs) model_loss = 0 batch_index = 0 batches_in_epoch = np.ceil(len(A_train) / batch_size) # Training loop for b in batches_train: X_, A_, E_, I_ = Batch(b[0], b[1], b[2]).get('XAEI') y_ = b[3] tr_feed_dict = { X_in: X_, A_in: A_.toarray(), E_in: E_, I_in: I_, target: y_, SW_KEY: np.ones((1, )) } outs = sess.run([train_step, loss], feed_dict=tr_feed_dict) model_loss += outs[-1] batch_index += 1 if batch_index == batches_in_epoch: print('Loss: {}'.format(model_loss / batches_in_epoch))
def build_gcn_model(trainset, testset, nb_node_features, nb_classes=1, batch_size=32, nb_epochs=100, lr=0.001, save_path=None): # Create model architecture X_in = Input(batch_shape=(None, nb_node_features)) A_in = Input(batch_shape=(None, None), sparse=True) I_in = Input(batch_shape=(None, ), dtype='int64') target = Input(tensor=tf.placeholder(tf.float32, shape=(None, nb_classes), name='target')) gc1 = GraphConv(64, activation='relu')([X_in, A_in]) gc2 = GraphConv(128, activation='relu')([gc1, A_in]) pool = GlobalAvgPool()([gc2, I_in]) dense1 = Dense(128, activation='relu')(pool) output = Dense(nb_classes, activation='sigmoid')(dense1) model = Model(inputs=[X_in, A_in, I_in], outputs=output) # Compile model #optimizer = Adam(lr=lr) opt = tf.train.AdamOptimizer(learning_rate=lr) model.compile(optimizer=opt, loss='binary_crossentropy', target_tensors=target, metrics=['accuracy']) model.summary() loss = model.total_loss train_step = opt.minimize(loss) # Initialize all variables sess = K.get_session() init_op = tf.global_variables_initializer() sess.run(init_op) # Get train and test data [A_train, X_train, y_train] = trainset [A_test, X_test, y_test] = testset SW_KEY = 'dense_2_sample_weights:0' # Keras automatically creates a placeholder for sample weights, which must be fed best_accuracy = 0 for i in range(nb_epochs): # Train # TODO: compute class weight and use it in loss function batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size) model_loss = 0 prediction = [] for b in batches_train: batch = Batch(b[0], b[1]) X_, A_, I_ = batch.get('XAI') y_ = b[2] tr_feed_dict = {X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), I_in: I_, target: y_, SW_KEY: np.ones((1,))} outs = sess.run([train_step, loss, output], feed_dict=tr_feed_dict) model_loss += outs[1] prediction.append(list(outs[2].flatten())) y_train_predict = (np.concatenate(prediction)[:len(y_train)] > 0.5).astype('uint8') train_accuracy = accuracy_score(y_train, y_train_predict) train_loss = model_loss / (np.ceil(len(y_train) / batch_size)) # Validation batches_val = batch_iterator([A_test, X_test, y_test], batch_size=batch_size) model_loss = 0 prediction = [] for b in batches_val: batch = Batch(b[0], b[1]) X_, A_, I_ = batch.get('XAI') y_ = b[2] tr_feed_dict = {X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), I_in: I_, target: y_, SW_KEY: np.ones((1,))} loss_, output_ = sess.run([loss, output], feed_dict=tr_feed_dict) model_loss += loss_ prediction.append(list(output_.flatten())) y_val_predict = (np.concatenate(prediction)[:len(y_test)] > 0.5).astype('uint8') val_accuracy = accuracy_score(y_test, y_val_predict) val_loss = model_loss / (np.ceil(len(y_test) / batch_size)) print('---------------------------------------------') print('Epoch {}: train_loss: {}, train_acc: {}, val_loss: {}, val_acc: {}'.format(i+1, train_loss, train_accuracy, val_loss, val_accuracy)) if val_accuracy > best_accuracy: best_accuracy = val_accuracy model.save(save_path) # Evaluate the model model = load_model(save_path) batches_val = batch_iterator([A_test, X_test, y_test], batch_size=batch_size) prediction = [] for b in batches_val: batch = Batch(b[0], b[1]) X_, A_, I_ = batch.get('XAI') y_ = b[2] tr_feed_dict = {X_in: X_, A_in: sp_matrix_to_sp_tensor_value(A_), I_in: I_, target: y_, SW_KEY: np.ones((1,))} output_ = sess.run([output], feed_dict=tr_feed_dict) prediction.append(list(output_.flatten())) y_val_predict = (np.concatenate(prediction)[:len(y_test)] > 0.5).astype('uint8') with warnings.catch_warnings(): warnings.simplefilter('ignore') # disable the warning on f1-score with not all labels scores = get_prediction_score(y_val, y_val_predict) return model, scores