Python 轴上的张量流条件

Python 轴上的张量流条件,python,numpy,tensorflow,keras,Python,Numpy,Tensorflow,Keras,我试图收集keras中张量内特定张量/(向量/矩阵)的指数。因此,我尝试将tf.gather与tf.where一起使用,以获取要在聚集函数中使用的索引 但是,tf.where在测试相等性时为匹配值提供元素索引。我希望能够找到与另一个相等的张量(向量)的索引(行) 这对于在张量中找到匹配一组感兴趣的一个热向量的一个热向量特别有用 到目前为止,我有一些代码来说明这个缺点: # standard import tensorflow as tf import numpy as np from skle

我试图收集keras中张量内特定张量/(向量/矩阵)的指数。因此,我尝试将
tf.gather
tf.where
一起使用,以获取要在聚集函数中使用的索引

但是,
tf.where
在测试相等性时为匹配值提供元素索引。我希望能够找到与另一个相等的张量(向量)的索引(行)

这对于在张量中找到匹配一组感兴趣的一个热向量的一个热向量特别有用

到目前为止,我有一些代码来说明这个缺点:

# standard 
import tensorflow as tf
import numpy as np
from sklearn.preprocessing import LabelBinarizer
sess = tf.Session()

# one-hot vector encoding labels
l = LabelBinarizer()
l.fit(['a','b','c'])

# input tensor 
t = tf.constant(l.transform(['a','a','c','b', 'a']))

# find the indices where 'c' is label
# ***THIS WORKS***
np.all(t.eval(session = sess) == l.transform(['c']), axis = 1)

# We need to do everything in tensorflow and then wrap in Lambda layer for keras so...
from keras import backend as K
# ***THIS DOES NOT WORK***
K.all(t.eval(session = sess) == l.transform(['c']), axis = 1)

# go on from here to get smaller subset of vectors from another tensor with the indicies given by `tf.gather`
显然,上面的代码显示我已经尝试让axis实现这个条件,它在numpy中运行良好,但是tensorflow版本并不容易从numpy移植


有更好的方法吗?

与您所做的类似,我们可以使用
tf.reduce\u all
,它是
np的tensorflow等价物。all

tf.reduce_all(t.eval(session = sess) == l.transform(['c']), axis = 1)