refactor config

This commit is contained in:
tqchen 2014-08-15 21:02:33 -07:00
parent dafa44753a
commit 3589e8252f
3 changed files with 189 additions and 213 deletions

View File

@ -57,7 +57,6 @@ class IObjFunction{
return base_score; return base_score;
} }
}; };
} // namespace learner } // namespace learner
} // namespace xgboost } // namespace xgboost

View File

@ -27,7 +27,7 @@ class ColMaker: public IUpdater<FMatrix> {
FMatrix &fmat, FMatrix &fmat,
const std::vector<unsigned> &root_index, const std::vector<unsigned> &root_index,
const std::vector<RegTree*> &trees) { const std::vector<RegTree*> &trees) {
fmat.InitColAccess();
for (size_t i = 0; i < trees.size(); ++i) { for (size_t i = 0; i < trees.size(); ++i) {
Builder builder(param); Builder builder(param);
builder.Update(gpair, fmat, root_index, trees[i]); builder.Update(gpair, fmat, root_index, trees[i]);

View File

@ -1,36 +1,24 @@
#ifndef XGBOOST_CONFIG_H #ifndef XGBOOST_UTILS_CONFIG_H_
#define XGBOOST_CONFIG_H #define XGBOOST_UTILS_CONFIG_H_
/*! /*!
* \file xgboost_config.h * \file config.h
* \brief helper class to load in configures from file * \brief helper class to load in configures from file
* \author Tianqi Chen: tianqi.tchen@gmail.com * \author Tianqi Chen
*/ */
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include "xgboost_utils.h" #include <istream>
#include <vector> #include <fstream>
#include "./utils.h"
namespace xgboost { namespace xgboost {
namespace utils { namespace utils {
/*! /*!
* \brief an iterator that iterates over a configure file and gets the configures * \brief base implementation of config reader
*/ */
class ConfigIterator{ class ConfigReaderBase {
public: public:
/*!
* \brief constructor
* \param fname name of configure file
*/
ConfigIterator(const char *fname){
fi = FopenCheck(fname, "r");
ch_buf = fgetc(fi);
}
/*! \brief destructor */
~ConfigIterator(){
fclose(fi);
}
/*! /*!
* \brief get current name, called after Next returns true * \brief get current name, called after Next returns true
* \return current parameter name * \return current parameter name
@ -50,7 +38,7 @@ namespace xgboost{
* \return true if there is value in next position * \return true if there is value in next position
*/ */
inline bool Next(void) { inline bool Next(void) {
while (!feof(fi)){ while (!this->IsEnd()) {
GetNextToken(s_name); GetNextToken(s_name);
if (s_name[0] == '=') return false; if (s_name[0] == '=') return false;
if (GetNextToken( s_buf ) || s_buf[0] != '=') return false; if (GetNextToken( s_buf ) || s_buf[0] != '=') return false;
@ -59,26 +47,49 @@ namespace xgboost{
} }
return false; return false;
} }
private: // called before usage
FILE *fi; inline void Init(void) {
char ch_buf; ch_buf = this->GetChar();
char s_name[256], s_val[256], s_buf[246]; }
inline void SkipLine(){ protected:
/*!
* \brief to be implemented by subclass,
* get next token, return EOF if end of file
*/
virtual char GetChar(void) = 0;
/*! \brief to be implemented by child, check if end of stream */
virtual bool IsEnd(void) = 0;
private:
char ch_buf;
char s_name[100000], s_val[100000], s_buf[100000];
inline void SkipLine(void) {
do { do {
ch_buf = fgetc(fi); ch_buf = this->GetChar();
} while (ch_buf != EOF && ch_buf != '\n' && ch_buf != '\r'); } while (ch_buf != EOF && ch_buf != '\n' && ch_buf != '\r');
} }
inline void ParseStr(char tok[]) { inline void ParseStr(char tok[]) {
int i = 0; int i = 0;
while ((ch_buf = fgetc(fi)) != EOF){ while ((ch_buf = this->GetChar()) != EOF) {
switch (ch_buf) { switch (ch_buf) {
case '\\': tok[i++] = fgetc(fi); break; case '\\': tok[i++] = this->GetChar(); break;
case '\"': tok[i++] = '\0'; case '\"': tok[i++] = '\0'; return;
return;
case '\r': case '\r':
case '\n': Error("unterminated string"); break; case '\n': Error("ConfigReader: unterminated string");
default: tok[i++] = ch_buf;
}
}
Error("ConfigReader: unterminated string");
}
inline void ParseStrML(char tok[]) {
int i = 0;
while ((ch_buf = this->GetChar()) != EOF) {
switch (ch_buf) {
case '\\': tok[i++] = this->GetChar(); break;
case '\'': tok[i++] = '\0'; return;
default: tok[i++] = ch_buf; default: tok[i++] = ch_buf;
} }
} }
@ -93,18 +104,22 @@ namespace xgboost{
case '#' : SkipLine(); new_line = true; break; case '#' : SkipLine(); new_line = true; break;
case '\"': case '\"':
if (i == 0) { if (i == 0) {
ParseStr(tok); ch_buf = fgetc(fi); return new_line; ParseStr(tok); ch_buf = this->GetChar(); return new_line;
} else {
Error("ConfigReader: token followed directly by string");
} }
else{ case '\'':
Error("token followed directly by string"); if (i == 0) {
ParseStrML( tok ); ch_buf = this->GetChar(); return new_line;
} else {
Error("ConfigReader: token followed directly by string");
} }
case '=': case '=':
if (i == 0) { if (i == 0) {
ch_buf = fgetc(fi); ch_buf = this->GetChar();
tok[0] = '='; tok[0] = '=';
tok[1] = '\0'; tok[1] = '\0';
} } else {
else{
tok[i] = '\0'; tok[i] = '\0';
} }
return new_line; return new_line;
@ -113,7 +128,7 @@ namespace xgboost{
if (i == 0) new_line = true; if (i == 0) new_line = true;
case '\t': case '\t':
case ' ' : case ' ' :
ch_buf = fgetc(fi); ch_buf = this->GetChar();
if (i > 0) { if (i > 0) {
tok[i] = '\0'; tok[i] = '\0';
return new_line; return new_line;
@ -121,99 +136,61 @@ namespace xgboost{
break; break;
default: default:
tok[i++] = ch_buf; tok[i++] = ch_buf;
ch_buf = fgetc(fi); ch_buf = this->GetChar();
break; break;
} }
} }
return true; return true;
} }
}; };
};
namespace utils{
/*! /*!
* \brief a class that save parameter configurations * \brief an iterator use stream base, allows use all types of istream
* temporally and allows to get them out later
* there are two kinds of priority in ConfigSaver
*/ */
class ConfigSaver{ class ConfigStreamReader: public ConfigReaderBase {
public: public:
/*! \brief constructor */
ConfigSaver(void){ idx = 0; }
/*! \brief clear all saves */
inline void Clear(void){
idx = 0;
names.clear(); values.clear();
names_high.clear(); values_high.clear();
}
/*! /*!
* \brief push back a parameter setting * \brief constructor
* \param name name of parameter * \param istream input stream
* \param val value of parameter
* \param priority whether the setting has higher priority: high priority occurs
* latter when read from ConfigSaver, and can overwrite existing settings
*/ */
inline void PushBack(const char *name, const char *val, int priority = 0){ explicit ConfigStreamReader(std::istream &fin) : fin(fin) {}
if (priority == 0){
names.push_back(std::string(name));
values.push_back(std::string(val));
}
else{
names_high.push_back(std::string(name));
values_high.push_back(std::string(val));
}
}
/*! \brief set pointer to beginning of the ConfigSaver */
inline void BeforeFirst(void){
idx = 0;
}
/*!
* \brief move iterator to next position
* \return true if there is value in next position
*/
inline bool Next(void){
if (idx >= names.size() + names_high.size()){
return false;
}
idx++;
return true;
}
/*!
* \brief get current name, called after Next returns true
* \return current parameter name
*/
inline const char *name(void) const{
Assert(idx > 0, "can't call name before first");
size_t i = idx - 1;
if (i >= names.size()){
return names_high[i - names.size()].c_str();
}
else{
return names[i].c_str();
}
}
/*!
* \brief get current value, called after Next returns true
* \return current parameter value
*/
inline const char *val(void) const{
Assert(idx > 0, "can't call name before first");
size_t i = idx - 1;
if (i >= values.size()){
return values_high[i - values.size()].c_str();
}
else{
return values[i].c_str();
}
}
private:
std::vector<std::string> names;
std::vector<std::string> values;
std::vector<std::string> names_high;
std::vector<std::string> values_high;
size_t idx;
};
};
};
#endif
protected:
virtual char GetChar(void) {
return fin.get();
}
/*! \brief to be implemented by child, check if end of stream */
virtual bool IsEnd(void) {
return fin.eof();
}
private:
std::istream &fin;
};
/*!
* \brief an iterator that iterates over a configure file and gets the configures
*/
class ConfigIterator: public ConfigStreamReader {
public:
/*!
* \brief constructor
* \param fname name of configure file
*/
explicit ConfigIterator(const char *fname) : ConfigStreamReader(fi) {
fi.open(fname);
if (fi.fail()) {
utils::Error("cannot open file %s", fname);
}
ConfigReaderBase::Init();
}
/*! \brief destructor */
~ConfigIterator(void) {
fi.close();
}
private:
std::ifstream fi;
};
} // namespace utils
} // namespace xgboost
#endif // XGBOOST_UTILS_CONFIG_H_