通过神经网络中的函数将数据从 2D 数组保存到文件中
Saving data from a 2d array into file through a function in neural network
我是神经网络的新手。我有2个功能。初始化权重并节省权重。当我初始化权重时,我还需要将它们保存在一个单独的文件中。
我有以下代码:
//Function for saving weights
void saveWeights(int Hidden, int Inputs, double arr[][?]){
//double arr[][?];
ofstream myfile;
myfile.open("weights.txt");
for(int j = 0;j<Hidden;j++){
for(int i = 0;i<Inputs;i++){
myfile<<arr[i][j]<<endl;
}
}
myfile.close();
}
// set weights to random numbers
void initWeights(void){
for(int j = 0;j<numHidden;j++){
weightsHO[j] = getRand(0,1);
for(int i = 0;i<numInputs;i++){
weightsIH[i][j] = getRand(0,1);
cout<<"Weights = "<<weightsIH[i][j]<<endl;
saveWeights(numHidden, numInputs, weightsIH[i][j]);
}
}
}
问题是我不知道为 2D 数组中的列传递什么值,因为它是一个可变长度数组。
你能指导一下该怎么做吗?
我还尝试了以下方法:
//Function for saving weights
void saveWeights(double w){
ofstream myfile;
myfile.open("weights.txt");
myfile<<w<<endl;
myfile.close();
}
// set weights to random numbers
void initWeights(void){
for(int j = 0;j<numHidden;j++){
weightsHO[j] = getRand(0,1);
for(int i = 0;i<numInputs;i++){
weightsIH[i][j] = getRand(0,1);
z = weightsIH[i][j];
//cout<<"Weights = "<<weightsIH[i][j]<<endl;
saveWeights(z);
}
}
}
显示如何执行此操作的通用代码会很有帮助。提前感谢您的帮助。
有许多可能的解决方案。
1)将数组的维度写为文件中的第一个数字(一组数字)。
2) 写入文件时,在每行后插入NEW_LINE_CARACTER("",即 endl),并用空格分隔数字。
for(int j = 0;j<Hidden;j++){
for(int i = 0;i<Inputs;i++){
myfile<<arr[i][j]<<" ";
}
myfile<<endl
}
3) 使用尺寸等设置制作其他文件。
4) 使用与 C 样式数组不同的数据结构并尝试序列化。
等。
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素