101 int32 num_examples)
const {
106 transformed_ivector_sq.
ApplyPow(2.0);
109 inv_covar.
Add(1.0 / num_examples);
115 double dot_prod =
VecVec(inv_covar, transformed_ivector_sq);
116 return sqrt(
Dim() / dot_prod);
125 double normalization_factor;
129 normalization_factor = sqrt(transformed_ivector->
Dim())
130 / transformed_ivector->
Norm(2.0);
135 transformed_ivector->
Scale(normalization_factor);
136 return normalization_factor;
158 double loglike_given_class, loglike_without_class;
168 * transformed_train_ivector(
i);
171 double logdet = variance.
SumLog();
173 sqdiff.
AddVec(-1.0, mean);
176 loglike_given_class = -0.5 * (logdet +
M_LOG_2PI * dim +
177 VecVec(sqdiff, variance));
185 double logdet = variance.
SumLog();
187 loglike_without_class = -0.5 * (logdet +
M_LOG_2PI * dim +
188 VecVec(sqdiff, variance));
190 double loglike_ratio = loglike_given_class - loglike_without_class;
191 return loglike_ratio;
196 KALDI_ASSERT(smoothing_factor >= 0.0 && smoothing_factor <= 1.0);
199 KALDI_LOG <<
"Smoothing within-class covariance by " << smoothing_factor
200 <<
", Psi is initially: " <<
psi_;
202 within_class_covar.
Set(1.0);
205 within_class_covar.
AddVec(smoothing_factor, psi_);
211 psi_.DivElements(within_class_covar);
231 within_var(in_transform.
NumCols()),
232 psi_mat(in_transform.
NumCols()),
233 between_var_new(
Dim()),
234 within_var_new(
Dim());
239 psi_mat.AddDiagVec(1.0,
psi_);
240 transform_invert.
Invert();
241 within_var.AddMat2(1.0, transform_invert,
kNoTrans, 0.0);
242 between_var.AddMat2Sp(1.0, transform_invert,
kNoTrans, psi_mat, 0.0);
245 between_var_new.AddMat2Sp(1.0, in_transform,
kNoTrans, between_var, 0.0);
246 within_var_new.AddMat2Sp(1.0, in_transform,
kNoTrans, within_var, 0.0);
263 between_var_proj.
Eig(&s, &U);
269 KALDI_WARN <<
"Floored " << n <<
" eigenvalues of between-class " 270 <<
"variance to zero.";
297 offset_scatter_.AddMat2(weight, group,
kTrans, 1.0);
301 offset_scatter_.AddVec2(-n * weight, *mean);
303 class_info_.push_back(
ClassInfo(weight, mean, n));
307 class_weight_ += weight;
308 example_weight_ += weight *
n;
310 sum_.AddVec(weight, *mean);
314 for (
size_t i = 0;
i < class_info_.size();
i++)
315 delete class_info_[
i].mean;
319 for (
size_t i = 0;
i + 1 < class_info_.size();
i++)
320 if (class_info_[
i+1] < class_info_[
i])
331 example_weight_ = 0.0;
333 offset_scatter_.Resize(dim);
355 within_logdet, det_sign;
357 inv_within_var.
Invert(&within_logdet, &det_sign);
358 KALDI_ASSERT(det_sign == 1 &&
"Within-class covariance is singular");
360 double objf = -0.5 * (within_class_count * (within_logdet +
M_LOG_2PI *
Dim())
366 double tot_objf = 0.0;
371 double combined_var_logdet;
380 combined_inv_var.
Invert(&combined_var_logdet);
385 +
VecSpVec(mean, combined_inv_var, mean));
395 normalized_ans = ans / example_weights;
396 KALDI_LOG <<
"Within-class objf per sample is " << (ans1 / example_weights)
397 <<
", between-class is " << (ans2 / example_weights)
398 <<
", total is " << normalized_ans;
399 return normalized_ans;
481 double weight = info.
weight;
485 mixed_var.
AddSp(n, within_var_inv);
491 temp.
AddSpVec(n, within_var_inv, m, 0.0);
493 w.
AddSpVec(1.0, mixed_var, temp, 0.0);
529 KALDI_LOG <<
"Plda estimation iteration " <<
i 540 KALDI_LOG <<
"Norm of mean of iVector distribution is " 556 between_var_proj.
Eig(&s, &U);
562 KALDI_WARN <<
"Floored " << n <<
" eigenvalues of between-class " 563 <<
"variance to zero.";
577 KALDI_LOG <<
"Diagonal of between-class variance in normalized space is " << s;
589 psi.CopyDiagFromSp(tmp_between);
597 if (mean_stats_.Dim() == 0) {
598 mean_stats_.Resize(ivector.
Dim());
599 variance_stats_.Resize(ivector.
Dim());
602 tot_weight_ += weight;
603 mean_stats_.AddVec(weight, ivector);
604 variance_stats_.AddVec2(weight, ivector);
610 this->AddStats(weight, ivector_dbl);
616 int32 dim = mean_stats_.Dim();
619 mean.Scale(1.0 / tot_weight_);
621 variance.
Scale(1.0 / tot_weight_);
643 transform_mod.
Row(
i).Scale(1.0 / sqrt(1.0 + plda->
psi_(
i)));
656 variance_proj.
Eig(&s, &P);
658 KALDI_LOG <<
"Eigenvalues of adaptation-data total-covariance in space where " 659 <<
"training-data total-covariance is unit, is: " << s;
665 W(
i,
i) = 1.0 / (1.0 + plda->
psi_(
i)),
679 Wproj2.AddMat2Sp(1.0, P,
kTrans, W, 0.0);
690 between = Bproj2(
i,
i);
691 KALDI_LOG <<
"For " <<
i <<
"'th eigenvalue, value is " << s(
i)
692 <<
", within-class covar in this direction is " << within
693 <<
", between-class is " << between;
695 double excess_eig = s(
i) - 1.0;
698 Wproj2mod(
i,
i) += excess_within_covar;
699 Bproj2mod(
i,
i) += excess_between_covar;
720 combined_trans_inv.
Invert();
725 Wmod.AddMat2Sp(1.0, combined_trans_inv,
kNoTrans, Wproj2mod, 0.0);
742 Bmod_proj.
Eig(&psi_new, &Q);
754 KALDI_LOG <<
"Old diagonal of between-class covar was: " 755 << plda->
psi_ <<
", new diagonal is "
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool IsUnit(Real cutoff=1.0e-05) const
void Estimate(const PldaEstimationConfig &config, Plda *output)
Packed symetric matrix class.
SpMatrix< double > between_var_
void Write(std::ostream &out, bool binary) const
write to stream.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
SpMatrix< double > within_var_stats_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
double TransformIvector(const PldaConfig &config, const VectorBase< double > &ivector, int32 num_enroll_examples, VectorBase< double > *transformed_ivector) const
Transforms an iVector into a space where the within-class variance is unit and between-class variance...
int32 GetVerboseLevel()
Get verbosity level, usually set via command line '–verbose=' switch.
void Read(std::istream &is, bool binary)
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
Real SumLog() const
Returns sum of the logs of the elements.
Matrix< double > transform_
void Eig(VectorBase< Real > *s, MatrixBase< Real > *P=NULL) const
Solves the symmetric eigenvalue problem: at end we should have (*this) = P * diag(s) * P^T...
SpMatrix< double > offset_scatter_
void SetUnit()
< Set to zero
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void AddSpVec(const Real alpha, const SpMatrix< Real > &M, const VectorBase< Real > &v, const Real beta)
Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
MatrixIndexT NumRows() const
void CopyFromSp(const SpMatrix< Real > &other)
Real Norm(Real p) const
Compute the p-th norm of the vector.
void AddTp2Sp(const Real alpha, const TpMatrix< Real > &T, MatrixTransposeType transM, const SpMatrix< Real > &A, const Real beta=0.0)
The following function does: this <– beta*this + alpha * T * A * T^T.
static void ComputeNormalizingTransform(const SpMatrix< Real > &covar, MatrixBase< Real > *proj)
This function computes a projection matrix that when applied makes the covariance unit (i...
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
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)
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
PldaEstimator(const PldaStats &stats)
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. ...
void SmoothWithinClassCovariance(double smoothing_factor)
This function smooths the within-class covariance by adding to it, smoothing_factor (e...
void CopyFromTp(const TpMatrix< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given tpmatrix. (no resize is done).
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
double TraceSpSp(const SpMatrix< double > &A, const SpMatrix< double > &B)
Real VecSpVec(const VectorBase< Real > &v1, const SpMatrix< Real > &M, const VectorBase< Real > &v2)
Computes v1^T * M * v2.
Packed symetric matrix class.
void Write(std::ostream &os, bool binary) const
double LogLikelihoodRatio(const VectorBase< double > &transformed_enroll_ivector, int32 num_enroll_utts, const VectorBase< double > &transformed_test_ivector) const
Returns the log-likelihood ratio log (p(test_ivector | same) / p(test_ivector | different)).
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 GetStatsFromClassMeans()
GetStatsFromClassMeans() is the more complicated part of PLDA estimation.
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.
void ComputeDerivedVars()
double GetNormalizationFactor(const VectorBase< double > &transformed_ivector, int32 num_examples) const
This returns a normalization factor, which is a quantity we must multiply "transformed_ivector" by so...
double ComputeObjfPart2() const
Returns the part of the obj relating to the class means (total_not normalized)
BaseFloat between_covar_scale
void AddStats(double weight, const Vector< double > &ivector)
void MulRowsVec(const VectorBase< Real > &scale)
Equivalent to (*this) = diag(scale) * (*this).
double ComputeObjfPart1() const
Returns the part of the objf relating to offsets from the class means.
void GetOutput(Plda *plda)
void UpdatePlda(const PldaUnsupervisedAdaptorConfig &config, Plda *plda) const
void InvertElements()
Invert all elements.
double between_var_count_
void ApplyTransform(const Matrix< double > &in_transform)
Apply a transform to the PLDA model.
#define KALDI_ASSERT(cond)
void AddSamples(double weight, const Matrix< double > &group)
The dimension is set up the first time you add samples.
SpMatrix< double > within_var_
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void Set(Real f)
Set all members of a vector to a specified value.
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.
void ApplyPow(Real power)
Take all elements of vector to a power.
std::vector< ClassInfo > class_info_
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
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).
BaseFloat mean_diff_scale
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
SpMatrix< double > between_var_stats_
void Invert(Real *log_det=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
void AddMatTp(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const TpMatrix< Real > &B, MatrixTransposeType transB, const Real beta)
this <– beta*this + alpha*A*B.
Provides a vector abstraction class.
void Add(Real c)
Add a constant to each element of a vector.
void Invert(Real *logdet=NULL, Real *det_sign=NULL, bool inverse_needed=true)
matrix inverse.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
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.
double ComputeObjf() const
Returns the objective-function per sample.
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.
void GetStatsFromIntraClass()
BaseFloat within_covar_scale