зеркало из https://github.com/mozilla/kaldi.git
Added extra argument to a couple of matrix-library functions.
git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@1363 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
This commit is contained in:
Родитель
45e4d2f0a0
Коммит
8163148b37
|
@ -310,9 +310,9 @@ void CuVector<Real>::AddRowSumMat(Real alpha, const CuMatrix<Real> &mat, Real be
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
Vector<Real> tmp(mat.NumCols());
|
Vector<Real> tmp(mat.NumCols());
|
||||||
tmp.AddRowSumMat(mat.Mat());
|
tmp.AddRowSumMat(1.0, mat.Mat());
|
||||||
if(beta != 1.0) vec_.Scale(beta);
|
if(beta != 1.0) vec_.Scale(beta);
|
||||||
vec_.AddVec(alpha,tmp);
|
vec_.AddVec(alpha, tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,7 +360,7 @@ void CuVector<Real>::AddColSumMat(Real alpha, const CuMatrix<Real> &mat, Real be
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
Vector<Real> tmp(mat.NumRows());
|
Vector<Real> tmp(mat.NumRows());
|
||||||
tmp.AddColSumMat(mat.Mat());
|
tmp.AddColSumMat(1.0, mat.Mat());
|
||||||
if(beta != 1.0) vec_.Scale(beta);
|
if(beta != 1.0) vec_.Scale(beta);
|
||||||
vec_.AddVec(alpha,tmp);
|
vec_.AddVec(alpha,tmp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
if (subtract_mean) {
|
if (subtract_mean) {
|
||||||
Vector<BaseFloat> mean(features.NumCols());
|
Vector<BaseFloat> mean(features.NumCols());
|
||||||
mean.AddRowSumMat(features);
|
mean.AddRowSumMat(1.0, features);
|
||||||
mean.Scale(1.0 / features.NumRows());
|
mean.Scale(1.0 / features.NumRows());
|
||||||
for (int32 i = 0; i < features.NumRows(); i++)
|
for (int32 i = 0; i < features.NumRows(); i++)
|
||||||
features.Row(i).AddVec(-1.0, mean);
|
features.Row(i).AddVec(-1.0, mean);
|
||||||
|
|
|
@ -153,7 +153,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
if (subtract_mean) {
|
if (subtract_mean) {
|
||||||
Vector<BaseFloat> mean(features.NumCols());
|
Vector<BaseFloat> mean(features.NumCols());
|
||||||
mean.AddRowSumMat(features);
|
mean.AddRowSumMat(1.0, features);
|
||||||
mean.Scale(1.0 / features.NumRows());
|
mean.Scale(1.0 / features.NumRows());
|
||||||
for (int32 i = 0; i < features.NumRows(); i++)
|
for (int32 i = 0; i < features.NumRows(); i++)
|
||||||
features.Row(i).AddVec(-1.0, mean);
|
features.Row(i).AddVec(-1.0, mean);
|
||||||
|
|
|
@ -153,7 +153,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
if (subtract_mean) {
|
if (subtract_mean) {
|
||||||
Vector<BaseFloat> mean(features.NumCols());
|
Vector<BaseFloat> mean(features.NumCols());
|
||||||
mean.AddRowSumMat(features);
|
mean.AddRowSumMat(1.0, features);
|
||||||
mean.Scale(1.0 / features.NumRows());
|
mean.Scale(1.0 / features.NumRows());
|
||||||
for (size_t i = 0; i < features.NumRows(); i++)
|
for (size_t i = 0; i < features.NumRows(); i++)
|
||||||
features.Row(i).AddVec(-1.0, mean);
|
features.Row(i).AddVec(-1.0, mean);
|
||||||
|
|
|
@ -116,7 +116,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
if (subtract_mean) {
|
if (subtract_mean) {
|
||||||
Vector<BaseFloat> mean(features.NumCols());
|
Vector<BaseFloat> mean(features.NumCols());
|
||||||
mean.AddRowSumMat(features);
|
mean.AddRowSumMat(1.0, features);
|
||||||
mean.Scale(1.0 / features.NumRows());
|
mean.Scale(1.0 / features.NumRows());
|
||||||
for (int32 i = 0; i < features.NumRows(); i++)
|
for (int32 i = 0; i < features.NumRows(); i++)
|
||||||
features.Row(i).AddVec(-1.0, mean);
|
features.Row(i).AddVec(-1.0, mean);
|
||||||
|
|
|
@ -52,7 +52,7 @@ int main(int argc, char *argv[]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Vector<BaseFloat> mean(feats.NumCols());
|
Vector<BaseFloat> mean(feats.NumCols());
|
||||||
mean.AddRowSumMat(feats);
|
mean.AddRowSumMat(1.0, feats);
|
||||||
mean.Scale(1.0 / feats.NumRows());
|
mean.Scale(1.0 / feats.NumRows());
|
||||||
for (int32 i = 0; i < feats.NumRows(); i++)
|
for (int32 i = 0; i < feats.NumRows(); i++)
|
||||||
feats.Row(i).AddVec(-1.0, mean);
|
feats.Row(i).AddVec(-1.0, mean);
|
||||||
|
|
|
@ -309,7 +309,7 @@ UnitTestEstimateFullGmm() {
|
||||||
Vector<BaseFloat> mean(dim);
|
Vector<BaseFloat> mean(dim);
|
||||||
cov.AddMatMat(1.0, feats, kTrans, feats, kNoTrans, 0.0);
|
cov.AddMatMat(1.0, feats, kTrans, feats, kNoTrans, 0.0);
|
||||||
cov.Scale(1.0 / feats.NumRows());
|
cov.Scale(1.0 / feats.NumRows());
|
||||||
mean.AddRowSumMat(feats);
|
mean.AddRowSumMat(1.0, feats);
|
||||||
mean.Scale(1.0 / feats.NumRows());
|
mean.Scale(1.0 / feats.NumRows());
|
||||||
cov.AddVecVec(-1.0, mean, mean);
|
cov.AddVecVec(-1.0, mean, mean);
|
||||||
BaseFloat logdet = cov.LogDet();
|
BaseFloat logdet = cov.LogDet();
|
||||||
|
|
|
@ -763,38 +763,55 @@ void MatrixBase<double>::CopyFromTp(const TpMatrix<double> & M,
|
||||||
|
|
||||||
template<typename Real>
|
template<typename Real>
|
||||||
void MatrixBase<Real>::CopyRowsFromVec(const VectorBase<Real> &rv) {
|
void MatrixBase<Real>::CopyRowsFromVec(const VectorBase<Real> &rv) {
|
||||||
KALDI_ASSERT(rv.Dim() == num_rows_*num_cols_);
|
if (rv.Dim() == num_rows_*num_cols_) {
|
||||||
|
if (stride_ == num_cols_) {
|
||||||
if (stride_ == num_cols_) {
|
// one big copy operation.
|
||||||
// one big copy operation.
|
const Real *rv_data = rv.Data();
|
||||||
const Real *rv_data = rv.Data();
|
std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_);
|
||||||
std::memcpy(data_, rv_data, sizeof(Real)*num_rows_*num_cols_);
|
} else {
|
||||||
} else {
|
const Real *rv_data = rv.Data();
|
||||||
const Real *rv_data = rv.Data();
|
for (MatrixIndexT r = 0; r < num_rows_; r++) {
|
||||||
for (MatrixIndexT r = 0; r < num_rows_; r++) {
|
Real *row_data = RowData(r);
|
||||||
Real *row_data = RowData(r);
|
for (MatrixIndexT c = 0; c < num_cols_; c++) {
|
||||||
for (MatrixIndexT c = 0; c < num_cols_; c++) {
|
row_data[c] = rv_data[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<typename Real>
|
template<typename Real>
|
||||||
template<typename OtherReal>
|
template<typename OtherReal>
|
||||||
void MatrixBase<Real>::CopyRowsFromVec(const VectorBase<OtherReal> &rv) {
|
void MatrixBase<Real>::CopyRowsFromVec(const VectorBase<OtherReal> &rv) {
|
||||||
KALDI_ASSERT(rv.Dim() == num_rows_*num_cols_);
|
if (rv.Dim() == num_rows_*num_cols_) {
|
||||||
const OtherReal *rv_data = rv.Data();
|
const OtherReal *rv_data = rv.Data();
|
||||||
for (MatrixIndexT r = 0; r < num_rows_; r++) {
|
for (MatrixIndexT r = 0; r < num_rows_; r++) {
|
||||||
Real *row_data = RowData(r);
|
Real *row_data = RowData(r);
|
||||||
for (MatrixIndexT c = 0; c < num_cols_; c++) {
|
for (MatrixIndexT c = 0; c < num_cols_; c++) {
|
||||||
row_data[c] = static_cast<Real>(rv_data[c]);
|
row_data[c] = static_cast<Real>(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
|
template
|
||||||
void MatrixBase<float>::CopyRowsFromVec(const VectorBase<double> &rv);
|
void MatrixBase<float>::CopyRowsFromVec(const VectorBase<double> &rv);
|
||||||
template
|
template
|
||||||
|
@ -802,17 +819,28 @@ void MatrixBase<double>::CopyRowsFromVec(const VectorBase<float> &rv);
|
||||||
|
|
||||||
template<typename Real>
|
template<typename Real>
|
||||||
void MatrixBase<Real>::CopyColsFromVec(const VectorBase<Real> &rv) {
|
void MatrixBase<Real>::CopyColsFromVec(const VectorBase<Real> &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();
|
for (MatrixIndexT c = 0; c < num_cols_; c++) {
|
||||||
Real *m_inc_data = data_;
|
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++) {
|
v_inc_data += num_rows_;
|
||||||
m_inc_data[r * stride_] = v_inc_data[r];
|
m_inc_data ++;
|
||||||
}
|
}
|
||||||
v_inc_data += num_rows_;
|
} else if (rv.Dim() == num_rows_) {
|
||||||
m_inc_data ++;
|
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.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -130,14 +130,17 @@ class MatrixBase {
|
||||||
MatrixTransposeType Trans = kNoTrans);
|
MatrixTransposeType Trans = kNoTrans);
|
||||||
|
|
||||||
/// Inverse of vec() operator. Copies vector into matrix, row-by-row.
|
/// 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<Real> &v);
|
void CopyRowsFromVec(const VectorBase<Real> &v);
|
||||||
template<typename OtherReal>
|
template<typename OtherReal>
|
||||||
void CopyRowsFromVec(const VectorBase<OtherReal> &v);
|
void CopyRowsFromVec(const VectorBase<OtherReal> &v);
|
||||||
|
|
||||||
/// Copies vector into matrix, column-by-column.
|
/// 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<Real> &v);
|
void CopyColsFromVec(const VectorBase<Real> &v);
|
||||||
|
|
||||||
/// Copy vector into specific column of matrix.
|
/// Copy vector into specific column of matrix.
|
||||||
void CopyColFromVec(const VectorBase<Real> &v, const MatrixIndexT col);
|
void CopyColFromVec(const VectorBase<Real> &v, const MatrixIndexT col);
|
||||||
/// Copy vector into specific row of matrix.
|
/// Copy vector into specific row of matrix.
|
||||||
|
@ -745,6 +748,12 @@ std::istream & operator >> (std::istream & In, MatrixBase<Real> & M);
|
||||||
template<typename Real>
|
template<typename Real>
|
||||||
std::istream & operator >> (std::istream & In, Matrix<Real> & M);
|
std::istream & operator >> (std::istream & In, Matrix<Real> & M);
|
||||||
|
|
||||||
|
|
||||||
|
template<class Real>
|
||||||
|
bool SameDim(const MatrixBase<Real> &M, const MatrixBase<Real> &N) {
|
||||||
|
return (M.NumRows() == N.NumRows() && M.NumCols() == N.NumCols());
|
||||||
|
}
|
||||||
|
|
||||||
/// @} end of \addtogroup matrix_funcs_io
|
/// @} end of \addtogroup matrix_funcs_io
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -551,7 +551,7 @@ Real VectorBase<Real>::SumLog() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Real>
|
template<typename Real>
|
||||||
void VectorBase<Real>::AddRowSumMat(const MatrixBase<Real> &rM) {
|
void VectorBase<Real>::AddRowSumMat(Real alpha, const MatrixBase<Real> &rM) {
|
||||||
// note the double accumulator
|
// note the double accumulator
|
||||||
double sum;
|
double sum;
|
||||||
KALDI_ASSERT(dim_ == rM.NumCols());
|
KALDI_ASSERT(dim_ == rM.NumCols());
|
||||||
|
@ -560,12 +560,12 @@ void VectorBase<Real>::AddRowSumMat(const MatrixBase<Real> &rM) {
|
||||||
for (MatrixIndexT j = 0; j < rM.NumRows(); j++) {
|
for (MatrixIndexT j = 0; j < rM.NumRows(); j++) {
|
||||||
sum += rM(j, i);
|
sum += rM(j, i);
|
||||||
}
|
}
|
||||||
data_[i] += sum;
|
data_[i] += alpha * sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Real>
|
template<typename Real>
|
||||||
void VectorBase<Real>::AddColSumMat(const MatrixBase<Real> &rM) {
|
void VectorBase<Real>::AddColSumMat(Real alpha, const MatrixBase<Real> &rM) {
|
||||||
// note the double accumulator
|
// note the double accumulator
|
||||||
double sum;
|
double sum;
|
||||||
KALDI_ASSERT(dim_ == rM.NumRows());
|
KALDI_ASSERT(dim_ == rM.NumRows());
|
||||||
|
@ -574,7 +574,7 @@ void VectorBase<Real>::AddColSumMat(const MatrixBase<Real> &rM) {
|
||||||
for (MatrixIndexT j = 0; j < rM.NumCols(); j++) {
|
for (MatrixIndexT j = 0; j < rM.NumCols(); j++) {
|
||||||
sum += rM(i, j);
|
sum += rM(i, j);
|
||||||
}
|
}
|
||||||
data_[i] += sum;
|
data_[i] += alpha * sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -242,11 +242,11 @@ class VectorBase {
|
||||||
/// negative.
|
/// negative.
|
||||||
Real SumLog() const;
|
Real SumLog() const;
|
||||||
|
|
||||||
/// Adds sum of the rows of M to existing contents.
|
/// Adds sum of the rows of M to existing contents, times alpha.
|
||||||
void AddRowSumMat(const MatrixBase<Real>& M);
|
void AddRowSumMat(Real alpha, const MatrixBase<Real>& M);
|
||||||
|
|
||||||
/// Adds sum of the columns of M to existing contents.
|
/// Adds sum of the columns of M to existing contents.
|
||||||
void AddColSumMat(const MatrixBase<Real>& M);
|
void AddColSumMat(Real alpha, const MatrixBase<Real>& M);
|
||||||
|
|
||||||
/// Returns log(sum(exp())) without exp overflow
|
/// Returns log(sum(exp())) without exp overflow
|
||||||
/// If prune > 0.0, ignores terms less than the max - prune.
|
/// If prune > 0.0, ignores terms less than the max - prune.
|
||||||
|
|
|
@ -67,16 +67,16 @@ start:
|
||||||
for (MatrixIndexT i = 0;i < M->NumRows();i++)
|
for (MatrixIndexT i = 0;i < M->NumRows();i++)
|
||||||
for (MatrixIndexT j = 0;j < M->NumCols();j++)
|
for (MatrixIndexT j = 0;j < M->NumCols();j++)
|
||||||
(*M)(i, j) = RandGauss();
|
(*M)(i, j) = RandGauss();
|
||||||
if (M->NumRows() != 0 && M->Cond() > 100) {
|
if (M->NumRows() != 0 && M->Cond() > 100) {
|
||||||
printf("Condition number of random matrix large %f, trying again (this is normal)\n",
|
printf("Condition number of random matrix large %f, trying again (this is normal)\n",
|
||||||
(float) M->Cond());
|
(float) M->Cond());
|
||||||
goto start;
|
goto start;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Real> static void InitRand(SpMatrix<Real> *M) {
|
template<class Real> static void InitRand(SpMatrix<Real> *M) {
|
||||||
start:
|
start:
|
||||||
for (MatrixIndexT i = 0;i < M->NumRows();i++)
|
for (MatrixIndexT i = 0;i < M->NumRows();i++)
|
||||||
for (MatrixIndexT j = 0;j<=i;j++)
|
for (MatrixIndexT j = 0;j<=i;j++)
|
||||||
(*M)(i, j) = RandGauss();
|
(*M)(i, j) = RandGauss();
|
||||||
|
@ -115,14 +115,14 @@ static bool ApproxEqual(const SpMatrix<Real> &A,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* was:
|
/* was:
|
||||||
template<class Real>
|
template<class Real>
|
||||||
bool ApproxEqual(SpMatrix<Real> &A, SpMatrix<Real> &B, float tol = 0.001) {
|
bool ApproxEqual(SpMatrix<Real> &A, SpMatrix<Real> &B, float tol = 0.001) {
|
||||||
KALDI_ASSERT(A.NumRows() == B.NumRows()&&A.NumCols() == B.NumCols());
|
KALDI_ASSERT(A.NumRows() == B.NumRows()&&A.NumCols() == B.NumCols());
|
||||||
for (MatrixIndexT i = 0;i < A.NumRows();i++)
|
for (MatrixIndexT i = 0;i < A.NumRows();i++)
|
||||||
for (MatrixIndexT j = 0;j<=i;j++)
|
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;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<class Real> static void AssertEqual(Vector<Real> &A, Vector<Real> &B, float tol = 0.001) {
|
template<class Real> static void AssertEqual(Vector<Real> &A, Vector<Real> &B, float tol = 0.001) {
|
||||||
|
@ -209,6 +209,27 @@ static void UnitTestSpAddVec() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Real> 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<float> w(dimN); // test cross-type version of
|
||||||
|
// CopyRowsFromVec.
|
||||||
|
Vector<Real> v(dimM);
|
||||||
|
Matrix<Real> 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<class Real> static void UnitTestSpliceRows() {
|
template<class Real> static void UnitTestSpliceRows() {
|
||||||
|
|
||||||
|
@ -291,10 +312,10 @@ template<class Real> static void UnitTestRemoveRow() {
|
||||||
Vector<Real> N(V);
|
Vector<Real> N(V);
|
||||||
N.RemoveElement(i);
|
N.RemoveElement(i);
|
||||||
for (MatrixIndexT j = 0;j < i;j++) {
|
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++) {
|
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<Real> M(dimM, dimN);
|
Matrix<Real> M(dimM, dimN);
|
||||||
InitRand(&M);
|
InitRand(&M);
|
||||||
Vector<Real> Vr(dimN), Vc(dimM);
|
Vector<Real> Vr(dimN), Vc(dimM);
|
||||||
Vr.AddRowSumMat(M);
|
Vr.AddRowSumMat(0.5, M);
|
||||||
Vc.AddColSumMat(M);
|
Vc.AddColSumMat(0.5, M);
|
||||||
|
Vr.Scale(2.0);
|
||||||
|
Vc.Scale(2.0);
|
||||||
|
|
||||||
Vector<Real> V2r(dimN), V2c(dimM);
|
Vector<Real> V2r(dimN), V2c(dimM);
|
||||||
for (MatrixIndexT k = 0; k < dimM; k++) {
|
for (MatrixIndexT k = 0; k < dimM; k++) {
|
||||||
|
@ -511,7 +534,7 @@ static void UnitTestSimpleForMat() { // test some simple operates on all kinds
|
||||||
y.Cholesky(x);
|
y.Cholesky(x);
|
||||||
|
|
||||||
std::cout << "Matrix y is a lower triangular Cholesky decomposition of x:"
|
std::cout << "Matrix y is a lower triangular Cholesky decomposition of x:"
|
||||||
<< '\n';
|
<< '\n';
|
||||||
std::cout << y << '\n';
|
std::cout << y << '\n';
|
||||||
|
|
||||||
// test sp-matrix's LogPosDefDet() function
|
// test sp-matrix's LogPosDefDet() function
|
||||||
|
@ -825,13 +848,13 @@ template<class Real> static void UnitTestSherman() {
|
||||||
Matrix<Real> tt2(dimK, dimK);
|
Matrix<Real> tt2(dimK, dimK);
|
||||||
tt2.AddMatMat(1.0, V, kTrans, tt1, kNoTrans, 0.0);
|
tt2.AddMatMat(1.0, V, kTrans, tt1, kNoTrans, 0.0);
|
||||||
for (MatrixIndexT i = 0;i < dimK;i++)
|
for (MatrixIndexT i = 0;i < dimK;i++)
|
||||||
for (MatrixIndexT j = 0;j < dimK;j++)
|
for (MatrixIndexT j = 0;j < dimK;j++)
|
||||||
{
|
{
|
||||||
if (i == j)
|
if (i == j)
|
||||||
I(i, j) = 1.0;
|
I(i, j) = 1.0;
|
||||||
else
|
else
|
||||||
I(i, j) = 0.0;
|
I(i, j) = 0.0;
|
||||||
}
|
}
|
||||||
tt2.AddMat(1.0, I); // I = identity
|
tt2.AddMat(1.0, I); // I = identity
|
||||||
tt2.Invert();
|
tt2.Invert();
|
||||||
|
|
||||||
|
@ -975,25 +998,25 @@ template<class Real> static void UnitTestSvdNodestroy() {
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
template<class Real> 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.
|
template<class Real> 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
|
#ifndef HAVE_ATLAS
|
||||||
int Base = 10, Rand = 5, Iter = 25;
|
int Base = 10, Rand = 5, Iter = 25;
|
||||||
for (int iter = 0;iter < Iter;iter++) {
|
for (int iter = 0;iter < Iter;iter++) {
|
||||||
MatrixIndexT dimM = Base + rand() % Rand, dimN = Base + rand() % Rand;
|
MatrixIndexT dimM = Base + rand() % Rand, dimN = Base + rand() % Rand;
|
||||||
// if (dimM<dimN) std::swap(dimM, dimN); // M>=N.
|
// if (dimM<dimN) std::swap(dimM, dimN); // M>=N.
|
||||||
Matrix<Real> M(dimM, dimN);
|
Matrix<Real> M(dimM, dimN);
|
||||||
Matrix<Real> U(dimM, dimM), Vt(dimN, dimN); Vector<Real> v(std::min(dimM, dimN));
|
Matrix<Real> U(dimM, dimM), Vt(dimN, dimN); Vector<Real> v(std::min(dimM, dimN));
|
||||||
Matrix<Real> Utmp(dimM, 1); Matrix<Real> Vttmp(1, dimN);
|
Matrix<Real> Utmp(dimM, 1); Matrix<Real> Vttmp(1, dimN);
|
||||||
InitRand(&M);
|
InitRand(&M);
|
||||||
M.Svd(v, U, Vttmp, "A", "N");
|
M.Svd(v, U, Vttmp, "A", "N");
|
||||||
M.Svd(v, Utmp, Vt, "N", "A");
|
M.Svd(v, Utmp, Vt, "N", "A");
|
||||||
Matrix<Real> U2(dimM, dimM), Vt2(dimN, dimN); Vector<Real> v2(std::min(dimM, dimN));
|
Matrix<Real> U2(dimM, dimM), Vt2(dimN, dimN); Vector<Real> v2(std::min(dimM, dimN));
|
||||||
M.Svd(v, U2, Vt2, "A", "A");
|
M.Svd(v, U2, Vt2, "A", "A");
|
||||||
AssertEqual(U, U2); AssertEqual(Vt, Vt2);
|
AssertEqual(U, U2); AssertEqual(Vt, Vt2);
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
template<class Real> static void UnitTestSvdJustvec() { // Making sure gives same answer if we get just the vector, not the eigs.
|
template<class Real> static void UnitTestSvdJustvec() { // Making sure gives same answer if we get just the vector, not the eigs.
|
||||||
int Base = 10, Rand = 5, Iter = 25;
|
int Base = 10, Rand = 5, Iter = 25;
|
||||||
|
@ -1034,9 +1057,9 @@ template<class Real> static void UnitTestEig() {
|
||||||
for (int iter = 0;iter < 5;iter++) {
|
for (int iter = 0;iter < 5;iter++) {
|
||||||
MatrixIndexT dimM = 1 + iter;
|
MatrixIndexT dimM = 1 + iter;
|
||||||
/* if (iter < 10)
|
/* if (iter < 10)
|
||||||
dimM = 1 + rand() % 6;
|
dimM = 1 + rand() % 6;
|
||||||
else
|
else
|
||||||
dimM = 5 + rand()%10; */
|
dimM = 5 + rand()%10; */
|
||||||
Matrix<Real> M(dimM, dimM);
|
Matrix<Real> M(dimM, dimM);
|
||||||
InitRand(&M);
|
InitRand(&M);
|
||||||
Matrix<Real> P(dimM, dimM);
|
Matrix<Real> P(dimM, dimM);
|
||||||
|
@ -1392,21 +1415,21 @@ static void UnitTestTransposeScatter() {
|
||||||
|
|
||||||
for (MatrixIndexT i = 0;i < Ap.NumRows();i++) {
|
for (MatrixIndexT i = 0;i < Ap.NumRows();i++) {
|
||||||
for (MatrixIndexT j = 0; j<=i; j++) {
|
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 i = 0;i < M.NumRows();i++) {
|
||||||
for (MatrixIndexT j = 0; j < M.NumCols(); j++) {
|
for (MatrixIndexT j = 0; j < M.NumCols(); j++) {
|
||||||
M(i, j) = RandGauss();
|
M(i, j) = RandGauss();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
std::stringstream ss("1 2 3");
|
std::stringstream ss("1 2 3");
|
||||||
ss >> Ap;
|
ss >> Ap;
|
||||||
ss.clear();
|
ss.clear();
|
||||||
ss.str("5 6 7 8 9 10");
|
ss.str("5 6 7 8 9 10");
|
||||||
ss >> M;
|
ss >> M;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Af.CopyFromSp(Ap);
|
Af.CopyFromSp(Ap);
|
||||||
A_MT.AddMatMat(1.0, Af, kNoTrans, M, kTrans, 0.0);
|
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);
|
Op.AddMat2Sp(1.0, M, kNoTrans, Ap, 0.0);
|
||||||
|
|
||||||
|
|
||||||
// std::cout << "A" << '\n' << Af << '\n';
|
// std::cout << "A" << '\n' << Af << '\n';
|
||||||
// std::cout << "M" << '\n' << M << '\n';
|
// std::cout << "M" << '\n' << M << '\n';
|
||||||
// std::cout << "Op" << '\n' << Op << '\n';
|
// std::cout << "Op" << '\n' << Op << '\n';
|
||||||
|
|
||||||
for (MatrixIndexT i = 0; i < dimO; i++) {
|
for (MatrixIndexT i = 0; i < dimO; i++) {
|
||||||
for (MatrixIndexT j = 0; j<=i; j++) {
|
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);
|
Af.AddMatMat(1.0, M, kTrans, A_MT, kNoTrans, 1.0);
|
||||||
Ap.AddMat2Sp(1.0, M, kTrans, Op, 1.0);
|
Ap.AddMat2Sp(1.0, M, kTrans, Op, 1.0);
|
||||||
|
|
||||||
// std::cout << "Ap" << '\n' << Ap << '\n';
|
// std::cout << "Ap" << '\n' << Ap << '\n';
|
||||||
// std::cout << "Af" << '\n' << Af << '\n';
|
// std::cout << "Af" << '\n' << Af << '\n';
|
||||||
|
|
||||||
for (MatrixIndexT i = 0; i < dimA; i++) {
|
for (MatrixIndexT i = 0; i < dimA; i++) {
|
||||||
for (MatrixIndexT j = 0; j<=i; j++) {
|
for (MatrixIndexT j = 0; j<=i; j++) {
|
||||||
|
@ -1671,7 +1694,7 @@ template<class Real> static void UnitTestSimple() {
|
||||||
S.CopyFromMat(N);
|
S.CopyFromMat(N);
|
||||||
KALDI_ASSERT(S.IsZero());
|
KALDI_ASSERT(S.IsZero());
|
||||||
KALDI_ASSERT(S.IsDiagonal());
|
KALDI_ASSERT(S.IsDiagonal());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1885,14 +1908,14 @@ template<class Real> static void UnitTestRange() { // Testing SubMatrix class.
|
||||||
KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum());
|
KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum());
|
||||||
|
|
||||||
for (MatrixIndexT i = lenStart;i < lenEnd;i++)
|
for (MatrixIndexT i = lenStart;i < lenEnd;i++)
|
||||||
KALDI_ASSERT(V(i) == sub(i-lenStart));
|
KALDI_ASSERT(V(i) == sub(i-lenStart));
|
||||||
|
|
||||||
InitRand(&sub);
|
InitRand(&sub);
|
||||||
|
|
||||||
KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum());
|
KALDI_ASSERT(sub.Sum() == V.Range(lenStart, lenEnd-lenStart).Sum());
|
||||||
|
|
||||||
for (MatrixIndexT i = lenStart;i < lenEnd;i++)
|
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<class Real> static void UnitTestSolve() {
|
||||||
SolveQuadraticProblem(H, g, &x2);
|
SolveQuadraticProblem(H, g, &x2);
|
||||||
#endif
|
#endif
|
||||||
KALDI_ASSERT(VecVec(x2, g) -0.5* VecSpVec(x2, H, x2) >=
|
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.
|
// Check objf not decreased.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2086,16 +2109,16 @@ template<class Real> static void UnitTestSolve() {
|
||||||
M3.AddMatSp(1.0, Y, kNoTrans, Qinv, 0.0);
|
M3.AddMatSp(1.0, Y, kNoTrans, Qinv, 0.0);
|
||||||
if (Q.Cond() < 1000.0) {
|
if (Q.Cond() < 1000.0) {
|
||||||
AssertEqual(M2, M3); // This equality only holds if SigmaInv full-rank,
|
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),
|
Real a1 = TraceMatSpMat(M2, kTrans, SigmaInv, Y, kNoTrans),
|
||||||
a2 = TraceMatSpMatSp(M2, kNoTrans, Q, M2, kTrans, SigmaInv),
|
a2 = TraceMatSpMatSp(M2, kNoTrans, Q, M2, kTrans, SigmaInv),
|
||||||
b1 = TraceMatSpMat(M, kTrans, SigmaInv, Y, kNoTrans),
|
b1 = TraceMatSpMat(M, kTrans, SigmaInv, Y, kNoTrans),
|
||||||
b2 = TraceMatSpMatSp(M, kNoTrans, Q, M, kTrans, SigmaInv),
|
b2 = TraceMatSpMatSp(M, kNoTrans, Q, M, kTrans, SigmaInv),
|
||||||
a3 = a1 - 0.5 * a2,
|
a3 = a1 - 0.5 * a2,
|
||||||
b3 = b1 - 0.5 * b2;
|
b3 = b1 - 0.5 * b2;
|
||||||
KALDI_ASSERT(a3 >= b3);
|
KALDI_ASSERT(a3 >= b3);
|
||||||
// KALDI_LOG << "a3 = " << a3 << ", b3 = " << b3 << ", c3 = " << c3;
|
// KALDI_LOG << "a3 = " << a3 << ", b3 = " << b3 << ", c3 = " << c3;
|
||||||
} // Check objf not decreased.
|
} // Check objf not decreased.
|
||||||
|
@ -2114,8 +2137,8 @@ template<class Real> static void UnitTestSolve() {
|
||||||
Matrix<Real> M(dimO, dimM), G(dimO, dimM);
|
Matrix<Real> M(dimO, dimM), G(dimO, dimM);
|
||||||
M.SetRandn();
|
M.SetRandn();
|
||||||
G.SetRandn();
|
G.SetRandn();
|
||||||
// InitRand(&M);
|
// InitRand(&M);
|
||||||
// InitRand(&G);
|
// InitRand(&G);
|
||||||
|
|
||||||
Matrix<Real> M2(M);
|
Matrix<Real> M2(M);
|
||||||
|
|
||||||
|
@ -2128,13 +2151,13 @@ template<class Real> static void UnitTestSolve() {
|
||||||
|
|
||||||
{
|
{
|
||||||
Real a1 = TraceMatMat(M2, G, kTrans),
|
Real a1 = TraceMatMat(M2, G, kTrans),
|
||||||
a2 = TraceMatSpMatSp(M2, kNoTrans, Q1, M2, kTrans, P1),
|
a2 = TraceMatSpMatSp(M2, kNoTrans, Q1, M2, kTrans, P1),
|
||||||
a3 = TraceMatSpMatSp(M2, kNoTrans, Q2, M2, kTrans, P2),
|
a3 = TraceMatSpMatSp(M2, kNoTrans, Q2, M2, kTrans, P2),
|
||||||
b1 = TraceMatMat(M, G, kTrans),
|
b1 = TraceMatMat(M, G, kTrans),
|
||||||
b2 = TraceMatSpMatSp(M, kNoTrans, Q1, M, kTrans, P1),
|
b2 = TraceMatSpMatSp(M, kNoTrans, Q1, M, kTrans, P1),
|
||||||
b3 = TraceMatSpMatSp(M, kNoTrans, Q2, M, kTrans, P2),
|
b3 = TraceMatSpMatSp(M, kNoTrans, Q2, M, kTrans, P2),
|
||||||
a4 = a1 - 0.5 * a2 - 0.5 * a3,
|
a4 = a1 - 0.5 * a2 - 0.5 * a3,
|
||||||
b4 = b1 - 0.5 * b2 - 0.5 * b3;
|
b4 = b1 - 0.5 * b2 - 0.5 * b3;
|
||||||
KALDI_LOG << "a4 = " << a4 << ", b4 = " << b4;
|
KALDI_LOG << "a4 = " << a4 << ", b4 = " << b4;
|
||||||
KALDI_ASSERT(a4 >= b4);
|
KALDI_ASSERT(a4 >= b4);
|
||||||
} // Check objf not decreased.
|
} // Check objf not decreased.
|
||||||
|
@ -2205,15 +2228,15 @@ template<class Real> static void UnitTestTrace() {
|
||||||
ABC.AddMatMat(1.0, A, kNoTrans, BC, kNoTrans, 0.0);
|
ABC.AddMatMat(1.0, A, kNoTrans, BC, kNoTrans, 0.0);
|
||||||
|
|
||||||
Real
|
Real
|
||||||
t1 = TraceMat(ABC),
|
t1 = TraceMat(ABC),
|
||||||
t2 = ABC.Trace(),
|
t2 = ABC.Trace(),
|
||||||
t3 = TraceMatMat(A, BC),
|
t3 = TraceMatMat(A, BC),
|
||||||
t4 = TraceMatMat(AT, BC, kTrans),
|
t4 = TraceMatMat(AT, BC, kTrans),
|
||||||
t5 = TraceMatMat(BC, AT, kTrans),
|
t5 = TraceMatMat(BC, AT, kTrans),
|
||||||
t6 = TraceMatMatMat(A, kNoTrans, B, kNoTrans, C, kNoTrans),
|
t6 = TraceMatMatMat(A, kNoTrans, B, kNoTrans, C, kNoTrans),
|
||||||
t7 = TraceMatMatMat(AT, kTrans, B, kNoTrans, C, kNoTrans),
|
t7 = TraceMatMatMat(AT, kTrans, B, kNoTrans, C, kNoTrans),
|
||||||
t8 = TraceMatMatMat(AT, kTrans, BT, kTrans, C, kNoTrans),
|
t8 = TraceMatMatMat(AT, kTrans, BT, kTrans, C, kNoTrans),
|
||||||
t9 = TraceMatMatMat(AT, kTrans, BT, kTrans, CT, kTrans);
|
t9 = TraceMatMatMat(AT, kTrans, BT, kTrans, CT, kTrans);
|
||||||
|
|
||||||
Matrix<Real> ABC1(dimM, dimP); // tests AddMatMatMat.
|
Matrix<Real> ABC1(dimM, dimP); // tests AddMatMatMat.
|
||||||
ABC1.AddMatMatMat(1.0, A, kNoTrans, B, kNoTrans, C, kNoTrans, 0.0);
|
ABC1.AddMatMatMat(1.0, A, kNoTrans, B, kNoTrans, C, kNoTrans, 0.0);
|
||||||
|
@ -2228,8 +2251,8 @@ template<class Real> static void UnitTestTrace() {
|
||||||
|
|
||||||
Real tol = 0.001;
|
Real tol = 0.001;
|
||||||
KALDI_ASSERT((std::abs(t1-t2) < tol) && (std::abs(t2-t3) < tol) && (std::abs(t3-t4) < tol)
|
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(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(t7-t8) < tol) && (std::abs(t8-t9) < tol));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (MatrixIndexT i = 0;i < 5;i++) {
|
for (MatrixIndexT i = 0;i < 5;i++) {
|
||||||
|
@ -2833,7 +2856,7 @@ template<class Real> static void UnitTestCompressedMatrix() {
|
||||||
for (int i = 0; i < num_rows; i++) {
|
for (int i = 0; i < num_rows; i++) {
|
||||||
Vector<Real> V(num_cols);
|
Vector<Real> V(num_cols);
|
||||||
cmat.CopyRowToVec(i, &V); // get row.
|
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));
|
AssertEqual(M2(i, k), V(k));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2850,10 +2873,10 @@ template<class Real> static void UnitTestCompressedMatrix() {
|
||||||
//test of getting a submatrix
|
//test of getting a submatrix
|
||||||
if(num_rows != 0 && num_cols != 0){
|
if(num_rows != 0 && num_cols != 0){
|
||||||
int32 sub_row_offset = (num_rows == 1 ? 0 : rand() % (num_rows-1)),
|
int32 sub_row_offset = (num_rows == 1 ? 0 : rand() % (num_rows-1)),
|
||||||
sub_col_offset = (num_cols == 1 ? 0 : rand() % (num_cols-1));
|
sub_col_offset = (num_cols == 1 ? 0 : rand() % (num_cols-1));
|
||||||
// to make sure we don't mod by zero
|
// to make sure we don't mod by zero
|
||||||
int32 num_subrows = rand() % (num_rows-sub_row_offset),
|
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
|
if(num_subrows == 0 || num_subcols == 0){ // in case we randomized to
|
||||||
// empty matrix, at least make it correct
|
// empty matrix, at least make it correct
|
||||||
num_subrows = 0;
|
num_subrows = 0;
|
||||||
|
@ -2926,7 +2949,7 @@ template<class Real> static void MatrixUnitTest() {
|
||||||
UnitTestSplitRadixComplexFft2<Real>();
|
UnitTestSplitRadixComplexFft2<Real>();
|
||||||
UnitTestDct<Real>();
|
UnitTestDct<Real>();
|
||||||
UnitTestRealFft<Real>();
|
UnitTestRealFft<Real>();
|
||||||
KALDI_LOG << " Point C";
|
KALDI_LOG << " Point C";
|
||||||
UnitTestSplitRadixRealFft<Real>();
|
UnitTestSplitRadixRealFft<Real>();
|
||||||
UnitTestSvd<Real>();
|
UnitTestSvd<Real>();
|
||||||
UnitTestSvdNodestroy<Real>();
|
UnitTestSvdNodestroy<Real>();
|
||||||
|
@ -2934,14 +2957,14 @@ template<class Real> static void MatrixUnitTest() {
|
||||||
UnitTestSpAddVec<Real, float>();
|
UnitTestSpAddVec<Real, float>();
|
||||||
UnitTestSpAddVec<Real, double>();
|
UnitTestSpAddVec<Real, double>();
|
||||||
UnitTestSpInvert<Real>();
|
UnitTestSpInvert<Real>();
|
||||||
KALDI_LOG << " Point D";
|
KALDI_LOG << " Point D";
|
||||||
UnitTestTpInvert<Real>();
|
UnitTestTpInvert<Real>();
|
||||||
UnitTestIo<Real>();
|
UnitTestIo<Real>();
|
||||||
UnitTestIoCross<Real>();
|
UnitTestIoCross<Real>();
|
||||||
UnitTestHtkIo<Real>();
|
UnitTestHtkIo<Real>();
|
||||||
UnitTestScale<Real>();
|
UnitTestScale<Real>();
|
||||||
UnitTestTrace<Real>();
|
UnitTestTrace<Real>();
|
||||||
KALDI_LOG << " Point E";
|
KALDI_LOG << " Point E";
|
||||||
CholeskyUnitTestTr<Real>();
|
CholeskyUnitTestTr<Real>();
|
||||||
UnitTestAxpy<Real>();
|
UnitTestAxpy<Real>();
|
||||||
UnitTestSimple<Real>();
|
UnitTestSimple<Real>();
|
||||||
|
@ -2954,7 +2977,7 @@ template<class Real> static void MatrixUnitTest() {
|
||||||
// UnitTestSvdVariants<Real>();
|
// UnitTestSvdVariants<Real>();
|
||||||
UnitTestPower<Real>();
|
UnitTestPower<Real>();
|
||||||
UnitTestDeterminant<Real>();
|
UnitTestDeterminant<Real>();
|
||||||
KALDI_LOG << " Point F";
|
KALDI_LOG << " Point F";
|
||||||
UnitTestDeterminantSign<Real>();
|
UnitTestDeterminantSign<Real>();
|
||||||
UnitTestSger<Real>();
|
UnitTestSger<Real>();
|
||||||
UnitTestAddOuterProductPlusMinus<Real>();
|
UnitTestAddOuterProductPlusMinus<Real>();
|
||||||
|
@ -2965,13 +2988,14 @@ template<class Real> static void MatrixUnitTest() {
|
||||||
UnitTestSherman<Real>();
|
UnitTestSherman<Real>();
|
||||||
UnitTestSpVec<Real>();
|
UnitTestSpVec<Real>();
|
||||||
UnitTestLimitCondInvert<Real>();
|
UnitTestLimitCondInvert<Real>();
|
||||||
KALDI_LOG << " Point G";
|
KALDI_LOG << " Point G";
|
||||||
UnitTestFloorChol<Real>();
|
UnitTestFloorChol<Real>();
|
||||||
UnitTestFloorUnit<Real>();
|
UnitTestFloorUnit<Real>();
|
||||||
UnitTestLimitCond<Real>();
|
UnitTestLimitCond<Real>();
|
||||||
UnitTestMat2Vec<Real>();
|
UnitTestMat2Vec<Real>();
|
||||||
UnitTestSpLogExp<Real>();
|
UnitTestSpLogExp<Real>();
|
||||||
KALDI_LOG << " Point H";
|
KALDI_LOG << " Point H";
|
||||||
|
UnitTestCopyRowsAndCols<Real>();
|
||||||
UnitTestSpliceRows<Real>();
|
UnitTestSpliceRows<Real>();
|
||||||
UnitTestAddSp<Real>();
|
UnitTestAddSp<Real>();
|
||||||
UnitTestRemoveRow<Real>();
|
UnitTestRemoveRow<Real>();
|
||||||
|
@ -2982,12 +3006,12 @@ template<class Real> static void MatrixUnitTest() {
|
||||||
UnitTestSimpleForMat<Real>();
|
UnitTestSimpleForMat<Real>();
|
||||||
UnitTestNorm<Real>();
|
UnitTestNorm<Real>();
|
||||||
UnitTestMul<Real>();
|
UnitTestMul<Real>();
|
||||||
KALDI_LOG << " Point I";
|
KALDI_LOG << " Point I";
|
||||||
UnitTestSolve<Real>();
|
UnitTestSolve<Real>();
|
||||||
UnitTestMaxMin<Real>();
|
UnitTestMaxMin<Real>();
|
||||||
UnitTestInnerProd<Real>();
|
UnitTestInnerProd<Real>();
|
||||||
UnitTestScaleDiag<Real>();
|
UnitTestScaleDiag<Real>();
|
||||||
KALDI_LOG << " Point J";
|
KALDI_LOG << " Point J";
|
||||||
UnitTestTraceSpSpLower<Real>();
|
UnitTestTraceSpSpLower<Real>();
|
||||||
UnitTestTranspose<Real>();
|
UnitTestTranspose<Real>();
|
||||||
UnitTestAddVecToRows<Real>();
|
UnitTestAddVecToRows<Real>();
|
||||||
|
|
|
@ -72,7 +72,7 @@ class BiasedLinearity : public UpdatableComponent {
|
||||||
// compute gradient
|
// compute gradient
|
||||||
linearity_corr_.AddMatMat(1.0, err, kTrans, input, kNoTrans, momentum_);
|
linearity_corr_.AddMatMat(1.0, err, kTrans, input, kNoTrans, momentum_);
|
||||||
bias_corr_.Scale(momentum_);
|
bias_corr_.Scale(momentum_);
|
||||||
bias_corr_.AddRowSumMat(err);
|
bias_corr_.AddRowSumMat(1.0, err);
|
||||||
// l2 regularization
|
// l2 regularization
|
||||||
if (l2_penalty_ != 0.0) {
|
if (l2_penalty_ != 0.0) {
|
||||||
linearity_.AddMat(-learn_rate_*l2_penalty_*input.NumRows(), linearity_);
|
linearity_.AddMat(-learn_rate_*l2_penalty_*input.NumRows(), linearity_);
|
||||||
|
|
|
@ -128,7 +128,7 @@ void Rnnlm::Backpropagate(const MatrixBase<BaseFloat> &in_err) {
|
||||||
// update layer2
|
// update layer2
|
||||||
W2_.AddMatMat(-learn_rate_, h2_, kTrans, in_err, kNoTrans, 1.0);
|
W2_.AddMatMat(-learn_rate_, h2_, kTrans, in_err, kNoTrans, 1.0);
|
||||||
b2_corr_.SetZero();
|
b2_corr_.SetZero();
|
||||||
b2_corr_.AddRowSumMat(in_err);
|
b2_corr_.AddRowSumMat(1.0, in_err);
|
||||||
b2_.AddVec(-learn_rate_, b2_corr_);
|
b2_.AddVec(-learn_rate_, b2_corr_);
|
||||||
|
|
||||||
// LAYER1
|
// LAYER1
|
||||||
|
@ -136,7 +136,7 @@ void Rnnlm::Backpropagate(const MatrixBase<BaseFloat> &in_err) {
|
||||||
U1_corr_.SetZero();
|
U1_corr_.SetZero();
|
||||||
b1_corr_.SetZero();
|
b1_corr_.SetZero();
|
||||||
// accumulate gradient for layer1
|
// accumulate gradient for layer1
|
||||||
b1_corr_.AddRowSumMat(e2_);
|
b1_corr_.AddRowSumMat(1.0, e2_);
|
||||||
for(int32 r=0; r<e2_.NumRows();r++) {
|
for(int32 r=0; r<e2_.NumRows();r++) {
|
||||||
V1_corr_.Row(in_seq_[r]-1).AddVec(1.0, e2_.Row(r));
|
V1_corr_.Row(in_seq_[r]-1).AddVec(1.0, e2_.Row(r));
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ void Rnnlm::Backpropagate(const MatrixBase<BaseFloat> &in_err) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// accumulate graidient
|
// accumulate graidient
|
||||||
b1_corr_.AddRowSumMat(E);
|
b1_corr_.AddRowSumMat(1.0, E);
|
||||||
for(int32 r=0; r<E.NumRows();r++) {
|
for(int32 r=0; r<E.NumRows();r++) {
|
||||||
// :TODO: IS IT CORRECT?
|
// :TODO: IS IT CORRECT?
|
||||||
// V1_corr_.Row(in_seq_[r+step]).AddVec(1.0,E.Row(r));
|
// V1_corr_.Row(in_seq_[r+step]).AddVec(1.0,E.Row(r));
|
||||||
|
|
|
@ -273,8 +273,8 @@ double EbwAmSgmmUpdater::UpdateM(const MleAmSgmmAccs &num_accs,
|
||||||
|
|
||||||
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
||||||
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
||||||
num_count_vec.AddRowSumMat(num_accs.gamma_[j]);
|
num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]);
|
||||||
den_count_vec.AddRowSumMat(den_accs.gamma_[j]);
|
den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32 i = 0; i < I; i++) {
|
for (int32 i = 0; i < I; i++) {
|
||||||
|
@ -363,8 +363,8 @@ double EbwAmSgmmUpdater::UpdateWParallel(const MleAmSgmmAccs &num_accs,
|
||||||
|
|
||||||
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
||||||
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
||||||
num_count_vec.AddRowSumMat(num_accs.gamma_[j]);
|
num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]);
|
||||||
den_count_vec.AddRowSumMat(den_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),
|
// 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<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
||||||
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
||||||
num_count_vec.AddRowSumMat(num_accs.gamma_[j]);
|
num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]);
|
||||||
den_count_vec.AddRowSumMat(den_accs.gamma_[j]);
|
den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32 i = 0; i < I; i++) {
|
for (int32 i = 0; i < I; i++) {
|
||||||
|
@ -522,8 +522,8 @@ double EbwAmSgmmUpdater::UpdateVars(const MleAmSgmmAccs &num_accs,
|
||||||
|
|
||||||
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
Vector<double> num_count_vec(I), den_count_vec(I), impr_vec(I);
|
||||||
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
for (int32 j = 0; j < num_accs.num_states_; j++) {
|
||||||
num_count_vec.AddRowSumMat(num_accs.gamma_[j]);
|
num_count_vec.AddRowSumMat(1.0, num_accs.gamma_[j]);
|
||||||
den_count_vec.AddRowSumMat(den_accs.gamma_[j]);
|
den_count_vec.AddRowSumMat(1.0, den_accs.gamma_[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32 i = 0; i < I; i++) {
|
for (int32 i = 0; i < I; i++) {
|
||||||
|
|
|
@ -538,7 +538,7 @@ BaseFloat AmSgmm2::LogLikelihood(const Sgmm2PerFrameDerivedVars &per_frame_vars,
|
||||||
substate_cache.remaining_log_like = max;
|
substate_cache.remaining_log_like = max;
|
||||||
int32 num_substates = loglikes.NumCols();
|
int32 num_substates = loglikes.NumCols();
|
||||||
substate_cache.likes.Resize(num_substates); // zeroes it.
|
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 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)
|
// each substate index. You have to multiply by exp(remaining_log_like)
|
||||||
// to get a real likelihood.
|
// to get a real likelihood.
|
||||||
|
@ -613,7 +613,7 @@ void AmSgmm2::SplitSubstatesInGroup(const Vector<BaseFloat> &pdf_occupancies,
|
||||||
int32 split_m; // substate to split.
|
int32 split_m; // substate to split.
|
||||||
{
|
{
|
||||||
Vector<BaseFloat> substate_count(tgt_M);
|
Vector<BaseFloat> substate_count(tgt_M);
|
||||||
substate_count.AddRowSumMat(c_j);
|
substate_count.AddRowSumMat(1.0, c_j);
|
||||||
BaseFloat *data = substate_count.Data();
|
BaseFloat *data = substate_count.Data();
|
||||||
split_m = std::max_element(data, data+cur_M) - data;
|
split_m = std::max_element(data, data+cur_M) - data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,10 +50,10 @@ void EbwAmSgmm2Updater::Update(const MleAmSgmm2Accs &num_accs,
|
||||||
|
|
||||||
Vector<double> gamma_num(num_accs.num_gaussians_);
|
Vector<double> gamma_num(num_accs.num_gaussians_);
|
||||||
for (int32 j1 = 0; j1 < num_accs.num_groups_; j1++)
|
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<double> gamma_den(den_accs.num_gaussians_);
|
Vector<double> gamma_den(den_accs.num_gaussians_);
|
||||||
for (int32 j1 = 0; j1 < den_accs.num_groups_; j1++)
|
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;
|
BaseFloat tot_impr = 0.0;
|
||||||
|
|
||||||
|
|
|
@ -617,7 +617,7 @@ void MleAmSgmm2Updater::Update(const MleAmSgmm2Accs &accs,
|
||||||
|
|
||||||
Vector<double> gamma_i(accs.num_gaussians_);
|
Vector<double> gamma_i(accs.num_gaussians_);
|
||||||
for (int32 j1 = 0; j1 < accs.num_groups_; j1++)
|
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.
|
// accs.gamma_[j1], to gamma_i.
|
||||||
|
|
||||||
if (flags & kSgmmPhoneProjections)
|
if (flags & kSgmmPhoneProjections)
|
||||||
|
|
|
@ -63,7 +63,7 @@ void LdaEstimate::Estimate(int32 target_dim,
|
||||||
// total covariance
|
// total covariance
|
||||||
double sum = zero_acc_.Sum();
|
double sum = zero_acc_.Sum();
|
||||||
Vector<double> total_mean(dim);
|
Vector<double> total_mean(dim);
|
||||||
total_mean.AddRowSumMat(first_acc_);
|
total_mean.AddRowSumMat(1.0, first_acc_);
|
||||||
total_mean.Scale(1/sum);
|
total_mean.Scale(1/sum);
|
||||||
SpMatrix<double> total_covar(total_second_acc_);
|
SpMatrix<double> total_covar(total_second_acc_);
|
||||||
total_covar.Scale(1/sum);
|
total_covar.Scale(1/sum);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче