void network::datasetOnLineTrain(mat dataset,int TRAINNING_TYPE,int MAX_TRAINNING_TIME) { int time=0; //上一轮训练下来的误差 double round_error; mat serror; //赋值训练最小误差为较大的数 min_error=DBL_MAX; //样本的输入和输出 rowvec sample,out; while(time<MAX_TRAINNING_TIME) { round_error=0; for(int i=0;i<dataset.n_rows;i++) { sample=dataset.row(i).cols(0,in_vec-1); out=dataset.row(i).cols(in_vec,dataset.n_cols-1); if(TRAINNING_TYPE==BP_NONE) { simplyBPTrain(sample,out); } else if(TRAINNING_TYPE==BP_WITH_SPARSE) { withSparseTrain(sample,out); } else{ return; } } updateOut(dataset.cols(0,in_vec-1)); serror=dataset.cols(in_vec,dataset.n_cols-1)-output; round_error=norm(sum(serror%serror/dataset.n_rows),2); time++; if(round_error<min_error) { min_error=round_error; for(int i=layer_num;i>=1;i--) { mw[i]=w[i]; mo[i]=o[i]; } } if(time%SHOW_TIME==0) cout<<"第"<<time<<"次训练的误差变化为"<<setprecision(50)<<round_error<<endl; if(round_error<tor_error) { error=round_error; break; } } cout<<"本次训练了"<<time<<"次,最小训练误差为"<<min_error<<endl<<"最终训练误差为"<<round_error<<endl; }
Error decompressString(const std::vector<unsigned char>& compressedData, std::string* str) { if (compressedData.empty()) { *str = ""; return Success(); } size_t dataLen = compressedData.size(); ByteBuffer srcBuff(dataLen); srcBuff.append(compressedData.data(), dataLen); // It's unlikely we were able to compress the original data to < 1/2 of it's size. // If we did, we can make multiple decompression passes. ByteBuffer destBuff(dataLen * 2); z_stream zStream; size_t remainIn, remainOut; makeZStream(srcBuff, destBuff, &remainIn, &remainOut, &zStream); int res = inflateInit(&zStream); if (res != Z_OK) return systemError(res, "ZLib initialization error", ERROR_LOCATION); while (res != Z_STREAM_END) { updateOut(&destBuff, &remainOut, &zStream); res = inflate(&zStream, Z_NO_FLUSH); destBuff.setDataSize(zStream.total_out); if ((res == Z_DATA_ERROR) || (res == Z_NEED_DICT) || (res == Z_MEM_ERROR)) { inflateEnd(&zStream); LOG_ERROR_MESSAGE("Could not decompress data\"" + std::string(reinterpret_cast<const char*>(compressedData.data()), dataLen) + "\""); return systemError(res, "ZLib inflation error", ERROR_LOCATION); } updateIn(&srcBuff, &remainIn, &zStream); } inflateEnd(&zStream); *str = destBuff.toString(); return Success(); }
void network::batStructLearningBySparse(mat dataset,int MAX_TRAINNING_TIME,int bat_size){ int time=0; //一轮下来的误差 double round_error; //误差数组 mat serror; min_error=DBL_MAX; int chunks,remain,start,end; chunks=dataset.n_rows/bat_size; remain=dataset.n_rows%bat_size; while(time<MAX_TRAINNING_TIME){ for(int i=0;i<chunks;i++) { start=i*bat_size; end=(i+1)*bat_size-1; withSparseTrain(dataset.rows(start,end),dataset.rows(start,end)); } //剩下的样本训练 if(remain!=0){ start=chunks*bat_size; end=dataset.n_rows-1; withSparseTrain(dataset.rows(start,end),dataset.rows(start,end)); } updateOut(dataset); serror=dataset-output; round_error=norm(sum(serror%serror/dataset.n_rows),2); time++; if(round_error<min_error) { min_error=round_error; for(int i=layer_num;i>=1;i--) { mw[i]=w[i]; mo[i]=o[i]; } } if(time%SHOW_TIME==0) cout<<"第"<<time<<"次训练的误差变化为"<<setprecision(50)<<round_error<<endl; if(round_error<tor_error) { error=round_error; break; } } }
Error compressString(const std::string& toCompress, std::vector<unsigned char>* compressedData) { if (toCompress.empty()) { *compressedData = std::vector<unsigned char>(); return Success(); } size_t dataLen = toCompress.size(); ByteBuffer srcBuff(dataLen); ByteBuffer destBuff(dataLen); srcBuff.append(toCompress); z_stream zStream; size_t remainIn, remainOut; makeZStream(srcBuff, destBuff, &remainIn, &remainOut, &zStream); int res = deflateInit(&zStream, Z_BEST_COMPRESSION); if (res != Z_OK) return systemError(res, "ZLib initialization error", ERROR_LOCATION); while (res != Z_STREAM_END) { updateOut(&destBuff, &remainOut, &zStream); res = deflate(&zStream, Z_FINISH); destBuff.setDataSize(zStream.total_out); if (res == Z_STREAM_ERROR) { deflateEnd(&zStream); LOG_ERROR_MESSAGE("Could not compress string \"" + toCompress + "\""); return systemError(res, "ZLib deflation error", ERROR_LOCATION); } updateIn(&srcBuff, &remainIn, &zStream); } deflateEnd(&zStream); compressedData->assign(destBuff.get(), destBuff.get() + destBuff.dataSize()); return Success(); }
void network::withSparseTrain(mat sample,mat result) { updateOut(sample); //隐藏层惩罚因子向量 rowvec r[4]; //经过处理后的惩罚因子向量 rowvec ar[4]; if(1==layer_num) { //没有隐藏层,进行BP训练 simplyBPTrain(sample,result); } else { //对隐藏层处理 for(int i=layer_num-1; i>=1; i--) { //求出平均激活值 r[i]=sum(out[i])/sample.n_rows; ar[i]=sparse_para*(-roup/r[i]+(1-roup)/(1-r[i])); if(rs[i]!=0) ar[i].cols(0,rs[i]-1).fill(0); } //计算顶层残差 e[layer_num]=-(result-output)%output%(1-output); //计算隐藏层残差,并加上稀疏惩罚项 for(int i=layer_num-1;i>=1;i--){ e[i]=e[i+1]*w[i+1].t(); e[i].each_row()+=ar[i]; e[i]=e[i]%out[i]%(1-out[i]); } //计算梯度并修改权值 mat rdmat[MAX_LAYER]; for(int i=layer_num;i>=1;i--){ delta[i]=out[i-1].t()*e[i]/sample.n_rows; w[i]=w[i]-lr*delta[i]; //偏置修改 o[i]=o[i]-sum(e[i])/sample.n_rows; } weightRndChange(); } }
void network::simplyBPTrain(mat sample,mat result) { //前向更新 updateOut(sample); //计算顶层残差 e[layer_num]=-(result-output)%output%(1-output); for(int i=layer_num-1; i>=1; i--) { //计算隐藏层残差 e[i]=(e[i+1]*w[i+1].t())%out[i]%(1-out[i]); } //计算梯度并修改权值 mat rdmat[MAX_LAYER]; for(int i=layer_num; i>=1; i--) { delta[i]=out[i-1].t()*e[i]/sample.n_rows; w[i]=w[i]-lr*delta[i]; //偏置修改 o[i]=o[i]-sum(e[i])/sample.n_rows; } weightRndChange(); }
void network::structLearningBySparse(mat dataset,int MAX_TRAINNING_TIME){ int time=0; //上一轮训练下来的误差 double round_error; mat serror; //赋值训练最小误差为较大的数 min_error=DBL_MAX; while(time<MAX_TRAINNING_TIME) { round_error=0; for(int i=0;i<dataset.n_rows;i++) { withSparseTrain(dataset.row(i),dataset.row(i)); } updateOut(dataset); serror=dataset-output; round_error=norm(sum(serror%serror/dataset.n_rows),2); time++; if(round_error<min_error) { min_error=round_error; for(int i=layer_num;i>=1;i--) { mw[i]=w[i]; mo[i]=o[i]; } } if(time%SHOW_TIME==0) cout<<"第"<<time<<"次训练的误差变化为"<<setprecision(50)<<round_error<<endl; if(round_error<tor_error) { error=round_error; break; } } cout<<"本次训练了"<<time<<"次,最小训练误差为"<<min_error<<endl<<"最终训练误差为"<<round_error<<endl; }
void network::active(rowvec input) { updateOut(input); cout<<output; }
void network::datasetBatTrain(mat dataset,int TRAINNING_TYPE,int MAX_TRAINNING_TIME,int bat_size) { int time=0; //训练下来的误差 double round_error; mat serror; //样本的输入和输出 mat sample,out; min_error=DBL_MAX; int chunks,remain,start,end; chunks=dataset.n_rows/bat_size; remain=dataset.n_rows%bat_size; while(time<MAX_TRAINNING_TIME) { for(int i=0;i<chunks;i++) { start=i*bat_size; end=(i+1)*bat_size-1; sample=dataset.rows(start,end).cols(0,in_vec-1); out=dataset.rows(start,end).cols(in_vec,dataset.n_cols-1); if(TRAINNING_TYPE==BP_NONE) { simplyBPTrain(sample,out); } else if(TRAINNING_TYPE==BP_WITH_SPARSE){ withSparseTrain(sample,out); } else{ return; } } //剩下的样本训练 if(remain!=0){ start=chunks*bat_size; end=dataset.n_rows-1; sample=dataset.rows(start,end).cols(0,in_vec-1); out=dataset.rows(start,end).cols(in_vec,dataset.n_cols-1); if(TRAINNING_TYPE==BP_NONE) { simplyBPTrain(sample,out); } else if(TRAINNING_TYPE==BP_WITH_SPARSE){ withSparseTrain(sample,out); } else{ return; } } updateOut(dataset.cols(0,in_vec-1)); serror=dataset.cols(in_vec,dataset.n_cols-1)-output; round_error=norm(sum(serror%serror/dataset.n_rows),2); time++; if(round_error<min_error) { min_error=round_error; for(int i=layer_num;i>=1;i--) { mw[i]=w[i]; mo[i]=o[i]; } } if(time%SHOW_TIME==0) cout<<"第"<<time<<"次训练的误差变化为"<<setprecision(50)<<round_error<<endl; if(round_error<tor_error) { error=round_error; break; } } cout<<"本次训练了"<<time<<"次,最小训练误差为"<<min_error<<endl<<"最终训练误差为"<<round_error<<endl; }