diff --git a/src/cudamatrix/cu-vector-inl.h b/src/cudamatrix/cu-vector-inl.h index 943203290..6c965587c 100644 --- a/src/cudamatrix/cu-vector-inl.h +++ b/src/cudamatrix/cu-vector-inl.h @@ -310,9 +310,9 @@ void CuVector::AddRowSumMat(Real alpha, const CuMatrix &mat, Real be #endif { Vector tmp(mat.NumCols()); - tmp.AddRowSumMat(mat.Mat()); + tmp.AddRowSumMat(1.0, mat.Mat()); if(beta != 1.0) vec_.Scale(beta); - vec_.AddVec(alpha,tmp); + vec_.AddVec(alpha, tmp); } } @@ -360,7 +360,7 @@ void CuVector::AddColSumMat(Real alpha, const CuMatrix &mat, Real be #endif { Vector tmp(mat.NumRows()); - tmp.AddColSumMat(mat.Mat()); + tmp.AddColSumMat(1.0, mat.Mat()); if(beta != 1.0) vec_.Scale(beta); vec_.AddVec(alpha,tmp); } diff --git a/src/featbin/compute-fbank-feats.cc b/src/featbin/compute-fbank-feats.cc index 002802e7c..5d9423d1b 100644 --- a/src/featbin/compute-fbank-feats.cc +++ b/src/featbin/compute-fbank-feats.cc @@ -153,7 +153,7 @@ int main(int argc, char *argv[]) { } if (subtract_mean) { Vector mean(features.NumCols()); - mean.AddRowSumMat(features); + mean.AddRowSumMat(1.0, features); mean.Scale(1.0 / features.NumRows()); for (int32 i = 0; i < features.NumRows(); i++) features.Row(i).AddVec(-1.0, mean); diff --git a/src/featbin/compute-mfcc-feats.cc b/src/featbin/compute-mfcc-feats.cc index ad5ed3c76..bfabe777e 100644 --- a/src/featbin/compute-mfcc-feats.cc +++ b/src/featbin/compute-mfcc-feats.cc @@ -153,7 +153,7 @@ int main(int argc, char *argv[]) { } if (subtract_mean) { Vector mean(features.NumCols()); - mean.AddRowSumMat(features); + mean.AddRowSumMat(1.0, features); mean.Scale(1.0 / features.NumRows()); for (int32 i = 0; i < features.NumRows(); i++) features.Row(i).AddVec(-1.0, mean); diff --git a/src/featbin/compute-plp-feats.cc b/src/featbin/compute-plp-feats.cc index 110d43006..f856ae037 100644 --- a/src/featbin/compute-plp-feats.cc +++ b/src/featbin/compute-plp-feats.cc @@ -153,7 +153,7 @@ int main(int argc, char *argv[]) { } if (subtract_mean) { Vector mean(features.NumCols()); - mean.AddRowSumMat(features); + mean.AddRowSumMat(1.0, features); mean.Scale(1.0 / features.NumRows()); for (size_t i = 0; i < features.NumRows(); i++) features.Row(i).AddVec(-1.0, mean); diff --git a/src/featbin/compute-spectrogram-feats.cc b/src/featbin/compute-spectrogram-feats.cc index d6e6c2bd9..22b6c8e4d 100644 --- a/src/featbin/compute-spectrogram-feats.cc +++ b/src/featbin/compute-spectrogram-feats.cc @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) { } if (subtract_mean) { Vector mean(features.NumCols()); - mean.AddRowSumMat(features); + mean.AddRowSumMat(1.0, features); mean.Scale(1.0 / features.NumRows()); for (int32 i = 0; i < features.NumRows(); i++) features.Row(i).AddVec(-1.0, mean); diff --git a/src/featbin/remove-mean.cc b/src/featbin/remove-mean.cc index f94cadb7c..1598a4ea2 100644 --- a/src/featbin/remove-mean.cc +++ b/src/featbin/remove-mean.cc @@ -52,7 +52,7 @@ int main(int argc, char *argv[]) { continue; } Vector mean(feats.NumCols()); - mean.AddRowSumMat(feats); + mean.AddRowSumMat(1.0, feats); mean.Scale(1.0 / feats.NumRows()); for (int32 i = 0; i < feats.NumRows(); i++) feats.Row(i).AddVec(-1.0, mean); diff --git a/src/gmm/mle-full-gmm-test.cc b/src/gmm/mle-full-gmm-test.cc index 1a25fa3bd..00357e9ec 100644 --- a/src/gmm/mle-full-gmm-test.cc +++ b/src/gmm/mle-full-gmm-test.cc @@ -309,7 +309,7 @@ UnitTestEstimateFullGmm() { Vector mean(dim); cov.AddMatMat(1.0, feats, kTrans, feats, kNoTrans, 0.0); cov.Scale(1.0 / feats.NumRows()); - mean.AddRowSumMat(feats); + mean.AddRowSumMat(1.0, feats); mean.Scale(1.0 / feats.NumRows()); cov.AddVecVec(-1.0, mean, mean); BaseFloat logdet = cov.LogDet(); diff --git a/src/lat/word-align-lattice.cc b/src/lat/word-align-lattice.cc index 7686e5f0a..20c298e33 100644 --- a/src/lat/word-align-lattice.cc +++ b/src/lat/word-align-lattice.cc @@ -390,7 +390,7 @@ bool LatticeWordAligner::ComputationState::OutputOnePhoneWordArc( if (info.reorder) // we have to consume the following self-loop transition-ids. while (i < len && tmodel.IsSelfLoop(transition_ids_[i])) i++; if (i == len) return false; // we don't know if it ends here... so can't output arc. - + if (tmodel.TransitionIdToPhone(transition_ids_[i-1]) != phone && ! *error) { // another check. KALDI_WARN << "Phone changed unexpectedly in lattice " diff --git a/src/matrix/kaldi-matrix.cc b/src/matrix/kaldi-matrix.cc index 79a6bbaea..cc34db8e7 100644 --- a/src/matrix/kaldi-matrix.cc +++ b/src/matrix/kaldi-matrix.cc @@ -763,37 +763,54 @@ void MatrixBase::CopyFromTp(const TpMatrix & M, template void MatrixBase::CopyRowsFromVec(const VectorBase &rv) { - KALDI_ASSERT(rv.Dim() == num_rows_*num_cols_); - - if (stride_ == num_cols_) { - // one big copy operation. - const Real *rv_data = rv.Data(); - std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_); - } else { - const Real *rv_data = rv.Data(); - for (MatrixIndexT r = 0; r < num_rows_; r++) { - Real *row_data = RowData(r); - for (MatrixIndexT c = 0; c < num_cols_; c++) { - row_data[c] = rv_data[c]; + if (rv.Dim() == num_rows_*num_cols_) { + if (stride_ == num_cols_) { + // one big copy operation. + const Real *rv_data = rv.Data(); + std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_); + } else { + const Real *rv_data = rv.Data(); + for (MatrixIndexT r = 0; r < num_rows_; r++) { + Real *row_data = RowData(r); + for (MatrixIndexT c = 0; c < num_cols_; c++) { + row_data[c] = rv_data[c]; + } + rv_data += num_cols_; } - rv_data += num_cols_; } + } else if (rv.Dim() == num_cols_) { + const Real *rv_data = rv.Data(); + for (MatrixIndexT r = 0; r < num_rows_; r++) + std::memcpy(RowData(r), rv_data, sizeof(Real)*num_cols_); + } else { + KALDI_ERR << "Wrong sized arguments"; } } template template void MatrixBase::CopyRowsFromVec(const VectorBase &rv) { - KALDI_ASSERT(rv.Dim() == num_rows_*num_cols_); - const OtherReal *rv_data = rv.Data(); - for (MatrixIndexT r = 0; r < num_rows_; r++) { - Real *row_data = RowData(r); - for (MatrixIndexT c = 0; c < num_cols_; c++) { - row_data[c] = static_cast(rv_data[c]); + if (rv.Dim() == num_rows_*num_cols_) { + const OtherReal *rv_data = rv.Data(); + for (MatrixIndexT r = 0; r < num_rows_; r++) { + Real *row_data = RowData(r); + for (MatrixIndexT c = 0; c < num_cols_; c++) { + row_data[c] = static_cast(rv_data[c]); + } + rv_data += num_cols_; } - rv_data += num_cols_; + } else if (rv.Dim() == num_cols_) { + const OtherReal *rv_data = rv.Data(); + Real *first_row_data = RowData(0); + for (MatrixIndexT c = 0; c < num_cols_; c++) + first_row_data[c] = rv_data[c]; + for (MatrixIndexT r = 1; r < num_rows_; r++) + std::memcpy(RowData(r), first_row_data, sizeof(Real)*num_cols_); + } else { + KALDI_ERR << "Wrong sized arguments."; } } + template void MatrixBase::CopyRowsFromVec(const VectorBase &rv); @@ -802,17 +819,28 @@ void MatrixBase::CopyRowsFromVec(const VectorBase &rv); template void MatrixBase::CopyColsFromVec(const VectorBase &rv) { - KALDI_ASSERT(rv.Dim() == num_rows_*num_cols_); + if (rv.Dim() == num_rows_*num_cols_) { + const Real *v_inc_data = rv.Data(); + Real *m_inc_data = data_; - const Real *v_inc_data = rv.Data(); - Real *m_inc_data = data_; - - for (MatrixIndexT c = 0; c < num_cols_; c++) { - for (MatrixIndexT r = 0; r < num_rows_; r++) { - m_inc_data[r * stride_] = v_inc_data[r]; + for (MatrixIndexT c = 0; c < num_cols_; c++) { + for (MatrixIndexT r = 0; r < num_rows_; r++) { + m_inc_data[r * stride_] = v_inc_data[r]; + } + v_inc_data += num_rows_; + m_inc_data ++; } - v_inc_data += num_rows_; - m_inc_data ++; + } else if (rv.Dim() == num_rows_) { + const Real *v_inc_data = rv.Data(); + Real *m_inc_data = data_; + for (MatrixIndexT r = 0; r < num_rows_; r++) { + BaseFloat value = *(v_inc_data++); + for (MatrixIndexT c = 0; c < num_cols_; c++) + m_inc_data[c] = value; + m_inc_data += stride_; + } + } else { + KALDI_ERR << "Wrong size of arguments."; } } diff --git a/src/matrix/kaldi-matrix.h b/src/matrix/kaldi-matrix.h index 62d65834b..d3976982a 100644 --- a/src/matrix/kaldi-matrix.h +++ b/src/matrix/kaldi-matrix.h @@ -128,16 +128,19 @@ class MatrixBase { template void CopyFromTp(const TpMatrix &M, MatrixTransposeType Trans = kNoTrans); - + /// Inverse of vec() operator. Copies vector into matrix, row-by-row. - /// Note that rv.Dim() must equal NumRows()*NumCols(). + /// Note that rv.Dim() must either equal NumRows()*NumCols() or + /// NumCols()-- this has two modes of operation. void CopyRowsFromVec(const VectorBase &v); template void CopyRowsFromVec(const VectorBase &v); /// Copies vector into matrix, column-by-column. - /// Note that rv.Dim() must equal NumRows()*NumCols(). + /// Note that rv.Dim() must either equal NumRows()*NumCols() or NumRows(); + /// this has two modes of operation. void CopyColsFromVec(const VectorBase &v); + /// Copy vector into specific column of matrix. void CopyColFromVec(const VectorBase &v, const MatrixIndexT col); /// Copy vector into specific row of matrix. @@ -745,6 +748,12 @@ std::istream & operator >> (std::istream & In, MatrixBase & M); template std::istream & operator >> (std::istream & In, Matrix & M); + +template +bool SameDim(const MatrixBase &M, const MatrixBase &N) { + return (M.NumRows() == N.NumRows() && M.NumCols() == N.NumCols()); +} + /// @} end of \addtogroup matrix_funcs_io diff --git a/src/matrix/kaldi-vector.cc b/src/matrix/kaldi-vector.cc index 77023ea3d..89419b3d0 100644 --- a/src/matrix/kaldi-vector.cc +++ b/src/matrix/kaldi-vector.cc @@ -551,7 +551,7 @@ Real VectorBase::SumLog() const { } template -void VectorBase::AddRowSumMat(const MatrixBase &rM) { +void VectorBase::AddRowSumMat(Real alpha, const MatrixBase &rM) { // note the double accumulator double sum; KALDI_ASSERT(dim_ == rM.NumCols()); @@ -560,12 +560,12 @@ void VectorBase::AddRowSumMat(const MatrixBase &rM) { for (MatrixIndexT j = 0; j < rM.NumRows(); j++) { sum += rM(j, i); } - data_[i] += sum; + data_[i] += alpha * sum; } } template -void VectorBase::AddColSumMat(const MatrixBase &rM) { +void VectorBase::AddColSumMat(Real alpha, const MatrixBase &rM) { // note the double accumulator double sum; KALDI_ASSERT(dim_ == rM.NumRows()); @@ -574,7 +574,7 @@ void VectorBase::AddColSumMat(const MatrixBase &rM) { for (MatrixIndexT j = 0; j < rM.NumCols(); j++) { sum += rM(i, j); } - data_[i] += sum; + data_[i] += alpha * sum; } } diff --git a/src/matrix/kaldi-vector.h b/src/matrix/kaldi-vector.h index 7d1c0514d..764591bdd 100644 --- a/src/matrix/kaldi-vector.h +++ b/src/matrix/kaldi-vector.h @@ -242,11 +242,11 @@ class VectorBase { /// negative. Real SumLog() const; - /// Adds sum of the rows of M to existing contents. - void AddRowSumMat(const MatrixBase& M); + /// Adds sum of the rows of M to existing contents, times alpha. + void AddRowSumMat(Real alpha, const MatrixBase& M); /// Adds sum of the columns of M to existing contents. - void AddColSumMat(const MatrixBase& M); + void AddColSumMat(Real alpha, const MatrixBase& M); /// Returns log(sum(exp())) without exp overflow /// If prune > 0.0, ignores terms less than the max - prune. diff --git a/src/matrix/matrix-lib-test.cc b/src/matrix/matrix-lib-test.cc index aa6b8e461..27d600ea0 100644 --- a/src/matrix/matrix-lib-test.cc +++ b/src/matrix/matrix-lib-test.cc @@ -67,16 +67,16 @@ start: for (MatrixIndexT i = 0;i < M->NumRows();i++) for (MatrixIndexT j = 0;j < M->NumCols();j++) (*M)(i, j) = RandGauss(); - if (M->NumRows() != 0 && M->Cond() > 100) { - printf("Condition number of random matrix large %f, trying again (this is normal)\n", - (float) M->Cond()); - goto start; - } + if (M->NumRows() != 0 && M->Cond() > 100) { + printf("Condition number of random matrix large %f, trying again (this is normal)\n", + (float) M->Cond()); + goto start; + } } template static void InitRand(SpMatrix *M) { - start: +start: for (MatrixIndexT i = 0;i < M->NumRows();i++) for (MatrixIndexT j = 0;j<=i;j++) (*M)(i, j) = RandGauss(); @@ -115,14 +115,14 @@ static bool ApproxEqual(const SpMatrix &A, } /* was: -template -bool ApproxEqual(SpMatrix &A, SpMatrix &B, float tol = 0.001) { - KALDI_ASSERT(A.NumRows() == B.NumRows()&&A.NumCols() == B.NumCols()); - for (MatrixIndexT i = 0;i < A.NumRows();i++) - for (MatrixIndexT j = 0;j<=i;j++) - if (std::abs(A(i, j)-B(i, j)) > tol*std::max(1.0, (double) (std::abs(A(i, j))+std::abs(B(i, j))))) return false; - return true; -} + template + bool ApproxEqual(SpMatrix &A, SpMatrix &B, float tol = 0.001) { + KALDI_ASSERT(A.NumRows() == B.NumRows()&&A.NumCols() == B.NumCols()); + for (MatrixIndexT i = 0;i < A.NumRows();i++) + for (MatrixIndexT j = 0;j<=i;j++) + if (std::abs(A(i, j)-B(i, j)) > tol*std::max(1.0, (double) (std::abs(A(i, j))+std::abs(B(i, j))))) return false; + return true; + } */ template static void AssertEqual(Vector &A, Vector &B, float tol = 0.001) { @@ -208,7 +208,28 @@ static void UnitTestSpAddVec() { AssertEqual(S, T); } } - + +template static void UnitTestCopyRowsAndCols() { + // Test other mode of CopyRowsFromVec, and CopyColsFromVec, + // where vector is duplicated. + for (int32 i = 0; i < 30; i++) { + int32 dimM = 1 + rand() % 5, dimN = 1 + rand() % 5; + Vector w(dimN); // test cross-type version of + // CopyRowsFromVec. + Vector v(dimM); + Matrix M(dimM, dimN), N(dimM, dimN); + InitRand(&v); + InitRand(&w); + M.CopyColsFromVec(v); + N.CopyRowsFromVec(w); + for (int32 r = 0; r < dimM; r++) { + for (int32 c = 0; c < dimN; c++) { + KALDI_ASSERT(M(r, c) == v(r)); + KALDI_ASSERT(N(r, c) == w(c)); + } + } + } +} template static void UnitTestSpliceRows() { @@ -221,8 +242,8 @@ template static void UnitTestSpliceRows() { Matrix M(dimM, dimN); M.CopyRowsFromVec(V); V10.CopyRowsFromMat(M); - AssertEqual(V, V10); - + AssertEqual(V, V10); + for (MatrixIndexT i = 0;i < dimM;i++) for (MatrixIndexT j = 0;j < dimN;j++) KALDI_ASSERT(M(i, j) == V(i*dimN + j)); @@ -291,10 +312,10 @@ template static void UnitTestRemoveRow() { Vector N(V); N.RemoveElement(i); for (MatrixIndexT j = 0;j < i;j++) { - KALDI_ASSERT(V(j) == N(j)); + KALDI_ASSERT(V(j) == N(j)); } for (MatrixIndexT j = i+1;j < dimM;j++) { - KALDI_ASSERT(V(j) == N(j-1)); + KALDI_ASSERT(V(j) == N(j-1)); } } @@ -354,8 +375,10 @@ static void UnitTestSimpleForVec() { // testing some simple operaters on vector Matrix M(dimM, dimN); InitRand(&M); Vector Vr(dimN), Vc(dimM); - Vr.AddRowSumMat(M); - Vc.AddColSumMat(M); + Vr.AddRowSumMat(0.5, M); + Vc.AddColSumMat(0.5, M); + Vr.Scale(2.0); + Vc.Scale(2.0); Vector V2r(dimN), V2c(dimM); for (MatrixIndexT k = 0; k < dimM; k++) { @@ -511,7 +534,7 @@ static void UnitTestSimpleForMat() { // test some simple operates on all kinds y.Cholesky(x); std::cout << "Matrix y is a lower triangular Cholesky decomposition of x:" - << '\n'; + << '\n'; std::cout << y << '\n'; // test sp-matrix's LogPosDefDet() function @@ -825,13 +848,13 @@ template static void UnitTestSherman() { Matrix tt2(dimK, dimK); tt2.AddMatMat(1.0, V, kTrans, tt1, kNoTrans, 0.0); for (MatrixIndexT i = 0;i < dimK;i++) - for (MatrixIndexT j = 0;j < dimK;j++) - { - if (i == j) - I(i, j) = 1.0; - else - I(i, j) = 0.0; - } + for (MatrixIndexT j = 0;j < dimK;j++) + { + if (i == j) + I(i, j) = 1.0; + else + I(i, j) = 0.0; + } tt2.AddMat(1.0, I); // I = identity tt2.Invert(); @@ -975,25 +998,25 @@ template static void UnitTestSvdNodestroy() { /* -template static void UnitTestSvdVariants() { // just make sure it doesn't crash if we call it but don't want left or right singular vectors. there are KALDI_ASSERTs inside the Svd. -#ifndef HAVE_ATLAS + template static void UnitTestSvdVariants() { // just make sure it doesn't crash if we call it but don't want left or right singular vectors. there are KALDI_ASSERTs inside the Svd. + #ifndef HAVE_ATLAS int Base = 10, Rand = 5, Iter = 25; for (int iter = 0;iter < Iter;iter++) { - MatrixIndexT dimM = Base + rand() % Rand, dimN = Base + rand() % Rand; - // if (dimM=N. - Matrix M(dimM, dimN); - Matrix U(dimM, dimM), Vt(dimN, dimN); Vector v(std::min(dimM, dimN)); - Matrix Utmp(dimM, 1); Matrix Vttmp(1, dimN); - InitRand(&M); - M.Svd(v, U, Vttmp, "A", "N"); - M.Svd(v, Utmp, Vt, "N", "A"); - Matrix U2(dimM, dimM), Vt2(dimN, dimN); Vector v2(std::min(dimM, dimN)); - M.Svd(v, U2, Vt2, "A", "A"); - AssertEqual(U, U2); AssertEqual(Vt, Vt2); + MatrixIndexT dimM = Base + rand() % Rand, dimN = Base + rand() % Rand; + // if (dimM=N. + Matrix M(dimM, dimN); + Matrix U(dimM, dimM), Vt(dimN, dimN); Vector v(std::min(dimM, dimN)); + Matrix Utmp(dimM, 1); Matrix Vttmp(1, dimN); + InitRand(&M); + M.Svd(v, U, Vttmp, "A", "N"); + M.Svd(v, Utmp, Vt, "N", "A"); + Matrix U2(dimM, dimM), Vt2(dimN, dimN); Vector v2(std::min(dimM, dimN)); + M.Svd(v, U2, Vt2, "A", "A"); + AssertEqual(U, U2); AssertEqual(Vt, Vt2); } -#endif -}*/ + #endif + }*/ template static void UnitTestSvdJustvec() { // Making sure gives same answer if we get just the vector, not the eigs. int Base = 10, Rand = 5, Iter = 25; @@ -1034,9 +1057,9 @@ template static void UnitTestEig() { for (int iter = 0;iter < 5;iter++) { MatrixIndexT dimM = 1 + iter; /* if (iter < 10) - dimM = 1 + rand() % 6; - else - dimM = 5 + rand()%10; */ + dimM = 1 + rand() % 6; + else + dimM = 5 + rand()%10; */ Matrix M(dimM, dimM); InitRand(&M); Matrix P(dimM, dimM); @@ -1392,21 +1415,21 @@ static void UnitTestTransposeScatter() { for (MatrixIndexT i = 0;i < Ap.NumRows();i++) { for (MatrixIndexT j = 0; j<=i; j++) { - Ap(i, j) = RandGauss(); + Ap(i, j) = RandGauss(); } } for (MatrixIndexT i = 0;i < M.NumRows();i++) { for (MatrixIndexT j = 0; j < M.NumCols(); j++) { - M(i, j) = RandGauss(); + M(i, j) = RandGauss(); } } -/* - std::stringstream ss("1 2 3"); - ss >> Ap; - ss.clear(); - ss.str("5 6 7 8 9 10"); - ss >> M; -*/ + /* + std::stringstream ss("1 2 3"); + ss >> Ap; + ss.clear(); + ss.str("5 6 7 8 9 10"); + ss >> M; + */ Af.CopyFromSp(Ap); A_MT.AddMatMat(1.0, Af, kNoTrans, M, kTrans, 0.0); @@ -1414,9 +1437,9 @@ static void UnitTestTransposeScatter() { Op.AddMat2Sp(1.0, M, kNoTrans, Ap, 0.0); -// std::cout << "A" << '\n' << Af << '\n'; -// std::cout << "M" << '\n' << M << '\n'; -// std::cout << "Op" << '\n' << Op << '\n'; + // std::cout << "A" << '\n' << Af << '\n'; + // std::cout << "M" << '\n' << M << '\n'; + // std::cout << "Op" << '\n' << Op << '\n'; for (MatrixIndexT i = 0; i < dimO; i++) { for (MatrixIndexT j = 0; j<=i; j++) { @@ -1429,8 +1452,8 @@ static void UnitTestTransposeScatter() { Af.AddMatMat(1.0, M, kTrans, A_MT, kNoTrans, 1.0); Ap.AddMat2Sp(1.0, M, kTrans, Op, 1.0); -// std::cout << "Ap" << '\n' << Ap << '\n'; -// std::cout << "Af" << '\n' << Af << '\n'; + // std::cout << "Ap" << '\n' << Ap << '\n'; + // std::cout << "Af" << '\n' << Af << '\n'; for (MatrixIndexT i = 0; i < dimA; i++) { for (MatrixIndexT j = 0; j<=i; j++) { @@ -1671,7 +1694,7 @@ template static void UnitTestSimple() { S.CopyFromMat(N); KALDI_ASSERT(S.IsZero()); KALDI_ASSERT(S.IsDiagonal()); - } + } } @@ -1885,14 +1908,14 @@ template static void UnitTestRange() { // Testing SubMatrix class. KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum()); for (MatrixIndexT i = lenStart;i < lenEnd;i++) - KALDI_ASSERT(V(i) == sub(i-lenStart)); + KALDI_ASSERT(V(i) == sub(i-lenStart)); InitRand(&sub); KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum()); for (MatrixIndexT i = lenStart;i < lenEnd;i++) - KALDI_ASSERT(V(i) == sub(i-lenStart)); + KALDI_ASSERT(V(i) == sub(i-lenStart)); } } @@ -2047,7 +2070,7 @@ template static void UnitTestSolve() { SolveQuadraticProblem(H, g, &x2); #endif KALDI_ASSERT(VecVec(x2, g) -0.5* VecSpVec(x2, H, x2) >= - VecVec(x, g) -0.5* VecSpVec(x, H, x)); + VecVec(x, g) -0.5* VecSpVec(x, H, x)); // Check objf not decreased. } @@ -2086,16 +2109,16 @@ template static void UnitTestSolve() { M3.AddMatSp(1.0, Y, kNoTrans, Qinv, 0.0); if (Q.Cond() < 1000.0) { AssertEqual(M2, M3); // This equality only holds if SigmaInv full-rank, - // which is overwhelmingly likely if dimO > dimM + // which is overwhelmingly likely if dimO > dimM } { Real a1 = TraceMatSpMat(M2, kTrans, SigmaInv, Y, kNoTrans), - a2 = TraceMatSpMatSp(M2, kNoTrans, Q, M2, kTrans, SigmaInv), - b1 = TraceMatSpMat(M, kTrans, SigmaInv, Y, kNoTrans), - b2 = TraceMatSpMatSp(M, kNoTrans, Q, M, kTrans, SigmaInv), - a3 = a1 - 0.5 * a2, - b3 = b1 - 0.5 * b2; + a2 = TraceMatSpMatSp(M2, kNoTrans, Q, M2, kTrans, SigmaInv), + b1 = TraceMatSpMat(M, kTrans, SigmaInv, Y, kNoTrans), + b2 = TraceMatSpMatSp(M, kNoTrans, Q, M, kTrans, SigmaInv), + a3 = a1 - 0.5 * a2, + b3 = b1 - 0.5 * b2; KALDI_ASSERT(a3 >= b3); // KALDI_LOG << "a3 = " << a3 << ", b3 = " << b3 << ", c3 = " << c3; } // Check objf not decreased. @@ -2114,8 +2137,8 @@ template static void UnitTestSolve() { Matrix M(dimO, dimM), G(dimO, dimM); M.SetRandn(); G.SetRandn(); -// InitRand(&M); -// InitRand(&G); + // InitRand(&M); + // InitRand(&G); Matrix M2(M); @@ -2128,13 +2151,13 @@ template static void UnitTestSolve() { { Real a1 = TraceMatMat(M2, G, kTrans), - a2 = TraceMatSpMatSp(M2, kNoTrans, Q1, M2, kTrans, P1), - a3 = TraceMatSpMatSp(M2, kNoTrans, Q2, M2, kTrans, P2), - b1 = TraceMatMat(M, G, kTrans), - b2 = TraceMatSpMatSp(M, kNoTrans, Q1, M, kTrans, P1), - b3 = TraceMatSpMatSp(M, kNoTrans, Q2, M, kTrans, P2), - a4 = a1 - 0.5 * a2 - 0.5 * a3, - b4 = b1 - 0.5 * b2 - 0.5 * b3; + a2 = TraceMatSpMatSp(M2, kNoTrans, Q1, M2, kTrans, P1), + a3 = TraceMatSpMatSp(M2, kNoTrans, Q2, M2, kTrans, P2), + b1 = TraceMatMat(M, G, kTrans), + b2 = TraceMatSpMatSp(M, kNoTrans, Q1, M, kTrans, P1), + b3 = TraceMatSpMatSp(M, kNoTrans, Q2, M, kTrans, P2), + a4 = a1 - 0.5 * a2 - 0.5 * a3, + b4 = b1 - 0.5 * b2 - 0.5 * b3; KALDI_LOG << "a4 = " << a4 << ", b4 = " << b4; KALDI_ASSERT(a4 >= b4); } // Check objf not decreased. @@ -2205,15 +2228,15 @@ template static void UnitTestTrace() { ABC.AddMatMat(1.0, A, kNoTrans, BC, kNoTrans, 0.0); Real - t1 = TraceMat(ABC), - t2 = ABC.Trace(), - t3 = TraceMatMat(A, BC), - t4 = TraceMatMat(AT, BC, kTrans), - t5 = TraceMatMat(BC, AT, kTrans), - t6 = TraceMatMatMat(A, kNoTrans, B, kNoTrans, C, kNoTrans), - t7 = TraceMatMatMat(AT, kTrans, B, kNoTrans, C, kNoTrans), - t8 = TraceMatMatMat(AT, kTrans, BT, kTrans, C, kNoTrans), - t9 = TraceMatMatMat(AT, kTrans, BT, kTrans, CT, kTrans); + t1 = TraceMat(ABC), + t2 = ABC.Trace(), + t3 = TraceMatMat(A, BC), + t4 = TraceMatMat(AT, BC, kTrans), + t5 = TraceMatMat(BC, AT, kTrans), + t6 = TraceMatMatMat(A, kNoTrans, B, kNoTrans, C, kNoTrans), + t7 = TraceMatMatMat(AT, kTrans, B, kNoTrans, C, kNoTrans), + t8 = TraceMatMatMat(AT, kTrans, BT, kTrans, C, kNoTrans), + t9 = TraceMatMatMat(AT, kTrans, BT, kTrans, CT, kTrans); Matrix ABC1(dimM, dimP); // tests AddMatMatMat. ABC1.AddMatMatMat(1.0, A, kNoTrans, B, kNoTrans, C, kNoTrans, 0.0); @@ -2228,8 +2251,8 @@ template static void UnitTestTrace() { Real tol = 0.001; KALDI_ASSERT((std::abs(t1-t2) < tol) && (std::abs(t2-t3) < tol) && (std::abs(t3-t4) < tol) - && (std::abs(t4-t5) < tol) && (std::abs(t5-t6) < tol) && (std::abs(t6-t7) < tol) - && (std::abs(t7-t8) < tol) && (std::abs(t8-t9) < tol)); + && (std::abs(t4-t5) < tol) && (std::abs(t5-t6) < tol) && (std::abs(t6-t7) < tol) + && (std::abs(t7-t8) < tol) && (std::abs(t8-t9) < tol)); } for (MatrixIndexT i = 0;i < 5;i++) { @@ -2833,11 +2856,11 @@ template static void UnitTestCompressedMatrix() { for (int i = 0; i < num_rows; i++) { Vector V(num_cols); cmat.CopyRowToVec(i, &V); // get row. - for (MatrixIndexT k = 0;k < num_cols;k++) { + for (MatrixIndexT k = 0; k < num_cols; k++) { AssertEqual(M2(i, k), V(k)); } } - + // test CopyColToVec for (int i = 0; i < num_cols; i++) { Vector V(num_rows); @@ -2850,10 +2873,10 @@ template static void UnitTestCompressedMatrix() { //test of getting a submatrix if(num_rows != 0 && num_cols != 0){ int32 sub_row_offset = (num_rows == 1 ? 0 : rand() % (num_rows-1)), - sub_col_offset = (num_cols == 1 ? 0 : rand() % (num_cols-1)); - // to make sure we don't mod by zero + sub_col_offset = (num_cols == 1 ? 0 : rand() % (num_cols-1)); + // to make sure we don't mod by zero int32 num_subrows = rand() % (num_rows-sub_row_offset), - num_subcols = rand() % (num_cols-sub_col_offset); + num_subcols = rand() % (num_cols-sub_col_offset); if(num_subrows == 0 || num_subcols == 0){ // in case we randomized to // empty matrix, at least make it correct num_subrows = 0; @@ -2926,7 +2949,7 @@ template static void MatrixUnitTest() { UnitTestSplitRadixComplexFft2(); UnitTestDct(); UnitTestRealFft(); - KALDI_LOG << " Point C"; + KALDI_LOG << " Point C"; UnitTestSplitRadixRealFft(); UnitTestSvd(); UnitTestSvdNodestroy(); @@ -2934,14 +2957,14 @@ template static void MatrixUnitTest() { UnitTestSpAddVec(); UnitTestSpAddVec(); UnitTestSpInvert(); - KALDI_LOG << " Point D"; + KALDI_LOG << " Point D"; UnitTestTpInvert(); UnitTestIo(); UnitTestIoCross(); UnitTestHtkIo(); UnitTestScale(); UnitTestTrace(); - KALDI_LOG << " Point E"; + KALDI_LOG << " Point E"; CholeskyUnitTestTr(); UnitTestAxpy(); UnitTestSimple(); @@ -2954,7 +2977,7 @@ template static void MatrixUnitTest() { // UnitTestSvdVariants(); UnitTestPower(); UnitTestDeterminant(); - KALDI_LOG << " Point F"; + KALDI_LOG << " Point F"; UnitTestDeterminantSign(); UnitTestSger(); UnitTestAddOuterProductPlusMinus(); @@ -2965,13 +2988,14 @@ template static void MatrixUnitTest() { UnitTestSherman(); UnitTestSpVec(); UnitTestLimitCondInvert(); - KALDI_LOG << " Point G"; + KALDI_LOG << " Point G"; UnitTestFloorChol(); UnitTestFloorUnit(); UnitTestLimitCond(); UnitTestMat2Vec(); UnitTestSpLogExp(); - KALDI_LOG << " Point H"; + KALDI_LOG << " Point H"; + UnitTestCopyRowsAndCols(); UnitTestSpliceRows(); UnitTestAddSp(); UnitTestRemoveRow(); @@ -2982,12 +3006,12 @@ template static void MatrixUnitTest() { UnitTestSimpleForMat(); UnitTestNorm(); UnitTestMul(); - KALDI_LOG << " Point I"; + KALDI_LOG << " Point I"; UnitTestSolve(); UnitTestMaxMin(); UnitTestInnerProd(); UnitTestScaleDiag(); - KALDI_LOG << " Point J"; + KALDI_LOG << " Point J"; UnitTestTraceSpSpLower(); UnitTestTranspose(); UnitTestAddVecToRows(); diff --git a/src/nnet_cpu/nnet-biasedlinearity.h b/src/nnet_cpu/nnet-biasedlinearity.h index 99d302492..16bb40c45 100644 --- a/src/nnet_cpu/nnet-biasedlinearity.h +++ b/src/nnet_cpu/nnet-biasedlinearity.h @@ -72,7 +72,7 @@ class BiasedLinearity : public UpdatableComponent { // compute gradient linearity_corr_.AddMatMat(1.0, err, kTrans, input, kNoTrans, momentum_); bias_corr_.Scale(momentum_); - bias_corr_.AddRowSumMat(err); + bias_corr_.AddRowSumMat(1.0, err); // l2 regularization if (l2_penalty_ != 0.0) { linearity_.AddMat(-learn_rate_*l2_penalty_*input.NumRows(), linearity_); diff --git a/src/nnet_cpu/nnet-rnnlm.cc b/src/nnet_cpu/nnet-rnnlm.cc index 35ceb97c9..bc08c380b 100644 --- a/src/nnet_cpu/nnet-rnnlm.cc +++ b/src/nnet_cpu/nnet-rnnlm.cc @@ -128,7 +128,7 @@ void Rnnlm::Backpropagate(const MatrixBase &in_err) { // update layer2 W2_.AddMatMat(-learn_rate_, h2_, kTrans, in_err, kNoTrans, 1.0); b2_corr_.SetZero(); - b2_corr_.AddRowSumMat(in_err); + b2_corr_.AddRowSumMat(1.0, in_err); b2_.AddVec(-learn_rate_, b2_corr_); // LAYER1 @@ -136,7 +136,7 @@ void Rnnlm::Backpropagate(const MatrixBase &in_err) { U1_corr_.SetZero(); b1_corr_.SetZero(); // accumulate gradient for layer1 - b1_corr_.AddRowSumMat(e2_); + b1_corr_.AddRowSumMat(1.0, e2_); for(int32 r=0; r &in_err) { } // accumulate graidient - b1_corr_.AddRowSumMat(E); + b1_corr_.AddRowSumMat(1.0, E); for(int32 r=0; r num_count_vec(I), den_count_vec(I), impr_vec(I); for (int32 j = 0; j < num_accs.num_states_; j++) { - num_count_vec.AddRowSumMat(num_accs.gamma_[j]); - den_count_vec.AddRowSumMat(den_accs.gamma_[j]); + num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]); + den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]); } for (int32 i = 0; i < I; i++) { @@ -363,8 +363,8 @@ double EbwAmSgmmUpdater::UpdateWParallel(const MleAmSgmmAccs &num_accs, Vector num_count_vec(I), den_count_vec(I), impr_vec(I); for (int32 j = 0; j < num_accs.num_states_; j++) { - num_count_vec.AddRowSumMat(num_accs.gamma_[j]); - den_count_vec.AddRowSumMat(den_accs.gamma_[j]); + num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]); + den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]); } // Get the F_i and g_i quantities-- this is done in parallel (multi-core), @@ -448,8 +448,8 @@ double EbwAmSgmmUpdater::UpdateN(const MleAmSgmmAccs &num_accs, Vector num_count_vec(I), den_count_vec(I), impr_vec(I); for (int32 j = 0; j < num_accs.num_states_; j++) { - num_count_vec.AddRowSumMat(num_accs.gamma_[j]); - den_count_vec.AddRowSumMat(den_accs.gamma_[j]); + num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]); + den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]); } for (int32 i = 0; i < I; i++) { @@ -522,8 +522,8 @@ double EbwAmSgmmUpdater::UpdateVars(const MleAmSgmmAccs &num_accs, Vector num_count_vec(I), den_count_vec(I), impr_vec(I); for (int32 j = 0; j < num_accs.num_states_; j++) { - num_count_vec.AddRowSumMat(num_accs.gamma_[j]); - den_count_vec.AddRowSumMat(den_accs.gamma_[j]); + num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]); + den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]); } for (int32 i = 0; i < I; i++) { diff --git a/src/sgmm2/am-sgmm.cc b/src/sgmm2/am-sgmm.cc index 7d5a2d331..dfb5634a6 100644 --- a/src/sgmm2/am-sgmm.cc +++ b/src/sgmm2/am-sgmm.cc @@ -538,7 +538,7 @@ BaseFloat AmSgmm2::LogLikelihood(const Sgmm2PerFrameDerivedVars &per_frame_vars, substate_cache.remaining_log_like = max; int32 num_substates = loglikes.NumCols(); substate_cache.likes.Resize(num_substates); // zeroes it. - substate_cache.likes.AddRowSumMat(loglikes); // add likelihoods [not in log!] for + substate_cache.likes.AddRowSumMat(1.0, loglikes); // add likelihoods [not in log!] for // each column [i.e. summing over the rows], so we get the sum for // each substate index. You have to multiply by exp(remaining_log_like) // to get a real likelihood. @@ -613,7 +613,7 @@ void AmSgmm2::SplitSubstatesInGroup(const Vector &pdf_occupancies, int32 split_m; // substate to split. { Vector substate_count(tgt_M); - substate_count.AddRowSumMat(c_j); + substate_count.AddRowSumMat(1.0, c_j); BaseFloat *data = substate_count.Data(); split_m = std::max_element(data, data+cur_M) - data; } diff --git a/src/sgmm2/estimate-am-sgmm-ebw.cc b/src/sgmm2/estimate-am-sgmm-ebw.cc index 418953b68..37a39ece3 100644 --- a/src/sgmm2/estimate-am-sgmm-ebw.cc +++ b/src/sgmm2/estimate-am-sgmm-ebw.cc @@ -50,10 +50,10 @@ void EbwAmSgmm2Updater::Update(const MleAmSgmm2Accs &num_accs, Vector gamma_num(num_accs.num_gaussians_); for (int32 j1 = 0; j1 < num_accs.num_groups_; j1++) - gamma_num.AddRowSumMat(num_accs.gamma_[j1]); + gamma_num.AddRowSumMat(1.0, num_accs.gamma_[j1]); Vector gamma_den(den_accs.num_gaussians_); for (int32 j1 = 0; j1 < den_accs.num_groups_; j1++) - gamma_den.AddRowSumMat(den_accs.gamma_[j1]); + gamma_den.AddRowSumMat(1.0, den_accs.gamma_[j1]); BaseFloat tot_impr = 0.0; diff --git a/src/sgmm2/estimate-am-sgmm.cc b/src/sgmm2/estimate-am-sgmm.cc index c2bf348e7..a744118aa 100644 --- a/src/sgmm2/estimate-am-sgmm.cc +++ b/src/sgmm2/estimate-am-sgmm.cc @@ -617,7 +617,7 @@ void MleAmSgmm2Updater::Update(const MleAmSgmm2Accs &accs, Vector gamma_i(accs.num_gaussians_); for (int32 j1 = 0; j1 < accs.num_groups_; j1++) - gamma_i.AddRowSumMat(accs.gamma_[j1]); // add sum of rows of + gamma_i.AddRowSumMat(1.0, accs.gamma_[j1]); // add sum of rows of // accs.gamma_[j1], to gamma_i. if (flags & kSgmmPhoneProjections) diff --git a/src/transform/lda-estimate.cc b/src/transform/lda-estimate.cc index a2a190c7e..abe9c2e37 100644 --- a/src/transform/lda-estimate.cc +++ b/src/transform/lda-estimate.cc @@ -63,7 +63,7 @@ void LdaEstimate::Estimate(int32 target_dim, // total covariance double sum = zero_acc_.Sum(); Vector total_mean(dim); - total_mean.AddRowSumMat(first_acc_); + total_mean.AddRowSumMat(1.0, first_acc_); total_mean.Scale(1/sum); SpMatrix total_covar(total_second_acc_); total_covar.Scale(1/sum);