【问题标题】:Element wise dot product of tensors in theanotheano中张量的元素明智点积
【发布时间】:2016-10-26 05:31:24
【问题描述】:

有没有办法在 theano.假设我有以下张量:

>>>t1.shape.eval()
array([5, 3, 3])
>>>t2.shape.eval()
array([5, 3, 1)])

我该如何做点积,以便 t1 的 5 个 (3*3) 矩阵中的每一个都与 t2 的每个 (3*1) 矩阵相点缀,最终给我:

output.shape.eval()
array([5, 3, 1])

t1 和 t2 在 Theano 中都是共享变量。

t1=T.shared(np.ones((5,3,3)))
t2=T.shared(np.ones((5,3,1)))

【问题讨论】:

    标签: python theano deep-learning lstm


    【解决方案1】:

    我相信,如果您将第二个张量 t2 沿第二个轴(索引 = 1,因为它的索引为 0)洗牌,那么您可以使用 theano.tensor.tensordot() (reference) 来获得所需的结果。

    >>> import theano
    >>> import theano.tensor as T
    >>> import numpy as np
    >>>
    >>> t1 = theano.shared(np.ones((5, 3, 3)))
    >>> t2 = theano.shared(np.ones((5, 3, 1)))
    >>>
    >>> t2_new = t2.dimshuffle(0, 'x', 1, 2)  # will have shape (5, 1, 3, 1)
    >>> desired_tensor = T.tensordot(t1, t2_new)
    >>> desired_tensor.shape.eval()
    array([5, 3, 1])
    >>>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-10
      • 2016-03-22
      • 2016-10-11
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 2019-11-26
      • 2019-04-21
      相关资源
      最近更新 更多