double VilmaOracle::PwVilmaRegularized<Loss>::risk(const double *weights, double *subgrad) { const int pw_dim = data_->GetDataDim() * kPW; std::fill(subgrad, subgrad + GetOracleParamsDim(), 0); const int nexamples = data_->GetDataNumExamples(); DenseVecD w(pw_dim, const_cast<double *>(weights)); // free_params is theta for Ord and beta for Mord DenseVecD free_params(this->GetFreeParamsDim(), const_cast<double *>(weights) + pw_dim); ProjectData(w, data_, wx_buffer_.get(), kPW); double *wx = wx_buffer_.get(); double obj = 0; for (int example_idx = 0; example_idx < nexamples; ++example_idx) { double val = this->UpdateSingleExampleGradient(free_params, wx, example_idx, subgrad, subgrad + pw_dim); obj += val; wx += kPW; } // normalize for (int i = 0; i < this->GetOracleParamsDim(); ++i) subgrad[i] /= nexamples; obj /= nexamples; return obj; }
void MainWindow::OpenFile(const QString& file){ setStatusTip(UTF8("打开文件:")+file); bool test_ok = false; QFile fp(file); test_ok = fp.exists(); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("文件不存在:\n")+file); test_ok = fp.open(QFile::ReadOnly); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法读取文件:\n")+file); QTextStream CurrentFile(&fp); //读取文件中的配置 QString Line; QString OData; QTextStream ProjectData(&OData); bool switcher = false; while( !CurrentFile.atEnd() ){ Line = CurrentFile.readLine(); if( !switcher ){ //非 配置or导出 部分 if(Line.startsWith(StartIndent) || Line.startsWith(AutoGenIndent)){ switcher = true; continue; } }else{ //配置or导出 部分 switcher = !(Line.startsWith(EndIndent) || Line.startsWith(AutoGenEndIndent)); ProjectData<<Line<<endl; } } bool start = false; while(!ProjectData.atEnd()){ QString Line = ProjectData.readLine(); if( Line == "[GLOBAL]" ){ start = true; continue; } if( Line == "[END]" ){ start = false; continue; } if(!start) continue; Line = Line.trimmed(); QString ID = Line.section(' ',0,0).toLower(); ID.truncate(ID.length()-1); QString Value = multiLine( Line.section(' ',1) ); CurrentSetting->setProperty(ID.toStdString().data(),Value); } if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法在文件中识别本程序的格式")); ProjectData.reset(); wm->restart(); //移动当前目录 QString stuff_dir = file + STUFF_FOLDER; QDir stuff(stuff_dir); if( !stuff.exists() ){ stuff.mkdir(stuff_dir); } stuff.setCurrent(stuff_dir); qDebug()<<"[OPEN FILE]Current dir is "<<QDir::currentPath(); wm->SessionResume(OData); CurrentFilePath = file; this->setWindowTitle(file+QString(" - cssSprites")); }