lda-estimate.h
Go to the documentation of this file.
1 // transform/lda-estimate.h
2 
3 // Copyright 2009-2011 Jan Silovsky
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_TRANSFORM_LDA_ESTIMATE_H_
21 #define KALDI_TRANSFORM_LDA_ESTIMATE_H_
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "matrix/matrix-lib.h"
26 
27 namespace kaldi {
28 
33  BaseFloat within_class_factor; // TODO: remove this eventually, it
34  // is deprecated (that code is now in ../nnet2/get-feature-transform.{h,cc})
35  LdaEstimateOptions(): remove_offset(false), dim(40), allow_large_dim(false),
36  within_class_factor(1.0) { }
37 
38  void Register(OptionsItf *opts) {
39  opts->Register("remove-offset", &remove_offset, "If true, output an affine "
40  "transform that makes the projected data mean equal to zero.");
41  opts->Register("dim", &dim, "Dimension to project to with LDA");
42  opts->Register("allow-large-dim", &allow_large_dim, "If true, allow an LDA "
43  "dimension larger than the number of classes.");
44  opts->Register("within-class-factor", &within_class_factor, "(Deprecated) If 1.0, do "
45  "conventional LDA where the within-class variance will be "
46  "unit in the projected space. May be set to less than 1.0, "
47  "which scales the features to have less variance, particularly "
48  "for dimensions where between-class variance is small; "
49  "this is a feature being experimented with for neural-net "
50  "input.");
51  }
52 };
53 
57 class LdaEstimate {
58  public:
60 
62  void Init(int32 num_classes, int32 dimension);
64  int32 NumClasses() const { return first_acc_.NumRows(); }
66  int32 Dim() const { return first_acc_.NumCols(); }
68  void ZeroAccumulators();
70  void Scale(BaseFloat f);
72  double TotCount() { return zero_acc_.Sum(); }
73 
75  void Accumulate(const VectorBase<BaseFloat> &data, int32 class_id, BaseFloat weight = 1.0);
76 
85  void Estimate(const LdaEstimateOptions &opts,
87  Matrix<BaseFloat> *Mfull = NULL) const;
88 
89  void Read(std::istream &in_stream, bool binary, bool add);
90  void Write(std::ostream &out_stream, bool binary) const;
91 
92  protected:
96 
99  static void AddMeanOffset(const VectorBase<double> &total_mean,
100  Matrix<BaseFloat> *projection);
101 
103  void GetStats(SpMatrix<double> *total_covar,
104  SpMatrix<double> *between_covar,
105  Vector<double> *total_mean,
106  double *sum) const;
107 
108  // Disallow assignment operator.
109  LdaEstimate &operator = (const LdaEstimate &other);
110 };
111 
112 } // End namespace kaldi
113 
114 #endif // KALDI_TRANSFORM_LDA_ESTIMATE_H_
115 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for computing linear discriminant analysis (LDA) transform.
Definition: lda-estimate.h:57
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: lda-estimate.h:66
Matrix< double > first_acc_
Definition: lda-estimate.h:94
kaldi::int32 int32
Vector< double > zero_acc_
Definition: lda-estimate.h:93
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
int32 NumClasses() const
Returns the number of classes.
Definition: lda-estimate.h:64
double TotCount()
Return total count of the data.
Definition: lda-estimate.h:72
SpMatrix< double > total_second_acc_
Definition: lda-estimate.h:95
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
void Register(OptionsItf *opts)
Definition: lda-estimate.h:38