logistic-regression.h
Go to the documentation of this file.
1 // ivector/logistic-regression.h
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 #ifndef KALDI_IVECTOR_LOGISTIC_REGRESSION_H_
21 #define KALDI_IVECTOR_LOGISTIC_REGRESSION_H_
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "matrix/matrix-lib.h"
26 #include <numeric>
27 
28 
29 namespace kaldi {
30 
33  mix_up;
34  double normalizer,
35  power;
36  LogisticRegressionConfig(): max_steps(20), mix_up(0),
37  normalizer(0.0025), power(0.15){ }
38  void Register(OptionsItf *opts) {
39  opts->Register("max-steps", &max_steps,
40  "Maximum steps in L-BFGS.");
41  opts->Register("normalizer", &normalizer,
42  "Coefficient for L2 regularization.");
43  opts->Register("mix-up", &mix_up,
44  "Target number of mixture components to create, "
45  "if supplied.");
46  opts->Register("power", &power,
47  "Power rule for determining the number of mixtures "
48  "to create.");
49  }
50 };
51 
53  public:
54  // xs and ys are the training data. Each row of xs is a vector
55  // corresponding to the class label in the same row of ys.
56  void Train(const Matrix<BaseFloat> &xs, const std::vector<int32> &ys,
57  const LogisticRegressionConfig &conf);
58 
59  // Calculates the log posterior of the class label given the input xs.
60  // The rows of log_posteriors corresponds to the rows of xs: the
61  // individual data points to be evaluated. The columns of
62  // log_posteriors are the integer class labels.
63  void GetLogPosteriors(const Matrix<BaseFloat> &xs,
64  Matrix<BaseFloat> *log_posteriors);
65 
66  // Calculates the log posterior of the class label given the input x.
67  // The indices of log_posteriors are the class labels.
68  void GetLogPosteriors(const Vector<BaseFloat> &x,
69  Vector<BaseFloat> *log_posteriors);
70 
71  void Write(std::ostream &os, bool binary) const;
72  void Read(std::istream &is, bool binary);
73 
74  void ScalePriors(const Vector<BaseFloat> &prior_scales);
75 
76  protected:
77  void friend UnitTestTrain();
78  void friend UnitTestPosteriors();
79 
80  private:
81  // Performs a step in the L-BFGS. This is mostly used internally
82  // By Train() and for testing.
83  BaseFloat DoStep(const Matrix<BaseFloat> &xs,
84  Matrix<BaseFloat> *xw, const std::vector<int32> &ys,
87 
88  void TrainParameters(const Matrix<BaseFloat> &xs,
89  const std::vector<int32> &ys,
90  const LogisticRegressionConfig &conf,
91  Matrix<BaseFloat> *xw);
92 
93  // Creates the mixture components. Uses conf.mix_up, conf.power,
94  // the occupancy of ys and GetSplitTargets() to determin the number
95  // of mixture components for each weight index.
96  void MixUp(const std::vector<int32> &ys, const int32 &num_classes,
97  const LogisticRegressionConfig &conf);
98 
99  // Returns the objective function given the training data, xs, ys.
100  // The gradient is also calculated, and returned in grad. Uses
101  // L2 regularization.
102  BaseFloat GetObjfAndGrad(const Matrix<BaseFloat> &xs,
103  const std::vector<int32> &ys,
104  const Matrix<BaseFloat> &xw,
105  Matrix<BaseFloat> *grad,
106  BaseFloat normalizer);
107 
108  // Sets the weights and class map. This is generally used for testing.
109  void SetWeights(const Matrix<BaseFloat> &weights,
110  const std::vector<int32> classes);
111  // Before mixture components or added, or if mix_up <= num_classes
112  // each row of weights_ corresponds to a class label.
113  // If mix_up > num_classes and after MixUp() is called the rows
114  // correspond to the mixture components. In either case each column
115  // corresponds to a feature in the input vectors (and the last column
116  // is an offset).
118  // Maps from the row of weights_ to the class. Normally the
119  // identity mapping, but may not be for multi-mixture logistic
120  // regression.
121  std::vector<int32> class_;
122 };
123 
124 }
125 
126 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
kaldi::int32 int32
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
A class representing a vector.
Definition: kaldi-vector.h:406
std::vector< int32 > class_
Matrix< BaseFloat > weights_
void UnitTestPosteriors()