将由二项式向量组成的矩阵转换为连续零的范围

Transform a matrix made of binomial vectors to ranges for consecutive zeros

我想弄清楚如何在 theano 中象征性地进行这种转换,这是一个大小未定的矩阵

发件人:

 [[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1],
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1],
  .
  .
  ]

收件人:

[[1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 0, 1, 0, 1, 2, 3, 0],
 [1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 0, 0, 0, 0, 0, 0],
 .
 .
 ]

因此,对于每一个连续的 0,我都希望增加范围,每当我偶然发现 1 时,范围就会重置。

这是一种使用低效扫描的方法:

import theano
import theano.tensor as tt


def inner_step(x_t_t, y_t_tm1):
    return tt.switch(x_t_t, 0, y_t_tm1 + 1)


def outer_step(x_t):
    return theano.scan(inner_step, sequences=[x_t], outputs_info=[0])[0]


def compile():
    x = tt.bmatrix()
    y = theano.scan(outer_step, sequences=[x])[0]
    return theano.function([x], y)


def main():
    f = compile()
    data = [[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1]]
    print f(data)

main()

当 运行 时,打印:

[[1 2 3 0 1 2 3 4 5 0 0 1 0 1 2 3 0]
 [1 2 3 4 5 6 7 8 0 1 2 0 0 0 0 0 0]]