This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<BaseFloat>, or a CompressedMatrix, or a SparseMatrix<BaseFloat>. More...
#include <sparse-matrix.h>
Public Member Functions | |
GeneralMatrixType | Type () const |
Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix. More... | |
void | Compress () |
void | Uncompress () |
void | Write (std::ostream &os, bool binary) const |
void | Read (std::istream &is, bool binary) |
Note: if you write a compressed matrix in text form, it will be read as a regular full matrix. More... | |
const SparseMatrix< BaseFloat > & | GetSparseMatrix () const |
Returns the contents as a SparseMatrix. More... | |
void | SwapSparseMatrix (SparseMatrix< BaseFloat > *smat) |
Swaps the with the given SparseMatrix. More... | |
const CompressedMatrix & | GetCompressedMatrix () const |
Returns the contents as a compressed matrix. More... | |
void | SwapCompressedMatrix (CompressedMatrix *cmat) |
Swaps the with the given CompressedMatrix. More... | |
const Matrix< BaseFloat > & | GetFullMatrix () const |
Returns the contents as a Matrix<BaseFloat>. More... | |
void | GetMatrix (Matrix< BaseFloat > *mat) const |
Outputs the contents as a matrix. More... | |
void | SwapFullMatrix (Matrix< BaseFloat > *mat) |
Swaps the with the given Matrix. More... | |
void | CopyToMat (MatrixBase< BaseFloat > *mat, MatrixTransposeType trans=kNoTrans) const |
Copies contents, regardless of type, to "mat", which must be correctly sized. More... | |
void | CopyToMat (CuMatrixBase< BaseFloat > *cu_mat, MatrixTransposeType trans=kNoTrans) const |
Copies contents, regardless of type, to "cu_mat", which must be correctly sized. More... | |
void | AddToMat (BaseFloat alpha, MatrixBase< BaseFloat > *mat, MatrixTransposeType trans=kNoTrans) const |
Adds alpha times *this to mat. More... | |
void | AddToMat (BaseFloat alpha, CuMatrixBase< BaseFloat > *cu_mat, MatrixTransposeType trans=kNoTrans) const |
Adds alpha times *this to cu_mat. More... | |
void | Scale (BaseFloat alpha) |
Scale each element of matrix by alpha. More... | |
GeneralMatrix & | operator= (const MatrixBase< BaseFloat > &mat) |
Assignment from regular matrix. More... | |
GeneralMatrix & | operator= (const CompressedMatrix &mat) |
Assignment from compressed matrix. More... | |
GeneralMatrix & | operator= (const SparseMatrix< BaseFloat > &smat) |
Assignment from SparseMatrix<BaseFloat> More... | |
MatrixIndexT | NumRows () const |
MatrixIndexT | NumCols () const |
GeneralMatrix (const MatrixBase< BaseFloat > &mat) | |
GeneralMatrix (const CompressedMatrix &cmat) | |
GeneralMatrix (const SparseMatrix< BaseFloat > &smat) | |
GeneralMatrix () | |
GeneralMatrix & | operator= (const GeneralMatrix &other) |
GeneralMatrix (const GeneralMatrix &other) | |
void | Clear () |
void | Swap (GeneralMatrix *other) |
Private Attributes | |
Matrix< BaseFloat > | mat_ |
CompressedMatrix | cmat_ |
SparseMatrix< BaseFloat > | smat_ |
This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<BaseFloat>, or a CompressedMatrix, or a SparseMatrix<BaseFloat>.
It handles the I/O for you, i.e. you read and write a single object type. It is useful for neural-net training targets which might be sparse or not, and might be compressed or not.
Definition at line 282 of file sparse-matrix.h.
|
inlineexplicit |
Definition at line 365 of file sparse-matrix.h.
|
inlineexplicit |
Definition at line 367 of file sparse-matrix.h.
|
inlineexplicit |
Definition at line 369 of file sparse-matrix.h.
|
inline |
Definition at line 371 of file sparse-matrix.h.
References SparseVector< Real >::operator=().
|
inline |
Definition at line 375 of file sparse-matrix.h.
References SparseVector< Real >::Swap().
void AddToMat | ( | BaseFloat | alpha, |
MatrixBase< BaseFloat > * | mat, | ||
MatrixTransposeType | trans = kNoTrans |
||
) | const |
Adds alpha times *this to mat.
Definition at line 1145 of file sparse-matrix.cc.
References MatrixBase< Real >::AddMat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, and kaldi::kSparseMatrix.
void AddToMat | ( | BaseFloat | alpha, |
CuMatrixBase< BaseFloat > * | cu_mat, | ||
MatrixTransposeType | trans = kNoTrans |
||
) | const |
Adds alpha times *this to cu_mat.
Implemented in ../cudamatrix/cu-sparse-matrix.cc
Definition at line 679 of file cu-sparse-matrix.cc.
References CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::AddSmat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kSparseMatrix, and CuMatrixBase< Real >::Mat().
void Clear | ( | ) |
Definition at line 740 of file sparse-matrix.cc.
Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractObjectRange(), kaldi::FilterGeneralMatrixRows(), and kaldi::UnitTestGeneralMatrix().
void Compress | ( | ) |
Definition at line 802 of file sparse-matrix.cc.
Referenced by kaldi::nnet3::GenerateSimpleNnetTrainingExample(), and kaldi::UnitTestGeneralMatrix().
void CopyToMat | ( | MatrixBase< BaseFloat > * | mat, |
MatrixTransposeType | trans = kNoTrans |
||
) | const |
Copies contents, regardless of type, to "mat", which must be correctly sized.
See also GetMatrix(), which will size its output for you.
Definition at line 831 of file sparse-matrix.cc.
References MatrixBase< Real >::CopyFromMat(), KALDI_ASSERT, and MatrixBase< Real >::NumRows().
Referenced by kaldi::AppendGeneralMatrixRows().
void CopyToMat | ( | CuMatrixBase< BaseFloat > * | cu_mat, |
MatrixTransposeType | trans = kNoTrans |
||
) | const |
Copies contents, regardless of type, to "cu_mat", which must be correctly sized.
Implemented in ../cudamatrix/cu-sparse-matrix.cc
Definition at line 582 of file cu-sparse-matrix.cc.
References CuMatrixBase< Real >::CopyFromMat(), CuSparseMatrix< Real >::CopyToMat(), KALDI_ERR, kaldi::kCompressedMatrix, kaldi::kFullMatrix, kaldi::kNoTrans, kaldi::kSparseMatrix, kaldi::kTrans, CuMatrixBase< Real >::Mat(), and CuMatrix< Real >::Swap().
const CompressedMatrix & GetCompressedMatrix | ( | ) | const |
Returns the contents as a compressed matrix.
This will only work if Type() returns kCompressedMatrix, or NumRows() == 0; otherwise it will crash.
Definition at line 872 of file sparse-matrix.cc.
References KALDI_ERR.
Referenced by kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().
Returns the contents as a Matrix<BaseFloat>.
This will only work if Type() returns kFullMatrix, or NumRows() == 0; otherwise it will crash.
Definition at line 878 of file sparse-matrix.cc.
References KALDI_ERR.
Referenced by kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().
Outputs the contents as a matrix.
This will work regardless of Type(). Sizes its output, unlike CopyToMat().
Definition at line 817 of file sparse-matrix.cc.
References kaldi::kUndefined, and Matrix< Real >::Resize().
Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::nnet3::ExampleApproxEqual(), kaldi::ExtractObjectRange(), NnetIo::operator==(), kaldi::nnet3::PerturbImageInNnetExample(), and kaldi::UnitTestGeneralMatrix().
const SparseMatrix< BaseFloat > & GetSparseMatrix | ( | ) | const |
Returns the contents as a SparseMatrix.
This will only work if Type() returns kSparseMatrix, or NumRows() == 0; otherwise it will crash.
Definition at line 854 of file sparse-matrix.cc.
References KALDI_ERR.
Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().
MatrixIndexT NumCols | ( | ) | const |
Definition at line 791 of file sparse-matrix.cc.
Referenced by NnetComputer::AcceptInputs(), NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::AppendGeneralMatrixRows(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), kaldi::nnet3::GetIoSizes(), NnetIoStructureCompare::operator()(), NnetIo::operator==(), NnetComputeProb::ProcessOutputs(), and kaldi::UnitTestGeneralMatrix().
MatrixIndexT NumRows | ( | ) | const |
Definition at line 781 of file sparse-matrix.cc.
Referenced by NnetComputer::AcceptInputs(), NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::AppendGeneralMatrixRows(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), kaldi::nnet3::FilterExample(), kaldi::FilterGeneralMatrixRows(), kaldi::nnet3::GetIoSizes(), main(), NnetIo::NnetIo(), NnetIoStructureCompare::operator()(), NnetIo::operator==(), kaldi::nnet3::ProcessFile(), kaldi::UnitTestGeneralMatrix(), and NnetIo::Write().
GeneralMatrix & operator= | ( | const MatrixBase< BaseFloat > & | mat | ) |
GeneralMatrix & operator= | ( | const CompressedMatrix & | mat | ) |
GeneralMatrix & operator= | ( | const SparseMatrix< BaseFloat > & | smat | ) |
GeneralMatrix & operator= | ( | const GeneralMatrix & | other | ) |
Definition at line 764 of file sparse-matrix.cc.
References GeneralMatrix::cmat_, GeneralMatrix::mat_, and GeneralMatrix::smat_.
void Read | ( | std::istream & | is, |
bool | binary | ||
) |
Note: if you write a compressed matrix in text form, it will be read as a regular full matrix.
Definition at line 901 of file sparse-matrix.cc.
Referenced by NnetIo::Read(), and kaldi::UnitTestGeneralMatrix().
void Scale | ( | BaseFloat | alpha | ) |
Scale each element of matrix by alpha.
Definition at line 844 of file sparse-matrix.cc.
void Swap | ( | GeneralMatrix * | other | ) |
Definition at line 1226 of file sparse-matrix.cc.
References GeneralMatrix::cmat_, GeneralMatrix::mat_, and GeneralMatrix::smat_.
Referenced by NnetIo::Swap().
void SwapCompressedMatrix | ( | CompressedMatrix * | cmat | ) |
Swaps the with the given CompressedMatrix.
This will only work if Type() returns kCompressedMatrix, or NumRows() == 0.
Definition at line 866 of file sparse-matrix.cc.
References KALDI_ERR, and CompressedMatrix::Swap().
Referenced by kaldi::ExtractRowRangeWithPadding().
Swaps the with the given Matrix.
This will only work if Type() returns kFullMatrix, or NumRows() == 0.
Definition at line 885 of file sparse-matrix.cc.
References KALDI_ERR, and Matrix< Real >::Swap().
Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().
void SwapSparseMatrix | ( | SparseMatrix< BaseFloat > * | smat | ) |
Swaps the with the given SparseMatrix.
This will only work if Type() returns kSparseMatrix, or NumRows() == 0.
Definition at line 860 of file sparse-matrix.cc.
References KALDI_ERR, and SparseMatrix< Real >::Swap().
Referenced by kaldi::AppendGeneralMatrixRows(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterGeneralMatrixRows(), and kaldi::UnitTestGeneralMatrix().
GeneralMatrixType Type | ( | ) | const |
Returns the type of the matrix: kSparseMatrix, kCompressedMatrix or kFullMatrix.
If this matrix is empty, returns kFullMatrix.
Definition at line 772 of file sparse-matrix.cc.
References kaldi::kCompressedMatrix, kaldi::kFullMatrix, and kaldi::kSparseMatrix.
Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), kaldi::nnet3::ComputeObjectiveFunction(), CuMatrixBase< float >::CopyFromGeneralMat(), kaldi::ExtractObjectRange(), kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterGeneralMatrixRows().
void Uncompress | ( | ) |
Definition at line 809 of file sparse-matrix.cc.
References kaldi::kUndefined.
void Write | ( | std::ostream & | os, |
bool | binary | ||
) | const |
Definition at line 891 of file sparse-matrix.cc.
Referenced by kaldi::UnitTestGeneralMatrix(), and NnetIo::Write().
|
private |
Definition at line 385 of file sparse-matrix.h.
Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().
Definition at line 384 of file sparse-matrix.h.
Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().
|
private |
Definition at line 386 of file sparse-matrix.h.
Referenced by GeneralMatrix::operator=(), and GeneralMatrix::Swap().