为什么我的程序在 reading/writing 时将最高有效数字丢弃到文件中?
Why is my program dropping the most significant figure when reading/writing to a file?
我正在编写两个函数来将 4 维向量保存到文件中,然后将该向量读回文件中。
void saveLayer(string filename, int layer){
ofstream ofile(filename, ios::out | ios::trunc);
vector<vector<vector<vector<double>>>> oweights(layers[layer]->weights);
for(vector<vector<vector<double>>> stratg:oweights){
for(vector<vector<double>>layeri:stratg){
for(vector<double> neuronq:layeri){
for(double q:neuronq){
ofile<<setprecision(15)<<q;
}
}
}
}
vector<vector<vector<double>>> obiases=layers[layer]->biases;
for(vector<vector<double>>z:obiases){
for(vector<double> w:z){
for(double q:w){
ofile<<setprecision(15)<<q;
}
}
}
ofile.close();
}
void loadLayer(string filename, int nS, vector<int> layerStruct){
vector<vector<vector<vector<double>>>> newWeights;
vector<vector<vector<double>>> newBiases;
ifstream ifile(filename, ios::in);
for(int i=0; i<nS; i++){
vector<vector<vector<double>>> netWeights;
for(int z=1; z<layerStruct.size(); z++){
vector<vector<double>> layerWeights;
for(int x=0; x<layerStruct[z]; x++){
vector<double> neuronWeights;
for(int y=0; y<layerStruct[z-1]; y++){
double w;
ifile>>w;
neuronWeights.push_back(w);
}
layerWeights.push_back(neuronWeights);
}
netWeights.push_back(layerWeights);
}
newWeights.push_back(netWeights);
}
for(int i=0; i<nS; i++){
vector<vector<double>> netBiases;
for(int z=1; z<layerStruct.size(); z++){
vector<double> layerBiases;
for(int x=0; x<layerStruct[z]; x++){
double neuronBias;
ifile>>neuronBias;
layerBiases.push_back(neuronBias);
}
netBiases.push_back(layerBiases);
}
newBiases.push_back(netBiases);
}
layers[0]->numStrats=nS;
layers[0]->weights=newWeights;
layers[0]->biases=newBiases;
ifile.close();
}
我的问题是,当它应该写入或读取 1.2345 时,它读取或写入了 0.2345,丢弃了最高有效数字。我不知道问题是出在保存功能还是加载功能上。如果能解释这是为什么,我将不胜感激。
写入输出文件的数据之间没有分隔符。给定 1.234 和 5.678
ofile<<setprecision(15)<<q;
会将 1.2345.678 写入输出文件。你所有的数字都被混合成一个大团块。回读时
ifile>>w;
不知道一个数字应该在哪里结束,下一个数字应该从哪里开始。结果它一直读取,直到找到一个不可能属于 double
.
的字符
例如。 1.2345.678 将被读取到 1.2345,在那里找到第二个 '.'
。浮点数不能有 2 个小数位(即使这个是 double
),所以解析器停止,在流中留下 .678 供下一次读取,以及 returns 1.2345。下次读取 0.678 左右并返回 0.678。
最重要的数字现在神秘地消失了,并且因为 double
仅适用于 15 位数字,您可能不会注意到它被固定为前一个数字作为其第 16 位数字。
可能的解决方案:在每个数字后写一个space。
ofile<<setprecision(15)<<q << ' ';
我正在编写两个函数来将 4 维向量保存到文件中,然后将该向量读回文件中。
void saveLayer(string filename, int layer){
ofstream ofile(filename, ios::out | ios::trunc);
vector<vector<vector<vector<double>>>> oweights(layers[layer]->weights);
for(vector<vector<vector<double>>> stratg:oweights){
for(vector<vector<double>>layeri:stratg){
for(vector<double> neuronq:layeri){
for(double q:neuronq){
ofile<<setprecision(15)<<q;
}
}
}
}
vector<vector<vector<double>>> obiases=layers[layer]->biases;
for(vector<vector<double>>z:obiases){
for(vector<double> w:z){
for(double q:w){
ofile<<setprecision(15)<<q;
}
}
}
ofile.close();
}
void loadLayer(string filename, int nS, vector<int> layerStruct){
vector<vector<vector<vector<double>>>> newWeights;
vector<vector<vector<double>>> newBiases;
ifstream ifile(filename, ios::in);
for(int i=0; i<nS; i++){
vector<vector<vector<double>>> netWeights;
for(int z=1; z<layerStruct.size(); z++){
vector<vector<double>> layerWeights;
for(int x=0; x<layerStruct[z]; x++){
vector<double> neuronWeights;
for(int y=0; y<layerStruct[z-1]; y++){
double w;
ifile>>w;
neuronWeights.push_back(w);
}
layerWeights.push_back(neuronWeights);
}
netWeights.push_back(layerWeights);
}
newWeights.push_back(netWeights);
}
for(int i=0; i<nS; i++){
vector<vector<double>> netBiases;
for(int z=1; z<layerStruct.size(); z++){
vector<double> layerBiases;
for(int x=0; x<layerStruct[z]; x++){
double neuronBias;
ifile>>neuronBias;
layerBiases.push_back(neuronBias);
}
netBiases.push_back(layerBiases);
}
newBiases.push_back(netBiases);
}
layers[0]->numStrats=nS;
layers[0]->weights=newWeights;
layers[0]->biases=newBiases;
ifile.close();
}
我的问题是,当它应该写入或读取 1.2345 时,它读取或写入了 0.2345,丢弃了最高有效数字。我不知道问题是出在保存功能还是加载功能上。如果能解释这是为什么,我将不胜感激。
写入输出文件的数据之间没有分隔符。给定 1.234 和 5.678
ofile<<setprecision(15)<<q;
会将 1.2345.678 写入输出文件。你所有的数字都被混合成一个大团块。回读时
ifile>>w;
不知道一个数字应该在哪里结束,下一个数字应该从哪里开始。结果它一直读取,直到找到一个不可能属于 double
.
例如。 1.2345.678 将被读取到 1.2345,在那里找到第二个 '.'
。浮点数不能有 2 个小数位(即使这个是 double
),所以解析器停止,在流中留下 .678 供下一次读取,以及 returns 1.2345。下次读取 0.678 左右并返回 0.678。
最重要的数字现在神秘地消失了,并且因为 double
仅适用于 15 位数字,您可能不会注意到它被固定为前一个数字作为其第 16 位数字。
可能的解决方案:在每个数字后写一个space。
ofile<<setprecision(15)<<q << ' ';