代码之家  ›  专栏  ›  技术社区  ›  n0shadow

使用位操作确定多热编码的有效性

  •  0
  • n0shadow  · 技术社区  · 4 年前

    假设我有 N 项和表示在结果中包含这些项的二进制数:

    N = 4
    
    # items 1 and 3 will be included in the result
    vector = 0b0101
    
    # item 2 will be included in the result
    vector = 0b0010
    
    

    我还收到了一份冲突列表,其中指出了哪些项目不能同时包含在结果中:

    conflicts = [
      0b0110, # any result that contains items 1 AND 2 is invalid
      0b0111, # any result that contains AT LEAST 2 items from {1, 2, 3} is invalid
    ]
    

    给定此冲突列表,我们可以确定早期冲突的有效性 vector s:

    # invalid as it triggers conflict 1: [0, 1, 1, 1]
    vector = 0b0101
    
    # valid as it triggers no conflicts
    vector = 0b0010
    

    在这种情况下,如何使用位操作来确定向量或大型向量列表相对于冲突列表的有效性?

    提供的解决方案 here 我们已经完成了大部分工作,但我不确定如何将其适应整数用例(以完全避免numpy数组和numba)。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Franck    4 年前
    N = 4
    
    # items 1 and 3 will be included in the result
    vector = 0b0101
    
    # item 2 will be included in the result
    vector = 0b0010
    
    conflicts = [
      0b0110, # any result that contains items 1 AND 2 is invalid
      0b0111, # any result that contains AT LEAST 2 items from {1, 2, 3} is invalid
    ]
    
    def find_conflict(vector, conflicts):
        found_conflict = False
        for v in conflicts:
            result = vector & v # do a logical AND operation
            if result != 0: # there are common elements
                number_of_bits_set = bin(result).count("1") # count number of common elements
                if number_of_bits_set >= 2: # check common limit for detection of invalid vectors
                    found_conflict = True
                    print(f"..Conflict between {bin(vector)} and {bin(v)}: {bin(result)}")
        if found_conflict:
            print(f"Conflict found for {bin(vector)}.")
        else:
            print(f"No conflict found for {bin(vector)}.")
    
    # invalid as it triggers conflict 1: [0, 1, 1, 1]
    vector = 0b0101
    find_conflict(vector, conflicts)
    
    # valid as it triggers no conflicts
    vector = 0b0010
    find_conflict(vector, conflicts)
    
    $ python3 pythontest.py
    ..Conflict between 0b101 and 0b111: 0b101
    Conflict found for 0b101.
    No conflict found for 0b10.
    $