【问题标题】:Tensorflow bit-wise NOT operation for floats用于浮点数的 Tensorflow 按位非运算
【发布时间】:2017-06-17 15:54:10
【问题描述】:

我有一个 Tensorflow 布尔向量,我需要对其进行按位反转,以使所有 0(假)的值都变为 1(真),反之亦然。 tf.logical_not 存在,但它只需要布尔向量。是否存在在整数或浮点数向量上执行此操作的 Tensorflow,因此我不需要重新转换我的张量?

【问题讨论】:

    标签: tensorflow bit-manipulation


    【解决方案1】:

    我看到的唯一方法是将张量 tf.cast 转换为 tf.bool 然后调用 tf.logical_not 然后再回退。

    dtype = tensor.dtype
    btensor = tf.cast(tensor, tf.bool)
    not_tensor = tf.logical_not(btensor)
    tensor = tf.cast(not_tensor, dtype)
    

    【讨论】:

    • 如果是按值,而不是按位,那么 x = 1 怎么样。 - x
    【解决方案2】:

    TensorFlow 不包含用于执行按位 NOT 的特定运算符,但您可以使用 tf.bitcast() 和整数上的算术运算来模拟它。例如,以下将反转单个浮点值中的位:

    input_value = tf.constant(37.0)
    
    bitcast_to_int32 = tf.bitcast(input_value, tf.int32)
    
    invert_bits = tf.constant(-1) - bitcast_to_int32
    
    bitcast_to_float = tf.bitcast(invert_bits, tf.float32)
    

    您可以使用this answer 将浮点值转换为二进制来确认输入和输出是按位取反的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 2011-07-17
      • 2011-01-13
      • 2010-12-15
      • 2022-07-17
      • 1970-01-01
      • 2012-06-17
      相关资源
      最近更新 更多