简体   繁体   English

xt::random::binomial 根据 output 大小返回不同的结果

[英]xt::random::binomial returns different results based on output size

I am writing code to generate an NxN matrix of 0s and 1s in XTensor where the probability of an entry being 1 is 1/N.我正在编写代码以在 XTensor 中生成 0 和 1 的 NxN 矩阵,其中条目为 1 的概率为 1/N。 Additionally, I want to discard all values on the diagonal and above.此外,我想丢弃对角线及以上的所有值。 Finally, I want to find the indices of all 1s.最后,我想找到所有 1 的索引。 Hence, I am using the following code:因此,我使用以下代码:

auto binomial = xt::tril(
                    xt::random::binomial(
                            xt::shape<uint32_t>({N, N}), 1, 1/N
                    ),
                    1
            );
std::vector<std::array<unsigned int, 2>> vals = xt::argwhere(binomial);

The expected size of vals here should be N. This is true when I try N=100, N=1000, N=10000, but does not hold when I try N=100000.这里 vals 的预期大小应该是 N。当我尝试 N=100、N=1000、N=10000 时这是正确的,但当我尝试 N=100000 时不成立。 Are there any limitations to this approach that I am not aware of?我不知道这种方法有什么限制吗?

It appears that you have to worry about types here.看来您必须在这里担心类型。 Internally, you should be able to resolve indices up the N * N .在内部,您应该能够解析N * N的索引。 For you N = 1e5 that means 1e10 .对你来说N = 1e5这意味着1e10 The maximum size of uint32_t is about 4e9 , so that means that you overflow. uint32_t的最大大小约为4e9 ,因此这意味着您会溢出。

What does seem to work is似乎有效的是

size_t N = 100000;
auto binomial = xt::tril(
                xt::random::binomial(
                        xt::shape<size_t>({N, N}), 1, 1/(double)N
                ),
                1
        );
auto vals = xt::argwhere(binomial);

I wonder if this should be considered a bug though.我想知道这是否应该被视为一个错误。 Looking at the API I would expect that xt::shape<uint32_t>({N, N}) should be of a type that is able to handle the maximum N that you want.查看 API 我希望xt::shape<uint32_t>({N, N})应该是能够处理您想要的最大N的类型。 Rather, it seems that xt::shape<T> sets T for internal sizes, so that T should be of a type that can handle N^d.相反,似乎xt::shape<T>为内部大小设置了T ,因此T应该是可以处理 N^d 的类型。 I wonder if it should be considered fair that you are able to influence internal types in this way.我想知道您能够以这种方式影响内部类型是否应该被认为是公平的。 Please consider opening a bug report (with a link to this question) to resolve this.请考虑打开错误报告(带有指向此问题的链接)来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM