tf.cond() 返回形状未知的张量 [英] tf.cond() returning a tensor of shape unknown
问题描述
下面是我传递给 keras Lambda 层的函数.
Below is the function that I am passing to a keras Lambda layer.
tf.cond()
的输出出现问题.它返回
的形状.输入张量 (t) 和恒权张量分别具有 (None,6)
和 (6,)
的形状.当我在 tf.cond()
之外添加这两个时,我会得到一个形状为 (None,6)
的张量,这正是我需要的.但是,当从 tf.cond()
中返回相同的添加操作时,我得到一个形状为
的张量.
I am getting a problem with the output of tf.cond()
. It returns a shape of <unknown>
. The input tensor (t) and the constant weight tensor have shapes of (None,6)
and (6,)
, respectively. When I add these two outside of tf.cond()
then I get a tensor of shape (None,6)
, which is what I need it to be. However, when the same add operation is returned from within tf.cond()
, I get a tensor of shape <unknown>
.
当这个操作通过 tf.cond()
进行时会发生什么变化.
What changes when this operation goes via tf.cond()
.
def class_segmentation(t):
class_segments = tf.constant([0,0,1,1,2,2])
a = tf.math.segment_mean(t, class_segments, name=None)
b = tf.math.argmax(a)
left_weights = tf.constant([1.0,1.0,0.0,0.0,0.0,0.0])
middle_weights = tf.constant([0.0,0.0,1.0,1.0,0.0,0.0])
right_weights = tf.constant([0.0,0.0,0.0,0.0,1.0,1.0])
zero_weights = tf.constant([0.0,0.0,0.0,0.0,0.0,0.0])
c = tf.cond(tf.math.equal(b,0), lambda: tf.math.add(t, left_weights), lambda: zero_weights)
d = tf.cond(tf.math.equal(b,1), lambda: tf.math.add(t, middle_weights ), lambda: zero_weights)
e = tf.cond(tf.math.equal(b,2), lambda: tf.math.add(t, right_weights), lambda: zero_weights)
f = tf.math.add_n([c,d,e])
print("Tensor shape: ", f.shape) # returns "Unknown"
return f
推荐答案
您的代码存在一些问题.
You have a few problems in your code.
tf.math.segment_mean()
期望class_segments
与输入t
的第一维具有相同的形状.所以None
必须等于6
才能运行你的代码.这很可能是导致您获得 unknown 形状的原因 - 因为张量的形状取决于在运行时确定的None
.您可以为要运行的代码应用转换(不确定这是否是您要实现的目标),例如.
tf.math.segment_mean()
expectsclass_segments
to have the same shape as first dimension of your inputt
. SoNone
must be equal6
in order for your code to run. This is most likely cause of you getting the unknown shape - because the shape of your tensors depends onNone
which is determined on runtime. You could apply transformation for your code to run (not sure if that is what you are trying to achieve), eg.
a = tf.math.segment_mean(tf.transpose(t), class_segments)
- 在
tf.cond()
true_fn
和false_fn
必须返回相同形状的张量.在您的情况下true_fn
返回(None, 6)
因为 broadcasting 和false_fn
返回形状为(6,)
的张量. tf.cond()
中的谓词a> 必须降到 0 级.例如,如果您要申请b = tf.math.argmax(tf.math.segment_mean(tf.transpose(t), class_segments), 0)
那么b
的形状将是(None)
和tf.cond()
将是 广播 到相同的形状(这会引发错误).
- In
tf.cond()
true_fn
andfalse_fn
must return tensors of same shape. In your casetrue_fn
returns(None, 6)
because of broadcasting andfalse_fn
returns tensor of shape(6,)
. - The predicate in
tf.cond()
must be reduced to a rank 0. For example, if you were to applyb = tf.math.argmax(tf.math.segment_mean(tf.transpose(t), class_segments), 0)
then the shape ofb
would be(None)
and the predicatepred
intf.cond()
will be broadcasted to the same shape (which will raise an error).
如果不知道您想要获得进一步的帮助是不可能的.
Without knowing what are you trying to achieve further help is impossible.
这篇关于tf.cond() 返回形状未知的张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!