【问题标题】:tensorflow efficient way for tensor multiplication张量乘法的张量流有效方法
【发布时间】:2023-03-07 23:27:01
【问题描述】:

我在 tensorflow 中有两个张量,第一个张量是 3-D,第二个是 2D。我想像这样将它们相乘:

x = tf.placeholder(tf.float32, shape=[sequence_length, batch_size, hidden_num]) 
w = tf.get_variable("w", [hidden_num, 50]) 
b = tf.get_variable("b", [50])

output_list = []
for step_index in range(sequence_length):
    output = tf.matmul(x[step_index,  :,  :], w) + b
    output_list.append(output)
output = tf.pack(outputs_list)

我使用循环进行乘法运算,但我认为它太慢了。使这个过程尽可能简单/干净的最佳方法是什么?

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    您可以使用batch_matmul。不幸的是,batch_matmul 似乎不支持沿批处理维度进行广播,因此您必须平铺您的w 矩阵。这将使用更多内存,但所有操作都将保留在 TensorFlow 中

    a = tf.ones((5, 2, 3))
    b = tf.ones((3, 1))
    b = tf.reshape(b, (1, 3, 1))
    b = tf.tile(b, [5, 1, 1])
    c = tf.batch_matmul(a, b) # use tf.matmul in TF 1.0 
    sess = tf.InteractiveSession()
    sess.run(tf.shape(c))
    

    这给了

    array([5, 2, 1], dtype=int32)
    

    【讨论】:

    • 谢谢你的回答,但是我有一个问题,使用tf.tile后,b的变量大小发生了变化,因为里面有一些无用的数据。所以很难计算 tf.nn.l2_loss(b)
    • 顺便说一句,瓦片输入可以是张量,因此您可以从 tf.shape(w) 动态构造形状 arg
    • 在 tensorflow 1.0 中,使用tf.matmul 而不是tf.batch_matmul
    【解决方案2】:

    您可以使用map_fn,它沿第一维扫描函数。

    x = tf.placeholder(tf.float32, shape=[sequence_length, batch_size, hidden_num]) 
    w = tf.get_variable("w", [hidden_num, 50]) 
    b = tf.get_variable("b", [50])
    
    def mul_fn(current_input):
        return tf.matmul(current_input, w) + b
    
    output = tf.map_fn(mul_fn, x)
    

    I used this at one point 沿序列实现 softmax 扫描。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-15
      • 2018-11-19
      • 2018-05-24
      • 1970-01-01
      • 2023-03-20
      • 2018-02-24
      • 1970-01-01
      • 2019-03-05
      相关资源
      最近更新 更多