26 using namespace kaldi;
30 "Estimate PCA transform; dimension reduction is optional (if not specified\n" 31 "we don't reduce the dimension; if you specify --normalize-variance=true,\n" 32 "we normalize the (centered) covariance of the features, and if you specify\n" 33 "--normalize-mean=true the mean is also normalized. So a variety of transform\n" 34 "types are supported. Because this type of transform does not need too much\n" 35 "data to estimate robustly, we don't support separate accumulator files;\n" 36 "this program reads in the features directly. For large datasets you may\n" 37 "want to subset the features (see example below)\n" 38 "By default the program reads in matrices (e.g. features), but with\n" 39 "--read-vectors=true, can read in vectors (e.g. iVectors).\n" 41 "Usage: est-pca [options] (<feature-rspecifier>|<vector-rspecifier>) <pca-matrix-out>\n" 43 "utils/shuffle_list.pl data/train/feats.scp | head -n 5000 | sort | \\\n" 44 " est-pca --dim=50 scp:- some/dir/0.mat\n";
47 bool read_vectors =
false;
48 bool normalize_variance =
false;
49 bool normalize_mean =
false;
51 std::string full_matrix_wxfilename;
53 po.Register(
"binary", &binary,
"Write accumulators in binary mode.");
54 po.Register(
"dim", &dim,
"Feature dimension requested (if <= 0, uses full " 56 po.Register(
"read-vectors", &read_vectors,
"If true, read in single vectors " 57 "instead of feature matrices");
58 po.Register(
"normalize-variance", &normalize_variance,
"If true, make a " 59 "transform that normalizes variance to one.");
60 po.Register(
"normalize-mean", &normalize_mean,
"If true, output an affine " 61 "transform that subtracts the data mean.");
62 po.Register(
"write-full-matrix", &full_matrix_wxfilename,
63 "Write full version of the matrix to this location (including " 67 if (po.NumArgs() != 2) {
72 std::string rspecifier = po.GetArg(1),
73 pca_mat_wxfilename = po.GetArg(2);
75 int32 num_done = 0, num_err = 0;
83 for (; !feat_reader.Done(); feat_reader.Next()) {
85 if (mat.NumRows() == 0) {
92 sumsq.
Resize(mat.NumCols());
94 if (sum.
Dim() != mat.NumCols()) {
95 KALDI_WARN <<
"Feature dimension mismatch " << sum.
Dim() <<
" vs. " 102 count += mat.NumRows();
105 KALDI_LOG <<
"Accumulated stats from " << num_done <<
" feature files, " 106 << num_err <<
" with errors; " << count <<
" frames.";
111 for (; !vec_reader.Done(); vec_reader.Next()) {
113 if (vec.Dim() == 0) {
118 if (sum.
Dim() == 0) {
122 if (sum.
Dim() != vec.Dim()) {
123 KALDI_WARN <<
"Feature dimension mismatch " << sum.
Dim() <<
" vs. " 133 KALDI_LOG <<
"Accumulated stats from " << num_done <<
" vectors, " 134 << num_err <<
" with errors.";
138 sum.
Scale(1.0 / count);
139 sumsq.
Scale(1.0 / count);
143 int32 full_dim = sum.
Dim();
144 if (dim <= 0) dim = full_dim;
146 KALDI_ERR <<
"Final dimension " << dim <<
" is greater than feature " 147 <<
"dimension " << full_dim;
155 KALDI_LOG <<
"Eigenvalues in PCA are " << s;
156 KALDI_LOG <<
"Sum of PCA eigenvalues is " << s.Sum() <<
", sum of kept " 157 <<
"eigenvalues is " << s.Range(0, dim).Sum();
163 if (normalize_variance) {
164 for (int32
i = 0;
i < full_dim;
i++) {
165 double this_var = s(
i), min_var = 1.0e-15;
166 if (this_var < min_var) {
167 KALDI_WARN <<
"--normalize-variance option: very tiny variance " << s(
i)
168 <<
"encountered, treating as " << min_var;
171 double scale = 1.0 / sqrt(this_var);
173 transform.Row(
i).Scale(scale);
179 if (normalize_mean) {
180 offset.AddMatVec(-1.0, transform,
kNoTrans, sum, 0.0);
181 transform.Resize(full_dim, full_dim + 1,
kCopyData);
182 transform.CopyColFromVec(offset, full_dim);
187 if (full_matrix_wxfilename !=
"") {
191 transform_float.Resize(dim, transform_float.NumCols(),
kCopyData);
195 }
catch(
const std::exception &e) {
196 std::cerr << e.what();
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...
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
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...
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void AddVec2(const Real alpha, const VectorBase< OtherReal > &v)
rank-one update, this <– this + alpha v v'
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
MatrixIndexT Dim() const
Returns the dimension of the vector.
void Scale(Real alpha)
Multiplies all elements by this constant.
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void SortSvd(VectorBase< Real > *s, MatrixBase< Real > *U, MatrixBase< Real > *Vt, bool sort_on_absolute_value)
Function to ensure that SVD is sorted.