title: Tensorflow, tf.case() vs tf.cond() with logical operation
body: <p>
  I have 2 tensors X, and Y. </p>
  
  <pre><code>X = tf.constant([[-1,-2,-3],[4,5,6]], dtype=tf.float32)
  Y = tf.constant([[-2,2,2],[-2,2,2]], dtype=tf.float32)
  </code></pre>
  
  <p>The operation I want is:</p>
  
  <p>if ((X > 0 and Y > 0) or (X &lt; 0 and Y &lt; 0)): X+Y
  else: X-Y</p>
  
  <p>I followed the sample at the <a href="https://www.tensorflow.org/api_docs/python/tf/case" rel="nofollow noreferrer">tf.case</a>:</p>
  
  <p>Example 2:
  Pseudocode:</p>
  
  <pre><code>if (x &lt; y &amp;&amp; x &gt; z) raise OpError("Only one predicate may evaluate true");
  if (x &lt; y) return 17;
  else if (x &gt; z) return 23;
  else return -1;
  </code></pre>
  
  <p>Expressions:</p>
  
  <pre><code>def f1(): return tf.constant(17)
  def f2(): return tf.constant(23)
  def f3(): return tf.constant(-1)
  r = case({tf.less(x, y): f1, tf.greater(x, z): f2},
           default=f3, exclusive=True)
  </code></pre>
  
  
  
  <p>The reason tf.case() wouldn't work is that the condition must be a scalar tensor so it wouldn't support a 2D tensor.</p>
  
  <pre><code>Each pair contains a boolean scalar tensor and a python callable 
  </code></pre>
  
  <p>Tried with tf.cond() and failed:</p>
  
  
  
  <pre><code>import tensorflow as tf
  
  a = tf.constant([[-1,-2,-3],[4,5,6]], dtype=tf.float32)
  b = tf.constant([[-2,2,2],[-2,2,2]], dtype=tf.float32)
  
  zero = tf.fill(tf.shape(a), 0.0)
  
  def f1(): return tf.add(a,b)
  def f2(): return zero
  
  c = tf.cond(tf.less(a,b), f1, f2)
  
  with tf.Session() as session:
      r_c = session.run([c])
      print (r_c)
  </code></pre>
  
  <p>
  Here is the error report:</p>
  
  <pre><code>    Shape must be rank 0 but is rank 2 for 'cond/Switch' 
  (op: 'Switch') with input shapes: [2,3], [2,3].
  </code></pre>