logistic-regression-train.cc
Go to the documentation of this file.
1 // ivectorbin/logistic-regression-train.cc
2 
3 // Copyright 2014 David Snyder
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
24 
25 
26 int main(int argc, char *argv[]) {
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
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Train(const Matrix< BaseFloat > &xs, const std::vector< int32 > &ys, const LogisticRegressionConfig &conf)
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#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.
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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