logistic-regression-eval.cc File Reference
Include dependency graph for logistic-regression-eval.cc:

Go to the source code of this file.

Functions

int ComputeLogPosteriors (ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)
 
int32 ComputeScores (ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ ComputeLogPosteriors()

int ComputeLogPosteriors ( ParseOptions po,
const LogisticRegressionConfig config,
bool  apply_log 
)

Definition at line 27 of file logistic-regression-eval.cc.

References VectorBase< Real >::ApplyExp(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), LogisticRegression::GetLogPosteriors(), KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), kaldi::ReadKaldiObject(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

Referenced by main().

29  {
30  std::string model = po.GetArg(1),
31  vector_rspecifier = po.GetArg(2),
32  log_posteriors_wspecifier = po.GetArg(3);
33 
34  LogisticRegression classifier;
35  ReadKaldiObject(model, &classifier);
36 
37  std::vector<Vector<BaseFloat> > vectors;
38  SequentialBaseFloatVectorReader vector_reader(vector_rspecifier);
39  BaseFloatVectorWriter posterior_writer(log_posteriors_wspecifier);
40  std::vector<std::string> utt_list;
41  int32 num_utt_done = 0;
42 
43  for (; !vector_reader.Done(); vector_reader.Next()) {
44  std::string utt = vector_reader.Key();
45  const Vector<BaseFloat> &vector = vector_reader.Value();
46  Vector<BaseFloat> log_posteriors;
47  classifier.GetLogPosteriors(vector, &log_posteriors);
48  if (!apply_log)
49  log_posteriors.ApplyExp();
50  posterior_writer.Write(utt, log_posteriors);
51  num_utt_done++;
52  }
53  KALDI_LOG << "Calculated log posteriors for " << num_utt_done << " vectors.";
54  return (num_utt_done == 0 ? 1 : 0);
55 }
void ApplyExp()
Apply exponential to each value in vector.
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
void GetLogPosteriors(const Matrix< BaseFloat > &xs, Matrix< BaseFloat > *log_posteriors)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ ComputeScores()

int32 ComputeScores ( ParseOptions po,
const LogisticRegressionConfig config,
bool  apply_log 
)

Definition at line 57 of file logistic-regression-eval.cc.

References MatrixBase< Real >::ApplyExp(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), LogisticRegression::GetLogPosteriors(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), kaldi::ReadKaldiObject(), MatrixBase< Real >::Row(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

Referenced by main().

58  {
59  std::string model_rspecifier = po.GetArg(1),
60  trials_rspecifier = po.GetArg(2),
61  vector_rspecifier = po.GetArg(3),
62  scores_out = po.GetArg(4);
63 
64  SequentialInt32Reader class_reader(trials_rspecifier);
66  ReadKaldiObject(model_rspecifier, &classifier);
67 
68  std::vector<Vector<BaseFloat> > vectors;
69  std::vector<int32> ys;
70  std::vector<std::string> utt_list;
71  int32 num_utt_done = 0, num_utt_err = 0;
72 
73  RandomAccessBaseFloatVectorReader vector_reader(vector_rspecifier);
74  for (; !class_reader.Done(); class_reader.Next()) {
75  std::string utt = class_reader.Key();
76  int32 class_label = class_reader.Value();
77  if (!vector_reader.HasKey(utt)) {
78  KALDI_WARN << "No vector for utterance " << utt;
79  num_utt_err++;
80  } else {
81  utt_list.push_back(utt);
82  ys.push_back(class_label);
83  const Vector<BaseFloat> &vector = vector_reader.Value(utt);
84  vectors.push_back(vector);
85  num_utt_done++;
86  }
87  }
88 
89  if (vectors.empty()) {
90  KALDI_WARN << "Read no input";
91  return 1;
92  }
93 
94  Matrix<BaseFloat> xs(vectors.size(), vectors[0].Dim());
95  for (int i = 0; i < vectors.size(); i++) {
96  xs.Row(i).CopyFromVec(vectors[i]);
97  }
98 
99  Matrix<BaseFloat> log_posteriors;
100  classifier.GetLogPosteriors(xs, &log_posteriors);
101 
102  bool binary = false;
103  Output ko(scores_out.c_str(), binary);
104 
105  if (!apply_log)
106  log_posteriors.ApplyExp();
107 
108  for (int i = 0; i < ys.size(); i++) {
109  ko.Stream() << utt_list[i] << " " << ys[i] << " " << log_posteriors(i, ys[i]) << std::endl;
110  }
111  KALDI_LOG << "Calculated scores for " << num_utt_done
112  << " vectors with "
113  << num_utt_err << " missing. ";
114  return (num_utt_done == 0 ? 1 : 0);
115 }
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
void GetLogPosteriors(const Matrix< BaseFloat > &xs, Matrix< BaseFloat > *log_posteriors)
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 117 of file logistic-regression-eval.cc.

References ComputeLogPosteriors(), ComputeScores(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), LogisticRegressionConfig::Register(), and ParseOptions::Register().

117  {
118  using namespace kaldi;
119  typedef kaldi::int32 int32;
120  try {
121  const char *usage =
122  "Evaluates a model on input vectors and outputs either\n"
123  "log posterior probabilities or scores.\n"
124  "Usage1: logistic-regression-eval <model> <input-vectors-rspecifier>\n"
125  " <output-log-posteriors-wspecifier>\n"
126  "Usage2: logistic-regression-eval <model> <trials-file> <input-vectors-rspecifier>\n"
127  " <output-scores-file>\n";
128 
129  ParseOptions po(usage);
130 
131  bool apply_log = true;
132  po.Register("apply-log", &apply_log,
133  "If false, apply Exp to the log posteriors output. This is "
134  "helpful when combining posteriors from multiple logistic "
135  "regression models.");
137  config.Register(&po);
138  po.Read(argc, argv);
139 
140  if (po.NumArgs() != 3 && po.NumArgs() != 4) {
141  po.PrintUsage();
142  exit(1);
143  }
144 
145  if (po.NumArgs() == 4) {
146  return ComputeScores(po, config, apply_log);
147  } else {
148  return ComputeLogPosteriors(po, config, apply_log);
149  }
150 
151  } catch(const std::exception &e) {
152  std::cerr << e.what();
153  return -1;
154  }
155 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 ComputeScores(ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)
kaldi::int32 int32
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int ComputeLogPosteriors(ParseOptions &po, const LogisticRegressionConfig &config, bool apply_log)