具有动态蒙版的Tensorflow boolean_mask [英] Tensorflow boolean_mask with dynamic mask

查看:215
本文介绍了具有动态蒙版的Tensorflow boolean_mask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boolean_mask的文档指出,必须静态知道蒙版的形状.但是如果你这样做

The documentation of boolean_mask says that the shape of the mask must be known statically. But if you do

mask.set_shape([None])
tf.boolean_mask(tensor, mask)

它似乎工作正常.有什么理由不这样做吗?

it seems to work fine. Is there any reason to not do this?

推荐答案

查看文档紧密揭示了它与面罩的尺寸有关,而不是其整个形状:

Looking at the documentation closely reveals that it concerns the dimensionality of the mask, not its whole shape:

mask:K-D布尔张量,K <= N,并且K必须是静态已知的.

mask: K-D boolean tensor, K <= N and K must be known statically.

您的蒙版现在的大小为None,这意味着它的静态形状(包括尺寸)是完全未知的.您的选择是要确保静态地知道蒙版的尺寸(例如,确保由输出尺寸已知的操作生成的蒙版,或以已知的尺寸填充占位符),或者强制执行有关您的尺寸的信息已知,但是不能在构建计算图时进行推断.您可以通过 set_shape 来完成.

Your mask now has size None, meaning its static shape is completely unknown, including the dimension. Your options are to either to ensure that the dimensionality of the mask is statically known (e.g., make sure its produced by an operation whose output dimensions are known, or feed a placeholder with known dimensions), or to enforce information about the size that you know, but that cannot be inferred at time of the construction of the computational graph. The latter you can do by set_shape.

运行mask.set_shape([None])时,您将强制假设蒙版的尺寸将始终为1(因为None在括号中),尽管元素的数量未知.如果确定蒙版始终是一维的,那很好.

When you run mask.set_shape([None]), you are enforcing an assumption that the dimensionality of the mask will always be 1 (since None is in brackets), although the number of elements is unknown. If you are certain that your mask will always be 1-dimensional, this is fine to do.

这篇关于具有动态蒙版的Tensorflow boolean_mask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆