tf.cond() 返回形状未知的张量 [英] tf.cond() returning a tensor of shape unknown

查看:40
本文介绍了tf.cond() 返回形状未知的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我传递给 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.

  1. tf.math.segment_mean() 期望 class_segments 与输入 t 的第一维具有相同的形状.所以 None 必须等于 6 才能运行你的代码.这很可能是导致您获得 unknown 形状的原因 - 因为张量的形状取决于在运行时确定的 None.您可以为要运行的代码应用转换(不确定这是否是您要实现的目标),例如.
  1. tf.math.segment_mean() expects class_segments to have the same shape as first dimension of your input t. So None must be equal 6 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 on None 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)

  1. tf.cond()true_fnfalse_fn 必须返回相同形状的张量.在您的情况下 true_fn 返回 (None, 6) 因为 broadcastingfalse_fn 返回形状为 (6,) 的张量.
  2. tf.cond() 中的谓词a> 必须降到 0 级.例如,如果您要申请b = tf.math.argmax(tf.math.segment_mean(tf.transpose(t), class_segments), 0)那么 b 的形状将是 (None)tf.cond() 将是 广播 到相同的形状(这会引发错误).
  1. In tf.cond() true_fn and false_fn must return tensors of same shape. In your case true_fn returns (None, 6) because of broadcasting and false_fn returns tensor of shape (6,).
  2. The predicate in tf.cond() must be reduced to a rank 0. For example, if you were to apply b = tf.math.argmax(tf.math.segment_mean(tf.transpose(t), class_segments), 0) then the shape of b would be (None) and the predicate pred in tf.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屋!

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