CompareAndBitpack

public final class CompareAndBitpack

Compare values of `input` to `threshold` and pack resulting bits into a `uint8`.

Each comparison returns a boolean `true` (if `input_value > threshold`) or and `false` otherwise.

This operation is useful for Locality-Sensitive-Hashing (LSH) and other algorithms that use hashing approximations of cosine and `L2` distances; codes can be generated from an input via:

codebook_size = 50
 codebook_bits = codebook_size * 32
 codebook = tf.get_variable('codebook', [x.shape[-1].value, codebook_bits],
                            dtype=x.dtype,
                            initializer=tf.orthogonal_initializer())
 codes = compare_and_threshold(tf.matmul(x, codebook), threshold=0.)
 codes = tf.bitcast(codes, tf.int32)  # go from uint8 to int32
 # now codes has shape x.shape[:-1] + [codebook_size]
 
NOTE: Currently, the innermost dimension of the tensor must be divisible by 8.

Given an `input` shaped `[s0, s1, ..., s_n]`, the output is a `uint8` tensor shaped `[s0, s1, ..., s_n / 8]`.

Constants

String OP_NAME The name of this op, as known by TensorFlow core engine

Public Methods

Output<TUint8>
asOutput()
Returns the symbolic handle of the tensor.
static <T extends TType> CompareAndBitpack
create(Scope scope, Operand<T> input, Operand<T> threshold)
Factory method to create a class wrapping a new CompareAndBitpack operation.
Output<TUint8>
output()
The bitpacked comparisons.

Inherited Methods

Constants

public static final String OP_NAME

The name of this op, as known by TensorFlow core engine

Constant Value: "CompareAndBitpack"

Public Methods

public Output<TUint8> asOutput ()

Returns the symbolic handle of the tensor.

Inputs to TensorFlow operations are outputs of another TensorFlow operation. This method is used to obtain a symbolic handle that represents the computation of the input.

public static CompareAndBitpack create (Scope scope, Operand<T> input, Operand<T> threshold)

Factory method to create a class wrapping a new CompareAndBitpack operation.

Parameters
scope current scope
input Values to compare against `threshold` and bitpack.
threshold Threshold to compare against.
Returns
  • a new instance of CompareAndBitpack

public Output<TUint8> output ()

The bitpacked comparisons.