Exemplo n.º 1
0
 def cluster_emb(self, entities):
     kmeans_model, kmeans_model2, left_weights, right_weights =\
         self.cluster(self, entities)
     left_cluster_labels = kmeans_model.labels_
     right_cluster_labels = kmeans_model2.labels_
     left_emb = kmeans_model.cluster_centers_
     right_emb = kmeans_model2.cluster_centers_
     left_weight_result = autovivify_list()
     right_weight_result = autovivify_list()
     for w, c in zip(left_weights, left_cluster_labels):
         left_weight_result[c].append(w)
     for w, c in zip(right_weights, right_cluster_labels):
         right_weight_result[c].append(w)
     left_weight = [0.] * 100
     right_weight = [0.] * 100
     for c, weights in left_weight_result.items():
         left_weight[c] = sum(weights)
     for c, weights in right_weight_result.items():
         right_weight[c] = sum(weights)
     left_emb = torch.tensor(left_emb, dtype=torch.float).unsqueeze(0)
     left_weight = weight_normalize(left_weight, min_v=1e-3, max_v=0.2)
     left_weight = torch.tensor(left_weight, dtype=torch.float).unsqueeze(0)
     right_emb = torch.tensor(right_emb, dtype=torch.float).unsqueeze(0)
     right_weight = weight_normalize(right_weight, min_v=1e-3, max_v=0.2)
     right_weight = torch.tensor(
         right_weight, dtype=torch.float).unsqueeze(0)
     return left_emb, left_weight, right_emb, right_weight
Exemplo n.º 2
0
def sort_expanding_patterns_by_meb(policy, state, storage, num=100):
    """
    use meb function to filter top_num patterns (with weights)

    :param storage:
    :param state:
    :param num:  (Default value = 100)

    """
    # all_pos = len(state.top) + len(state.pos_seeds)
    patterns = []
    for p, (df, pos_matched, neg_matched) in state.extracted_patterns.items():
        weight = rlogf(df, pos_matched, neg_matched)
        patterns.append((p, weight))

    items = heapq.nlargest(num * 2, patterns, key=lambda x: x[1])

    ps = [p for p, _ in items]
    probs, _ = policy.predict(state, ps)

    pattern_probs = [(p, w) for p, w in zip(ps, probs)]

    items = heapq.nlargest(num, pattern_probs, key=lambda x: x[1])

    patterns = [p[0] for p in pattern_probs]
    pattern_weights = [p[1] for p in pattern_probs]
    pattern_weights = weight_normalize(pattern_weights)
    return patterns, pattern_weights
Exemplo n.º 3
0
 def get_patternset_embedding(self, patterns_weights):
     patterns = [p[0] for p in patterns_weights]
     weights = [p[1] for p in patterns_weights]
     weights = weight_normalize(weights)
     weights = torch.from_numpy(weights).unsqueeze(0).float()
     embeddings = self.get_direct_embeddings(patterns)
     entity_embedding = torch.mm(weights, embeddings)
     return entity_embedding.squeeze(0)
Exemplo n.º 4
0
 def get_adpe(self, storage, state, core_entities, context_count):
     adaptions = state.adaptions if state else None
     patterns, weights = get_adpe(storage,
                                  core_entities,
                                  context_count=context_count,
                                  adaptions=adaptions)
     weights = weight_normalize(weights)
     result = (patterns, weights)
     return [result]
Exemplo n.º 5
0
 def get_dpe(self, storage, state, entities, context_count):
     pattern_weights = []
     for entity in entities:
         patterns, weights = get_adpe(storage, [entity],
                                      context_count=context_count,
                                      adaptions=state.adaptions)
         weights = weight_normalize(weights)
         result = (patterns, weights)
         pattern_weights.append(result)
     return pattern_weights
 def predict(self, env, state, actions):
     weights = weight_normalize(state.priors)
     return weights.tolist(), 0