【问题标题】:Writing an if statement in tensorflow and comparing the values of two tensors在 tensorflow 中编写 if 语句并比较两个张量的值
【发布时间】:2017-01-18 13:44:18
【问题描述】:

我有条件:

if tf.shape(n_spec)[0] < tf.shape(s_spec)[0]:
    n_spec = tf.concat(0, [tf.zeros([empty_cols, n_spec.get_shape()[1]], tf.int32), n_spec]) 

其中 n_spec 和 s_spec 是两个张量(二维数组),如果一个在 0 维中小于另一个,我想执行该连接。当我尝试这个时,tensorflow 会抛出一个错误:

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

如何重新表述上述内容?

【问题讨论】:

    标签: if-statement boolean tensorflow


    【解决方案1】:

    从您的代码来看,n_spec 的形状似乎是静态已知的(因为您使用的是n_spec.get_shape())。在这种情况下,您可以简单地执行以下操作:

    if n_spec.get_shape()[0] < s_spec.get_shape()[0]:
      n_spec = tf.concat(0, [tf.zeros([empty_cols, n_spec.get_shape()[1]], tf.int32), n_spec])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-21
      • 2017-10-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 2017-11-16
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多