【问题标题】:How to apply scalar multiplication using elements of one tensor along the elements of another tensor?如何使用一个张量的元素沿着另一个张量的元素应用标量乘法?
【发布时间】:2017-08-30 07:14:44
【问题描述】:

给定两个张量,A (mxnxq) 和 B (mxnx 1),如何创建一个循环遍历 A 行的函数,将 B (nx 1) 的每个元素视为 标量并将它们应用于 A (nxq) 的子矩阵的向量 (qx 1)?

例如,A 是 (6000, 1000, 300) 形状。 B 是 (6000, 1000, 1)。循环遍历 A 的 6000 个“切片”,对于 A (, 1000, 300) 的 1000 个子矩阵的每个向量,从向量 B (, 1000, 1) 的子矩阵中应用每个元素的标量乘法。

我的措辞可能非常糟糕。当出现问题时,我会相应地调整措辞。

旁注:我正在使用 Python,所以 Theano 可能是最好的选择?

【问题讨论】:

    标签: python tensorflow theano theano.scan


    【解决方案1】:

    如下使用tf.mul

    import tensorflow as tf
    a = tf.constant([[[1,2,1,2],[3,4,1,2],[5,6,10,12]],[[7,8,1,2],[9,10,1,1],[11,12,0,3]]])
    b= tf.constant([[[7],[8],[9]],[[1],[2],[3]]])
    res=tf.mul(a,b)
    sess=tf.Session()
    print(sess.run(res)) 
    

    哪个打印:

    [[[  7  14   7  14]
      [ 24  32   8  16]
      [ 45  54  90 108]]
    
     [[  7   8   1   2]
      [ 18  20   2   2]
      [ 33  36   0   9]]]
    

    【讨论】:

    • 哇,这很有效。所以我猜 Tensorflow 的 tf.mul 会自动执行此操作。我虽然会涉及到一些嵌套的 theano.scan。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2015-09-21
    • 2023-01-13
    • 2019-09-13
    • 2021-01-11
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多