theano,用相对较小的值索引一个大矩阵,但超出了边界

theano, indexing a large matrix with relatively small value, but got out of boundary

这是我的 dl4mt(神经机器翻译)theano 代码的一部分,带有行号。 src_positions是一个int64的vector,我打印出来的结果,这个值不超过16。 但是当我使用 src_positions 索引 attention_mask_ 时,其形状是 (100, 100)。它得到了索引超出范围的错误。

奇怪的部分来了:

  1. 首先,attention_mask_和gaussian_mask_的形状相同。
  2. 当我使用 0.1 * src_positions 进行索引时(将第 5 行替换为第 4 行注释)。第 8 行保持不变,程序 运行 很好...
  3. 奇怪的是,当我将第 8 行替换为带注释的第 7 行,但保留第 5 行不变时,程序仍然可以 运行!

我不确定问题是不是...真的很奇怪。希望有人能给我一些建议。

    1] p_t_s = p_t * sntlens  # n_samples * 1, pt in equation
    2] src_positions = tensor.cast(tensor.floor(p_t_s), 'int64') # (n_samples, 1)
    3] src_positions = src_positions.reshape([src_positions.shape[0], ])
    4] # batch_mask = attention_mask_[tensor.cast(src_positions * 0.1, 'int64')]      # n_sample * maxlen
    5] batch_mask = attention_mask_[src_positions]      # n_sample * maxlen
    6] attn_mask = batch_mask[:, :msk_.shape[0]] * msk_.T      # n_sample * n_timestep 
    7] # batch_gauss_mask = gaussian_mask_[tensor.cast(src_positions * 0.1, 'int64')]      # n_sample * maxlen
    8] batch_gauss_mask = gaussian_mask_[src_positions]   # n_sample * maxlen 
    9] gauss_mask = batch_gauss_mask[:, :msk_.shape[0]] * msk_.T      # n_sample * n_timestep 

问题似乎是基于src_positions出现的。根据你的描述不会有问题的。也许 src_positions 被您的代码更改,而不是您发布的部分