39 double d =
static_cast<double>(f);
60 double *tot_count)
const {
68 total_mean->
Scale(1.0 / sum);
69 total_covar->
Scale(1.0 / sum);
70 total_covar->
AddVec2(-1.0, *total_mean);
72 between_covar->
Resize(dim);
74 for (
int32 c = 0; c < num_class; c++) {
81 between_covar->
AddVec2(-1.0, *total_mean);
97 GetStats(&total_covar, &bc_covar, &total_mean, &count);
101 wc_covar.
AddSp(-1.0, bc_covar);
107 KALDI_LOG <<
"Cholesky failed (possibly not +ve definite), so adding " << smooth
108 <<
" to diagonal and trying again.\n";
110 wc_covar(
i,
i) += smooth;
115 wc_covar_sqrt_mat.
Invert();
123 tmp_mat.
Svd(&svd_d, &svd_u, &svd_vt);
127 KALDI_LOG <<
"LDA singular values are " << svd_d;
129 KALDI_LOG <<
"Sum of all singular values is " << svd_d.Sum();
130 KALDI_LOG <<
"Sum of selected singular values is " <<
137 m->
Resize(target_dim, dim);
147 for (
int32 i = 0;
i < svd_d.Dim();
i++) {
150 scale = sqrt(new_var / old_var);
151 if (i < m->NumRows())
152 m->
Row(
i).Scale(scale);
154 mfull->
Row(
i).Scale(scale);
181 int32 num_classes, dim;
193 KALDI_ERR <<
"LdaEstimate::Read, dimension or classes count mismatch, " 195 <<
" vs. " <<(num_classes) <<
", " << (dim);
198 Init(num_classes, dim);
201 Init(num_classes, dim);
210 while (token !=
"</LDAACCS>") {
211 if (token ==
"<ZERO_ACCS>") {
212 tmp_zero_acc.
Read(in_stream, binary,
false);
216 }
else if (token ==
"<FIRST_ACCS>") {
217 tmp_first_acc.
Read(in_stream, binary,
false);
221 }
else if (token ==
"<SECOND_ACCS>") {
222 tmp_sec_acc.
Read(in_stream, binary,
false);
224 if (tmp_zero_acc(c) != 0)
225 tmp_sec_acc.
AddVec2(1.0 / tmp_zero_acc(c), tmp_first_acc.
Row(c));
231 KALDI_ERR <<
"Unexpected token '" << token <<
"' in file ";
241 WriteToken(out_stream, binary,
"<NUMCLASSES>");
244 WriteToken(out_stream, binary,
"<ZERO_ACCS>");
246 zero_acc_bf.
Write(out_stream, binary);
247 WriteToken(out_stream, binary,
"<FIRST_ACCS>");
249 first_acc_bf.
Write(out_stream, binary);
250 WriteToken(out_stream, binary,
"<SECOND_ACCS>");
257 tmp_sec_acc_bf.
Write(out_stream, binary);
void Accumulate(const VectorBase< BaseFloat > &data, int32 class_id, BaseFloat weight=1.0)
Accumulates data.
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void Write(std::ostream &out, bool binary) const
write to stream.
void CopyColFromVec(const VectorBase< Real > &v, const MatrixIndexT col)
Copy vector into specific column of matrix.
void Read(std::istream &in, bool binary, bool add=false)
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Write(std::ostream &out, bool binary) const
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
int32 Dim() const
Returns the dimensionality of the 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...
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
void Scale(BaseFloat f)
Scales all accumulators.
Matrix< double > first_acc_
void Write(std::ostream &out_stream, bool binary) const
void AddMat(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transA=kNoTrans)
*this += alpha * M [or M^T]
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void Init(int32 num_classes, int32 dimension)
Allocates memory for accumulators.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
MatrixIndexT NumRows() const
Vector< double > zero_acc_
void CopyFromSp(const SpMatrix< Real > &other)
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v'
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
void Cholesky(const SpMatrix< Real > &orig)
void Estimate(const LdaEstimateOptions &opts, Matrix< BaseFloat > *M, Matrix< BaseFloat > *Mfull=NULL) const
Estimates the LDA transform matrix m.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void Read(std::istream &in_stream, bool binary, bool add)
void Scale(Real alpha)
Multiply each element with a scalar value.
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
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.
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
BaseFloat within_class_factor
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
MatrixIndexT Dim() const
Returns the dimension of the vector.
void SetZero()
Sets matrix to zero.
void Scale(Real alpha)
Multiplies all elements by this constant.
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
int32 NumClasses() const
Returns the number of classes.
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.
Real Sum() const
Returns sum of the elements.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void AddMat2Sp(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
Extension of rank-N update: this <– beta*this + alpha * M * A * M^T.
SpMatrix< double > total_second_acc_
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.
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...
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).
void Svd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt) const
Compute SVD (*this) = U diag(s) Vt.
void ZeroAccumulators()
Sets all accumulators to zero.
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
Provides a vector abstraction class.
void SetZero()
Set vector to all zeros.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.