Skip to main content
 首页 » 编程设计

python之Theano 中的 numpy.matmul

2024年10月01日7Terrylee

长话短说
我想在 theano 中复制 numpy.matmul 的功能。执行此操作的最佳方法是什么?

太短;没看懂
查看 theano.tensor.dot 和 theano.tensor.tensordot,我没有看到进行简单的批量矩阵乘法的简单方法。即把N维张量的最后两个维度当作矩阵,相乘。我是否需要对 theano.tensor.batched_dot 进行一些愚蠢的使用?或者*shudder*自己循环播放而不广播!?

请您参考如下方法:

当前的拉取请求不支持广播,所以我暂时想到了这个。我可能会清理它,添加更多功能,并提交我自己的 PR 作为临时解决方案。在那之前,我希望这对某人有帮助! 我包含了测试以表明它复制了 numpy.matmul,假设输入符合我更严格的(临时)断言。

此外,.scan 在 argmin(*sequencelengths) 迭代时停止迭代序列。因此,我相信不匹配的数组形状不会引发任何异常。

import theano as th 
import theano.tensor as tt 
import numpy as np 
 
 
def matmul(a: tt.TensorType, b: tt.TensorType, _left=False): 
    """Replicates the functionality of numpy.matmul, except that 
    the two tensors must have the same number of dimensions, and their ndim must exceed 1.""" 
 
    # TODO ensure that broadcastability is maintained if both a and b are broadcastable on a dim. 
 
    assert a.ndim == b.ndim  # TODO support broadcasting for differing ndims. 
    ndim = a.ndim 
    assert ndim >= 2 
 
    # If we should left multiply, just swap references. 
    if _left: 
        tmp = a 
        a = b 
        b = tmp 
 
    # If a and b are 2 dimensional, compute their matrix product. 
    if ndim == 2: 
        return tt.dot(a, b) 
    # If they are larger... 
    else: 
        # If a is broadcastable but b is not. 
        if a.broadcastable[0] and not b.broadcastable[0]: 
            # Scan b, but hold a steady. 
            # Because b will be passed in as a, we need to left multiply to maintain 
            #  matrix orientation. 
            output, _ = th.scan(matmul, sequences=[b], non_sequences=[a[0], 1]) 
        # If b is broadcastable but a is not. 
        elif b.broadcastable[0] and not a.broadcastable[0]: 
            # Scan a, but hold b steady. 
            output, _ = th.scan(matmul, sequences=[a], non_sequences=[b[0]]) 
        # If neither dimension is broadcastable or they both are. 
        else: 
            # Scan through the sequences, assuming the shape for this dimension is equal. 
            output, _ = th.scan(matmul, sequences=[a, b]) 
        return output 
 
 
def matmul_test() -> bool: 
    vlist = [] 
    flist = [] 
    ndlist = [] 
    for i in range(2, 30): 
        dims = int(np.random.random() * 4 + 2) 
 
        # Create a tuple of tensors with potentially different broadcastability. 
        vs = tuple( 
            tt.TensorVariable( 
                tt.TensorType('float64', 
                              tuple((p < .3) for p in np.random.ranf(dims-2)) 
                              # Make full matrices 
                              + (False, False) 
                ) 
            ) 
            for _ in range(2) 
        ) 
        vs = tuple(tt.swapaxes(v, -2, -1) if j % 2 == 0 else v for j, v in enumerate(vs)) 
 
        f = th.function([*vs], [matmul(*vs)]) 
 
        # Create the default shape for the test ndarrays 
        defshape = tuple(int(np.random.random() * 5 + 1) for _ in range(dims)) 
        # Create a test array matching the broadcastability of each v, for each v. 
        nds = tuple( 
            np.random.ranf( 
                tuple(s if not v.broadcastable[j] else 1 for j, s in enumerate(defshape)) 
            ) 
            for v in vs 
        ) 
        nds = tuple(np.swapaxes(nd, -2, -1) if j % 2 == 0 else nd for j, nd in enumerate(nds)) 
 
        ndlist.append(nds) 
        vlist.append(vs) 
        flist.append(f) 
 
    for i in range(len(ndlist)): 
        assert np.allclose(flist[i](*ndlist[i]), np.matmul(*ndlist[i])) 
 
    return True 
 
 
if __name__ == "__main__": 
    print("matmul_test -> " + str(matmul_test()))