审查移植C++;对于每个带有Lambda到Java的 我试图将C++代码移植到java。p>
代码片段如下所示:审查移植C++;对于每个带有Lambda到Java的 我试图将C++代码移植到java。p>,java,c++,lambda,Java,C++,Lambda,代码片段如下所示: uint32_t maxNumSource = newLayer.size.x * newLayer.size.y; for (auto &plane : newLayer.flatConvolveMatrix) { std::for_each(std::begin(plane), std::end(plane), [maxNumSource](float &weight) { weight = (float)(((randomFlo
uint32_t maxNumSource = newLayer.size.x * newLayer.size.y;
for (auto &plane : newLayer.flatConvolveMatrix) {
std::for_each(std::begin(plane), std::end(plane), [maxNumSource](float &weight) {
weight = (float)(((randomFloat() * 2) - 1.0f) / sqrt(maxNumSource));
});
}
vector<vector<float>> flatConvolveMatrix;
其中flatConvolveMatrix是newLayer的成员,声明如下:
uint32_t maxNumSource = newLayer.size.x * newLayer.size.y;
for (auto &plane : newLayer.flatConvolveMatrix) {
std::for_each(std::begin(plane), std::end(plane), [maxNumSource](float &weight) {
weight = (float)(((randomFloat() * 2) - 1.0f) / sqrt(maxNumSource));
});
}
vector<vector<float>> flatConvolveMatrix;
向量平坦卷积矩阵;
<>我不确定C++中的“权重”变量是如何处理的。作为参数进入for_的每个循环,对吗?
“[maxNumSource]”是什么意思
到目前为止,我在Java中提出了:
Integer maxNumSource = newLayer.size.x * newLayer.size.y;
for ( Vector<Double> plane : newLayer.flatConvolveMatrix ) {
double weight = 0;
for ( Double value : plane ) {
weight += randomFloat() * 2 - 1.0 / Math.sqrt(maxNumSource);
}
}
Integer maxNumSource=newLayer.size.x*newLayer.size.y;
对于(向量平面:newLayer.flatConvolveMatrix){
双倍重量=0;
用于(双值:平面){
weight+=randomFloat()*2-1.0/Math.sqrt(maxNumSource);
}
}
我的解释正确吗?部分是lambda的捕获列表,它允许lambda在其主体中使用变量
maxNumSource
不,有一个错误:
C++版本修改<代码>平面< /> >中的元素,而java版本不修改。它应该是:
for (Double value : plane)
value = (randomFloat() * 2 - 1.0) / Math.sqrt(maxNumSource);
^^^^^^^^^^^^^^^^^^^^^^^^^^^
operator precendence :)
^^^^
modifies the current 'value' in 'plane'
randomFloat()*2-1.0/Math.sqrt(maxNumSource)的可能重复项
应该是(randomFloat()*2-1.0)/Math.sqrt(maxNumSource)代码>与C++代码匹配。@ NathanOliver,你说得对。优先。但我主要关心的是权重变量。这是怎么回事?您通常应该避免使用基本数据类型的类版本(Integer,Double),而直接使用简单的数据类型(int,Double)。如果有特定的需要,只使用类版本。我不知道“代码>向量<代码>是从哪里来的,但是如果是你的代码,考虑放弃它。你是说变量“权重”是“平面”的一个实际项目吗?如果是的话,那真的打开了我的心扉。@GuilhermeRamos是的,因为它是通过引用float&