How to find indices from a Keras tensor for a particular value similar to numpy.where()

1,560 views
Skip to first unread message

sarith.k...@gmail.com

unread,
Apr 23, 2017, 6:51:30 PM4/23/17
to Keras-users

Hi,


I am searching for a command which is similar to python "numpy.where()". Basically, my idea is to extract the indices from a tensor. In python I can do simply f_j=(np.where(X==j)) which gives me specific indices(f_j) for the value j.


ex: X= [0 1 1 0 0 2 3 ]
f_j=(np.where(X==1))
f_j= [1 2]


Is there is any similar function which I can use for this purpose?


I tried to write array search for a tensor. However I end up with error when calling "if K.equal():" line as
TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.


y_true = [0 1 1 0 0 2 3 ]

def loss(y_true, y_pred):
         b=K.equal(y_true,0)
         b=K.cast(b,dtype='float32')
         for i in range(0,7):
                 if K.equal(b[i],1):
                        ........


 I am wondering why there is not any inbuilt function for this purpose. In keras backend simply If I know the indices I can use K.gather() to select those values. However, when comes to this problem I couldn't able to think of any method to find indices by using existing keras backend commands. So the only option is writing a searching algorithm using "for loop" and "if command". Moreover, when writing if command apparently it does not support keras boolean tensors.

Daπid

unread,
Apr 24, 2017, 3:17:29 AM4/24/17
to sarith.k...@gmail.com, Keras-users
What is your ultimate goal? Which loss function are you trying to implement? I fear this may be a instance of the XY problem, and there may be easier ways to accomplish it.

https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/9068a1b3-b74f-4e2f-a732-98be6e41d108%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sarith Fernando

unread,
Apr 24, 2017, 3:38:52 AM4/24/17
to Keras-users, sarith.k...@gmail.com
Hi, thanks for the reply. The python version of the loss function is as bellow. Originally taken from http://www.itl.nist.gov/iad/mig/tests/lre/2007/LRE07EvalPlan-v8b.pdf

________________________________________________
from __future__ import print_function
import numpy as np
from numpy import genfromtxt

classf = genfromtxt('classf', delimiter=',')-1
evaluand = genfromtxt('evaluand', delimiter=',')


def avg_detection_cost(evaluand,classf):
    Ptar=0.5
    NoTargets,T=evaluand.shape
    NoClasses =int(max(classf))+1
    Priors=(np.ones(NoTargets)-np.eye(NoTargets))*(1-Ptar)/(NoTargets-1)+np.eye(NoTargets)*Ptar
    c=0
    for j in range(NoClasses):
        f_j=(np.where(classf==j))
        count_j = len(f_j[0])
        for i in range(NoTargets):
            c=c+Priors[j][i]*np.sum(cavg(evaluand[i][f_j],i==j))/count_j


    c=c/NoTargets
    return c

def cavg(decisions,target):
    Cmiss=1
    Cfa =1
    if target:
        ca=Cmiss*(decisions==0)
    else:
        ca =Cfa*(decisions==1)
    return ca


cd= avg_detection_cost(evaluand,classf)
print(cd)

__________________________________________________________________

I have successfully converted almost 90% of the code to work with keras. Here is the keras version.

def c_loss(y_true, y_pred):
    y_pred=K.clip(y_pred, _EPSILON, 1.0 - _EPSILON)
    y_pred=K.log(y_pred)

    Ptar=0.5
    NoTargets=14
    Priors=K.variable(np.ones(NoTargets)-np.eye(NoTargets))*(1-Ptar)/(NoTargets-1)+np.eye(NoTargets)*Ptar
    c=K.variable(0)
    for j in range(0,14):#range(NoClasses):
        f_j=np.array(np.where(y_true==j))
        for i in range(0,14):#range(NoTargets):

            f_jj = K.reshape(K.gather(y_pred[i], f_j),(-1,1))
            z=K.mean(cavgt(f_jj,i==j))
            c=c+Priors[j][i]*z

    c=c/K.constant(NoTargets)
    score = K.ones_like(y_true) * c
    return score

def cavgt(decisions,target):
    if target:
        ca=K.equal(decisions,0)
    else:
        ca =K.equal(decisions,1)
    return ca

However, the highlighted line is the problem which I am struggling to fix. Any idea how can I fix it? And also wondering whether that I am doing it correctly or not?


On Monday, April 24, 2017 at 5:17:29 PM UTC+10, David Menéndez Hurtado wrote:
What is your ultimate goal? Which loss function are you trying to implement? I fear this may be a instance of the XY problem, and there may be easier ways to accomplish it.

https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem
On 24 April 2017 at 00:51, <sarith.k...@gmail.com> wrote:

Hi,


I am searching for a command which is similar to python "numpy.where()". Basically, my idea is to extract the indices from a tensor. In python I can do simply f_j=(np.where(X==j)) which gives me specific indices(f_j) for the value j.


ex: X= [0 1 1 0 0 2 3 ]
f_j=(np.where(X==1))
f_j= [1 2]


Is there is any similar function which I can use for this purpose?


I tried to write array search for a tensor. However I end up with error when calling "if K.equal():" line as
TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.


y_true = [0 1 1 0 0 2 3 ]

def loss(y_true, y_pred):
         b=K.equal(y_true,0)
         b=K.cast(b,dtype='float32')
         for i in range(0,7):
                 if K.equal(b[i],1):
                        ........


 I am wondering why there is not any inbuilt function for this purpose. In keras backend simply If I know the indices I can use K.gather() to select those values. However, when comes to this problem I couldn't able to think of any method to find indices by using existing keras backend commands. So the only option is writing a searching algorithm using "for loop" and "if command". Moreover, when writing if command apparently it does not support keras boolean tensors.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages