1
$\begingroup$

I notice there are attempts to implement WARP loss in Keras such as (https://stackoverflow.com/questions/46299554/implimentation-of-warp-loss-in-keras) But I have not seen any githubs or publications of a tensorflow version of WARP loss. I was seeing where to start to implement the algorithm.

The current implementation is:

def warp_loss(y, yhat):
    # y: (10,1)
    # yhat: (10, 1)
    # for all positives randomly sample until we find yhat_pos < yhat_neg
    max_tries = 9
    y = tf.squeeze(y)
    y = tf.Print(y, [y], summarize=-1)
    yhat = tf.squeeze(yhat)
    positive = tf.zeros_like(yhat)
    negative = tf.zeros_like(yhat)
    # Gather Zero Indicies
    zero = tf.constant(0, dtype=tf.float32)
    where = tf.not_equal(y, zero)
    one_ind = tf.where(where)
    #Gather 1 Indicies
    where = tf.equal(y, zero)
    zero_ind = tf.where(where)
    one_ind = tf.squeeze(one_ind, -1)
    zero_ind = tf.squeeze(zero_ind, -1)
    one_ind = tf.Print(one_ind, [one_ind], summarize=-1)
    time_steps = tf.shape(y)[0]
    searches = tf.constant([1], shape=())
    # Loop for random sample
    def condition(x):
        x = tf.add(x, 1)
        return x <= time_steps

    def body(x):
        # Sample and compare
        r_pos = tf.reshape(tf.py_func(lambda x: np.random.choice(x,1),[one_ind], tf.int32),())
        r_neg = tf.reshape(tf.py_func(lambda x: np.random.choice(x,1),[zero_ind], tf.int32),())
        res = tf.cond(tf.less(yhat[r_pos],yhat[r_neg]), lambda: tf.multiply(tf.subtract(yhat[r_neg], yhat[r_pos]), tf.cast(tf.log(tf.divide(x, tf.constant([max_tries]))),tf.float32)), lambda: tf.constant(0, dtype=tf.float32))
        return tf.reshape(res, ())
    #N = searches
    #L = np.log(9)/N
    #total_loss = L * difference
    res = tf.while_loop(condition, body, [searches])
    return tf.cast(res, tf.float32)#tf.reduce_sum(tf.add(tf.reduce_sum(tf.cast(one_ind, tf.float32)), yhat))#tf.cast(tf.reduce_sum(one_ind), tf.float32)

But throws an error of:

ValueError: An operation has None for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

$\endgroup$

closed as off-topic by whuber Feb 2 at 0:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.