コード例 #1
0
ファイル: graphsage.py プロジェクト: zwbjtu123/MCNS
    def _build(self):
        num_samples = [layer_info.num_samples for layer_info in self.layer_infos]

        # positive examples pairs embeddings (self.outputs)
        self.samples1, support_sizes1 = self.sample(self.inputs1, self.layer_infos)  # samples :sampled node id
        self.samples2, support_sizes2 = self.sample(self.inputs2, self.layer_infos)
        self.outputs1, self.aggregators = self.aggregate(self.samples1, [self.features], self.dims, num_samples,
                                                         support_sizes1, concat=self.concat, model_size=self.model_size)
        self.outputs2, _ = self.aggregate(self.samples2, [self.features], self.dims, num_samples,
                                          support_sizes2, aggregators=self.aggregators, concat=self.concat,
                                          model_size=self.model_size)

        # negative examples embeddings
        self.neg_sample, neg_support_sizes = self.sample(self.neg_samples, self.layer_infos,
                                                         self.number)
        self.neg_outputs, _ = self.aggregate(self.neg_sample, [self.features], self.dims, num_samples,
                                                 neg_support_sizes, batch_size=self.number,
                                                 aggregators=self.aggregators,
                                                 concat=self.concat, model_size=self.model_size)

        dim_mult = 2 if self.concat else 1

        # unsupervised loss
        self.link_pred_layer = BipartiteEdgePredLayer(dim_mult * self.dims[-1],
                                                      dim_mult * self.dims[-1], self.placeholders, act=tf.nn.sigmoid,
                                                      bilinear_weights=False,
                                                      name='edge_predict')

        for aggregator in self.aggregators:
            for var in aggregator.vars.values():
                tf.add_to_collection("params",var)

        self.outputs1 = tf.nn.l2_normalize(self.outputs1, 1)
        self.outputs2 = tf.nn.l2_normalize(self.outputs2, 1)
        self.neg_outputs = tf.nn.l2_normalize(self.neg_outputs, 1)
コード例 #2
0
 def _build(self):
     self.outputs1 = tf.nn.embedding_lookup(self.target_embeds, self.inputs1)
     self.outputs2 = tf.nn.embedding_lookup(self.context_weights, self.inputs2)
     self.true_b = tf.nn.embedding_lookup(self.context_bias, self.inputs2)
     self.neg_outputs = tf.nn.embedding_lookup(self.context_weights, self.neg_samples)
     self.neg_b = tf.nn.embedding_lookup(self.context_bias, self.neg_samples)
     
     self.link_pred_layer = BipartiteEdgePredLayer(self.embedding_dim, self.embedding_dim,
             self.placeholders, bilinear_weights=False)
コード例 #3
0
ファイル: gcn.py プロジェクト: phychaos/MCNS
    def build(self):
        """ Wrapper for _build() """
        with tf.variable_scope(self.name):
            self._build()

        # Build sequential layer model
        self.activations.append(self.inputs)
        for layer in self.layers:
            hidden = layer(self.activations[-1])
            self.activations.append(hidden)
        self.embedding = self.activations[-1]  # embedding matrix
        self.embedding = tf.nn.l2_normalize(self.embedding, 1)

        self.outputs1 = tf.nn.embedding_lookup(self.embedding, self.inputs1)
        self.outputs2 = tf.nn.embedding_lookup(self.embedding, self.inputs2)
        self.neg_outputs = tf.nn.embedding_lookup(self.embedding,
                                                  self.neg_samples)

        self.link_pred_layer = BipartiteEdgePredLayer(self.output_dim,
                                                      self.output_dim,
                                                      self.placeholders,
                                                      act=tf.nn.sigmoid,
                                                      bilinear_weights=False,
                                                      name='edge_predict')

        # Store model variables for easy access
        variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                      scope=self.name)
        self.vars = {var.name: var for var in variables}

        # Build metrics
        self._loss()
        self._accuracy()
        self.opt_op = self.optimizer.minimize(self.loss)
        self.p_probs = self.link_pred_layer.get_probs(self.outputs1,
                                                      self.outputs2)