This class defines, for each EventKeyType, a set of initial questions that it tries and also a number of iterations for which to refine the questions to increase likelihood. More...

#include <build-tree-questions.h>

Collaboration diagram for Questions:

Public Member Functions

const QuestionsForKeyGetQuestionsOf (EventKeyType key) const
 
void SetQuestionsOf (EventKeyType key, const QuestionsForKey &options_of_key)
 
void GetKeysWithQuestions (std::vector< EventKeyType > *keys_out) const
 
bool HasQuestionsForKey (EventKeyType key) const
 
 ~Questions ()
 
 Questions ()
 Initializer with arguments. More...
 
void InitRand (const BuildTreeStatsType &stats, int32 num_quest, int32 num_iters_refine, AllKeysType all_keys_type)
 InitRand attempts to generate "reasonable" random questions. More...
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &is, bool binary)
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (Questions)
 

Private Attributes

std::vector< QuestionsForKey * > key_options_
 
std::map< EventKeyType, size_tkey_idx_
 

Detailed Description

This class defines, for each EventKeyType, a set of initial questions that it tries and also a number of iterations for which to refine the questions to increase likelihood.

It is perhaps a bit more than an options class, as it contains the actual questions.

Definition at line 77 of file build-tree-questions.h.

Constructor & Destructor Documentation

◆ ~Questions()

~Questions ( )
inline

Definition at line 108 of file build-tree-questions.h.

References kaldi::DeletePointers().

void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
std::vector< QuestionsForKey * > key_options_

◆ Questions()

Questions ( )
inline

Initializer with arguments.

After using this you would have to set up the config for each key you are going to use, or use InitRand().

Definition at line 113 of file build-tree-questions.h.

References kaldi::InitRand(), QuestionsForKey::Read(), and QuestionsForKey::Write().

113 { }

Member Function Documentation

◆ GetKeysWithQuestions()

void GetKeysWithQuestions ( std::vector< EventKeyType > *  keys_out) const
inline

Definition at line 101 of file build-tree-questions.h.

References kaldi::CopyMapKeysToVector(), and KALDI_ASSERT.

Referenced by DecisionTreeSplitter::FindBestSplit(), and kaldi::TestBuildTree().

101  {
102  KALDI_ASSERT(keys_out != NULL);
103  CopyMapKeysToVector(key_idx_, keys_out);
104  }
void CopyMapKeysToVector(const std::map< A, B > &m, std::vector< A > *v)
Copies the keys in a map to a vector.
Definition: stl-utils.h:126
std::map< EventKeyType, size_t > key_idx_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetQuestionsOf()

const QuestionsForKey& GetQuestionsOf ( EventKeyType  key) const
inline

Definition at line 79 of file build-tree-questions.h.

References KALDI_ASSERT, and KALDI_ERR.

Referenced by kaldi::ComputeInitialSplit(), kaldi::FindBestSplitForKey(), kaldi::TestBuildTree(), kaldi::TestQuestionsInitRand(), and kaldi::TestSplitDecisionTree().

79  {
80  std::map<EventKeyType, size_t>::const_iterator iter;
81  if ( (iter = key_idx_.find(key)) == key_idx_.end()) {
82  KALDI_ERR << "Questions: no options for key "<< key;
83  }
84  size_t idx = iter->second;
85  KALDI_ASSERT(idx < key_options_.size());
86  key_options_[idx]->Check();
87  return *(key_options_[idx]);
88  }
std::vector< QuestionsForKey * > key_options_
std::map< EventKeyType, size_t > key_idx_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ HasQuestionsForKey()

bool HasQuestionsForKey ( EventKeyType  key) const
inline

Definition at line 105 of file build-tree-questions.h.

Referenced by DecisionTreeSplitter::FindBestSplit(), kaldi::TestBuildTree(), kaldi::TestQuestionsInitRand(), and kaldi::TestSplitDecisionTree().

105  {
106  return (key_idx_.count(key) != 0);
107  }
std::map< EventKeyType, size_t > key_idx_

◆ InitRand()

void InitRand ( const BuildTreeStatsType stats,
int32  num_quest,
int32  num_iters_refine,
AllKeysType  all_keys_type 
)

InitRand attempts to generate "reasonable" random questions.

Only of use for debugging. This initializer creates a config that is ready to use. e.g. num_iters_refine = 0 means just use stated questions (if >1, will use different questions at each split of the tree).

Definition at line 89 of file build-tree-questions.cc.

References kaldi::FindAllKeys(), rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_WARN, kaldi::kAllKeysUnion, RefineClustersOptions::num_iters, kaldi::PossibleValues(), kaldi::RandInt(), QuestionsForKey::refine_opts, and kaldi::SortAndUniq().

Referenced by kaldi::GenRandContextDependency(), kaldi::GenRandContextDependencyLarge(), kaldi::TestBuildTree(), kaldi::TestClusterEventMapRestricted(), kaldi::TestQuestionsInitRand(), kaldi::TestShareEventMapLeaves(), and kaldi::TestSplitDecisionTree().

90  {
91  std::vector<EventKeyType> all_keys;
92  FindAllKeys(stats, all_keys_type, &all_keys); // get all keys.
93  if (all_keys_type == kAllKeysUnion) {
94  KALDI_WARN << "Questions::InitRand(), using union of all keys. This may work depending on how you are building the tree but may crash (depends if you have already ensured that stats currently on the same leaf all share the same set of keys.)";
95  }
96 
97  for (size_t i = 0;i < all_keys.size();i++) {
98  EventKeyType key = all_keys[i];
99  std::vector<EventValueType> all_values;
100  bool b = PossibleValues(key, stats, &all_values); // get possible values.
101  if (all_keys_type != kAllKeysUnion) KALDI_ASSERT(b);
102  KALDI_ASSERT(all_values.size() != 0); // since key exists in stats, must have some value defined.
103  QuestionsForKey q_for_key;
104  q_for_key.refine_opts.num_iters = num_iters_refine;
105  q_for_key.initial_questions.clear(); // Make sure empty.
106  if (all_values.size() == 1) { // can have no meaningful questions because only 1 possible value. use empty set of questions.
107  ; // Do nothing. No questions.
108  } else {
109  q_for_key.initial_questions.resize((size_t)num_quest);
110  for (size_t i = 0;i < (size_t)num_quest;i++) {
111  std::vector<EventValueType> &this_quest = q_for_key.initial_questions[i];
112  for (size_t j = 0;j < all_values.size()/2;j++)
113  this_quest.push_back(all_values[RandInt(0, all_values.size()-1)]);
114  SortAndUniq(&this_quest);
115  KALDI_ASSERT(!this_quest.empty());
116  }
117  SortAndUniq(&q_for_key.initial_questions); // Ensure unique questions.
118  }
119  q_for_key.Check();
120  SetQuestionsOf(key, q_for_key);
121  }
122 }
void SetQuestionsOf(EventKeyType key, const QuestionsForKey &options_of_key)
void FindAllKeys(const BuildTreeStatsType &stats, AllKeysType keys_type, std::vector< EventKeyType > *keys_out)
FindAllKeys puts in *keys the (sorted, unique) list of all key identities in the stats.
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
bool PossibleValues(EventKeyType key, const BuildTreeStatsType &stats, std::vector< EventValueType > *ans)
Convenience function e.g.
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( Questions  )
private

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)

Definition at line 63 of file build-tree-questions.cc.

References kaldi::DeletePointers(), kaldi::ExpectToken(), KALDI_ERR, QuestionsForKey::Read(), kaldi::ReadBasicType(), and kaldi::ReadToken().

Referenced by main(), and kaldi::TestQuestionsInitRand().

63  {
64  // First, clear anything present.
66  key_options_.clear();
67  key_idx_.clear();
68 
69  ExpectToken(is, binary, "<Questions>");
70 
71  std::vector<EventKeyType> keys_with_options;
72 
73  while (1) {
74  std::string token;
75  ReadToken(is, binary, &token);
76  if (token == "</Questions>") return;
77  else {
78  if (token != "<Key>")
79  KALDI_ERR << "Questions::Read, expecting <Key>, got "<<token;
80  EventKeyType key;
81  ReadBasicType(is, binary, &key);
82  QuestionsForKey opts;
83  opts.Read(is, binary);
84  SetQuestionsOf(key, opts);
85  }
86  }
87 }
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
std::vector< QuestionsForKey * > key_options_
void SetQuestionsOf(EventKeyType key, const QuestionsForKey &options_of_key)
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
std::map< EventKeyType, size_t > key_idx_
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ SetQuestionsOf()

void SetQuestionsOf ( EventKeyType  key,
const QuestionsForKey options_of_key 
)
inline

Definition at line 89 of file build-tree-questions.h.

References QuestionsForKey::Check(), KALDI_ASSERT, and QuestionsForKey::QuestionsForKey().

Referenced by main().

89  {
90  options_of_key.Check();
91  if (key_idx_.count(key) == 0) {
92  key_idx_[key] = key_options_.size();
93  key_options_.push_back(new QuestionsForKey());
94  *(key_options_.back()) = options_of_key;
95  } else {
96  size_t idx = key_idx_[key];
97  KALDI_ASSERT(idx < key_options_.size());
98  *(key_options_[idx]) = options_of_key;
99  }
100  }
std::vector< QuestionsForKey * > key_options_
std::map< EventKeyType, size_t > key_idx_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Write()

void Write ( std::ostream &  os,
bool  binary 
) const

Definition at line 48 of file build-tree-questions.cc.

References rnnlm::i, QuestionsForKey::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by kaldi::TestQuestionsInitRand().

48  {
49  WriteToken(os, binary, "<Questions>");
50 
51  std::vector<EventKeyType> keys_with_options;
52  this->GetKeysWithQuestions(&keys_with_options);
53  for (size_t i = 0; i < keys_with_options.size(); i++) {
54  EventKeyType key = keys_with_options[i];
55  WriteToken(os, binary, "<Key>");
56  WriteBasicType(os, binary, key);
57  const QuestionsForKey &opts = GetQuestionsOf(key);
58  opts.Write(os, binary);
59  }
60  WriteToken(os, binary, "</Questions>");
61 }
void GetKeysWithQuestions(std::vector< EventKeyType > *keys_out) const
const QuestionsForKey & GetQuestionsOf(EventKeyType key) const
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Member Data Documentation

◆ key_idx_

std::map<EventKeyType, size_t> key_idx_
private

Definition at line 127 of file build-tree-questions.h.

◆ key_options_

std::vector<QuestionsForKey*> key_options_
private

Definition at line 126 of file build-tree-questions.h.


The documentation for this class was generated from the following files: