28 int32 *pdf_id)
const {
31 event_vec.reserve(
N_+1);
32 event_vec.push_back(std::make_pair
34 static_cast<EventValueType>(pdf_class)));
37 event_vec.push_back(std::make_pair
38 (static_cast<EventKeyType>(
i),
39 static_cast<EventValueType>(phoneseq[
i])));
40 KALDI_ASSERT(static_cast<EventAnswerType>(phoneseq[i]) >= 0);
47 bool ensure_all_covered,
48 std::vector<int32> *hmm_lengths) {
50 int32 num_phones = phone_ids.size();
55 int32 max_phone = *std::max_element(phone_ids.begin(), phone_ids.end());
57 hmm_lengths->resize(max_phone + 1, -1);
58 std::vector<bool> is_ctx_dep(max_phone + 1);
60 for (
int32 i = 0;
i <= max_phone;
i++) {
61 (*hmm_lengths)[
i] = 1 +
Rand() % 3;
64 for (
size_t i = 0;
i < (
size_t) num_phones;
i++)
66 <<
", (phone_id, hmm_length, is_ctx_dep) == " 67 << (phone_ids[
i]) <<
" " << ((*hmm_lengths)[phone_ids[
i]])
68 <<
" " << (is_ctx_dep[phone_ids[
i]]);
71 size_t dim = 3 +
Rand() % 20;
72 GenRandStats(dim, num_stats, N, P, phone_ids, *hmm_lengths,
73 is_ctx_dep, ensure_all_covered, &stats);
78 int32 num_quest =
Rand() % 10, num_iters = rand () % 5;
84 std::vector<std::vector<int32> > phone_sets(phone_ids.size());
85 for (
size_t i = 0; i < phone_ids.size(); i++)
86 phone_sets[i].push_back(phone_ids[i]);
87 std::vector<bool> share_roots(phone_sets.size(),
true),
88 do_split(phone_sets.size(),
true);
90 tree =
BuildTree(qopts, phone_sets, *hmm_lengths, share_roots,
91 do_split, stats, thresh, 1000, 0.0, P);
99 bool ensure_all_covered,
100 std::vector<int32> *hmm_lengths) {
102 int32 num_phones = phone_ids.size();
103 int32 num_stats = 3000;
104 float ctx_dep_prob = 0.9;
106 hmm_lengths->clear();
107 int32 max_phone = *std::max_element(phone_ids.begin(), phone_ids.end());
108 hmm_lengths->resize(max_phone + 1, -1);
109 std::vector<bool> is_ctx_dep(max_phone + 1);
111 for (
int32 i = 0;
i <= max_phone;
i++) {
112 (*hmm_lengths)[
i] = 1 +
Rand() % 3;
115 for (
size_t i = 0;
i < (
size_t) num_phones;
i++) {
116 KALDI_VLOG(2) <<
"For idx = "<<
i <<
", (phone_id, hmm_length, is_ctx_dep) == " << (phone_ids[
i]) <<
" " << ((*hmm_lengths)[phone_ids[
i]]) <<
" " << (is_ctx_dep[phone_ids[
i]]);
120 size_t dim = 3 +
Rand() % 20;
121 GenRandStats(dim, num_stats, N, P, phone_ids, *hmm_lengths, is_ctx_dep, ensure_all_covered, &stats);
126 int32 num_quest = 40, num_iters = 0;
132 std::vector<std::vector<int32> > phone_sets(phone_ids.size());
133 for (
size_t i = 0;
i < phone_ids.size();
i++)
134 phone_sets[
i].push_back(phone_ids[
i]);
135 std::vector<bool> share_roots(phone_sets.size(),
true),
136 do_split(phone_sets.size(),
true);
138 tree =
BuildTree(qopts, phone_sets, *hmm_lengths, share_roots,
139 do_split, stats, thresh, 1000, 0.0, P);
151 WriteToken(os, binary,
"EndContextDependency");
166 if (token ==
"ToLength") {
168 delete to_num_pdf_classes;
171 if (token ==
"ToPdf") {
174 KALDI_ERR <<
"Got unexpected token " << token
175 <<
" reading context-dependency object.";
182 const std::vector<int32> &phones,
183 int32 self_loop_pdf_class,
int32 forward_pdf_class,
184 const std::vector<int32> &phone_window,
186 std::vector<int32> new_phone_window(phone_window);
189 std::vector<EventAnswerType> forward_pdfs, self_loop_pdfs;
193 for (
size_t i = 0;
i <
N_;
i++)
194 if (phone_window[
i] >= 0)
195 vec.push_back(std::make_pair(static_cast<EventKeyType>(
i),
196 static_cast<EventValueType>(phone_window[
i])));
197 vec.push_back(std::make_pair(
kPdfClass, static_cast<EventValueType>(forward_pdf_class)));
198 std::sort(vec.begin(), vec.end());
202 if (self_loop_pdf_class < 0) {
205 for (
int32 forward_pdf: forward_pdfs) {
206 pairs->insert(std::pair<int32,int32>(forward_pdf, -1));
213 for (
size_t i = 0; i <
N_; i++)
214 if (phone_window[i] >= 0)
215 vec.push_back(std::make_pair(static_cast<EventKeyType>(i),
216 static_cast<EventValueType>(phone_window[i])));
217 vec.push_back(std::make_pair(
kPdfClass, static_cast<EventValueType>(self_loop_pdf_class)));
218 std::sort(vec.begin(), vec.end());
222 if (forward_pdfs.size() == 1 || self_loop_pdfs.size() == 1) {
223 for (
size_t m = 0; m < forward_pdfs.size(); m++)
224 for (
size_t n = 0;
n < self_loop_pdfs.size();
n++)
225 pairs->insert(std::make_pair(forward_pdfs[m], self_loop_pdfs[
n]));
230 int32 min_dist = N_ - 1;
231 for (
int32 i = 0; i <
N_; i++) {
233 if (phone_window[i] == -1 && dist < min_dist) {
245 new_phone_window[position] = 0;
247 new_phone_window, pairs);
249 for (
size_t i = 0 ; i < phones.size(); i++) {
250 new_phone_window[position] = phones[
i];
252 new_phone_window, pairs);
258 const std::vector<int32> &phones,
259 const std::vector<std::vector<std::pair<int32, int32> > > &pdf_class_pairs,
260 std::vector<std::vector<std::vector<std::pair<int32, int32> > > > *pdf_info)
const {
263 pdf_info->resize(1 + *std::max_element(phones.begin(), phones.end()));
264 std::vector<int32> phone_window(
N_, -1);
266 for (
size_t i = 0 ;
i < phones.size();
i++) {
269 (*pdf_info)[phone].resize(pdf_class_pairs[phone].size());
270 for (
size_t j = 0;
j < pdf_class_pairs[phone].size();
j++) {
272 int32 pdf_class = pdf_class_pairs[phone][
j].first,
273 self_loop_pdf_class = pdf_class_pairs[phone][
j].second;
274 phone_window[
P_] = phone;
277 EnumeratePairs(phones, self_loop_pdf_class, pdf_class, phone_window, &pairs);
278 unordered_set<std::pair<int32, int32>,
PairHasher<int32> >::iterator iter = pairs.begin(),
280 for (; iter != end; ++iter)
281 (*pdf_info)[phone][
j].push_back(*iter);
282 std::sort( ((*pdf_info)[phone][
j]).begin(), ((*pdf_info)[phone][j]).end());
288 const std::vector<int32> &phones,
289 const std::vector<int32> &num_pdf_classes,
290 std::vector<std::vector<std::pair<int32, int32> > > *pdf_info)
const {
295 for (
size_t i = 0 ;
i < phones.size();
i++) {
298 vec.push_back(std::make_pair(static_cast<EventKeyType>(
P_),
299 static_cast<EventValueType>(phone)));
301 KALDI_ASSERT(static_cast<size_t>(phone) < num_pdf_classes.size());
304 for (
int32 pos = 0; pos < len; pos++) {
306 vec[0] = std::make_pair(static_cast<EventKeyType>(
P_),
307 static_cast<EventValueType>(phone));
308 vec[1] = std::make_pair(
kPdfClass, static_cast<EventValueType>(pos));
309 std::sort(vec.begin(), vec.end());
310 std::vector<EventAnswerType> pdfs;
314 KALDI_WARN <<
"ContextDependency::GetPdfInfo, no pdfs returned for position "<< pos <<
" of phone " << phone <<
". Continuing but this is a serious error.";
316 for (
size_t j = 0;
j < pdfs.size();
j++) {
317 KALDI_ASSERT(static_cast<size_t>(pdfs[
j]) < pdf_info->size());
318 (*pdf_info)[pdfs[
j]].push_back(std::make_pair(phone, pos));
322 for (
size_t i = 0;
i < pdf_info->size();
i++) {
323 std::sort( ((*pdf_info)[
i]).begin(), ((*pdf_info)[i]).end());
332 const std::vector<int32> &phone2num_pdf_classes) {
333 std::vector<std::vector<int32> > phone_sets(phones.size());
334 for (
size_t i = 0;
i < phones.size();
i++) phone_sets[
i].push_back(phones[
i]);
335 std::vector<bool> share_roots(phones.size(),
false);
337 int32 num_leaves = 0, P = 0, N = 1;
338 EventMap *pdf_map =
GetStubMap(P, phone_sets, phone2num_pdf_classes, share_roots, &num_leaves);
344 const std::vector<int32> &phone2num_pdf_classes) {
345 std::vector<bool> share_roots(phone_sets.size(),
false);
347 int32 num_leaves = 0, P = 0, N = 1;
348 EventMap *pdf_map =
GetStubMap(P, phone_sets, phone2num_pdf_classes, share_roots, &num_leaves);
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
virtual void GetPdfInfo(const std::vector< int32 > &phones, const std::vector< int32 > &num_pdf_classes, std::vector< std::vector< std::pair< int32, int32 > > > *pdf_info) const
GetPdfInfo returns a vector indexed by pdf-id, saying for each pdf which pairs of (phone...
ContextDependency * GenRandContextDependencyLarge(const std::vector< int32 > &phone_ids, int N, int P, bool ensure_all_covered, std::vector< int32 > *hmm_lengths)
GenRandContextDependencyLarge is like GenRandContextDependency but generates a larger tree with speci...
This class defines, for each EventKeyType, a set of initial questions that it tries and also a number...
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
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...
virtual bool Compute(const std::vector< int32 > &phoneseq, int32 pdf_class, int32 *pdf_id) const
returns success or failure; outputs pdf to pdf_id For positions that were outside the sequence (due t...
ContextDependency * MonophoneContextDependency(const std::vector< int32 > &phones, const std::vector< int32 > &phone2num_pdf_classes)
ContextDependency * MonophoneContextDependencyShared(const std::vector< std::vector< int32 > > &phone_sets, const std::vector< int32 > &phone2num_pdf_classes)
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq's (removes duplicates) from a vector.
void GenRandStats(int32 dim, int32 num_stats, int32 N, int32 P, const std::vector< int32 > &phone_ids, const std::vector< int32 > &phone2hmm_length, const std::vector< bool > &is_ctx_dep, bool ensure_all_phones_covered, BuildTreeStatsType *stats_out)
GenRandStats generates random statistics of the form used by BuildTree.
virtual bool Map(const EventType &event, EventAnswerType *ans) const =0
void EnumeratePairs(const std::vector< int32 > &phones, int32 self_loop_pdf_class, int32 forward_pdf_class, const std::vector< int32 > &context, unordered_set< std::pair< int32, int32 >, PairHasher< int32 > > *pairs) const
static EventMap * Read(std::istream &is, bool binary)
a Read function that reads an arbitrary EventMap; also works for NULL pointers.
void DeleteBuildTreeStats(BuildTreeStatsType *stats)
This frees the Clusterable* pointers in "stats", where non-NULL, and sets them to NULL...
static const EventKeyType kPdfClass
virtual int32 NumPdfs() const
NumPdfs() returns the number of acoustic pdfs (they are numbered 0.. NumPdfs()-1).
std::vector< std::pair< EventKeyType, EventValueType > > EventType
void Write(std::ostream &os, bool binary) const
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
void InitRand(const BuildTreeStatsType &stats, int32 num_quest, int32 num_iters_refine, AllKeysType all_keys_type)
InitRand attempts to generate "reasonable" random questions.
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
int Rand(struct RandomState *state)
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
void Read(std::istream &is, bool binary)
Read context-dependency object from disk; throws on error.
#define KALDI_ASSERT(cond)
EventMap * BuildTree(Questions &qopts, const std::vector< std::vector< int32 > > &phone_sets, const std::vector< int32 > &phone2num_pdf_classes, const std::vector< bool > &share_roots, const std::vector< bool > &do_split, const BuildTreeStatsType &stats, BaseFloat thresh, int32 max_leaves, BaseFloat cluster_thresh, int32 P, bool round_num_leaves)
BuildTree is the normal way to build a set of decision trees.
ContextDependency * GenRandContextDependency(const std::vector< int32 > &phone_ids, bool ensure_all_covered, std::vector< int32 > *hmm_lengths)
GenRandContextDependency is mainly of use for debugging.
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...
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
std::vector< std::pair< EventType, Clusterable * > > BuildTreeStatsType
virtual void MultiMap(const EventType &event, std::vector< EventAnswerType > *ans) const =0
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
virtual void Write(std::ostream &os, bool binary)=0
Write to stream.
#define KALDI_COMPILE_TIME_ASSERT(b)
A hashing function-object for pairs of ints.