必须指定tf.boolean_mask,mask_dimension?

时间:2017-05-16 19:51:18

标签: filter tensorflow boolean tensor

使用tf.boolean_mask()时,会引发值错误。它读取"必须指定掩模尺寸的数量,即使某些尺寸为无。例如。 shape = [None]没问题,但shape = None不行。

我怀疑在创建布尔掩码时会出现问题,因为当我手工创建一个布尔掩码时,一切正常。但是,到目前为止,我已经检查了s的形状和类型,并且无法发现任何可疑的东西。两者似乎都与我手工创建的布尔掩码的形状和类型相同。

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]   

任何帮助非常感谢。

2 个答案:

答案 0 :(得分:1)

以下示例适用于我。

imagesAdi.getImages()
    .map { list -> list.map { chooseRaceMapper.invoke(it) } }
    .doOnSuccess { }

输出:

public class YourActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_menu_category);

    recyclerView = (RecyclerView) findViewById(R.id.food_menu);
    GridLayoutManager mGrid = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mGrid);
    recyclerView.setHasFixedSize(true);

}
}

提供可运行的代码段以重现错误。我想你可能在做错了。

更新

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))

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

答案 1 :(得分:0)

错误消息指出未定义蒙版的形状。如果您打印tf.shape(s),您会得到什么?我打赌您的代码问题是s的形状完全未知,您可以使用s.set_shape((None))这样的简单调用来解决这个问题(只需指定s是一维张量)。请考虑以下代码段:

X = np.random.randint(0, 2, (100, 100, 3))
with tf.Session() as sess:
    X_tf = tf.placeholder(tf.int8)
    # X_tf.set_shape((None, None, None))
    y = tf.greater(tf.reduce_max(X_tf, axis=(0, 1)), 0)
    print(tf.shape(y))
    z = tf.boolean_mask(X_tf, y, axis=2)
    print(sess.run(z, feed_dict={X_tf: X}))

这会打印Tensor("Shape_3:0", shape=(?,), dtype=int32)的形状(即使y的尺寸都未知)并返回与您相同的错误。但是,如果您取消注释set_shape行,则X_tf已知为3维,因此s为1维。然后代码工作。所以,我认为您需要做的就是在s.set_shape((None))来电后添​​加py_func来电。

相关问题