All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SparseMatrix< Real > Class Template Reference

#include <matrix-common.h>

Collaboration diagram for SparseMatrix< Real >:

Public Member Functions

MatrixIndexT NumRows () const
 
MatrixIndexT NumCols () const
 
MatrixIndexT NumElements () const
 
Real Sum () const
 
Real FrobeniusNorm () const
 
template<class OtherReal >
void CopyToMat (MatrixBase< OtherReal > *other, MatrixTransposeType t=kNoTrans) const
 
void CopyElementsToVec (VectorBase< Real > *other) const
 Copies the values of all the elements in SparseMatrix into a VectorBase object. More...
 
template<class OtherReal >
void CopyFromSmat (const SparseMatrix< OtherReal > &other)
 Copies data from another sparse matrix. More...
 
void AddToMat (BaseFloat alpha, MatrixBase< Real > *other, MatrixTransposeType t=kNoTrans) const
 Does *other = *other + alpha * *this. More...
 
SparseMatrix< Real > & operator= (const SparseMatrix< Real > &other)
 
 SparseMatrix (const SparseMatrix< Real > &other)
 
void Swap (SparseMatrix< Real > *other)
 
SparseVector< Real > * Data ()
 
const SparseVector< Real > * Data () const
 
 SparseMatrix (int32 dim, const std::vector< std::vector< std::pair< MatrixIndexT, Real > > > &pairs)
 
void SetRandn (BaseFloat zero_prob)
 Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed- mostly for purposes of testing. More...
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &os, bool binary)
 
const SparseVector< Real > & Row (MatrixIndexT r) const
 
void SetRow (int32 r, const SparseVector< Real > &vec)
 Sets row r to "vec"; makes sure it has the correct dimension. More...
 
void AppendSparseMatrixRows (std::vector< SparseMatrix< Real > > *inputs)
 Sets *this to all the rows of *inputs appended together; this function is destructive of the inputs. More...
 
 SparseMatrix ()
 
 SparseMatrix (int32 num_rows, int32 num_cols)
 
void Resize (MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
 Resizes the matrix; analogous to Matrix::Resize(). More...
 
void Scale (Real alpha)
 Scale all elements in sparse matrix. More...
 

Private Attributes

std::vector< SparseVector< Real > > rows_
 

Detailed Description

template<typename Real>
class kaldi::SparseMatrix< Real >

Definition at line 63 of file matrix-common.h.

Constructor & Destructor Documentation

SparseMatrix ( const SparseMatrix< Real > &  other)
inline

Definition at line 155 of file sparse-matrix.h.

155 { *this = other; }
SparseMatrix ( int32  dim,
const std::vector< std::vector< std::pair< MatrixIndexT, Real > > > &  pairs 
)

Definition at line 547 of file sparse-matrix.cc.

References SparseMatrix< Real >::rows_.

549  :
550  rows_(pairs.size()) {
551  MatrixIndexT num_rows = pairs.size();
552  for (MatrixIndexT row = 0; row < num_rows; row++) {
553  SparseVector<Real> svec(dim, pairs[row]);
554  rows_[row].Swap(&svec);
555  }
556 }
int32 MatrixIndexT
Definition: matrix-common.h:96
std::vector< SparseVector< Real > > rows_
SparseMatrix ( )
inline

Definition at line 193 of file sparse-matrix.h.

193 { }
SparseMatrix ( int32  num_rows,
int32  num_cols 
)
inline

Definition at line 195 of file sparse-matrix.h.

195 { Resize(num_rows, num_cols); }
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
Resizes the matrix; analogous to Matrix::Resize().

Member Function Documentation

void AddToMat ( BaseFloat  alpha,
MatrixBase< Real > *  other,
MatrixTransposeType  t = kNoTrans 
) const

Does *other = *other + alpha * *this.

Definition at line 475 of file sparse-matrix.cc.

References MatrixBase< Real >::Data(), SparseVector< Real >::Data(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), SparseVector< Real >::NumElements(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

Referenced by GeneralMatrix::AddToMat(), and kaldi::UnitTestSparseMatrixAddToMat().

477  {
478  if (trans == kNoTrans) {
479  MatrixIndexT num_rows = rows_.size();
480  KALDI_ASSERT(other->NumRows() == num_rows);
481  for (MatrixIndexT i = 0; i < num_rows; i++) {
482  SubVector<Real> vec(*other, i);
483  rows_[i].AddToVec(alpha, &vec);
484  }
485  } else {
486  Real *other_col_data = other->Data();
487  MatrixIndexT other_stride = other->Stride(),
488  num_rows = NumRows(), num_cols = NumCols();
489  KALDI_ASSERT(num_rows == other->NumCols() && num_cols == other->NumRows());
490  for (MatrixIndexT row = 0; row < num_rows; row++, other_col_data++) {
491  const SparseVector<Real> &svec = rows_[row];
492  MatrixIndexT num_elems = svec.NumElements();
493  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
494  for (MatrixIndexT e = 0; e < num_elems; e++)
495  other_col_data[sdata[e].first * other_stride] +=
496  alpha * sdata[e].second;
497  }
498  }
499 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
void AppendSparseMatrixRows ( std::vector< SparseMatrix< Real > > *  inputs)

Sets *this to all the rows of *inputs appended together; this function is destructive of the inputs.

Requires, obviously, that the inputs all have the same dimension (although some may be empty).

Definition at line 585 of file sparse-matrix.cc.

References KALDI_ASSERT, and KALDI_ERR.

Referenced by kaldi::AppendGeneralMatrixRows().

586  {
587  rows_.clear();
588  size_t num_rows = 0;
589  typename std::vector<SparseMatrix<Real> >::iterator
590  input_iter = inputs->begin(),
591  input_end = inputs->end();
592  for (; input_iter != input_end; ++input_iter)
593  num_rows += input_iter->rows_.size();
594  rows_.resize(num_rows);
595  typename std::vector<SparseVector<Real> >::iterator
596  row_iter = rows_.begin(),
597  row_end = rows_.end();
598  for (input_iter = inputs->begin(); input_iter != input_end; ++input_iter) {
599  typename std::vector<SparseVector<Real> >::iterator
600  input_row_iter = input_iter->rows_.begin(),
601  input_row_end = input_iter->rows_.end();
602  for (; input_row_iter != input_row_end; ++input_row_iter, ++row_iter)
603  row_iter->Swap(&(*input_row_iter));
604  }
605  KALDI_ASSERT(row_iter == row_end);
606  int32 num_cols = NumCols();
607  for (row_iter = rows_.begin(); row_iter != row_end; ++row_iter) {
608  if (row_iter->Dim() != num_cols)
609  KALDI_ERR << "Appending rows with inconsistent dimensions, "
610  << row_iter->Dim() << " vs. " << num_cols;
611  }
612  inputs->clear();
613 }
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
void CopyElementsToVec ( VectorBase< Real > *  other) const

Copies the values of all the elements in SparseMatrix into a VectorBase object.

Definition at line 390 of file sparse-matrix.cc.

References VectorBase< Real >::Data(), VectorBase< Real >::Dim(), rnnlm::i, rnnlm::j, and KALDI_ASSERT.

390  {
391  KALDI_ASSERT(other->Dim() == NumElements());
392  Real *dst_data = other->Data();
393  int32 dst_index = 0;
394  for (int32 i = 0; i < rows_.size(); ++i) {
395  for (int32 j = 0; j < rows_[i].NumElements(); ++j) {
396  dst_data[dst_index] =
397  static_cast<Real>(rows_[i].GetElement(j).second);
398  dst_index++;
399  }
400  }
401 }
MatrixIndexT NumElements() const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
template void CopyFromSmat ( const SparseMatrix< OtherReal > &  other)

Copies data from another sparse matrix.

We will add the transpose option later when it is necessary.

Definition at line 405 of file sparse-matrix.cc.

References SparseMatrix< Real >::NumRows(), and SparseMatrix< Real >::Row().

Referenced by CuSparseMatrix< Real >::CopyToSmat().

405  {
406  rows_.resize(other.NumRows());
407  if (rows_.size() == 0) return;
408  for (int32 r = 0; r < rows_.size(); ++r) {
409  rows_[r].CopyFromSvec(other.Row(r));
410  }
411 }
std::vector< SparseVector< Real > > rows_
template void CopyToMat ( MatrixBase< OtherReal > *  other,
MatrixTransposeType  t = kNoTrans 
) const

Definition at line 351 of file sparse-matrix.cc.

References MatrixBase< Real >::Data(), SparseVector< Real >::Data(), rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), SparseVector< Real >::NumElements(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::SetZero(), and MatrixBase< Real >::Stride().

Referenced by CuMatrixBase< Real >::CopyFromGeneralMat(), GeneralMatrix::CopyToMat(), GeneralMatrix::GetMatrix(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestSparseMatrixAddToMat(), kaldi::UnitTestSparseMatrixFrobeniusNorm(), kaldi::UnitTestSparseMatrixSum(), and kaldi::UnitTestSparseMatrixTraceMatSmat().

352  {
353  if (trans == kNoTrans) {
354  MatrixIndexT num_rows = rows_.size();
355  KALDI_ASSERT(other->NumRows() == num_rows);
356  for (MatrixIndexT i = 0; i < num_rows; i++) {
357  SubVector<OtherReal> vec(*other, i);
358  rows_[i].CopyElementsToVec(&vec);
359  }
360  } else {
361  OtherReal *other_col_data = other->Data();
362  MatrixIndexT other_stride = other->Stride(),
363  num_rows = NumRows(), num_cols = NumCols();
364  KALDI_ASSERT(num_rows == other->NumCols() && num_cols == other->NumRows());
365  other->SetZero();
366  for (MatrixIndexT row = 0; row < num_rows; row++, other_col_data++) {
367  const SparseVector<Real> &svec = rows_[row];
368  MatrixIndexT num_elems = svec.NumElements();
369  const std::pair<MatrixIndexT, Real> *sdata = svec.Data();
370  for (MatrixIndexT e = 0; e < num_elems; e++)
371  other_col_data[sdata[e].first * other_stride] = sdata[e].second;
372  }
373  }
374 }
MatrixIndexT NumRows() const
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
SparseVector< Real > * Data ( )

Definition at line 313 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::CopyFromSmat().

313  {
314  if (rows_.empty())
315  return NULL;
316  else
317  return rows_.data();
318 }
std::vector< SparseVector< Real > > rows_
const SparseVector< Real > * Data ( ) const

Definition at line 321 of file sparse-matrix.cc.

321  {
322  if (rows_.empty())
323  return NULL;
324  else
325  return rows_.data();
326 }
std::vector< SparseVector< Real > > rows_
Real FrobeniusNorm ( ) const

Definition at line 338 of file sparse-matrix.cc.

References rnnlm::i, and rnnlm::j.

Referenced by kaldi::UnitTestSparseMatrixFrobeniusNorm().

338  {
339  Real squared_sum = 0;
340  for (int32 i = 0; i < rows_.size(); ++i) {
341  const std::pair<MatrixIndexT, Real> *row_data = rows_[i].Data();
342  for (int32 j = 0; j < rows_[i].NumElements(); ++j) {
343  squared_sum += row_data[j].second * row_data[j].second;
344  }
345  }
346  return std::sqrt(squared_sum);
347 }
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols ( ) const
MatrixIndexT NumElements ( ) const

Definition at line 304 of file sparse-matrix.cc.

References rnnlm::i.

304  {
305  int32 num_elements = 0;
306  for (int32 i = 0; i < rows_.size(); ++i) {
307  num_elements += rows_[i].NumElements();
308  }
309  return num_elements;
310 }
std::vector< SparseVector< Real > > rows_
SparseMatrix< Real > & operator= ( const SparseMatrix< Real > &  other)

Definition at line 535 of file sparse-matrix.cc.

References SparseMatrix< Real >::rows_.

536  {
537  rows_ = other.rows_;
538  return *this;
539 }
std::vector< SparseVector< Real > > rows_
void Read ( std::istream &  os,
bool  binary 
)

Definition at line 446 of file sparse-matrix.cc.

References kaldi::ExpectToken(), KALDI_ASSERT, KALDI_ERR, and kaldi::ReadBasicType().

Referenced by CuSparseMatrix< Real >::Read(), and GeneralMatrix::Read().

446  {
447  if (binary) {
448  ExpectToken(is, binary, "SM");
449  int32 num_rows;
450  ReadBasicType(is, binary, &num_rows);
451  KALDI_ASSERT(num_rows >= 0 && num_rows < 10000000);
452  rows_.resize(num_rows);
453  for (int32 row = 0; row < num_rows; row++)
454  rows_[row].Read(is, binary);
455  } else {
456  std::string str;
457  is >> str;
458  if (str.substr(0, 5) != "rows=")
459  KALDI_ERR << "Reading sparse matrix, expected 'rows=xxx', got " << str;
460  std::string rows_str = str.substr(5, std::string::npos);
461  std::istringstream rows_istr(rows_str);
462  int32 num_rows = -1;
463  rows_istr >> num_rows;
464  if (num_rows < 0 || rows_istr.fail()) {
465  KALDI_ERR << "Reading sparse vector, expected 'rows=[int]', got " << str;
466  }
467  rows_.resize(num_rows);
468  for (int32 row = 0; row < num_rows; row++)
469  rows_[row].Read(is, binary);
470  }
471 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void Read(std::istream &os, bool binary)
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:188
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
void Resize ( MatrixIndexT  rows,
MatrixIndexT  cols,
MatrixResizeType  resize_type = kSetZero 
)

Resizes the matrix; analogous to Matrix::Resize().

resize_type == kUndefined behaves the same as kSetZero.

Definition at line 566 of file sparse-matrix.cc.

References KALDI_ASSERT, kaldi::kCopyData, kaldi::kSetZero, and kaldi::kUndefined.

Referenced by GeneralMatrix::Clear(), kaldi::FilterSparseMatrixRows(), and SparseMatrix< BaseFloat >::SparseMatrix().

568  {
569  KALDI_ASSERT(num_rows >= 0 && num_cols >= 0);
570  if (resize_type == kSetZero || resize_type == kUndefined) {
571  rows_.clear();
572  Resize(num_rows, num_cols, kCopyData);
573  } else {
574  // Assume resize_type == kCopyData from here.
575  int32 old_num_rows = rows_.size(), old_num_cols = NumCols();
576  SparseVector<Real> initializer(num_cols);
577  rows_.resize(num_rows, initializer);
578  if (num_cols != old_num_cols)
579  for (int32 row = 0; row < old_num_rows; row++)
580  rows_[row].Resize(num_cols, kCopyData);
581  }
582 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
MatrixIndexT NumCols() const
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero)
Resizes the matrix; analogous to Matrix::Resize().
const SparseVector< Real > & Row ( MatrixIndexT  r) const

Definition at line 522 of file sparse-matrix.cc.

References KALDI_ASSERT.

Referenced by NnetLdaStatsAccumulator::AccStatsFromOutput(), kaldi::nnet3::ComputeAccuracy(), SparseMatrix< Real >::CopyFromSmat(), kaldi::ExtractRowRangeWithPadding(), kaldi::FilterSparseMatrixRows(), and kaldi::TraceMatSmat().

522  {
523  KALDI_ASSERT(static_cast<size_t>(r) < rows_.size());
524  return rows_[r];
525 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
void Scale ( Real  alpha)

Scale all elements in sparse matrix.

Definition at line 616 of file sparse-matrix.cc.

Referenced by GeneralMatrix::Scale().

616  {
617  MatrixIndexT num_rows = rows_.size();
618  for (MatrixIndexT row = 0; row < num_rows; row++)
619  rows_[row].Scale(alpha);
620 }
void Scale(Real alpha)
Scale all elements in sparse matrix.
int32 MatrixIndexT
Definition: matrix-common.h:96
std::vector< SparseVector< Real > > rows_
void SetRandn ( BaseFloat  zero_prob)

Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob and else normally distributed- mostly for purposes of testing.

Definition at line 559 of file sparse-matrix.cc.

Referenced by CuSparseMatrix< Real >::SetRandn(), kaldi::UnitTestCuSparseMatrixCopyToSmat(), kaldi::UnitTestCuSparseMatrixFrobeniusNorm(), kaldi::UnitTestCuSparseMatrixSum(), kaldi::UnitTestCuSparseMatrixSwap(), kaldi::UnitTestCuSparseMatrixTraceMatSmat(), kaldi::UnitTestGeneralMatrix(), kaldi::UnitTestSparseMatrixAddToMat(), kaldi::UnitTestSparseMatrixFrobeniusNorm(), kaldi::UnitTestSparseMatrixSum(), and kaldi::UnitTestSparseMatrixTraceMatSmat().

559  {
560  MatrixIndexT num_rows = rows_.size();
561  for (MatrixIndexT row = 0; row < num_rows; row++)
562  rows_[row].SetRandn(zero_prob);
563 }
void SetRandn(BaseFloat zero_prob)
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob an...
int32 MatrixIndexT
Definition: matrix-common.h:96
std::vector< SparseVector< Real > > rows_
void SetRow ( int32  r,
const SparseVector< Real > &  vec 
)

Sets row r to "vec"; makes sure it has the correct dimension.

Definition at line 528 of file sparse-matrix.cc.

References SparseVector< Real >::Dim(), and KALDI_ASSERT.

Referenced by kaldi::ExtractRowRangeWithPadding(), and kaldi::FilterSparseMatrixRows().

528  {
529  KALDI_ASSERT(static_cast<size_t>(r) < rows_.size() &&
530  vec.Dim() == rows_[0].Dim());
531  rows_[r] = vec;
532 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< SparseVector< Real > > rows_
Real Sum ( ) const

Definition at line 329 of file sparse-matrix.cc.

References rnnlm::i.

Referenced by kaldi::UnitTestSparseMatrixSum().

329  {
330  Real sum = 0;
331  for (int32 i = 0; i < rows_.size(); ++i) {
332  sum += rows_[i].Sum();
333  }
334  return sum;
335 }
std::vector< SparseVector< Real > > rows_
void Swap ( SparseMatrix< Real > *  other)

Definition at line 542 of file sparse-matrix.cc.

References SparseMatrix< Real >::rows_.

Referenced by GeneralMatrix::Swap(), and GeneralMatrix::SwapSparseMatrix().

542  {
543  rows_.swap(other->rows_);
544 }
std::vector< SparseVector< Real > > rows_
void Write ( std::ostream &  os,
bool  binary 
) const

Definition at line 422 of file sparse-matrix.cc.

References kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by CuSparseMatrix< Real >::Write(), and GeneralMatrix::Write().

422  {
423  if (binary) {
424  // Note: we can use the same marker for float and double SparseMatrix,
425  // because internally we use WriteBasicType and ReadBasicType to read the
426  // floats and doubles, and this will automatically take care of type
427  // conversion.
428  WriteToken(os, binary, "SM");
429  int32 num_rows = rows_.size();
430  WriteBasicType(os, binary, num_rows);
431  for (int32 row = 0; row < num_rows; row++)
432  rows_[row].Write(os, binary);
433  } else {
434  // The format is "rows=10 dim=20 [ 1 0.4 9 1.2 ] dim=20 [ 3 1.7 19 0.6 ] ..
435  // not 100% efficient, but easy to work with, and we can re-use the
436  // read/write code from SparseVector.
437  int32 num_rows = rows_.size();
438  os << "rows=" << num_rows << " ";
439  for (int32 row = 0; row < num_rows; row++)
440  rows_[row].Write(os, binary);
441  os << "\n"; // Might make it a little more readable.
442  }
443 }
void Write(std::ostream &os, bool binary) const
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
std::vector< SparseVector< Real > > rows_
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Member Data Documentation

std::vector<SparseVector<Real> > rows_
private

The documentation for this class was generated from the following files: