首页 文章

扫描theano中不同尺寸的张量

提问于
浏览
3

我正在用 theano 移动我的第一步,我无法弄清楚如何解决这个问题,这实际上非常容易 .

我有一个 3 * 4 * 2 张量,如下所示:

[1 1] | [2 2] | [3 3]
[1 1] | [2 2] | [3 3]
[0 0] | [2 2] | [3 3]
[9 9] | [0 0] | [3 3]

所以我有 N=3 个序列,每个序列的长度为 L=4 ,其元素是维 d=2 的向量 . 实际上,序列可以有不同的长度,但我可以想到用 [0 0] 向量填充它们,如上所示 .

我想要做的是,首先扫描张量的第一个轴并总结列表中的所有向量 up to the the first [0 0] vector - 这就是我在第一个张量切片末尾添加[9 9]的原因,以便检查总和退出条件[1] . 我最终应该在 [[2 2], [6 6], [12 12]] . 我试过很多方法来解决这个问题,这在我看来只是一个嵌套的循环问题......但总是有一些奇怪的错误[2] .

谢谢,
朱利奥

[1]:实际问题是为NLP目的训练递归神经网络,其中 N 是批次的维度, L 是批次中句子的最大长度, d 是每个单词表示的维度 . 我省略了这个问题,以便我可以专注于最简单的编码方面 .
[2]我省略了失败的历史,也许我可以在以后添加它们 .

1 回答

  • 4

    如果序列始终为零填充,则可以沿着感兴趣的轴求和,因为填充区域不会改变总和 . 但是,如果填充区域可能包含非零值,则有两种方法 .

    • 使用扫描 . 这很慢,如果可能应该避免 . 事实上它可以避免,因为,

    • 创建二进制掩码并乘以填充区域 .

    这里有一些代码说明了这三种方法 . 对于允许非零填充区域( v2v3 )的两种方法,计算需要额外的输入:给出批次内序列长度的向量 .

    import numpy
    import theano
    import theano.tensor as tt
    
    
    def v1():
        # NOTE: [9, 9] element changed to [0, 0] 
        # since zero padding must be used for
        # this method
        x_data = [[[1, 1], [1, 1], [0, 0], [0, 0]],
                  [[2, 2], [2, 2], [2, 2], [0, 0]],
                  [[3, 3], [3, 3], [3, 3], [3, 3]]]
        x = tt.tensor3()
        x.tag.test_value = x_data
        y = x.sum(axis=1)
        f = theano.function([x], outputs=y)
        print f(x_data)
    
    
    def v2_step(i_t, s_tm1, x, l):
        in_sequence = tt.lt(i_t, l).dimshuffle(0, 'x')
        s_t = s_tm1 + tt.switch(in_sequence, x[i_t], 0)
        return s_t
    
    
    def v2():
        x_data = [[[1, 1], [1, 1], [0, 0], [9, 9]],
                  [[2, 2], [2, 2], [2, 2], [0, 0]],
                  [[3, 3], [3, 3], [3, 3], [3, 3]]]
        l_data = [2, 3, 4]
        x = tt.tensor3()
        x.tag.test_value = x_data
        l = tt.lvector()
        l.tag.test_value = l_data
        # Must dimshuffle first because scan can only iterate over first (0'th) axis.
        x_hat = x.dimshuffle(1, 0, 2)
        y, _ = theano.scan(v2_step, sequences=[tt.arange(x_hat.shape[0])],
                           outputs_info=[tt.zeros_like(x_hat[0])],
                           non_sequences=[x_hat, l], strict=True)
        f = theano.function([x, l], outputs=y[-1])
        print f(x_data, l_data)
    
    
    def v3():
        x_data = [[[1, 1], [1, 1], [0, 0], [9, 9]],
                  [[2, 2], [2, 2], [2, 2], [0, 0]],
                  [[3, 3], [3, 3], [3, 3], [3, 3]]]
        l_data = [2, 3, 4]
        x = tt.tensor3()
        x.tag.test_value = x_data
        l = tt.lvector()
        l.tag.test_value = l_data
        indexes = tt.arange(x.shape[1]).dimshuffle('x', 0)
        mask = tt.lt(indexes, l.dimshuffle(0, 'x')).dimshuffle(0, 1, 'x')
        y = (mask * x).sum(axis=1)
        f = theano.function([x, l], outputs=y)
        print f(x_data, l_data)
    
    
    def main():
        theano.config.compute_test_value = 'raise'
        v1()
        v2()
        v3()
    
    
    main()
    

    一般来说, if your step function is dependent on the output of a previous step then you need to use scan .

    如果原则上每个步骤/迭代都可以同时执行(即它们根本不相互依赖),那么通常有一种更有效的方法可以在不使用扫描的情况下执行此操作

相关问题