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

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 26 of file logistic-regression-train.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), LogisticRegressionConfig::Register(), ParseOptions::Register(), MatrixBase< Real >::Row(), LogisticRegression::Train(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and kaldi::WriteKaldiObject().

26  {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Trains a model using Logistic Regression with L-BFGS from\n"
32  "a set of vectors. The class labels in <classes-rspecifier>\n"
33  "must be a set of integers such that there are no gaps in \n"
34  "its range and the smallest label must be 0.\n"
35  "Usage: logistic-regression-train <vector-rspecifier>\n"
36  "<classes-rspecifier> <model-out>\n";
37 
38  ParseOptions po(usage);
39 
40  bool binary = true;
42  config.Register(&po);
43  po.Register("binary", &binary, "Write output in binary mode");
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  std::string vector_rspecifier = po.GetArg(1),
52  class_rspecifier = po.GetArg(2),
53  model_out = po.GetArg(3);
54 
55  RandomAccessBaseFloatVectorReader vector_reader(vector_rspecifier);
56  SequentialInt32Reader class_reader(class_rspecifier);
57 
58  std::vector<int32> ys;
59  std::vector<std::string> utt_ids;
60  std::vector<Vector<BaseFloat> > vectors;
61 
62  int32 num_utt_done = 0, num_utt_err = 0;
63 
64  int32 num_classes = 0;
65  for (; !class_reader.Done(); class_reader.Next()) {
66  std::string utt = class_reader.Key();
67  int32 class_label = class_reader.Value();
68  if (!vector_reader.HasKey(utt)) {
69  KALDI_WARN << "No vector for utterance " << utt;
70  num_utt_err++;
71  } else {
72  ys.push_back(class_label);
73  const Vector<BaseFloat> &vector = vector_reader.Value(utt);
74  vectors.push_back(vector);
75 
76  // Since there are no gaps in the class labels and we
77  // start at 0, the largest label is the number of the
78  // of the classes - 1.
79  if (class_label > num_classes) {
80  num_classes = class_label;
81  }
82  num_utt_done++;
83  }
84  }
85 
86  // Since the largest label is 1 minus the number of
87  // classes.
88  num_classes += 1;
89 
90  KALDI_LOG << "Retrieved " << num_utt_done << " vectors with "
91  << num_utt_err << " missing. "
92  << "There were " << num_classes << " class labels.";
93 
94  if (num_utt_done == 0)
95  KALDI_ERR << "No vectors processed. Unable to train.";
96 
97  Matrix<BaseFloat> xs(vectors.size(), vectors[0].Dim());
98  for (int i = 0; i < vectors.size(); i++) {
99  xs.Row(i).CopyFromVec(vectors[i]);
100  }
101  vectors.clear();
102 
103  LogisticRegression classifier = LogisticRegression();
104  classifier.Train(xs, ys, config);
105  WriteKaldiObject(classifier, model_out, binary);
106 
107  return 0;
108  } catch(const std::exception &e) {
109  std::cerr << e.what();
110  return -1;
111  }
112 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
void Train(const Matrix< BaseFloat > &xs, const std::vector< int32 > &ys, const LogisticRegressionConfig &conf)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
A class representing a vector.
Definition: kaldi-vector.h:406
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153