KWIK

作者: 不会停的蜗牛 | 来源:发表于2020-04-07 23:58 被阅读0次

KWIK 算法来自论文 Knows What It Knows: A Framework For Self-Aware Learning

下面实现其中的 Algorithm 2: The enumeration algorithm

首先问题中列出所有的可能组合情况,然后就是要遍历每个情况

用 votes 来存储每个组合情况的投票结果
遍历 H 中的每个 h
h 的第一个位置当作 instigator的,第二个位置是 peacemaker的
patrons 就是一轮的出席数据,例如 0 0 0 1
所以就是 patrons 是固定的一个,按照 H 中的 12 个下标组合情况一个一个去试验,每个组合就是假定这就是真正的下标了,然后给出fight的结果



def pred_or_learn(patrons, num_patrons, H, fight):
    
    # corner case:if all patrons present, no fight will occur for sure
    if sum(patrons) == num_patrons:
        return (H, 0)
    
    # 用来存储每个组合情况的投票结果
    votes = []
    
    # 遍历 H 中的每个 h
    for h in H:
        
        instigator_ind, peacemaker_ind = h[0], h[1]         # 【h 的第一个位置当作 instigator的,第二个位置是 peacemaker的】
        instigator_presence = (patrons[instigator_ind] == 1)    # 【patrons 就是一轮的出席数据,例如 0 0 0 1】
        peacemaker_presence = (patrons[peacemaker_ind] == 1)    # 【所以就是 patrons 是固定的一个,按照 H 中的 12 个下标组合情况一个一个去试验,每个组合就是假定这就是真正的下标了,然后给出fight的结果】
        
        # fight 产生的条件
        if (instigator_presence and not peacemaker_presence):
            votes.append(1)
        else:
            votes.append(0)

    if (sum(votes) == len(votes) or sum(votes) == 0):       
        return (H, votes[0])
    
    else:
        remove_indices = [i for i, x in enumerate(votes) if x == (1-fight)]
        for ind in sorted(remove_indices, reverse=True):
            del H[ind]
        return (H, -1)    

相关文章

网友评论

    本文标题:KWIK

    本文链接:https://www.haomeiwen.com/subject/bfpaphtx.html