IIUC,你可以用tf
个操作来try 类似的东西,但我不确定在GPU上会有多高效:
import tensorflow as tf
tensor = tf.constant([[0., 1., 2., 3., 4.],
[5., 1., 7., 3., 2.],
[9., 9., 2., 3., 5.],
[2., 6., 2., 8., 4.]])
pool_cols = [tf.constant([0, 1]), tf.constant([2, 3, 4])]
def column_max_pooling(tensor, pool_cols):
results = []
tensor_shape = tf.shape(tensor)
for col in pool_cols:
col_shape = tf.shape(col)
t = tf.gather_nd(tensor, tf.transpose(tf.stack([tf.tile(tf.range(tensor_shape[0]), [col_shape[0]]), tf.repeat(col, [tensor_shape[0]])])))
t = tf.reduce_max(tf.transpose(tf.reshape(t, (col_shape[0], tensor_shape[0]))), axis=-1, keepdims=True)
results.append(t)
return tf.concat(results, axis=-1)
print(column_max_pooling(tensor, pool_cols))
tf.Tensor(
[[1. 4.]
[5. 7.]
[9. 5.]
[6. 8.]], shape=(4, 2), dtype=float32)
如果您可以保证订单为pool_cols
,您也可以try 使用tf.math.unsorted_segment_max
:
import tensorflow as tf
tensor = tf.constant([[0., 1., 2., 3., 4.],
[5., 1., 7., 3., 2.],
[9., 9., 2., 3., 5.],
[2., 6., 2., 8., 4.]])
pool_cols = [tf.constant([0, 1]), tf.constant([2, 3, 4])]
result = tf.transpose(tf.math.unsorted_segment_max(tf.transpose(tensor), tf.concat([tf.repeat(idx, tf.shape(col)[0])for idx, col in enumerate(pool_cols)], axis=0), num_segments=len(pool_cols)))
print(result)
tf.Tensor(
[[1. 4.]
[5. 7.]
[9. 5.]
[6. 8.]], shape=(4, 2), dtype=float32)