如何高效转换tensorflow中的示例程序?

How to convert sample program in tensorflow efficiently?

我的代码:

  data = [0, 2]
  f = numpy.array([[[1, 2], [3, 4]],
       [[4, 5], [7, 5]], 
       [[6, 3], [7, 9]]])
  l = []
  for i in data :
    l.append(f[i])
  return np.maximum.reduce(l)  

输出: [[6, 3], [7, 9]] f[0] 和 f[2] 之间的元素最大,因为数据是 0 和 2
我只需要使用 tf.while_loop 和任何其他张量流函数

以张量流格式实现相同的代码

您可以尝试这样的操作:

import tensorflow as tf

data = [0, 2]
f = tf.constant([[[1, 2], [3, 4]],
      [[4, 5], [7, 5]], 
      [[6, 3], [7, 9]]])
x = tf.gather(f, data)
x = tf.reduce_max(x, axis=0)
print(x)
tf.Tensor(
[[6 3]
 [7 9]], shape=(2, 2), dtype=int32)

关于您在评论中提出的问题,请尝试这样的操作:

fn = 4 
x = tf.random.normal((1, 2, 2, 4))
x = tf.squeeze(tf.split(x[0, :, :, :], fn, axis=-1), axis=-1)