6

给定两个矩阵 A (1000 x 100) 和 B (100 x 1000),而不是直接在 tensorflow 中计算它们的乘积,即 ,tf.dot(A,B)我想先从 A 中选择 10 个列(随机),从 B 中选择 10 行,然后使用tf.dot(A_s,B_s)

自然,第二次乘法应该快得多,因为所需的乘法次数减少了 10 倍。

然而,实际上,在 tensorflow 中选择矩阵 A 的给定列来创建 A_s 似乎是一个效率极低的过程。

给定 中所需列的索引idx,我尝试了以下解决方案来创建 A_s。解决方案根据其性能进行排名:

  1. . A_s = tf.transpose(tf.gather(tf.unstack(A, axis=1), idx))

tf.dot(A_s,B_s)tf.dot(A,B)因为创建 A_s 太昂贵,所以慢了 5 倍。

    2.


     p_shape = K.shape(params)
     p_flat = K.reshape(params, [-1])
     i_flat = K.reshape(K.reshape(
        K.arange(0, p_shape[0]) * p_shape[1], [-1, 1]) + indices, [-1])
     indices = [i_flat]
     v = K.transpose(indices)
     updates = i_flat * 0 - 1
     shape = tf.to_int32([p_shape[0] * p_shape[1]])
     scatter = tf.scatter_nd(v, updates, shape) + 1
     out_temp = tf.dynamic_partition(p_flat,
                     partitions=scatter, num_partitions=2)[0]
     A_s = tf.reshape(out_temp, [p_shape[0], -1])

导致产品速度降低 6-7 倍

    3.


      X,Y =  tf.meshgrid((tf.range(0, p_shape[0])),indices)
      idx = K.concatenate([K.expand_dims(
           K.reshape((X),[-1]),1), 
           K.expand_dims(K.reshape((Y),[-1]),1)],axis=1)
      A_s = tf.reshape(tf.gather_nd(params, idx), [p_shape[0], -1])

慢 10-12 倍。

非常感谢任何关于如何提高列选择过程效率的想法。

PS1:我在 CPU 上运行了所有实验。

PS2:矩阵 A 是占位符而不是变量。在某些实现中,它可能会出现问题,因为它的形状可能无法推断出来。

4

0 回答 0