theano,索引一个具有较小值的大矩阵,但超出边界 [英] theano, indexing a large matrix with relatively small value, but got out of boundary
问题描述
这是我的带有行号的dl4mt(神经机器翻译)的theano代码的一部分. src_positions是int64的向量,我将其打印出结果,其值不超过16. 但是当我使用src_positions来索引attention_mask_时,其形状为(100,100).它的索引超出了错误范围.
here is part of my theano code for dl4mt (neural machine translation) with line number. The src_positions is a vector of int64, which I printed the result, with the value no more than 16. but when I use src_positions to index attention_mask_, of which the shape is (100, 100). it got index out of bound error.
这是奇怪的部分:
- 首先,attention_mask_和gaussian_mask_具有相同的形状.
- 当我使用0.1 * src_positions进行索引时(用注释的第4行替换第5行).第8行保持不变,程序运行良好...
- 更奇怪的是,当我用注释的第7行替换第8行,但保持第5行不变时,程序仍然可以运行!
我不确定问题是否存在……这真的很奇怪.希望有人能给我一些建议.
I'm not sure whether the problem is ... It's really really weird. hope someone can give me some advice.
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由您的代码(而不是您发布的零件)更改了
It seems that the problem occurs based on the src_positions. There won't be any problem sccording to your description. Maybe the src_positions is changed by your codes other than your posted part
这篇关于theano,索引一个具有较小值的大矩阵,但超出边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!