Open2

torch.unique - 2D

PINTOPINTO
def tf_unique_2d(x):
    x_shape = tf.cast(tf.shape(x), dtype=tf.int64)  # (3,2)
    tf.print('x_shape[0]', x_shape[0])
    tf.print('x_shape[1]', x_shape[1])



    x1 = tf.tile(x, [1, x_shape[0]])  # [[1,2],[1,2],[1,2],[3,4],[3,4],[3,4]..]
    x2 = tf.tile(x, [x_shape[0], 1])  # [[1,2],[1,2],[1,2],[3,4],[3,4],[3,4]..]


    tf.print("[x_shape[0] * x_shape[0] =", np.asarray([x_shape[0] * x_shape[0]], dtype=np.int64))
    x1_2 = tf.reshape(x1, [x_shape[0] * x_shape[0], x_shape[1]])
    x2_2 = tf.reshape(x2, [x_shape[0] * x_shape[0], x_shape[1]])
    cond = tf.reduce_all(tf.equal(x1_2, x2_2), axis=1)
    cond = tf.reshape(cond, [x_shape[0], x_shape[0]])  # reshaping cond to match x1_2 & x2_2
    cond_shape = tf.shape(cond)
    cond_cast = tf.cast(cond, tf.int32)  # convertin condition boolean to int
    cond_zeros = tf.zeros(cond_shape, tf.int32)  # replicating condition tensor into all 0's

    # CREATING RANGE TENSOR
    r = tf.range(x_shape[0])
    r = tf.add(tf.tile(r, [x_shape[0]]), 1)
    r = tf.reshape(r, [x_shape[0], x_shape[0]])

    # converting TRUE=1 FALSE=MAX(index)+1 (which is invalid by default) so when we take min it wont get selected & in end we will only take values <max(indx).
    f1 = tf.multiply(tf.ones(cond_shape, tf.int64), x_shape[0] + 1)
    f2 = tf.ones(cond_shape, tf.int64)
    cond_cast2 = tf.where(tf.equal(cond_cast, cond_zeros), f1, f2)  # if false make it max_index+1 else keep it 1

    # multiply range with new int boolean mask
    r_cond_mul = tf.multiply(r, cond_cast2)
    r_cond_mul2 = tf.reduce_min(r_cond_mul, axis=1)
    r_cond_mul3, unique_idx = tf.unique(r_cond_mul2)
    r_cond_mul4 = tf.subtract(r_cond_mul3, 1)

    # get actual values from unique
    op = tf.gather(x, r_cond_mul4)

    return (op)

# torch.Size([46971, 2])
# ↓
# torch.Size([3951, 2])

# my_tensor = tf.constant([[1, 2],
#                          [3, 4],
#                          [5, 6],
#                          [1, 2]])
my_tensor = tf.random.uniform([46971, 2], minval=1, maxval=512,dtype=tf.int64)
tf.print("my_tensor.shape =", my_tensor.shape)
op = tf_unique_2d(my_tensor)
tf.print("op =", op)