小编ant*_*ine的帖子

有效地计算numpy中的parafac/CP产品

这个问题集中在numpy上.

我有一组矩阵,它们共享相同数量的列并具有不同的行数.我们称它们为A,B,C,D等,让它们的尺寸为IaxK IbxK,IcxK等

我想要的是有效地计算IaxIbxIc ...张量P定义如下:P(ia,ib,ic,id,即......)=\sum_k A(ia,k)B(ib,k)C (IC,K)...

因此,如果我有两个因素,我最终得到简单的矩阵产品.

当然,我可以通过外部产品"手动"计算,例如:

    def parafac(factors,components=None):
        ndims = len(factors)
        ncomponents = factors[0].shape[1]
        total_result=array([])
        if components is None:
            components=range(ncomponents)

        for k in components:
            #for each component (to save memory)
            result = array([])
            for dim in range(ndims-1,-1,-1):
                #Augments model with next dimension
                current_dim_slice=[slice(None,None,None)]
                current_dim_slice.extend([None]*(ndims-dim-1))
                current_dim_slice.append(k)
                if result.size:
                    result = factors[dim].__getitem__(tuple(current_dim_slice))*result[None,...]
                else:
                    result = factors[dim].__getitem__(tuple(current_dim_slice))
            if total_result.size:
                total_result+=result
            else:
                total_result=result
        return total_result
Run Code Online (Sandbox Code Playgroud)

不过,我想要一些计算效率更高的东西,比如依赖内置的numpy函数,但我找不到相关的函数,有人可以帮助我吗?

干杯,谢谢

python numpy

6
推荐指数
1
解决办法
1744
查看次数

标签 统计

numpy ×1

python ×1