我有以下問題:我有一個形狀為 (1600, 29) 的張量,我想獲得軸 1 的最大值。結果應該是 (1600, 1) 張量。為了簡化,我將使用 (5,3) 張量來演示我的問題:B = tf.constant([[2, 20, 30], [2, 7, 6], [3, 11, 16], [19, 1, 8], [14, 45, 23]])x = x = tf.math.argmax(B, 1) --> 5 Values [2 1 2 0 1]z = tf.gather(B, x, axis=1) --> Shape: (5,5) [[30 20 30 2 20] [ 6 7 6 2 7] [16 11 16 3 11] [ 8 1 8 19 1] [23 45 23 14 45]]好的,現在,x 給了我軸上的最大元素,但是,tf.gather 不返回 [30, 7, 16, 19, 45],而是一些奇怪的張量。如何正確“減少”尺寸?我的“相當”骯臟的方式是這樣的:eye_z = tf.eye(5, 5)intermed_result = z*eye_zresult = tf.linalg.matvec(intermed_result,tf.transpose(tf.constant([1,1,1,1,1], dtype=tf.float32)))這導致正確的張量:[30. 7. 16. 19. 45.]
1 回答

繁花不似錦
TA貢獻1851條經驗 獲得超4個贊
你有tf.math.reduce_max
這個:
m = tf.math.reduce_max(B, axis=1) sess = tf.InteractiveSession() sess.run(m)# array([30, 7, 16, 19, 45])
添加回答
舉報
0/150
提交
取消