问题描述
我正在使用神经网络,所以我想随机创建权重.因此,如果我创建30个神经网络,那么每个神经网络最终都具有相同的权重(假定是随机的),因此当我给它们提供相同的输入时,输出应该是相同的,否则就不会输出.有帮助吗?
I am working with neural networks, and I want to create the weights randomly. SO if I create 30 Neural networks every single one of them ends up having the same weights(supposed to be random) so when i give them all the same input the output is the same, when it shouldn't. Any help?
这是主要功能
int main(){
std::vector<Improved_NN> v;
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> distribution(-1.0,1.0);
for(int i = 0; i < 30; i++)
{
Improved_NN temp;
temp.initialize_weights(generator, distribution);
v.push_back(temp);
}
Board temp;
for(int i = 0; i < 30; i++)
{
std::cout <<"\n" << v[i].executeFromExternal(temp);
}
而initialize_weights在这里:
And the initialize_weights is here:
void Improved_NN::initialize_weights(std::default_random_engine gen,std::uniform_real_distribution<double> dist){
int k,v = 0;
for(k = 0;k<NUM_HIDDEN_1;k++){
for(v = 0 ; v < NUM_INPUTS; v++){
mlp_t.w_h1_i[k][v]=dist(gen);
//std::cout<<mlp_t.w_h1_i[k][v]<<std::endl;
}
}
for(k = 0;k<NUM_HIDDEN_2;k++){
for(v = 0 ; v < NUM_HIDDEN_1; v++){
mlp_t.w_h2_h1[k][v]=dist(gen);
//std::cout<<mlp_t.w_h2_h1[k][v]<<std::endl;
}
}
for(k = 0;k<NUM_HIDDEN_3;k++){
for(v = 0 ; v < NUM_HIDDEN_2; v++){
mlp_t.w_h3_h2[k][v]=dist(gen);
//std::cout<<mlp_t.w_h3_h2[k][v]<<std::endl;
}
}
for (int a = 0 ; a < NUM_HIDDEN_3;a++){
mlp_t.w_o_h[0][a] = dist(gen);
//std::cout<<mlp_t.w_o_h[0][a]<<std::endl;
}
}
这是我每次执行时得到的输出.
This is the output i get every time i execute.
0.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.521458
0.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.5214580.521458
非常感谢您.
推荐答案
好了,就像普通的rand()
一样,您需要为生成器提供初始种子,该种子必须具有不同的值,以便生成器生成不同的序列:
Well, as with plain old rand()
you need to provide initial seed for the generator which needs to be different value in order for generator to generate different sequence:
std::random_device rd;
std::default_random_engine generator(rd());
此外,正如user3018144所指出的,请使用单个生成器,而不要使用30个不同的生成器:
Also, as user3018144 pointed out, use single generator, instead of 30 different:
int main(){
std::vector<Improved_NN> v;
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> distribution(-1.0,1.0);
for(int i = 0; i < 30; i++)
{
Improved_NN temp;
/*Problem is here*/
temp.initialize_weights(generator, distribution);
v.push_back(temp);
}
Board temp;
for(int i = 0; i < 30; i++)
{
std::cout <<"\n" << v[i].executeFromExternal(temp);
}
//for the number of generations, do this....
}
另外,就在这里:
void Improved_NN::initialize_weights(std::default_random_engine gen,std::uniform_real_distribution<double> dist){
您正在按值传递生成器,这将创建现有生成器的副本.而是通过引用传递:
you're passing the generators by value, which will create a copy of the existing generator. Pass by reference instead:
void Improved_NN::initialize_weights(std::default_random_engine& gen, std::uniform_real_distribution<double>& dist){
这篇关于c ++-default_random_engine始终创建相同系列的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!