23 using namespace kaldi;
32 if (tmp.
Cond() < 100)
break;
33 std::cout <<
"Condition number of random matrix large " 34 <<
static_cast<float>(tmp.
Cond()) <<
", trying again (this is normal)" 40 if (matrix_sqrt != NULL) matrix_sqrt->Cholesky(*matrix);
42 if ((matrix_sqrt == NULL) && (logdet == NULL)) {
50 std::cout <<
"Testing I/O, binary = " << binary <<
'\n';
52 size_t dim = lda_est.
Dim();
54 lda_est.
Write(
Output(
"tmp_stats", binary).Stream(), binary);
59 Input ki(
"tmp_stats", &binary_in);
63 Input ki2(
"tmp_stats", &binary_in);
94 std::cout <<
"Running test with " << num_class <<
" classes and " 95 << dim <<
" dimensional vectors" <<
'\n';
101 std::vector<SpMatrix<BaseFloat> > vars_f(num_class);
102 std::vector<TpMatrix<BaseFloat> > vars_f_sqrt(num_class);
103 for (
size_t mix = 0; mix < num_class; mix++) {
104 vars_f[mix].Resize(dim);
105 vars_f_sqrt[mix].Resize(dim);
108 for (
size_t m = 0; m < num_class; m++) {
109 for (
size_t d = 0;
d < dim;
d++) {
117 size_t vec_count = 1000;
119 std::vector<int32> feats_class(num_class * vec_count);
121 for (
size_t m = 0; m < num_class; m++) {
122 for (
size_t i = 0;
i < vec_count;
i++) {
123 for (
size_t d = 0;
d < dim;
d++) {
126 feats.
Row(counter).CopyFromVec(means_f.
Row(m));
127 feats.
Row(counter).AddTpVec(1.0, vars_f_sqrt[m],
kNoTrans, rnd_vec, 1.0);
128 feats_class[counter] = m;
138 for (
size_t i = 0;
i < counter;
i++) {
140 class_mean.
Row(feats_class[
i]).AddVec(1.0, tmp_vec_d);
141 total_mean.
AddVec(1.0, tmp_vec_d);
142 total_covar.
AddVec2(1.0, tmp_vec_d);
144 total_mean.
Scale(1/static_cast<double>(counter));
145 total_covar.
Scale(1/static_cast<double>(counter));
146 total_covar.
AddVec2(-1.0, total_mean);
149 for (
size_t c = 0; c < num_class; c++) {
150 class_mean.
Row(c).Scale(1/static_cast<double>(vec_count));
151 bc_covar.
AddVec2(static_cast<double>(vec_count)/counter, class_mean.
Row(c));
153 bc_covar.
AddVec2(-1.0, total_mean);
156 wc_covar.
AddSp(-1.0, bc_covar);
160 lda_est.
Init(num_class, dim);
162 for (
size_t i = 0;
i < counter;
i++) {
169 lda_mat_bf_mean_remove;
170 lda_est.
Estimate(opts, &lda_mat_bf);
172 lda_est.
Estimate(opts, &lda_mat_bf_mean_remove);
177 mean_ext(mean_ext.
Dim() - 1) = 1.0;
196 for (
int32 i = 1; i < static_cast<int32>(dim);
i++) {
197 if (tmp_mat(
i,
i) < 1.0e-10) { tmp_mat(
i,
i) = 0.0; }
209 for (
int i = 0;
i < 2;
i++)
211 std::cout <<
"Test OK.\n";
void Accumulate(const VectorBase< BaseFloat > &data, int32 class_id, BaseFloat weight=1.0)
Accumulates data.
void AddMat2(const Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
rank-N update: if (transM == kNoTrans) (*this) = beta*(*this) + alpha * M * M^T, or (if transM == kTr...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Packed symetric matrix class.
bool IsDiagonal(Real cutoff=1.0e-05) const
Returns true if matrix is Diagonal.
Class for computing linear discriminant analysis (LDA) transform.
void rand_posdef_spmatrix(size_t dim, SpMatrix< BaseFloat > *matrix, TpMatrix< BaseFloat > *matrix_sqrt=NULL, BaseFloat *logdet=NULL)
Real Cond() const
Returns condition number by computing Svd.
int32 Dim() const
Returns the dimensionality of the feature vectors.
void Scale(BaseFloat f)
Scales all accumulators.
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]
float RandGauss(struct RandomState *state=NULL)
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.
bool IsZero(Real cutoff=1.0e-05) const
Returns true if matrix is all zeros.
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 Cholesky(const SpMatrix< Real > &orig)
void Estimate(const LdaEstimateOptions &opts, Matrix< BaseFloat > *M, Matrix< BaseFloat > *Mfull=NULL) const
Estimates the LDA transform matrix m.
Real LogPosDefDet() const
Computes log determinant but only for +ve-def matrices (it uses Cholesky).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
void Read(std::istream &in_stream, bool binary, bool add)
void AddSp(const Real alpha, const SpMatrix< Real > &Ma)
void SetRandn()
Sets to random values of a normal distribution.
void AddMatMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const MatrixBase< Real > &C, MatrixTransposeType transC, const Real beta)
this <– beta*this + alpha*A*B*C.
Packed symetric matrix class.
MatrixIndexT Dim() const
Returns the dimension of the vector.
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.
A class representing a vector.
#define KALDI_ASSERT(cond)
void UnitTestEstimateLda()
void ZeroAccumulators()
Sets all accumulators to zero.
void test_io(const LdaEstimate &lda_est, bool binary)
bool IsUnit(Real cutoff=1.0e-05) const
Returns true if the matrix is all zeros, except for ones on diagonal.
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)