前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

KWIK

作者头像
杨熹
发布2020-04-10 15:57:36
5790
发布2020-04-10 15:57:36
举报

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)    
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档