python - Tensorflow 中的 2D bool 掩码

标签 python tensorflow

我有一个可能很简单的 Tensorflow 问题。我试图弄清楚当我的掩码是 1D 时如何执行 2D boolean_mask。我有一个具有 N X F 维的 2D 张量 x 和一个具有 N 维的一维张量 maskmask 包含 0 和 1,我想将 xmask 值为 1 的样本保留。

threshold = tf.multiply(tf.ones_like(mask), 0.5)
mask = tf.greater(mask,threshold)
x = tf.boolean_mask(x,mask)

由于形状不兼容,我收到值错误。有没有简单的方法可以解决这个问题?

最佳答案

其中 N 是我想要扩展的功能数量。

threshold = tf.multiply(tf.ones_like(mask), 0.5)
mask = tf.greater(mask,threshold)
x = tf.boolean_mask(x,tf.tile(mask,[1,N]))

关于python - Tensorflow 中的 2D bool 掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44478149/

相关文章:

c++ - 将列表从 python 传递到 C++ vector 时如何加速 Boost::Python::Extract

python - 如何在 tkinter 中的 Treeview 上禁用多重选择

python - 属性错误 : __enter__ from "with tf.Session as sess:"

python - tf.estimator.Estimator 不记录任何事件文件,张量板不显示任何内容

python - 在数据框列上应用 curve_fit

python - 为什么在 MacOS 中启动 Django 服务器时出现错误?

Python 猴子修补由另一个模块执行的模块

python - 如何将 tf.data 转换应用于 DataFrame

python - 在 EC2 实例中调用 plot 时为 "Unknown element o"

python - tensorflow MNIST TFRecord