34 if (fabs(sum_p - 1.0) > 0.01 || fabs(sum_q - 1.0) > 0.01) {
35 KALDI_WARN <<
"KlDivergence: vectors are not close to being normalized " 36 << sum_p <<
", " << sum_q;
42 BaseFloat p_prob = p(
i) / sum_p, q_prob = q(
i) / sum_q;
43 ans += p_prob *
Log(p_prob / q_prob);
50 if (old_priors.
Dim() == 0) {
51 KALDI_LOG <<
"Model did not previously have priors attached.";
54 diff_prior.
AddVec(-1.0, old_priors);
57 diff_prior.
Max(&max_index);
58 KALDI_LOG <<
"Adjusting priors: largest absolute difference was for " 59 <<
"pdf " << max_index <<
", " << old_priors(max_index)
60 <<
" -> " << new_priors(max_index);
61 KALDI_LOG <<
"Adjusting priors: K-L divergence from old to new is " 70 int main(
int argc,
char *argv[]) {
72 using namespace kaldi;
77 "Set the priors of the nnet3 neural net to the computed posterios from the net,\n" 78 "on typical data (e.g. training data). This is correct under more general\n" 79 "circumstances than using the priors of the class labels in the training data\n" 81 "Typical usage of this program will involve computation of an average pdf-level\n" 82 "posterior with nnet3-compute or nnet3-compute-from-egs, piped into matrix-sum-rows\n" 83 "and then vector-sum, to compute the average posterior\n" 85 "Usage: nnet3-am-adjust-priors [options] <nnet-in> <summed-posterior-vector-in> <nnet-out>\n" 87 " nnet3-am-adjust-priors final.mdl counts.vec final.mdl\n";
89 bool binary_write =
true;
95 po.
Register(
"binary", &binary_write,
"Write output in binary mode");
96 po.
Register(
"prior-floor", &prior_floor,
"When setting priors, floor for " 97 "priors (only used to avoid generating NaNs upon inversion)");
106 std::string nnet_rxfilename = po.
GetArg(1),
107 posterior_vec_rxfilename = po.
GetArg(2),
108 nnet_wxfilename = po.
GetArg(3);
114 Input ki(nnet_rxfilename, &binary_read);
124 posterior_vec.
Scale(1.0 / posterior_vec.
Sum());
133 Output ko(nnet_wxfilename, binary_write);
137 KALDI_LOG <<
"Modified priors of neural network model and wrote it to " 140 }
catch(
const std::exception &e) {
141 std::cerr << e.what() <<
'\n';
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Read(std::istream &is, bool binary)
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
void SetPriors(const VectorBase< BaseFloat > &priors)
void PrintPriorDiagnostics(const Vector< BaseFloat > &old_priors, const Vector< BaseFloat > &new_priors)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
void Read(std::istream &is, bool binary)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
MatrixIndexT Dim() const
Returns the dimension of the vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
Real Sum() const
Returns sum of the elements.
const VectorBase< BaseFloat > & Priors() const
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
BaseFloat KlDivergence(const Vector< BaseFloat > &p, const Vector< BaseFloat > &q)
A class representing a vector.
#define KALDI_ASSERT(cond)
void ApplyAbs()
Take absolute value of each of the elements.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void Write(std::ostream &os, bool binary) const