示例#1
0
文件: models.py 项目: zhhhzhang/gcn
 def _accuracy(self):
     if self.model_config['Model'] in [11, 13, 14, 15]:
         if self.model_config['Model'] in [
                 14, 15
         ] or self.model_config['Model11'] == 'weighted':
             sample2label = self.placeholders['sample2label']
             unsoftmaxed = tf.matmul(
                 self.outputs, sample2label) / tf.reduce_sum(
                     sample2label, axis=0, keep_dims=True)
             self.accuracy = masked_accuracy(
                 unsoftmaxed, self.placeholders['labels'],
                 self.placeholders['labels_mask'])
         elif self.model_config['Model11'] == 'nearest':
             sample2label = self.placeholders['sample2label']
             outputs = tf.one_hot(
                 tf.argmax(self.outputs, axis=1),
                 depth=self.placeholders['label_per_sample'].get_shape(
                 ).as_list()[1])
             unsoftmaxed = tf.matmul(outputs, sample2label)
             self.accuracy = masked_accuracy(
                 unsoftmaxed, self.placeholders['labels'],
                 self.placeholders['labels_mask'])
         else:
             raise ValueError(
                 "model_config['Model11'] should be either 'weighted' or 'nearest'"
             )
     else:
         self.accuracy = masked_accuracy(self.outputs,
                                         self.placeholders['labels'],
                                         self.placeholders['labels_mask'])
示例#2
0
    def _accuracy(self):

        if self.is_gcn:
            #self.merged = tf.math.add(tf.math.scalar_mul(self.outputs_weight, self.outputs),
            #tf.math.scalar_mul(self.outs_weight, self.outs))
            self.outs_for_graph = tf.nn.softmax(self.outs)
            self.accuracy = masked_accuracy(tf.nn.softmax(self.outs),
                                            self.placeholders['labels'],
                                            self.placeholders['labels_mask'])
            self.mo_accuarcy = masked_accuracy(
                self.outputs_weight * tf.nn.softmax(self.outs) +
                tf.nn.softmax(self.outputs), self.placeholders['labels'],
                self.placeholders['labels_mask'])
        else:
            self.accuracy = masked_accuracy(self.outputs,
                                            self.placeholders['labels'],
                                            self.placeholders['labels_mask'])
示例#3
0
 def _accuracy_of_class(self):
     self.accuracy_of_class = [
         masked_accuracy(
             self.outputs, self.placeholders['labels'],
             self.placeholders['labels_mask'] *
             self.placeholders['labels'][:, i])
         for i in range(self.placeholders['labels'].shape[1])
     ]
示例#4
0
文件: models.py 项目: zhhhzhang/gcn
 def _accuracy_of_class(self):
     if self.model_config['Model'] in [11, 13, 14, 15]:
         sample2label = self.placeholders['sample2label']
         unsoftmaxed = tf.matmul(self.outputs,
                                 sample2label) / tf.reduce_sum(
                                     sample2label, axis=0, keep_dims=True)
         self.accuracy_of_class = [
             masked_accuracy(
                 unsoftmaxed, self.placeholders['labels'],
                 self.placeholders['labels_mask'] *
                 self.placeholders['labels'][:, i])
             for i in range(self.placeholders['labels'].shape[1])
         ]
     else:
         self.accuracy_of_class = [
             masked_accuracy(
                 self.outputs, self.placeholders['labels'],
                 self.placeholders['labels_mask'] *
                 self.placeholders['labels'][:, i])
             for i in range(self.placeholders['labels'].shape[1])
         ]
示例#5
0
    def _accuracy_of_class(self):

        # if self.is_gcn:
        #self.merged = tf.math.add(tf.math.scalar_mul(self.outputs_weight, self.outputs),
        # tf.math.scalar_mul(self.outs_weight, self.outs))
        # self.accuracy_of_class = [masked_accuracy(self.merged, self.placeholders['labels'],
        # self.placeholders['labels_mask'] * self.placeholders['labels'][:, i])
        # for i in range(self.placeholders['labels'].shape[1])]
        #else:
        self.accuracy_of_class = [
            masked_accuracy(
                self.outputs, self.placeholders['labels'],
                self.placeholders['labels_mask'] *
                self.placeholders['labels'][:, i])
            for i in range(self.placeholders['labels'].shape[1])
        ]
示例#6
0
def baseline_predict(y_train,
                     y_test,
                     y_val,
                     train_mask,
                     test_mask,
                     val_mask,
                     label_samples=None):
    """Get baseline random predictions. If 'label_samples' is used then the predictor will take into account the frequencies of the labels in the file."""
    sess = tf.Session()
    acc_vector = []
    for t in [(y_train, train_mask), (y_val, val_mask), (y_test, test_mask)]:
        if label_samples == None:
            y_rnd = random_pred(t[0])
        else:
            y_rnd = weighted_random_pred(label_samples, len(t[0]))
        acc = sess.run(masked_accuracy(y_rnd, t[0], t[1]))
        acc_vector.append(acc)
    return acc_vector
示例#7
0
文件: train.py 项目: niklasmaki/gcn
print("Base model optimization Finished!")

feed_dict = construct_feed_dict(features, support, y_train, train_mask,
                                placeholders)
preds = sess.run(model.outputs, feed_dict=feed_dict)

print("Calculating neighorhood distance matrix...")
d2 = neighborhood_distance_matrix(adj, preds, FLAGS.dataset)

print("Calculating weighted distance matrix...")
d = weighted_distance_matrix(d1, d2)

print("Calculating the MAP estimate...")
map_support = map_estimate(adj, d, FLAGS.dataset)
map_support = [preprocess_adj(map_support)]

print("Sampling weights by running a GCN with MC dropout...")

final_model = model_func(placeholders, input_dim=features[2][1], logging=True)
sess.run(tf.global_variables_initializer())
final_pred_soft, final_pred = train_model(FLAGS, sess, final_model, features,
                                          map_support, y_train, y_val,
                                          train_mask, val_mask, placeholders)

test_acc = sess.run(masked_accuracy(final_pred_soft, y_test, test_mask))
print("Test accuracy: ", "{:.5f}".format(test_acc))

gcn_test_acc = sess.run(masked_accuracy(gcn_pred_soft, y_test, test_mask))
print("GCN Test accuracy: ", "{:.5f}".format(gcn_test_acc))
示例#8
0
 def _accuracy(self):
     self.accuracy = masked_accuracy(self.outputs,
                                     self.placeholders['labels'],
                                     self.placeholders['labels_mask'])