有效计算转移矩阵(m * m)*(n * n)的元素明智乘积以得出(mn * mn)矩阵 [英] Efficiently computing element wise product of transition matrices (m*m) * (n*n) to give (mn*mn) matrix

查看:170
本文介绍了有效计算转移矩阵(m * m)*(n * n)的元素明智乘积以得出(mn * mn)矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

分别考虑形状为(m,m)和(n,n)的输入矩阵X和Y.作为输出,我们需要提供一个(mn,mn)形状矩阵,以使其乘以两个矩阵中的相应条目. 这两个矩阵X和Y代表过渡矩阵.可以使用以下示例来说明所需的输出.这里,X是3 * 3矩阵,Y是2 * 2矩阵.

Consider input matrices X and Y of shapes (m,m) and (n,n) respectively. As an output we need to give a (mn,mn) shape matrix such that it multiplies corresponding entries in the two matrices. These two matrices X and Y represent transition matrices. A following example can be taken to illustrate the required output. Here, X is a 3*3 matrix and Y is a 2*2 matrix.

Matrix X
--------------
    x1  x2  x3    
x1|  a   b   c
x2|  d   e   f
x3|  g   h   i

Matrix Y
--------------
    y1  y2
y1|  j   k
y2|  l   m

Matrix Z (Output)
----------------------------------------
      x1y1  x1y2  x2y1  x2y2  x3y1  x3y2
x1y1|  aj    ak    bj    bk    cj    ck
x1y2|  al    am    bl    bm    cl    cm
x2y1|  dj    dk    ej    ek    fj    fk

 .
 .

以下是我为此任务编写的非矢量化函数:

Following is a non-vectorized function I have written for this task:

def transition_multiply(X,Y):
    num_rows_X=len(X)
    num_rows_Y=len(Y)
    out=[]
    count=0
    for i in range(num_rows_X):     
        for j in range(num_rows_Y):         
            out.append([])          
            for x in X[i]:
                 for y in Y[j]:                 
                     out[count].append(x*y)             
            count+=1
    return out

X=[[1,2,3],[2,3,4],[3,4,5]]
Y=[[2,4],[1,2]]
import numpy
print transition_multiply(numpy.array(X),numpy.array(Y))

我确实获得了所需的输出,但是意识到非矢量化版本的速度真的很慢.使用Numpy,矢量化此计算的最佳方法是什么.

I do get the required output but realize that the non-vectorized version would be really slow. What would be the best way to vectorize this computation, using Numpy.

对于那些感兴趣的人为什么需要这种计算.从组成转移矩阵制作阶乘隐马尔可夫模型的转移矩阵是需要的.

To those who are interested why this computation is needed. It is needed in making transition matrix of a Factorial Hidden Markov Model from constituent transition matrices.

推荐答案

这是 Kronecker产品,请参见 ="http://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html">此处.

这篇关于有效计算转移矩阵(m * m)*(n * n)的元素明智乘积以得出(mn * mn)矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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