Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 本征收缩与Numpy点_Python_C++_Numpy_Eigen3_Tensor - Fatal编程技术网

Python 本征收缩与Numpy点

Python 本征收缩与Numpy点,python,c++,numpy,eigen3,tensor,Python,C++,Numpy,Eigen3,Tensor,大家好,我在Numpy中有以下张量点积: import numpy as np tensorA = np.array([[[1,2,3], [4,5,6], [7,8,9]], [[10,11,12], [13,14,15], [16,17,18]], [[19,20,21],

大家好,我在Numpy中有以下张量点积:

import numpy as np

tensorA = np.array([[[1,2,3],
                  [4,5,6],
                  [7,8,9]],
                 [[10,11,12],
                  [13,14,15],
                  [16,17,18]],
                 [[19,20,21],
                  [22,23,24],
                  [25,26,27]]])

tensorB = np.array([[1,2],
                       [1,2],
                       [1,2]])

print tensorA.dot(tensorB)
它给出了以下答案:

[[[  6  12]
  [ 15  30]
  [ 24  48]]

 [[ 33  66]
  [ 42  84]
  [ 51 102]]

 [[ 60 120]
  [ 69 138]
  [ 78 156]]]
但是,当我在C++中做同样的事情时:

Eigen::Tensor<float, 3> tensorA(3,3,3);
tensorA.setValues({{{1,2,3},
              {4,5,6},
              {7,8,9}},

             {{10,11,12},
              {13,14,15},
              {16,17,18}},

             {{19,20,21},
              {22,23,24},
              {25,26,27}}});

Eigen::Tensor<float, 2> tensorB(3,2);
tensorB.setValues({{1,2},
                   {1,2},
                   {1,2}});

// Compute the traditional matrix product
Eigen::array<Eigen::IndexPair<float>, 1> product_dims = { Eigen::IndexPair<float>(0, 1) };
Eigen::Tensor<float, 3> AB = tensorA.contract(tensorB, product_dims);

为什么会发生这种情况。我想要一个张量点积,它等于numpy给我的。是否与C++中的产品Idim参数有关?还是有其他的bug?基本上,它需要将深度分量乘以3次。

< P>我不能帮助你使用C++代码,但是我可以以麻木的方式来识别正在发生的事情:

In [1]: A=np.arange(1,28).reshape(3,3,3)
In [3]: A
Out[3]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]],

       [[19, 20, 21],
        [22, 23, 24],
        [25, 26, 27]]])
In [5]: B=np.repeat([[1,2]],3, axis=0)
In [6]: B
Out[6]: 
array([[1, 2],
       [1, 2],
       [1, 2]])
你的圆点-记住A的最后一个和B的第二个到最后一个:

In [7]: A.dot(B)
Out[7]: 
array([[[  6,  12],
        [ 15,  30],
        [ 24,  48]],

       [[ 33,  66],
        [ 42,  84],
        [ 51, 102]],

       [[ 60, 120],
        [ 69, 138],
        [ 78, 156]]])
有了einsum索引,至少我很清楚:

In [8]: np.einsum('ijk,kl',A,B)    # notice the k pair
Out[8]: 
array([[[  6,  12],
        [ 15,  30],
        [ 24,  48]],

       [[ 33,  66],
        [ 42,  84],
        [ 51, 102]],

       [[ 60, 120],
        [ 69, 138],
        [ 78, 156]]])
<>但是如果我把EIUMUM改变为使用第二到最后两个,我得到你的C++结果:我想:

In [9]: np.einsum('ijk,jl',A,B)    # notice the j pair
Out[9]: 
array([[[ 12,  24],
        [ 15,  30],
        [ 18,  36]],

       [[ 39,  78],
        [ 42,  84],
        [ 45,  90]],

       [[ 66, 132],
        [ 69, 138],
        [ 72, 144]]])
In [9]: np.einsum('ijk,jl',A,B)    # notice the j pair
Out[9]: 
array([[[ 12,  24],
        [ 15,  30],
        [ 18,  36]],

       [[ 39,  78],
        [ 42,  84],
        [ 45,  90]],

       [[ 66, 132],
        [ 69, 138],
        [ 72, 144]]])