RegtreeMllrDiagGmm Class Reference

An MLLR mean transformation is an affine transformation of Gaussian means. More...

#include <regtree-mllr-diag-gmm.h>

Collaboration diagram for RegtreeMllrDiagGmm:

Public Member Functions

 RegtreeMllrDiagGmm ()
 
void Init (int32 num_xforms, int32 dim)
 Allocates memory for transform matrix & bias vector. More...
 
void SetUnit ()
 Initialize transform matrix to identity and bias vector to zero. More...
 
void TransformModel (const RegressionTree &regtree, AmDiagGmm *am)
 Apply the transform(s) to all the Gaussian means in the model. More...
 
void GetTransformedMeans (const RegressionTree &regtree, const AmDiagGmm &am, int32 pdf_index, MatrixBase< BaseFloat > *out) const
 Get all the transformed means for a given pdf. More...
 
void Write (std::ostream &out_stream, bool binary) const
 
void Read (std::istream &in_stream, bool binary)
 
void SetParameters (const MatrixBase< BaseFloat > &mat, int32 regclass)
 Mutators. More...
 
void set_bclass2xforms (const std::vector< int32 > &in)
 
const std::vector< Matrix< BaseFloat > > xform_matrices () const
 Accessors. More...
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (RegtreeMllrDiagGmm)
 

Private Attributes

std::vector< Matrix< BaseFloat > > xform_matrices_
 Transform matrices: size() = num_xforms_. More...
 
int32 num_xforms_
 Number of transforms == xform_matrices_.size() More...
 
std::vector< int32bclass2xforms_
 For each baseclass index of which transform to use; -1 => no xform. More...
 
int32 dim_
 Dimension of feature vectors. More...
 

Detailed Description

An MLLR mean transformation is an affine transformation of Gaussian means.

Definition at line 53 of file regtree-mllr-diag-gmm.h.

Constructor & Destructor Documentation

◆ RegtreeMllrDiagGmm()

RegtreeMllrDiagGmm ( )
inline

Definition at line 55 of file regtree-mllr-diag-gmm.h.

55 {}

Member Function Documentation

◆ GetTransformedMeans()

void GetTransformedMeans ( const RegressionTree regtree,
const AmDiagGmm am,
int32  pdf_index,
MatrixBase< BaseFloat > *  out 
) const

Get all the transformed means for a given pdf.

Definition at line 84 of file regtree-mllr-diag-gmm.cc.

References VectorBase< Real >::AddMatVec(), RegtreeMllrDiagGmm::bclass2xforms_, RegtreeMllrDiagGmm::dim_, RegressionTree::Gauss2BaseclassId(), AmDiagGmm::GetGaussianMean(), AmDiagGmm::GetPdf(), KALDI_ASSERT, kaldi::kNoTrans, RegtreeMllrDiagGmm::num_xforms_, RegressionTree::NumBaseclasses(), MatrixBase< Real >::NumCols(), DiagGmm::NumGauss(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Row(), and RegtreeMllrDiagGmm::xform_matrices_.

Referenced by TestXformMean().

87  {
88  KALDI_ASSERT(static_cast<int32>(bclass2xforms_.size()) ==
89  regtree.NumBaseclasses());
90  int32 num_gauss = am.GetPdf(pdf_index).NumGauss();
91  KALDI_ASSERT(out->NumRows() == num_gauss && out->NumCols() == dim_);
92 
93  Vector<BaseFloat> extended_mean(dim_+1);
94  extended_mean(dim_) = 1.0;
95 
96  for (int32 gauss_index = 0; gauss_index < num_gauss; gauss_index++) {
97  int32 bclass_index = regtree.Gauss2BaseclassId(pdf_index, gauss_index);
98  int32 xform_index = bclass2xforms_[bclass_index];
99  if (xform_index > -1) { // use a transform
100  KALDI_ASSERT(xform_index < num_xforms_);
101  SubVector<BaseFloat> tmp_mean(extended_mean.Range(0, dim_));
102  am.GetGaussianMean(pdf_index, gauss_index, &tmp_mean);
103  SubVector<BaseFloat> out_row(out->Row(gauss_index));
104  out_row.AddMatVec(1.0, xform_matrices_[xform_index], kNoTrans,
105  extended_mean, 0.0);
106  } else { // Copy untransformed mean
107  SubVector<BaseFloat> out_row(out->Row(gauss_index));
108  am.GetGaussianMean(pdf_index, gauss_index, &out_row);
109  }
110  }
111 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.
int32 dim_
Dimension of feature vectors.
kaldi::int32 int32
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
int32 num_xforms_
Number of transforms == xform_matrices_.size()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Init()

void Init ( int32  num_xforms,
int32  dim 
)

Allocates memory for transform matrix & bias vector.

Definition at line 29 of file regtree-mllr-diag-gmm.cc.

References RegtreeMllrDiagGmm::bclass2xforms_, RegtreeMllrDiagGmm::dim_, KALDI_ASSERT, RegtreeMllrDiagGmm::num_xforms_, and RegtreeMllrDiagGmm::xform_matrices_.

Referenced by RegtreeMllrDiagGmmAccs::Update().

29  {
30  if (num_xforms == 0) { // empty transform
31  xform_matrices_.clear();
32  dim_ = 0; // non-zero dimension is meaningless with empty transform
33  num_xforms_ = 0;
34  bclass2xforms_.clear();
35  } else {
36  KALDI_ASSERT(dim != 0); // if not empty, dim = 0 is meaningless
37  dim_ = dim;
38  num_xforms_ = num_xforms;
39  xform_matrices_.resize(num_xforms);
40  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
41  xform_itr_end = xform_matrices_.end();
42  for (; xform_itr != xform_itr_end; ++xform_itr) {
43  xform_itr->Resize(dim, dim+1);
44  xform_itr->SetUnit();
45  }
46  }
47 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.
int32 dim_
Dimension of feature vectors.
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
int32 num_xforms_
Number of transforms == xform_matrices_.size()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( RegtreeMllrDiagGmm  )
private

◆ Read()

void Read ( std::istream &  in_stream,
bool  binary 
)

Definition at line 134 of file regtree-mllr-diag-gmm.cc.

References RegtreeMllrDiagGmm::bclass2xforms_, RegtreeMllrDiagGmm::dim_, kaldi::ExpectToken(), KALDI_ASSERT, RegtreeMllrDiagGmm::num_xforms_, kaldi::ReadBasicType(), kaldi::ReadIntegerVector(), and RegtreeMllrDiagGmm::xform_matrices_.

134  {
135  ExpectToken(in, binary, "<MLLRXFORM>");
136  ExpectToken(in, binary, "<NUMXFORMS>");
137  ReadBasicType(in, binary, &num_xforms_);
138  ExpectToken(in, binary, "<DIMENSION>");
139  ReadBasicType(in, binary, &dim_);
140  KALDI_ASSERT(num_xforms_ >= 0 && dim_ >= 0); // can be 0 for empty xform
141 
143  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
144  xform_itr_end = xform_matrices_.end();
145  for (; xform_itr != xform_itr_end; ++xform_itr) {
146  ExpectToken(in, binary, "<XFORM>");
147  xform_itr->Read(in, binary);
148  KALDI_ASSERT(xform_itr->NumRows() == (xform_itr->NumCols() - 1)
149  && xform_itr->NumRows() == dim_);
150  }
151 
152  ExpectToken(in, binary, "<BCLASS2XFORMS>");
153  ReadIntegerVector(in, binary, &bclass2xforms_);
154  ExpectToken(in, binary, "</MLLRXFORM>");
155 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.
int32 dim_
Dimension of feature vectors.
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
int32 num_xforms_
Number of transforms == xform_matrices_.size()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ set_bclass2xforms()

void set_bclass2xforms ( const std::vector< int32 > &  in)
inline

Definition at line 75 of file regtree-mllr-diag-gmm.h.

Referenced by RegtreeMllrDiagGmmAccs::Update().

75 { bclass2xforms_ = in; }
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.

◆ SetParameters()

void SetParameters ( const MatrixBase< BaseFloat > &  mat,
int32  regclass 
)
inline

Mutators.

Definition at line 94 of file regtree-mllr-diag-gmm.h.

References kaldi::kNoTrans.

Referenced by RegtreeMllrDiagGmmAccs::Update().

95  {
96  xform_matrices_[regclass].CopyFromMat(mat, kNoTrans);
97 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.

◆ SetUnit()

void SetUnit ( )

Initialize transform matrix to identity and bias vector to zero.

Definition at line 49 of file regtree-mllr-diag-gmm.cc.

References RegtreeMllrDiagGmm::xform_matrices_.

49  {
50  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
51  xform_itr_end = xform_matrices_.end();
52  for (; xform_itr != xform_itr_end; ++xform_itr) {
53  xform_itr->SetUnit();
54  }
55 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.

◆ TransformModel()

void TransformModel ( const RegressionTree regtree,
AmDiagGmm am 
)

Apply the transform(s) to all the Gaussian means in the model.

Definition at line 57 of file regtree-mllr-diag-gmm.cc.

References RegtreeMllrDiagGmm::bclass2xforms_, AmDiagGmm::ComputeGconsts(), RegtreeMllrDiagGmm::dim_, RegressionTree::GetBaseclass(), AmDiagGmm::GetGaussianMean(), KALDI_ASSERT, kaldi::kNoTrans, RegtreeMllrDiagGmm::num_xforms_, RegressionTree::NumBaseclasses(), AmDiagGmm::SetGaussianMean(), and RegtreeMllrDiagGmm::xform_matrices_.

Referenced by TestMllrAccsIO(), and TestXformMean().

58  {
59  KALDI_ASSERT(static_cast<int32>(bclass2xforms_.size()) ==
60  regtree.NumBaseclasses());
61  Vector<BaseFloat> extended_mean(dim_+1), xformed_mean(dim_);
62  for (int32 bclass_index = 0, num_bclasses = regtree.NumBaseclasses();
63  bclass_index < num_bclasses; ++bclass_index) {
64  int32 xform_index;
65  if ((xform_index = bclass2xforms_[bclass_index]) > -1) {
66  KALDI_ASSERT(xform_index < num_xforms_);
67  const vector< pair<int32, int32> > &bclass =
68  regtree.GetBaseclass(bclass_index);
69  for (vector< pair<int32, int32> >::const_iterator itr = bclass.begin(),
70  end = bclass.end(); itr != end; ++itr) {
71  SubVector<BaseFloat> tmp_mean(extended_mean.Range(0, dim_));
72  am->GetGaussianMean(itr->first, itr->second, &tmp_mean);
73  extended_mean(dim_) = 1.0;
74  xformed_mean.AddMatVec(1.0, xform_matrices_[xform_index], kNoTrans,
75  extended_mean, 0.0);
76  am->SetGaussianMean(itr->first, itr->second, xformed_mean);
77  } // end iterating over Gaussians in baseclass
78  } // else keep the means untransformed
79  } // end iterating over all baseclasses
80  am->ComputeGconsts();
81 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.
int32 dim_
Dimension of feature vectors.
kaldi::int32 int32
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
int32 num_xforms_
Number of transforms == xform_matrices_.size()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Write()

void Write ( std::ostream &  out_stream,
bool  binary 
) const

Definition at line 114 of file regtree-mllr-diag-gmm.cc.

References RegtreeMllrDiagGmm::bclass2xforms_, RegtreeMllrDiagGmm::dim_, RegtreeMllrDiagGmm::num_xforms_, kaldi::WriteBasicType(), kaldi::WriteIntegerVector(), kaldi::WriteToken(), and RegtreeMllrDiagGmm::xform_matrices_.

114  {
115  WriteToken(out, binary, "<MLLRXFORM>");
116  WriteToken(out, binary, "<NUMXFORMS>");
117  WriteBasicType(out, binary, num_xforms_);
118  WriteToken(out, binary, "<DIMENSION>");
119  WriteBasicType(out, binary, dim_);
120 
121  vector< Matrix<BaseFloat> >::const_iterator xform_itr =
122  xform_matrices_.begin(), xform_itr_end = xform_matrices_.end();
123  for (; xform_itr != xform_itr_end; ++xform_itr) {
124  WriteToken(out, binary, "<XFORM>");
125  xform_itr->Write(out, binary);
126  }
127 
128  WriteToken(out, binary, "<BCLASS2XFORMS>");
129  WriteIntegerVector(out, binary, bclass2xforms_);
130  WriteToken(out, binary, "</MLLRXFORM>");
131 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.
int32 dim_
Dimension of feature vectors.
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
int32 num_xforms_
Number of transforms == xform_matrices_.size()
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

◆ xform_matrices()

const std::vector< Matrix<BaseFloat> > xform_matrices ( ) const
inline

Accessors.

Definition at line 78 of file regtree-mllr-diag-gmm.h.

78  {
79  return xform_matrices_;
80  }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices: size() = num_xforms_.

Member Data Documentation

◆ bclass2xforms_

std::vector<int32> bclass2xforms_
private

For each baseclass index of which transform to use; -1 => no xform.

Definition at line 87 of file regtree-mllr-diag-gmm.h.

Referenced by RegtreeMllrDiagGmm::GetTransformedMeans(), RegtreeMllrDiagGmm::Init(), RegtreeMllrDiagGmm::Read(), RegtreeMllrDiagGmm::TransformModel(), and RegtreeMllrDiagGmm::Write().

◆ dim_

◆ num_xforms_

int32 num_xforms_
private

◆ xform_matrices_


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