RegtreeFmllrDiagGmm Class Reference

An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine transformation of the feature vectors. More...

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

Collaboration diagram for RegtreeFmllrDiagGmm:

Public Member Functions

 RegtreeFmllrDiagGmm ()
 
 RegtreeFmllrDiagGmm (const RegtreeFmllrDiagGmm &other)
 
 ~RegtreeFmllrDiagGmm ()
 
void Init (size_t num_xforms, size_t dim)
 Allocates memory for transform matrix & bias vector. More...
 
void Validate ()
 Checks whether the various parameters are consistent. More...
 
void SetUnit ()
 Sets transform matrix to identity and bias vector to zero. More...
 
void ComputeLogDets ()
 Computes the log-determinant of the Jacobians for each transform. More...
 
void TransformFeature (const VectorBase< BaseFloat > &in, std::vector< Vector< BaseFloat > > *out) const
 Get the transformed features for each of the transforms. More...
 
void Write (std::ostream &out_stream, bool binary) const
 
void Read (std::istream &in_stream, bool binary)
 
int32 Dim () const
 Accessors. More...
 
int32 NumBaseClasses () const
 
int32 NumRegClasses () const
 
void GetXformMatrix (int32 xform_index, Matrix< BaseFloat > *out) const
 
void GetLogDets (VectorBase< BaseFloat > *out) const
 
int32 Base2RegClass (int32 bclass) const
 
void SetParameters (const MatrixBase< BaseFloat > &mat, size_t regclass)
 Mutators. More...
 
void set_bclass2xforms (const std::vector< int32 > &in)
 

Private Member Functions

void operator= (const RegtreeFmllrDiagGmm &)
 

Private Attributes

int32 dim_
 Dimension of feature vectors. More...
 
int32 num_xforms_
 Number of transform matrices. More...
 
std::vector< Matrix< BaseFloat > > xform_matrices_
 Transform matrices. More...
 
Vector< BaseFloatlogdet_
 Log-determinants of the Jacobians. More...
 
bool valid_logdet_
 Whether logdets are for current transforms. More...
 
std::vector< int32bclass2xforms_
 For each baseclass index of which transform to use; -1 => no xform. More...
 

Detailed Description

An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine transformation of the feature vectors.

This class supports multiple transforms, and a regression tree. For a single, feature-level transformation see fmllr-diag-gmm-global.h Note: the "regression classes" are the classes after tree-clustering, which are smaller in number than the "base classes" (these correspond to the leaves of the tree).

Definition at line 69 of file regtree-fmllr-diag-gmm.h.

Constructor & Destructor Documentation

◆ RegtreeFmllrDiagGmm() [1/2]

RegtreeFmllrDiagGmm ( )
inline

Definition at line 71 of file regtree-fmllr-diag-gmm.h.

71 : dim_(-1), num_xforms_(-1), valid_logdet_(false) {}
int32 num_xforms_
Number of transform matrices.
int32 dim_
Dimension of feature vectors.
bool valid_logdet_
Whether logdets are for current transforms.

◆ RegtreeFmllrDiagGmm() [2/2]

RegtreeFmllrDiagGmm ( const RegtreeFmllrDiagGmm other)
inlineexplicit

Definition at line 72 of file regtree-fmllr-diag-gmm.h.

73  : dim_(other.dim_), num_xforms_(other.num_xforms_),
74  xform_matrices_(other.xform_matrices_), logdet_(other.logdet_),
75  valid_logdet_(other.valid_logdet_),
76  bclass2xforms_(other.bclass2xforms_) {}
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
int32 dim_
Dimension of feature vectors.
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.
bool valid_logdet_
Whether logdets are for current transforms.

◆ ~RegtreeFmllrDiagGmm()

~RegtreeFmllrDiagGmm ( )
inline

Definition at line 77 of file regtree-fmllr-diag-gmm.h.

77 {}

Member Function Documentation

◆ Base2RegClass()

int32 Base2RegClass ( int32  bclass) const
inline

Definition at line 97 of file regtree-fmllr-diag-gmm.h.

Referenced by DecodableAmDiagGmmRegtreeFmllr::LogLikelihoodZeroBased().

97 { return bclass2xforms_[bclass]; }
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.

◆ ComputeLogDets()

void ComputeLogDets ( )

Computes the log-determinant of the Jacobians for each transform.

Definition at line 107 of file regtree-fmllr-diag-gmm.cc.

References RegtreeFmllrDiagGmm::dim_, KALDI_ASSERT, KALDI_ISNAN, MatrixBase< Real >::LogDet(), RegtreeFmllrDiagGmm::logdet_, RegtreeFmllrDiagGmm::num_xforms_, RegtreeFmllrDiagGmm::valid_logdet_, and RegtreeFmllrDiagGmm::xform_matrices_.

Referenced by RegtreeFmllrDiagGmm::Read().

107  {
108  logdet_.Resize(num_xforms_);
109  for (int32 r = 0; r < num_xforms_; r++) {
110  SubMatrix<BaseFloat> tmp_a(xform_matrices_[r], 0, dim_, 0,
111  dim_);
112  logdet_(r) = tmp_a.LogDet();
114  }
115  valid_logdet_ = true;
116 }
kaldi::int32 int32
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ISNAN
Definition: kaldi-math.h:72
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.
bool valid_logdet_
Whether logdets are for current transforms.

◆ Dim()

int32 Dim ( ) const
inline

Accessors.

Definition at line 92 of file regtree-fmllr-diag-gmm.h.

92 { return dim_; }
int32 dim_
Dimension of feature vectors.

◆ GetLogDets()

void GetLogDets ( VectorBase< BaseFloat > *  out) const
inline

Definition at line 131 of file regtree-fmllr-diag-gmm.h.

References VectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), and KALDI_ASSERT.

Referenced by DecodableAmDiagGmmRegtreeFmllr::LogLikelihoodZeroBased(), and kaldi::UnitTestRegtreeFmllrDiagGmm().

131  {
132  KALDI_ASSERT(valid_logdet_ && out->Dim() == logdet_.Dim());
133  out->CopyFromVec(logdet_);
134 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.
bool valid_logdet_
Whether logdets are for current transforms.

◆ GetXformMatrix()

void GetXformMatrix ( int32  xform_index,
Matrix< BaseFloat > *  out 
) const
inline

Definition at line 115 of file regtree-fmllr-diag-gmm.h.

References MatrixBase< Real >::CopyFromMat(), KALDI_ERR, kaldi::kNoTrans, and Matrix< Real >::Resize().

116  {
117  if (xform_index >= num_xforms_) {
118  KALDI_ERR << "Index (" << xform_index << ") out of range [0, "
119  << num_xforms_ << "]";
120  }
121  out->Resize(dim_, dim_ + 1);
122  out->CopyFromMat(xform_matrices_[xform_index], kNoTrans);
123 }
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
#define KALDI_ERR
Definition: kaldi-error.h:147
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
int32 dim_
Dimension of feature vectors.
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).

◆ Init()

void Init ( size_t  num_xforms,
size_t  dim 
)

Allocates memory for transform matrix & bias vector.

Definition at line 31 of file regtree-fmllr-diag-gmm.cc.

References RegtreeFmllrDiagGmm::dim_, KALDI_ASSERT, RegtreeFmllrDiagGmm::logdet_, RegtreeFmllrDiagGmm::num_xforms_, RegtreeFmllrDiagGmm::valid_logdet_, and RegtreeFmllrDiagGmm::xform_matrices_.

Referenced by RegtreeFmllrDiagGmmAccs::Update().

31  {
32  if (num_xforms == 0) { // empty transform
33  xform_matrices_.clear();
34  logdet_.Resize(0);
35  valid_logdet_ = false;
36  dim_ = 0; // non-zero dimension is meaningless with empty transform
37  num_xforms_ = 0;
38  } else {
39  KALDI_ASSERT(dim != 0); // if not empty, dim = 0 is meaningless
40  dim_ = dim;
41  num_xforms_ = num_xforms;
42  xform_matrices_.resize(num_xforms);
43  logdet_.Resize(num_xforms);
44  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
45  xform_itr_end = xform_matrices_.end();
46  for (; xform_itr != xform_itr_end; ++xform_itr) {
47  xform_itr->Resize(dim, dim+1);
48  xform_itr->SetUnit();
49  }
50  valid_logdet_ = true;
51  }
52 }
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.
bool valid_logdet_
Whether logdets are for current transforms.

◆ NumBaseClasses()

int32 NumBaseClasses ( ) const
inline

Definition at line 93 of file regtree-fmllr-diag-gmm.h.

Referenced by kaldi::UnitTestRegtreeFmllrDiagGmm().

93 { return bclass2xforms_.size(); }
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.

◆ NumRegClasses()

int32 NumRegClasses ( ) const
inline

◆ operator=()

void operator= ( const RegtreeFmllrDiagGmm )
private

◆ Read()

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

Definition at line 168 of file regtree-fmllr-diag-gmm.cc.

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

Referenced by kaldi::UnitTestRegtreeFmllrDiagGmm().

168  {
169  ExpectToken(in, binary, "<FMLLRXFORM>");
170  ExpectToken(in, binary, "<NUMXFORMS>");
171  ReadBasicType(in, binary, &num_xforms_);
172  ExpectToken(in, binary, "<DIMENSION>");
173  ReadBasicType(in, binary, &dim_);
174  KALDI_ASSERT(num_xforms_ >= 0 && dim_ >= 0); // can be 0 for empty xform
175 
177  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
178  xform_itr_end = xform_matrices_.end();
179  for (; xform_itr != xform_itr_end; ++xform_itr) {
180  ExpectToken(in, binary, "<XFORM>");
181  xform_itr->Read(in, binary);
182  KALDI_ASSERT(xform_itr->NumRows() == (xform_itr->NumCols() - 1)
183  && xform_itr->NumRows() == dim_);
184  }
185 
186  ExpectToken(in, binary, "<BCLASS2XFORMS>");
187  ReadIntegerVector(in, binary, &bclass2xforms_);
188  ExpectToken(in, binary, "</FMLLRXFORM>");
189  ComputeLogDets(); // so that the transforms can be used.
190 }
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
void ComputeLogDets()
Computes the log-determinant of the Jacobians for each transform.
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 transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.

◆ set_bclass2xforms()

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

Definition at line 101 of file regtree-fmllr-diag-gmm.h.

Referenced by RegtreeFmllrDiagGmmAccs::Update().

101 { 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,
size_t  regclass 
)
inline

Mutators.

Definition at line 125 of file regtree-fmllr-diag-gmm.h.

References kaldi::kNoTrans.

Referenced by RegtreeFmllrDiagGmmAccs::Update().

126  {
127  xform_matrices_[regclass].CopyFromMat(mat, kNoTrans);
128  valid_logdet_ = false;
129 }
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
bool valid_logdet_
Whether logdets are for current transforms.

◆ SetUnit()

void SetUnit ( )

Sets transform matrix to identity and bias vector to zero.

Definition at line 54 of file regtree-fmllr-diag-gmm.cc.

References RegtreeFmllrDiagGmm::dim_, KALDI_ASSERT, RegtreeFmllrDiagGmm::num_xforms_, and RegtreeFmllrDiagGmm::xform_matrices_.

54  {
55  KALDI_ASSERT(num_xforms_ > 0 && dim_ > 0);
56  vector< Matrix<BaseFloat> >::iterator xform_itr = xform_matrices_.begin(),
57  xform_itr_end = xform_matrices_.end();
58  for (; xform_itr != xform_itr_end; ++xform_itr) {
59  xform_itr->SetUnit();
60  }
61 }
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.

◆ TransformFeature()

void TransformFeature ( const VectorBase< BaseFloat > &  in,
std::vector< Vector< BaseFloat > > *  out 
) const

Get the transformed features for each of the transforms.

Definition at line 118 of file regtree-fmllr-diag-gmm.cc.

References VectorBase< Real >::Dim(), RegtreeFmllrDiagGmm::dim_, KALDI_ASSERT, KALDI_WARN, kaldi::kNoTrans, RegtreeFmllrDiagGmm::logdet_, RegtreeFmllrDiagGmm::num_xforms_, and RegtreeFmllrDiagGmm::xform_matrices_.

Referenced by DecodableAmDiagGmmRegtreeFmllr::LogLikelihoodZeroBased(), and kaldi::UnitTestRegtreeFmllrDiagGmm().

119  {
120  KALDI_ASSERT(out != NULL);
121 
122  if (xform_matrices_.size() == 0) { // empty transform
123  KALDI_ASSERT(num_xforms_ == 0 && dim_ == 0 && logdet_.Dim() == 0);
124  KALDI_WARN << "Asked to apply empty feature transform. Copying instead.";
125  out->resize(1);
126  (*out)[0].Resize(in.Dim());
127  (*out)[0].CopyFromVec(in);
128  return;
129  } else {
130  KALDI_ASSERT(in.Dim() == dim_);
131  // if (!valid_logdet_)
132  // KALDI_ERR << "Must call ComputeLogDets() before transforming data.";
133  // [no need for this check].
134  Vector<BaseFloat> extended_feat(dim_ + 1);
135  extended_feat.Range(0, dim_).CopyFromVec(in);
136  extended_feat(dim_) = 1.0;
138  out->resize(num_xforms_);
139  for (int32 xform_index = 0; xform_index < num_xforms_;
140  ++xform_index) {
141  (*out)[xform_index].Resize(dim_);
142  (*out)[xform_index].AddMatVec(1.0, xform_matrices_[xform_index],
143  kNoTrans, extended_feat, 0.0);
144  }
145  }
146 }
kaldi::int32 int32
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.

◆ Validate()

void Validate ( )

Checks whether the various parameters are consistent.

Definition at line 63 of file regtree-fmllr-diag-gmm.cc.

References RegtreeFmllrDiagGmm::bclass2xforms_, RegtreeFmllrDiagGmm::dim_, rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_WARN, RegtreeFmllrDiagGmm::logdet_, RegtreeFmllrDiagGmm::num_xforms_, and RegtreeFmllrDiagGmm::xform_matrices_.

Referenced by kaldi::UnitTestRegtreeFmllrDiagGmm().

63  {
64  if (dim_ < 0 || num_xforms_ < 0) { // uninitialized case
65  KALDI_ERR <<"Do not call Validate() with an uninitialized object (dim = "
66  << (dim_) << ", # transforms = " << (num_xforms_);
67  } else if (dim_ * num_xforms_ == 0) { // empty case
68  KALDI_ASSERT(num_xforms_ == 0 && dim_ == 0);
69  if (xform_matrices_.size() != 0 || logdet_.Dim() != 0) {
70  KALDI_ERR << "Number of transforms = " << (xform_matrices_.size())
71  << ", number of log-determinant terms = " << (logdet_.Dim())
72  << ". Expected number = 0";
73  }
74  return;
75  }
76 
77  // non-empty case: typical usage scenario
78  if (xform_matrices_.size() != static_cast<size_t>(num_xforms_)
79  || logdet_.Dim() != num_xforms_) {
80  KALDI_ERR << "Number of transforms = " << (xform_matrices_.size())
81  << ", number of log-determinant terms = " << (logdet_.Dim())
82  << ". `Expected number = " << (num_xforms_);
83  }
84  for (int32 i = 0; i < num_xforms_; i++) {
85  if (xform_matrices_[i].NumRows() != dim_ ||
86  xform_matrices_[i].NumCols() != (dim_+1)) {
87  KALDI_ERR << "For transform " << (i) << ": inconsistent size: rows = "
88  << (xform_matrices_[i].NumRows()) << ", cols = "
89  << xform_matrices_[i].NumCols() << ", dim = " << (dim_);
90  }
91  }
92  if (bclass2xforms_.size() > 0) {
93  for (int32 i = 0, maxi = bclass2xforms_.size(); i < maxi; i++) {
94  if (bclass2xforms_[i] >= num_xforms_) {
95  KALDI_ERR << "For baseclass " << (i) << ", transform index "
96  << (bclass2xforms_[i]) << " exceeds total transforms "
97  << (num_xforms_);
98  }
99  }
100  } else {
101  if (num_xforms_ > 1) {
102  KALDI_WARN << "Multiple FMLLR transforms found without baseclass info.";
103  }
104  }
105 }
kaldi::int32 int32
std::vector< int32 > bclass2xforms_
For each baseclass index of which transform to use; -1 => no xform.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 num_xforms_
Number of transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 dim_
Dimension of feature vectors.
Vector< BaseFloat > logdet_
Log-determinants of the Jacobians.

◆ Write()

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

Definition at line 148 of file regtree-fmllr-diag-gmm.cc.

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

Referenced by kaldi::UnitTestRegtreeFmllrDiagGmm().

148  {
149  WriteToken(out, binary, "<FMLLRXFORM>");
150  WriteToken(out, binary, "<NUMXFORMS>");
151  WriteBasicType(out, binary, num_xforms_);
152  WriteToken(out, binary, "<DIMENSION>");
153  WriteBasicType(out, binary, dim_);
154 
155  vector< Matrix<BaseFloat> >::const_iterator xform_itr =
156  xform_matrices_.begin(), xform_itr_end = xform_matrices_.end();
157  for (; xform_itr != xform_itr_end; ++xform_itr) {
158  WriteToken(out, binary, "<XFORM>");
159  xform_itr->Write(out, binary);
160  }
161 
162  WriteToken(out, binary, "<BCLASS2XFORMS>");
163  WriteIntegerVector(out, binary, bclass2xforms_);
164  WriteToken(out, binary, "</FMLLRXFORM>");
165 }
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 transform matrices.
std::vector< Matrix< BaseFloat > > xform_matrices_
Transform matrices.
int32 dim_
Dimension of feature vectors.
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

Member Data Documentation

◆ bclass2xforms_

std::vector<int32> bclass2xforms_
private

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

Definition at line 110 of file regtree-fmllr-diag-gmm.h.

Referenced by RegtreeFmllrDiagGmm::Read(), RegtreeFmllrDiagGmm::Validate(), and RegtreeFmllrDiagGmm::Write().

◆ dim_

◆ logdet_

◆ num_xforms_

◆ valid_logdet_

bool valid_logdet_
private

Whether logdets are for current transforms.

Definition at line 108 of file regtree-fmllr-diag-gmm.h.

Referenced by RegtreeFmllrDiagGmm::ComputeLogDets(), and RegtreeFmllrDiagGmm::Init().

◆ xform_matrices_


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