title: How to process different row in tensor based on the first column value in tensorflow
body: <p>let's say I have a 4 by 3 tensor:</p>
  
  <pre><code>sample = [[10, 15, 25], [1, 2, 3], [4, 4, 10], [5, 9, 8]]
  </code></pre>
  
  <p>I would like to return another tensor of shape 4: [r1,r2,r3,r4] where r is either equal to tf.reduce_sum(row) if row[0] is less than 5, or r is equal to tf.reduce_mean(row) if row[0] is greater or equal to 5.
  output:</p>
  
  <pre><code>output = [16.67, 6, 18, 7.33]
  </code></pre>
  
  <p>I'm not an adept to tensorflow, please do assist me on how to achieve the above in python 3 without a for loop.
  thank you</p>
  
  <p><strong>UPDATES:</strong></p>
  
  <p>So I've tried to adapt the answer given by @Onyambu to include two samples in the functions but it gave me an error in all instances.
  here is the answer for the first case:</p>
  
  <pre><code>def f(x):
      c = tf.constant(5,tf.float32)
      def fun1():
          return tf.reduce_sum(x)
      def fun2():
          return tf.reduce_mean(x)
      return tf.cond(tf.less(x[0],c),fun1,fun2)
  a = tf.map_fn(f,tf.constant(sample,tf.float32))
  </code></pre>
  
  <p>The above works well.</p>
  
  <p>The for two samples:</p>
  
  <pre><code>sample1 = [[10, 15, 25], [1, 2, 3], [4, 4, 10], [5, 9, 8]]
  sample2 = [[0, 15, 25], [1, 2, 3], [0, 4, 10], [1, 9, 8]]
  
  def f2(x1,x2):
      c = tf.constant(1,tf.float32)
      def fun1():
          return tf.reduce_sum(x1[:,0] - x2[:,0])
      def fun2():
          return tf.reduce_mean(x1 - x2)
      return tf.cond(tf.less(x2[0],c),fun1,fun2)
  a = tf.map_fn(f2,tf.constant(sample1,tf.float32), tf.constant(sample2,tf.float32))
  </code></pre>
  
  <p>The adaptation does give errors, but the principle is simple:</p>
  
  <ul>
  <li><p>calculate the tf.reduce_sum of sample1[:,0] - sample2[:,0] if row[0] is less than 1</p></li>
  <li><p>calculate the tf.reduce_sum of sample1 - sample2 if row[0] is greater or equal to 1</p></li>
  </ul>
  
  <p>Thank you for your assistance in advance!</p>