Skip to content

Commit

Permalink
more rich error in any, change parameter to enable unknown args (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen authored Aug 31, 2016
1 parent c5c3312 commit 9f1cb6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
3 changes: 2 additions & 1 deletion include/dmlc/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ inline const std::type_info& any::type() const {
template<typename T>
inline void any::check_type() const {
CHECK(type_ != nullptr)
<< "The any container is empty";
<< "The any container is empty"
<< " requested=" << typeid(T).name();
CHECK(type_->ptype_info == &typeid(T))
<< "The stored type mismatch"
<< " stored=" << type_->ptype_info->name()
Expand Down
34 changes: 25 additions & 9 deletions include/dmlc/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class FieldEntry;
// forward declare ParamManagerSingleton
template<typename PType>
struct ParamManagerSingleton;

/*! \brief option in parameter initialization */
enum ParamInitOption {
/*! \brief allow unknown parameters */
kAllowUnknown,
/*! \brief need to match exact parameters */
kAllMatch
};
} // namespace parameter
/*!
* \brief Information about a parameter field in string representations.
Expand Down Expand Up @@ -108,13 +116,17 @@ struct Parameter {
* and throw error if something wrong happens.
*
* \param kwargs map of keyword arguments, or vector of pairs
* \parma option The option on initialization.
* \tparam Container container type
* \throw ParamError when something go wrong.
*/
template<typename Container>
inline void Init(const Container &kwargs) {
inline void Init(const Container &kwargs,
parameter::ParamInitOption option = parameter::kAllowUnknown) {
PType::__MANAGER__()->RunInit(static_cast<PType*>(this),
kwargs.begin(), kwargs.end(), NULL);
kwargs.begin(), kwargs.end(),
NULL,
option == parameter::kAllowUnknown);
}
/*!
* \brief initialize the parameter by keyword arguments.
Expand All @@ -130,7 +142,8 @@ struct Parameter {
InitAllowUnknown(const Container &kwargs) {
std::vector<std::pair<std::string, std::string> > unknown;
PType::__MANAGER__()->RunInit(static_cast<PType*>(this),
kwargs.begin(), kwargs.end(), &unknown);
kwargs.begin(), kwargs.end(),
&unknown, true);
return unknown;
}
/*!
Expand Down Expand Up @@ -355,7 +368,8 @@ class ParamManager {
inline void RunInit(void *head,
RandomAccessIterator begin,
RandomAccessIterator end,
std::vector<std::pair<std::string, std::string> > *unknown_args) const {
std::vector<std::pair<std::string, std::string> > *unknown_args,
bool allow_unknown) const {
std::set<FieldAccessEntry*> selected_args;
for (RandomAccessIterator it = begin; it != end; ++it) {
FieldAccessEntry *e = Find(it->first);
Expand All @@ -367,11 +381,13 @@ class ParamManager {
if (unknown_args != NULL) {
unknown_args->push_back(*it);
} else {
std::ostringstream os;
os << "Cannot find argument \'" << it->first << "\', Possible Arguments:\n";
os << "----------------\n";
PrintDocString(os);
throw dmlc::ParamError(os.str());
if (!allow_unknown) {
std::ostringstream os;
os << "Cannot find argument \'" << it->first << "\', Possible Arguments:\n";
os << "----------------\n";
PrintDocString(os);
throw dmlc::ParamError(os.str());
}
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions test/parameter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,9 @@ int main(int argc, char *argv[]) {
printf("Parameters\n-----------\n%s", Param::__DOC__().c_str());
std::vector<std::pair<std::string, std::string> > unknown;
unknown = param.InitAllowUnknown(kwargs);
unknown = param2.InitAllowUnknown(unknown);
param2.Init(unknown);


if (unknown.size() != 0) {
std::ostringstream os;
os << "Cannot find argument \'" << unknown[0].first << "\', Possible Arguments:\n";
os << "----------------\n";
os << param.__DOC__();
os << param2.__DOC__();
throw dmlc::ParamError(os.str());
}
printf("-----\n");
printf("param.num_hidden=%d\n", param.num_hidden);
printf("param.learning_rate=%f\n", param.learning_rate);
Expand Down

0 comments on commit 9f1cb6f

Please sign in to comment.