FeatureTransformEstimateMulti Class Reference

#include <get-feature-transform.h>

Inheritance diagram for FeatureTransformEstimateMulti:
Collaboration diagram for FeatureTransformEstimateMulti:

Public Member Functions

void Estimate (const FeatureTransformEstimateOptions &opts, const std::vector< std::vector< int32 > > &indexes, Matrix< BaseFloat > *M) const
 This is as FeatureTransformEstimate, but for use in nnet-get-feature-transform-multi.cc, see the usage message of that program for a description of what it does. More...
 
- Public Member Functions inherited from FeatureTransformEstimate
void Estimate (const FeatureTransformEstimateOptions &opts, Matrix< BaseFloat > *M, TpMatrix< BaseFloat > *within_cholesky) const
 Estimates the LDA transform matrix m. More...
 
- Public Member Functions inherited from LdaEstimate
 LdaEstimate ()
 
void Init (int32 num_classes, int32 dimension)
 Allocates memory for accumulators. More...
 
int32 NumClasses () const
 Returns the number of classes. More...
 
int32 Dim () const
 Returns the dimensionality of the feature vectors. More...
 
void ZeroAccumulators ()
 Sets all accumulators to zero. More...
 
void Scale (BaseFloat f)
 Scales all accumulators. More...
 
double TotCount ()
 Return total count of the data. More...
 
void Accumulate (const VectorBase< BaseFloat > &data, int32 class_id, BaseFloat weight=1.0)
 Accumulates data. More...
 
void Estimate (const LdaEstimateOptions &opts, Matrix< BaseFloat > *M, Matrix< BaseFloat > *Mfull=NULL) const
 Estimates the LDA transform matrix m. More...
 
void Read (std::istream &in_stream, bool binary, bool add)
 
void Write (std::ostream &out_stream, bool binary) const
 

Private Member Functions

void EstimateTransformPart (const FeatureTransformEstimateOptions &opts, const std::vector< int32 > &indexes, const SpMatrix< double > &total_covar, const SpMatrix< double > &between_covar, const Vector< double > &mean, Matrix< BaseFloat > *M) const
 

Additional Inherited Members

- Protected Member Functions inherited from LdaEstimate
void GetStats (SpMatrix< double > *total_covar, SpMatrix< double > *between_covar, Vector< double > *total_mean, double *sum) const
 Extract a more processed form of the stats. More...
 
LdaEstimateoperator= (const LdaEstimate &other)
 
- Static Protected Member Functions inherited from FeatureTransformEstimate
static void EstimateInternal (const FeatureTransformEstimateOptions &opts, const SpMatrix< double > &total_covar, const SpMatrix< double > &between_covar, const Vector< double > &mean, Matrix< BaseFloat > *M, TpMatrix< BaseFloat > *C)
 
- Static Protected Member Functions inherited from LdaEstimate
static void AddMeanOffset (const VectorBase< double > &total_mean, Matrix< BaseFloat > *projection)
 This function modifies the LDA matrix so that it also subtracts the mean feature value. More...
 
- Protected Attributes inherited from LdaEstimate
Vector< double > zero_acc_
 
Matrix< double > first_acc_
 
SpMatrix< double > total_second_acc_
 

Detailed Description

Definition at line 157 of file get-feature-transform.h.

Member Function Documentation

◆ Estimate()

void Estimate ( const FeatureTransformEstimateOptions opts,
const std::vector< std::vector< int32 > > &  indexes,
Matrix< BaseFloat > *  M 
) const

This is as FeatureTransformEstimate, but for use in nnet-get-feature-transform-multi.cc, see the usage message of that program for a description of what it does.

Definition at line 165 of file get-feature-transform.cc.

References count, LdaEstimate::Dim(), LdaEstimate::GetStats(), rnnlm::i, kaldi::IsSortedAndUniq(), KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::Range(), FeatureTransformEstimateOptions::remove_offset, and Matrix< Real >::Resize().

Referenced by main().

168  {
169 
170  int32 input_dim = Dim(), output_dim = 0, num_transforms = indexes.size();
171  for (int32 i = 0; i < num_transforms; i++) { // some input-checking.
172  KALDI_ASSERT(indexes[i].size() > 0);
173  std::vector<int32> this_indexes(indexes[i]);
174  std::sort(this_indexes.begin(), this_indexes.end());
175  KALDI_ASSERT(IsSortedAndUniq(this_indexes)); // check for duplicates.
176  KALDI_ASSERT(this_indexes.front() >= 0);
177  KALDI_ASSERT(this_indexes.back() < input_dim);
178  output_dim += this_indexes.size();
179  }
180 
181  int32 input_dim_ext = (opts.remove_offset ? input_dim + 1 : input_dim);
182  M->Resize(output_dim, input_dim_ext);
183 
184  double count;
185  Vector<double> total_mean;
186  SpMatrix<double> total_covar, between_covar;
187  GetStats(&total_covar, &between_covar, &total_mean, &count);
188 
189  int32 cur_output_index = 0;
190  for (int32 i = 0; i < num_transforms; i++) {
191  Matrix<BaseFloat> M_tmp;
192  EstimateTransformPart(opts, indexes[i], total_covar, between_covar,
193  total_mean, &M_tmp);
194  int32 this_output_dim = indexes[i].size();
195  M->Range(cur_output_index, this_output_dim, 0, M->NumCols()).
196  CopyFromMat(M_tmp);
197  cur_output_index += this_output_dim;
198  }
199 
200 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: lda-estimate.h:66
kaldi::int32 int32
const size_t count
void EstimateTransformPart(const FeatureTransformEstimateOptions &opts, const std::vector< int32 > &indexes, const SpMatrix< double > &total_covar, const SpMatrix< double > &between_covar, const Vector< double > &mean, Matrix< BaseFloat > *M) const
void GetStats(SpMatrix< double > *total_covar, SpMatrix< double > *between_covar, Vector< double > *total_mean, double *sum) const
Extract a more processed form of the stats.
Definition: lda-estimate.cc:57
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63

◆ EstimateTransformPart()

void EstimateTransformPart ( const FeatureTransformEstimateOptions opts,
const std::vector< int32 > &  indexes,
const SpMatrix< double > &  total_covar,
const SpMatrix< double > &  between_covar,
const Vector< double > &  mean,
Matrix< BaseFloat > *  M 
) const
private

Definition at line 130 of file get-feature-transform.cc.

References SpMatrix< Real >::AddMat2Sp(), MatrixBase< Real >::AddMatMat(), VectorBase< Real >::AddMatVec(), FeatureTransformEstimateOptions::dim, LdaEstimate::Dim(), FeatureTransformEstimate::EstimateInternal(), rnnlm::i, kaldi::kCopyData, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), and Matrix< Real >::Resize().

136  {
137 
138  int32 full_dim = Dim(), proj_dim = indexes.size();
139  Matrix<double> transform(proj_dim, full_dim); // projects from full to projected dim.
140  for (int32 i = 0; i < proj_dim; i++)
141  transform(i, indexes[i]) = 1.0;
142 
143  SpMatrix<double> total_covar_proj(proj_dim), between_covar_proj(proj_dim);
144  Vector<double> mean_proj(proj_dim);
145  total_covar_proj.AddMat2Sp(1.0, transform, kNoTrans, total_covar, 0.0);
146  between_covar_proj.AddMat2Sp(1.0, transform, kNoTrans, between_covar, 0.0);
147  mean_proj.AddMatVec(1.0, transform, kNoTrans, mean, 0.0);
148 
149  Matrix<BaseFloat> M_proj;
150  FeatureTransformEstimateOptions opts_tmp(opts);
151  opts_tmp.dim = proj_dim;
152  EstimateInternal(opts_tmp, total_covar_proj, between_covar_proj, mean_proj,
153  &M_proj, NULL);
154  if (M_proj.NumCols() == proj_dim + 1) { // Extend transform to add the extra "1" that we
155  // use to handle mean shifts..
156  transform.Resize(proj_dim + 1, full_dim + 1, kCopyData);
157  transform(proj_dim, full_dim) = 1.0;
158  }
159  M->Resize(proj_dim, transform.NumCols());
160  // Produce output..
161  M->AddMatMat(1.0, M_proj, kNoTrans, Matrix<BaseFloat>(transform),
162  kNoTrans, 0.0);
163 }
int32 Dim() const
Returns the dimensionality of the feature vectors.
Definition: lda-estimate.h:66
kaldi::int32 int32
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
static void EstimateInternal(const FeatureTransformEstimateOptions &opts, const SpMatrix< double > &total_covar, const SpMatrix< double > &between_covar, const Vector< double > &mean, Matrix< BaseFloat > *M, TpMatrix< BaseFloat > *C)
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

The documentation for this class was generated from the following files: