我的代碼:
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 以 tensorflow 格式實作相同的代碼和任何其他張量流函式
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432295.html
