【发布时间】:2018-02-07 21:45:06
【问题描述】:
大家好,我在 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 中做同样的事情时:
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);
我明白了:
D: 3 R: 3 C: 2
[[12.000 24.000 ]
[15.000 30.000 ]
[18.000 36.000 ]
]
R: 3 C: 2
[[39.000 78.000 ]
[42.000 84.000 ]
[45.000 90.000 ]
]
R: 3 C: 2
[[66.000 132.000 ]
[69.000 138.000 ]
[72.000 144.000 ]
]
为什么会这样。我想要一个相当于 numpy 给我的张量点积。它与c ++中的product_dims参数有关吗?还是涉及其他一些错误?基本上,它需要将深度分量乘以 3 倍。
【问题讨论】:
标签: python c++ numpy eigen3 tensor