如何在Eigen中生成张量的外积? [英] How do I do outer product of tensors in Eigen?

查看:427
本文介绍了如何在Eigen中生成张量的外积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本征中,可以使用以下方法很容易地进行张量收缩:

  Tensor< double,1>张量1; 
Tensor< double,2>张量2;

//填充数据,以便
// tensor1的尺寸为[10],而tensor2的尺寸为[5,10]

std :: array< Eigen :: IndexPair< int> ;, 1> product_dims1 = {IndexPair< int<(1,0)};

自动张量= tensor2.contract(tensor1,product_dims1);

//现在张量的尺寸为[5]

我正在寻找对于一种与收缩相反的方法,这意味着需要两个张量A和B,分别为5 x 10和3 x 2尺寸,并定义一个新的张量C为5 x 10 x 3 x 2尺寸,

  C_ijkl = A_ij * B_kl 

如有必要,我可以轻松地编写这样的方法,但是我觉得如果使用本征特征方法,它将得到更好的优化。我还希望能够使用GPU的支持,如果您使用本机方法,对本征来说很容易。



谢谢。

解决方案

解决方案可能太简单了:您必须在没有索引的情况下进行合约



< pre class = lang-cxx prettyprint-override> Eigen :: array< Eigen :: IndexPair< long>,0> empty_index_list = {};
Tensor< double,2> A_ij(4,4);
Tensor< double,2> B_kl(4,4);
Tensor< double,4> C_ijkl = A_ij.contract(B_kl,empty_index_list);


In eigen, one can do quite easily tensor contractions using:

Tensor<double, 1> tensor1;
Tensor<double, 2> tensor2;

// fill with data so that
// tensor1 is of dimensions [10] and tensor2 of dimensions [5,10]

std::array<Eigen::IndexPair<int>, 1> product_dims1 = { IndexPair<int>(1, 0) };

auto tensor = tensor2.contract(tensor1, product_dims1);

// now tensor is of dimensions [5]

I am looking for a method that does the opposite of contraction, meaning it takes two tensors A and B, say of dimensions 5 x 10 and 3 x 2 and defines a new tensor C of dimensions 5 x 10 x 3 x 2 such that

  C_ijkl = A_ij * B_kl

I could easily write such a method if necessary, but I get the sense it would be more optimized if I used a native eigen method. I also want to be able to use GPU support which is quite easy with eigen if you use the native methods.

Thanks.

解决方案

The solution is perhaps too simple: You have to contract over no indices.

Eigen::array<Eigen::IndexPair<long>,0> empty_index_list = {};
Tensor<double, 2> A_ij(4, 4);
Tensor<double, 2> B_kl(4, 4);
Tensor<double, 4> C_ijkl = A_ij.contract(B_kl, empty_index_list);

这篇关于如何在Eigen中生成张量的外积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆