| 27 | |
| 28 | |
| 29 | class SuperviseSampleSolution(object): |
| 30 | def __init__(self, |
| 31 | parse_input_fn, |
| 32 | encoder_fn, |
| 33 | parse_group_emb_fn, |
| 34 | logit_fn=CosineLogits(), |
| 35 | metric_name='auc', |
| 36 | neg_sample_fn=None, |
| 37 | loss_fn=sigmoid_loss): |
| 38 | self.metric_name = metric_name |
| 39 | self.metric_class = tf_euler.utils.metrics.get(metric_name) |
| 40 | self.parse_input_fn = parse_input_fn |
| 41 | self.parse_group_emb_fn = parse_group_emb_fn |
| 42 | self.encoder = encoder_fn |
| 43 | self.neg_sample_fn = neg_sample_fn |
| 44 | self.logit_fn = logit_fn |
| 45 | self.loss_fn = loss_fn |
| 46 | |
| 47 | def embed(self, n_id): |
| 48 | return self.encoder(n_id) |
| 49 | |
| 50 | def __call__(self, inputs): |
| 51 | inputs = self.parse_input_fn(inputs) |
| 52 | label = inputs[0] |
| 53 | if len(inputs) == 2: |
| 54 | node_groups = inputs[1] |
| 55 | else: |
| 56 | node_groups = inputs[1:] |
| 57 | |
| 58 | if self.neg_sample_fn is not None: |
| 59 | neg_node_groups = self.neg_sample_fn(node_groups) |
| 60 | neg_labels = tf.zeros_like(neg_node_groups[0], dtype=tf.float32) |
| 61 | label = tf.concat([label, neg_labels], axis=0) |
| 62 | pos_node_groups = node_groups |
| 63 | node_groups = [] |
| 64 | for pos_nodes, neg_nodes in zip(pos_node_groups, neg_node_groups): |
| 65 | node_groups.append(tf.concat([pos_node_groups, neg_node_groups], axis=0)) |
| 66 | |
| 67 | node_groups_embedding = self.embed(node_groups) |
| 68 | target_embedding, context_embedding, output_embedding = self.parse_group_emb_fn(node_groups_embedding) |
| 69 | logit = self.logit_fn(target_embedding, context_emb=context_embedding) |
| 70 | loss = self.loss_fn(label, logit) |
| 71 | metric = self.metric_class(label, logit) |
| 72 | embedding = output_embedding |
| 73 | return (embedding, loss, self.metric_name, metric) |
| 74 | |
| 75 | class UnsuperviseSampleSolution(object): |
| 76 | def __init__(self, |
no outgoing calls
no test coverage detected