filter - tf.boolean_mask、mask_dimension必须指定吗?

标签 filter tensorflow boolean tensor

当使用tf.boolean_mask()时,会引发值错误。它显示“必须指定掩码维度数,即使某些维度为“无”。例如 shape=[None] 可以,但 shape=None 则不行。

我怀疑当我创建 boolean 掩码时出了问题,因为当我手动创建 boolean 掩码时,一切正常。但是,到目前为止我已经检查了 s 的形状和 dtype,没有发现任何可疑的地方。两者似乎与我手工创建的 boolean 蒙版的形状和类型相同。

Please see a screenshot of the problem. 以下内容应该允许您在您的计算机上重现该错误。您需要tensorflow、numpy 和scipy。

with tf.Session() as sess:
    # receive five embedded vectors
    v0 = tf.constant([[3.0,1.0,2.,4.,2.]])
    v1 = tf.constant([[4.0,0,1.0,4,1.]])
    v2 = tf.constant([[1.0,1.0,0.0,4.,8.]])
    v3 = tf.constant([[1.,4,2.,5.,2.]])
    v4 = tf.constant([[3.,2.,3.,2.,5.]])

    # concatenate the five embedded vectors into a matrix
    VT = tf.concat([v0,v1,v2,v3,v4],axis=0)

    # perform SVD on the concatenated matrix
    s, u1, u2   = tf.svd(VT)
    e = tf.square(s) # list of eigenvalues
    v = u1 # eigenvectors as column vectors

    # sample a set
    s = tf.py_func(sample_dpp_bin,[e,v],tf.bool)
    X = tf.boolean_mask(VT,s)
    print(X.eval())

这是生成s的代码。 s 是行列式点过程的样本(对于数学感兴趣的人)。 请注意,我使用 tf.py_func 来包装此 python 函数:

import tensorflow as tf
import numpy as np
from scipy.linalg import orth

def sample_dpp_bin(e_val,e_vec):
    # e_val = np.array of eigenvalues
    # e_vec = array of eigenvectors (= column vectors)
    eps = 0.01

    # sample a set of eigenvectors
    ind = (np.random.rand(len(e_val)) <= (e_val)/(1+e_val))
    k = sum(ind)
    if k == e_val.size:
        return np.ones(e_val.size,dtype=bool) # check for full set
    if k == 0:
        return np.zeros(e_val.size,dtype=bool)
    V = e_vec[:,np.array(ind)]

    # sample a set of k items 
    sample = np.zeros(e_val.size,dtype=bool)
    for l in range(k-1,-1,-1):
        p = np.sum(V**2,axis=1)
        p = np.cumsum(p / np.sum(p)) # item cumulative probabilities
        i = int((np.random.rand() <= p).argmax()) # choose random item
        sample[i] = True

        j = (np.abs(V[i,:])>eps).argmax() # pick an eigenvector not orthogonal to e_i
        Vj = V[:,j]
        V = orth(V - (np.outer(Vj,(V[i,:]/Vj[i]))))

    return sample

如果我打印 s 和 tf.reshape(s) 的输出是

[False  True  True  True  True]
[5]

如果我打印 VT 和 tf.reshape(VT) 的输出是

[[ 3.  1.  2.  4.  2.]
 [ 4.  0.  1.  4.  1.]
 [ 1.  1.  0.  4.  8.]
 [ 1.  4.  2.  5.  2.]
 [ 3.  2.  3.  2.  5.]]
[5 5]   

非常感谢任何帮助。

最佳答案

以下示例适用于我。

import tensorflow as tf
import numpy as np

tensor = [[1, 2], [3, 4], [5, 6]]
mask = np.array([True, False, True])

t_m = tf.boolean_mask(tensor, mask)
sess = tf.Session()
print(sess.run(t_m))

输出:

[[1 2]
 [5 6]]

提供可运行的代码片段以重现错误。我认为你可能在 s 中做错了什么。

更新:

s = tf.py_func(sample_dpp_bin,[e,v],tf.bool)
s_v = (s.eval())
X = tf.boolean_mask(VT,s_v)
print(X.eval())

mask 应该是 np 数组而不是 TF 张量。您不必使用 tf.pyfunc。

关于filter - tf.boolean_mask、mask_dimension必须指定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010410/

相关文章:

c++ - 使用 bool 作为左值

python - 使用列表根据不同的列值更改多个 boolean 列的值

java - Spring过滤请求

date - Power BI如何根据日期切片器和过滤器显示列的最新值?

sql - 如何遍历结构的字段以进行查询过滤

python - 在同一个 GPU 上运行多个 tensorflow 进程不安全吗?

javascript - tensorflow.js model.predict() 打印张量 [[NaN],]

python - 保存使用 BatchNorm 的 Tensorflow 模型

r - 比较 boolean 向量

c# - FileSystemWatcher 检测 zip 文件