25 int main(
int argc,
char *argv[]) {
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 " 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) {
187 if (full_matrix_wxfilename !=
"") {
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 CopyColFromVec(const VectorBase< Real > &v, const MatrixIndexT col)
Copy vector into specific column of matrix.
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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 Register(const std::string &name, bool *ptr, const std::string &doc)
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...
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
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.
int main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
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 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.