All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VectorBase< Real > Singleton Reference

Provides a vector abstraction class. More...

#include <kaldi-vector.h>

Inheritance diagram for VectorBase< Real >:
Collaboration diagram for VectorBase< Real >:

Public Member Functions

void SetZero ()
 Set vector to all zeros. More...
 
bool IsZero (Real cutoff=1.0e-06) const
 Returns true if matrix is all zeros. More...
 
void Set (Real f)
 Set all members of a vector to a specified value. More...
 
void SetRandn ()
 Set vector to random normally-distributed noise. More...
 
MatrixIndexT RandCategorical () const
 This function returns a random index into this vector, chosen with probability proportional to the corresponding element. More...
 
MatrixIndexT Dim () const
 Returns the dimension of the vector. More...
 
MatrixIndexT SizeInBytes () const
 Returns the size in memory of the vector, in bytes. More...
 
Real * Data ()
 Returns a pointer to the start of the vector's data. More...
 
const Real * Data () const
 Returns a pointer to the start of the vector's data (const). More...
 
Real operator() (MatrixIndexT i) const
 Indexing operator (const). More...
 
Real & operator() (MatrixIndexT i)
 Indexing operator (non-const). More...
 
SubVector< Real > Range (const MatrixIndexT o, const MatrixIndexT l)
 Returns a sub-vector of a vector (a range of elements). More...
 
const SubVector< Real > Range (const MatrixIndexT o, const MatrixIndexT l) const
 Returns a const sub-vector of a vector (a range of elements). More...
 
void CopyFromVec (const VectorBase< Real > &v)
 Copy data from another vector (must match own size). More...
 
template<typename OtherReal >
void CopyFromPacked (const PackedMatrix< OtherReal > &M)
 Copy data from a SpMatrix or TpMatrix (must match own size). More...
 
template<typename OtherReal >
void CopyFromVec (const VectorBase< OtherReal > &v)
 Copy data from another vector of different type (double vs. float) More...
 
template<typename OtherReal >
void CopyFromVec (const CuVectorBase< OtherReal > &v)
 Copy from CuVector. This is defined in ../cudamatrix/cu-vector.h. More...
 
void ApplyLog ()
 Apply natural log to all elements. More...
 
void ApplyLogAndCopy (const VectorBase< Real > &v)
 Apply natural log to another vector and put result in *this. More...
 
void ApplyExp ()
 Apply exponential to each value in vector. More...
 
void ApplyAbs ()
 Take absolute value of each of the elements. More...
 
MatrixIndexT ApplyFloor (Real floor_val)
 Applies floor to all elements. Returns number of elements floored. More...
 
MatrixIndexT ApplyCeiling (Real ceil_val)
 Applies ceiling to all elements. Returns number of elements changed. More...
 
MatrixIndexT ApplyFloor (const VectorBase< Real > &floor_vec)
 Applies floor to all elements. Returns number of elements floored. More...
 
Real ApplySoftMax ()
 Apply soft-max to vector and return normalizer (log sum of exponentials). More...
 
Real ApplyLogSoftMax ()
 Applies log soft-max to vector and returns normalizer (log sum of exponentials). More...
 
void Tanh (const VectorBase< Real > &src)
 Sets each element of *this to the tanh of the corresponding element of "src". More...
 
void Sigmoid (const VectorBase< Real > &src)
 Sets each element of *this to the sigmoid function of the corresponding element of "src". More...
 
void ApplyPow (Real power)
 Take all elements of vector to a power. More...
 
void ApplyPowAbs (Real power, bool include_sign=false)
 Take the absolute value of all elements of a vector to a power. More...
 
Real Norm (Real p) const
 Compute the p-th norm of the vector. More...
 
bool ApproxEqual (const VectorBase< Real > &other, float tol=0.01) const
 Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0). More...
 
void InvertElements ()
 Invert all elements. More...
 
template<typename OtherReal >
void AddVec (const Real alpha, const VectorBase< OtherReal > &v)
 Add vector : *this = *this + alpha * rv (with casting between floats and doubles) More...
 
void AddVec2 (const Real alpha, const VectorBase< Real > &v)
 Add vector : *this = *this + alpha * rv^2 [element-wise squaring]. More...
 
template<typename OtherReal >
void AddVec2 (const Real alpha, const VectorBase< OtherReal > &v)
 Add vector : *this = *this + alpha * rv^2 [element-wise squaring], with casting between floats and doubles. More...
 
void AddMatVec (const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
 Add matrix times vector : this <– beta*this + alpha*M*v. More...
 
void AddMatSvec (const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
 This is as AddMatVec, except optimized for where v contains a lot of zeros. More...
 
void AddSpVec (const Real alpha, const SpMatrix< Real > &M, const VectorBase< Real > &v, const Real beta)
 Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v. More...
 
void AddTpVec (const Real alpha, const TpMatrix< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
 Add triangular matrix times vector: this <– beta*this + alpha*M*v. More...
 
void ReplaceValue (Real orig, Real changed)
 Set each element to y = (x == orig ? changed : x). More...
 
void MulElements (const VectorBase< Real > &v)
 Multipy element-by-element by another vector. More...
 
template<typename OtherReal >
void MulElements (const VectorBase< OtherReal > &v)
 Multipy element-by-element by another vector of different type. More...
 
void DivElements (const VectorBase< Real > &v)
 Divide element-by-element by a vector. More...
 
template<typename OtherReal >
void DivElements (const VectorBase< OtherReal > &v)
 Divide element-by-element by a vector of different type. More...
 
void Add (Real c)
 Add a constant to each element of a vector. More...
 
void AddVecVec (Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
 Add element-by-element product of vectlrs: More...
 
void AddVecDivVec (Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
 Add element-by-element quotient of two vectors. More...
 
void Scale (Real alpha)
 Multiplies all elements by this constant. More...
 
void MulTp (const TpMatrix< Real > &M, const MatrixTransposeType trans)
 Multiplies this vector by lower-triangular marix: *this <– *this *M. More...
 
void Solve (const TpMatrix< Real > &M, const MatrixTransposeType trans)
 If trans == kNoTrans, solves M x = b, where b is the value of *this at input and x is the value of *this at output. More...
 
void CopyRowsFromMat (const MatrixBase< Real > &M)
 Performs a row stack of the matrix M. More...
 
template<typename OtherReal >
void CopyRowsFromMat (const MatrixBase< OtherReal > &M)
 
void CopyRowsFromMat (const CuMatrixBase< Real > &M)
 The following is implemented in ../cudamatrix/cu-matrix.cc. More...
 
void CopyColsFromMat (const MatrixBase< Real > &M)
 Performs a column stack of the matrix M. More...
 
void CopyRowFromMat (const MatrixBase< Real > &M, MatrixIndexT row)
 Extracts a row of the matrix M. More...
 
template<typename OtherReal >
void CopyRowFromMat (const MatrixBase< OtherReal > &M, MatrixIndexT row)
 Extracts a row of the matrix M with type conversion. More...
 
template<typename OtherReal >
void CopyRowFromSp (const SpMatrix< OtherReal > &S, MatrixIndexT row)
 Extracts a row of the symmetric matrix S. More...
 
template<typename OtherReal >
void CopyColFromMat (const MatrixBase< OtherReal > &M, MatrixIndexT col)
 Extracts a column of the matrix M. More...
 
void CopyDiagFromMat (const MatrixBase< Real > &M)
 Extracts the diagonal of the matrix M. More...
 
void CopyDiagFromPacked (const PackedMatrix< Real > &M)
 Extracts the diagonal of a packed matrix M; works for Sp or Tp. More...
 
void CopyDiagFromSp (const SpMatrix< Real > &M)
 Extracts the diagonal of a symmetric matrix. More...
 
void CopyDiagFromTp (const TpMatrix< Real > &M)
 Extracts the diagonal of a triangular matrix. More...
 
Real Max () const
 Returns the maximum value of any element, or -infinity for the empty vector. More...
 
Real Max (MatrixIndexT *index) const
 Returns the maximum value of any element, and the associated index. More...
 
Real Min () const
 Returns the minimum value of any element, or +infinity for the empty vector. More...
 
Real Min (MatrixIndexT *index) const
 Returns the minimum value of any element, and the associated index. More...
 
Real Sum () const
 Returns sum of the elements. More...
 
Real SumLog () const
 Returns sum of the logs of the elements. More...
 
void AddRowSumMat (Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
 Does *this = alpha * (sum of rows of M) + beta * *this. More...
 
void AddColSumMat (Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
 Does *this = alpha * (sum of columns of M) + beta * *this. More...
 
void AddDiagMat2 (Real alpha, const MatrixBase< Real > &M, MatrixTransposeType trans=kNoTrans, Real beta=1.0)
 Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans), or *this = diag(M^T M) + beta * *this (if trans == kTrans). More...
 
void AddDiagMatMat (Real alpha, const MatrixBase< Real > &M, MatrixTransposeType transM, const MatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
 Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoTrans; for transpose arguments, it behaves as you would expect. More...
 
Real LogSumExp (Real prune=-1.0) const
 Returns log(sum(exp())) without exp overflow If prune > 0.0, ignores terms less than the max - prune. More...
 
void Read (std::istream &in, bool binary, bool add=false)
 Reads from C++ stream (option to add to existing contents). More...
 
void Write (std::ostream &Out, bool binary) const
 Writes to C++ stream (option to write in binary). More...
 
template<>
void AddVec (const float alpha, const VectorBase< float > &rv)
 
template<>
void AddVec (const double alpha, const VectorBase< double > &rv)
 
template<>
void AddVec (const float alpha, const VectorBase< float > &v)
 
template<>
void AddVec (const double alpha, const VectorBase< double > &v)
 

Protected Member Functions

 ~VectorBase ()
 Destructor; does not deallocate memory, this is handled by child classes. More...
 
 VectorBase ()
 Empty initializer, corresponds to vector of zero size. More...
 
void CopyFromPtr (const Real *Data, MatrixIndexT sz)
 Load data into the vector: sz must match own size. More...
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (VectorBase)
 

Protected Attributes

Real * data_
 data memory area More...
 
MatrixIndexT dim_
 dimension of vector More...
 

Friends

class VectorBase< double >
 
class VectorBase< float >
 
class CuVectorBase< Real >
 
class CuVector< Real >
 

Detailed Description

template<typename Real>
singleton kaldi::VectorBase< Real >

Provides a vector abstraction class.

This class provides a way to work with vectors in kaldi. It encapsulates basic operations and memory optimizations.

Definition at line 39 of file kaldi-vector.h.

Constructor & Destructor Documentation

~VectorBase ( )
inlineprotected

Destructor; does not deallocate memory, this is handled by child classes.

This destructor is protected so this object so this object can only be deleted via a child.

Definition at line 354 of file kaldi-vector.h.

354 {}
VectorBase ( )
inlineexplicitprotected

Empty initializer, corresponds to vector of zero size.

Definition at line 357 of file kaldi-vector.h.

357  : data_(NULL), dim_(0) {
359  }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:134
Real * data_
data memory area
Definition: kaldi-vector.h:373

Member Function Documentation

void Add ( Real  c)

Add a constant to each element of a vector.

Definition at line 933 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by AmSgmm2::ComponentLogLikes(), kaldi::ComputeCorrelation(), OnlineNaturalGradient::ComputeZt(), OnlinePreconditioner::ComputeZt(), IvectorExtractor::GetAcousticAuxfWeight(), LogisticRegression::GetLogPosteriors(), Plda::GetNormalizationFactor(), ConstantFunctionComponent::InitFromConfig(), Plda::LogLikelihoodRatio(), main(), kaldi::nnet1::MomentStatistics(), PitchInterpolator::MultiplyObsProb(), OnlinePitchFeatureImpl::OnlinePitchFeatureImpl(), PdfPrior::PdfPrior(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), kaldi::ProcessWindow(), AccumDiagGmm::SmoothStats(), AccumDiagGmm::SmoothWithModel(), MleAmSgmm2Updater::UpdatePhoneVectorsInternal(), MleAmSgmm2Updater::UpdateSubstateWeights(), MleAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateWGetStats(), and MleSgmm2SpeakerAccs::UpdateWithU().

933  {
934  for (MatrixIndexT i = 0; i < dim_; i++) {
935  data_[i] += c;
936  }
937 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void AddColSumMat ( Real  alpha,
const MatrixBase< Real > &  M,
Real  beta = 1.0 
)

Does *this = alpha * (sum of columns of M) + beta * *this.

Definition at line 728 of file kaldi-vector.cc.

References data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, kaldi::kNoTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::RowData(), and VectorBase< Real >::Set().

Referenced by kaldi::CuVectorUnitTestAddColSumMat(), KlHmm::PropagateFnc(), kaldi::UnitTestAddColSumMatSpeed(), kaldi::UnitTestCuDiffLogSoftmax(), kaldi::UnitTestCuVectorAddColSumMat(), kaldi::UnitTestCuVectorAddColSumMatLarge(), and kaldi::UnitTestSimpleForVec().

728  {
729  KALDI_ASSERT(dim_ == M.NumRows());
730  MatrixIndexT num_cols = M.NumCols();
731 
732  // implement the function according to a dimension cutoff for computation efficiency
733  if (num_cols <= 64) {
734  for (MatrixIndexT i = 0; i < dim_; i++) {
735  double sum = 0.0;
736  const Real *src = M.RowData(i);
737  for (MatrixIndexT j = 0; j < num_cols; j++)
738  sum += src[j];
739  data_[i] = alpha * sum + beta * data_[i];
740  }
741  } else {
742  Vector<Real> ones(M.NumCols());
743  ones.Set(1.0);
744  this->AddMatVec(alpha, M, kNoTrans, ones, beta);
745  }
746 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
Definition: kaldi-vector.cc:91
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddDiagMat2 ( Real  alpha,
const MatrixBase< Real > &  M,
MatrixTransposeType  trans = kNoTrans,
Real  beta = 1.0 
)

Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans), or *this = diag(M^T M) + beta * *this (if trans == kTrans).

Definition at line 1275 of file kaldi-vector.cc.

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

Referenced by kaldi::CuVectorUnitTestAddDiagMat2(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), kaldi::SlidingWindowCmnInternal(), and kaldi::UnitTestAddDiagMat2().

1277  {
1278  if (trans == kNoTrans) {
1279  KALDI_ASSERT(this->dim_ == M.NumRows());
1280  MatrixIndexT rows = this->dim_, cols = M.NumCols(),
1281  mat_stride = M.Stride();
1282  Real *data = this->data_;
1283  const Real *mat_data = M.Data();
1284  for (MatrixIndexT i = 0; i < rows; i++, mat_data += mat_stride, data++)
1285  *data = beta * *data + alpha * cblas_Xdot(cols,mat_data,1,mat_data,1);
1286  } else {
1287  KALDI_ASSERT(this->dim_ == M.NumCols());
1288  MatrixIndexT rows = M.NumRows(), cols = this->dim_,
1289  mat_stride = M.Stride();
1290  Real *data = this->data_;
1291  const Real *mat_data = M.Data();
1292  for (MatrixIndexT i = 0; i < cols; i++, mat_data++, data++)
1293  *data = beta * *data + alpha * cblas_Xdot(rows, mat_data, mat_stride,
1294  mat_data, mat_stride);
1295  }
1296 }
float cblas_Xdot(const int N, const float *const X, const int incX, const float *const Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddDiagMatMat ( Real  alpha,
const MatrixBase< Real > &  M,
MatrixTransposeType  transM,
const MatrixBase< Real > &  N,
MatrixTransposeType  transN,
Real  beta = 1.0 
)

Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoTrans; for transpose arguments, it behaves as you would expect.

Definition at line 1299 of file kaldi-vector.cc.

References kaldi::cblas_Xdot(), MatrixBase< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Stride(), and kaldi::swap().

Referenced by kaldi::UnitTestAddDiagMatMat(), and kaldi::UnitTestCuDiffSoftmax().

1303  {
1304  MatrixIndexT dim = this->dim_,
1305  M_col_dim = (transM == kTrans ? M.NumRows() : M.NumCols()),
1306  N_row_dim = (transN == kTrans ? N.NumCols() : N.NumRows());
1307  KALDI_ASSERT(M_col_dim == N_row_dim); // this is the dimension we sum over
1308  MatrixIndexT M_row_stride = M.Stride(), M_col_stride = 1;
1309  if (transM == kTrans) std::swap(M_row_stride, M_col_stride);
1310  MatrixIndexT N_row_stride = N.Stride(), N_col_stride = 1;
1311  if (transN == kTrans) std::swap(N_row_stride, N_col_stride);
1312 
1313  Real *data = this->data_;
1314  const Real *Mdata = M.Data(), *Ndata = N.Data();
1315  for (MatrixIndexT i = 0; i < dim; i++, Mdata += M_row_stride, Ndata += N_col_stride, data++) {
1316  *data = beta * *data + alpha * cblas_Xdot(M_col_dim, Mdata, M_col_stride, Ndata, N_row_stride);
1317  }
1318 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
float cblas_Xdot(const int N, const float *const X, const int incX, const float *const Y, const int incY)
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatSvec ( const Real  alpha,
const MatrixBase< Real > &  M,
const MatrixTransposeType  trans,
const VectorBase< Real > &  v,
const Real  beta 
)

This is as AddMatVec, except optimized for where v contains a lot of zeros.

Definition at line 104 of file kaldi-vector.cc.

References VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::dim_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Stride(), and kaldi::Xgemv_sparsevec().

Referenced by kaldi::UnitTestVecmul().

108  {
109  KALDI_ASSERT((trans == kNoTrans && M.NumCols() == v.dim_ && M.NumRows() == dim_)
110  || (trans == kTrans && M.NumRows() == v.dim_ && M.NumCols() == dim_));
111  KALDI_ASSERT(&v != this);
112  Xgemv_sparsevec(trans, M.NumRows(), M.NumCols(), alpha, M.Data(), M.Stride(),
113  v.Data(), 1, beta, data_, 1);
114  return;
115  /*
116  MatrixIndexT this_dim = this->dim_, v_dim = v.dim_,
117  M_stride = M.Stride();
118  Real *this_data = this->data_;
119  const Real *M_data = M.Data(), *v_data = v.data_;
120  if (beta != 1.0) this->Scale(beta);
121  if (trans == kNoTrans) {
122  for (MatrixIndexT i = 0; i < v_dim; i++) {
123  Real v_i = v_data[i];
124  if (v_i == 0.0) continue;
125  // Add to *this, the i'th column of the Matrix, times v_i.
126  cblas_Xaxpy(this_dim, v_i * alpha, M_data + i, M_stride, this_data, 1);
127  }
128  } else { // The transposed case is slightly more efficient, I guess.
129  for (MatrixIndexT i = 0; i < v_dim; i++) {
130  Real v_i = v.data_[i];
131  if (v_i == 0.0) continue;
132  // Add to *this, the i'th row of the Matrix, times v_i.
133  cblas_Xaxpy(this_dim, v_i * alpha,
134  M_data + (i * M_stride), 1, this_data, 1);
135  }
136  }*/
137 }
void Xgemv_sparsevec(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, Real alpha, const Real *Mdata, MatrixIndexT stride, const Real *xdata, MatrixIndexT incX, Real beta, Real *ydata, MatrixIndexT incY)
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddMatVec ( const Real  alpha,
const MatrixBase< Real > &  M,
const MatrixTransposeType  trans,
const VectorBase< Real > &  v,
const Real  beta 
)

Add matrix times vector : this <– beta*this + alpha*M*v.

Calls BLAS GEMV.

Definition at line 91 of file kaldi-vector.cc.

References kaldi::cblas_Xgemv(), VectorBase< Real >::Data(), MatrixBase< Real >::Data(), data_, VectorBase< Real >::dim_, KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

Referenced by OnlineIvectorEstimationStats::AccStats(), FmllrDiagGmmAccs::AccumulateFromPosteriors(), MleSgmm2SpeakerAccs::AccumulateFromPosteriors(), LdaEstimate::AddMeanOffset(), kaldi::ApplyAffineTransform(), ApplyFmllrXform(), Fmpe::ApplyProjection(), IvectorExtractorStats::CommitStatsForWPoint(), AmSgmm2::ComponentLogLikes(), MfccComputer::Compute(), PlpComputer::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), Plda::ComputeDerivedVars(), AmSgmm2::ComputeFmllrPreXform(), AmSgmm2::ComputePerSpkDerivedVars(), MleAmSgmm2Updater::ComputeSMeans(), FmllrRawAccs::ConvertToSimpleStats(), kaldi::CuVectorUnitTestAddMatVec(), BasisFmllrEstimate::EstimateFmllrBasis(), FeatureTransformEstimateMulti::EstimateTransformPart(), kaldi::generate_features(), IvectorExtractor::GetAcousticAuxfMean(), IvectorExtractor::GetAcousticAuxfWeight(), kaldi::GetFeatDeriv(), OnlineTransform::GetFrame(), IvectorExtractor::GetIvectorDistMean(), IvectorExtractor::GetIvectorDistWeight(), LogisticRegression::GetLogPosteriors(), IvectorExtractorStats::GetOrthogonalIvectorTransform(), RegtreeMllrDiagGmm::GetTransformedMeans(), FmllrRawAccs::InitSingleFrameStats(), FullGmm::LogLikelihoods(), DiagGmm::LogLikelihoods(), DiagGmm::LogLikelihoodsPreselect(), DecodableAmDiagGmmUnmapped::LogLikelihoodZeroBased(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), MleAmSgmm2Updater::RenormalizeV(), kaldi::SolveDoubleQuadraticMatrixProblem(), kaldi::SolveQuadraticProblem(), Plda::TransformIvector(), UnitTestEstimateLda(), kaldi::UnitTestMul(), kaldi::UnitTestPldaEstimation(), HldaAccsDiagGmm::Update(), FmllrRawAccs::Update(), EbwAmSgmm2Updater::UpdatePhoneVectorsInternal(), MleAmSgmm2Updater::UpdatePhoneVectorsInternal(), IvectorExtractorStats::UpdatePrior(), MleSgmm2SpeakerAccs::UpdateWithU(), and kaldi::VecMatVec().

95  {
96  KALDI_ASSERT((trans == kNoTrans && M.NumCols() == v.dim_ && M.NumRows() == dim_)
97  || (trans == kTrans && M.NumRows() == v.dim_ && M.NumCols() == dim_));
98  KALDI_ASSERT(&v != this);
99  cblas_Xgemv(trans, M.NumRows(), M.NumCols(), alpha, M.Data(), M.Stride(),
100  v.Data(), 1, beta, data_, 1);
101 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xgemv(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, float alpha, const float *Mdata, MatrixIndexT stride, const float *xdata, MatrixIndexT incX, float beta, float *ydata, MatrixIndexT incY)
void AddRowSumMat ( Real  alpha,
const MatrixBase< Real > &  M,
Real  beta = 1.0 
)

Does *this = alpha * (sum of rows of M) + beta * *this.

Definition at line 708 of file kaldi-vector.cc.

References kaldi::cblas_Xaxpy(), kaldi::cblas_Xscal(), MatrixBase< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, kaldi::kTrans, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), VectorBase< Real >::Set(), and MatrixBase< Real >::Stride().

Referenced by CovarianceStats::AccStats(), PldaStats::AddSamples(), kaldi::nnet2::AverageConstPart(), IvectorExtractorStats::CommitStatsForW(), kaldi::CuVectorUnitTestAddRowSumMat(), LdaEstimate::GetStats(), AmSgmm2::LogLikelihood(), main(), AmSgmm2::SplitSubstatesInGroup(), kaldi::UnitTestAddRowSumMatSpeed(), kaldi::UnitTestCuVectorAddRowSumMat(), kaldi::UnitTestCuVectorAddRowSumMatLarge(), UnitTestEstimateFullGmm(), EbwAmSgmm2Updater::Update(), MleAmSgmm2Updater::Update(), and kaldi::nnet2::UpdateHash().

708  {
709  KALDI_ASSERT(dim_ == M.NumCols());
710  MatrixIndexT num_rows = M.NumRows(), stride = M.Stride(), dim = dim_;
711  Real *data = data_;
712 
713  // implement the function according to a dimension cutoff for computation efficiency
714  if (num_rows <= 64) {
715  cblas_Xscal(dim, beta, data, 1);
716  const Real *m_data = M.Data();
717  for (MatrixIndexT i = 0; i < num_rows; i++, m_data += stride)
718  cblas_Xaxpy(dim, alpha, m_data, 1, data, 1);
719 
720  } else {
721  Vector<Real> ones(M.NumRows());
722  ones.Set(1.0);
723  this->AddMatVec(alpha, M, kTrans, ones, beta);
724  }
725 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
Definition: kaldi-vector.cc:91
Real * data_
data memory area
Definition: kaldi-vector.h:373
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddSpVec ( const Real  alpha,
const SpMatrix< Real > &  M,
const VectorBase< Real > &  v,
const Real  beta 
)

Add symmetric positive definite matrix times vector: this <– beta*this + alpha*M*v.

Calls BLAS SPMV.

Definition at line 140 of file kaldi-vector.cc.

References kaldi::cblas_Xspmv(), VectorBase< Real >::Data(), PackedMatrix< Real >::Data(), data_, VectorBase< Real >::dim_, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by kaldi::CalBasisFmllrStepSize(), AmSgmm2::ComputePerFrameVars(), FullGmmNormal::CopyToFullGmm(), kaldi::CuVectorUnitTestAddSpVec(), kaldi::Debias(), kaldi::FmllrAuxfGradient(), kaldi::FmllrAuxFuncDiagGmm(), kaldi::FmllrInnerUpdate(), IvectorExtractor::GetAcousticAuxfMean(), FullGmm::GetComponentMean(), IvectorExtractor::GetIvectorDistribution(), PldaEstimator::GetStatsFromClassMeans(), kaldi::GpsrBasicAlpha(), kaldi::GpsrBB(), main(), kaldi::MllrAuxFunction(), FullGmm::SetInvCovars(), FullGmm::SetInvCovarsAndMeans(), kaldi::SolveQuadraticProblem(), AmSgmm2::SplitSubstatesInGroup(), SpMatrix< Real >::TopEigs(), kaldi::UnitTestLbfgs(), kaldi::UnitTestLinearCgd(), kaldi::UnitTestMul(), kaldi::UnitTestSolve(), MlltAccs::Update(), EbwAmSgmm2Updater::UpdatePhoneVectorsInternal(), and MleSgmm2SpeakerAccs::UpdateWithU().

143  {
144  KALDI_ASSERT(M.NumRows() == v.dim_ && dim_ == v.dim_);
145  KALDI_ASSERT(&v != this);
146  cblas_Xspmv(alpha, M.NumRows(), M.Data(), v.Data(), 1, beta, data_, 1);
147 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xspmv(const float alpha, const int num_rows, const float *Mdata, const float *v, const int v_inc, const float beta, float *y, const int y_inc)
void AddTpVec ( const Real  alpha,
const TpMatrix< Real > &  M,
const MatrixTransposeType  trans,
const VectorBase< Real > &  v,
const Real  beta 
)

Add triangular matrix times vector: this <– beta*this + alpha*M*v.

Works even if rv == *this.

Definition at line 1234 of file kaldi-vector.cc.

References VectorBase< Real >::dim_, KALDI_ASSERT, VectorBase< Real >::MulTp(), and PackedMatrix< Real >::NumRows().

Referenced by FastNnetCombiner::ComputeCurrentNnet(), FastNnetCombiner::ComputeObjfAndGradient(), FastNnetCombiner::ComputePreconditioner(), kaldi::nnet2::PerturbTrainingExample(), FullGmmNormal::Rand(), kaldi::UnitTestCuVectorAddTp(), kaldi::UnitTestCuVectorAddTpVec(), kaldi::UnitTestMul(), and kaldi::UnitTestTriVecSolver().

1237  {
1238  KALDI_ASSERT(dim_ == v.dim_ && dim_ == M.NumRows());
1239  if (beta == 0.0) {
1240  if (&v != this) CopyFromVec(v);
1241  MulTp(M, trans);
1242  if (alpha != 1.0) Scale(alpha);
1243  } else {
1244  Vector<Real> tmp(v);
1245  tmp.MulTp(M, trans);
1246  if (beta != 1.0) Scale(beta); // *this <-- beta * *this
1247  AddVec(alpha, tmp); // *this += alpha * M * v
1248  }
1249 }
void MulTp(const TpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular marix: *this <– *this *M.
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void Scale(Real alpha)
Multiplies all elements by this constant.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void AddVec ( const float  alpha,
const VectorBase< float > &  rv 
)
void AddVec< double > ( const double  alpha,
const VectorBase< double > &  rv 
)
void AddVec ( const float  alpha,
const VectorBase< float > &  v 
)

Definition at line 74 of file kaldi-vector.cc.

References kaldi::cblas_Xaxpy(), VectorBase< Real >::Data(), data_, VectorBase< Real >::dim_, and KALDI_ASSERT.

75  {
76  KALDI_ASSERT(dim_ == v.dim_);
77  KALDI_ASSERT(&v != this);
78  cblas_Xaxpy(dim_, alpha, v.Data(), 1, data_, 1);
79 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVec ( const double  alpha,
const VectorBase< double > &  v 
)

Definition at line 83 of file kaldi-vector.cc.

References kaldi::cblas_Xaxpy(), VectorBase< Real >::Data(), data_, VectorBase< Real >::dim_, and KALDI_ASSERT.

84  {
85  KALDI_ASSERT(dim_ == v.dim_);
86  KALDI_ASSERT(&v != this);
87  cblas_Xaxpy(dim_, alpha, v.Data(), 1, data_, 1);
88 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void cblas_Xaxpy(const int N, const float alpha, const float *X, const int incX, float *Y, const int incY)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVec ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

Add vector : *this = *this + alpha * rv (with casting between floats and doubles)

Definition at line 1021 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, rnnlm::i, and KALDI_ASSERT.

Referenced by OptimizeLbfgs< Real >::AcceptStep(), HldaAccsDiagGmm::AccumulateFromPosteriors(), AccumFullGmm::AccumulateFromPosteriors(), FmllrRawAccs::AccumulateFromPosteriors(), AccumDiagGmm::AccumulateFromPosteriors(), MleAmSgmm2Accs::AccumulateFromPosteriors(), MleSgmm2SpeakerAccs::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), VectorClusterable::Add(), AccumDiagGmm::Add(), IvectorExtractorStats::Add(), PldaStats::AddSamples(), PldaUnsupervisedAdaptor::AddStats(), Fmpe::ApplyProjection(), VectorBase< Real >::ApproxEqual(), IvectorExtractorStats::CommitStatsForM(), IvectorExtractorStats::CommitStatsForPrior(), AmSgmm2::ComponentLogLikes(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeAndSubtractMean(), PitchFrameInfo::ComputeBacktraces(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), AmSgmm2::ComputeGammaI(), Sgmm2Project::ComputeLdaStats(), kaldi::ComputeLocalCost(), OptimizeLbfgs< Real >::ComputeNewDirection(), PldaEstimator::ComputeObjfPart2(), EbwAmSgmm2Updater::ComputePhoneVecStats(), BasisFmllrEstimate::ComputeTransform(), CompressedAffineXformStats::CopyFromAffineXformStats(), kaldi::Debias(), kaldi::DiagGmmToStats(), kaldi::EBWUpdateGaussian(), kaldi::FmllrInnerUpdate(), kaldi::GenRandStats(), kaldi::GetFeatureMeanAndVariance(), IvectorExtractor::GetIvectorDistribution(), GetLogLikeTest(), NnetChainCombiner::GetNnetParameters(), NnetCombiner::GetNnetParameters(), PldaEstimator::GetStatsFromClassMeans(), AmSgmm2::GetSubstateSpeakerMean(), kaldi::GpsrBasic(), kaldi::GpsrBasicAlpha(), kaldi::GpsrBB(), kaldi::GpsrGradient(), kaldi::GpsrObjective(), FmllrRawAccs::InitSingleFrameStats(), kaldi::LinearCgd(), Plda::LogLikelihoodRatio(), main(), FullGmm::Merge(), DiagGmm::merged_components_logdet(), FullGmm::MergedComponentsLogdet(), SoftmaxComponent::MixUp(), PitchInterpolator::MultiplyObsProb(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), kaldi::nnet3::PrintPriorDiagnostics(), kaldi::nnet2::PrintPriorDiagnostics(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), kaldi::nnet2::ProcessFile(), AccumFullGmm::Read(), LdaEstimate::Read(), OptimizeLbfgs< Real >::Restart(), NnetChainCombiner::SelfTestDerivatives(), NnetCombiner::SelfTestDerivatives(), NnetChainCombiner::SelfTestModelDerivatives(), NnetCombiner::SelfTestModelDerivatives(), kaldi::SlidingWindowCmnInternal(), Plda::SmoothWithinClassCovariance(), kaldi::SolveQuadraticProblem(), VectorClusterable::Sub(), kaldi::TestClusterKMeansVector(), SpMatrix< Real >::TopEigs(), kaldi::TypeOneUsage(), kaldi::UnitTestAddDiagMat2(), kaldi::UnitTestAddDiagMatMat(), kaldi::UnitTestAddDiagVecMat(), kaldi::UnitTestAddVecCross(), kaldi::UnitTestCuVectorAddColSumMat(), kaldi::UnitTestCuVectorAddColSumMatLarge(), kaldi::UnitTestCuVectorAddRowSumMat(), kaldi::UnitTestCuVectorAddRowSumMatLarge(), kaldi::UnitTestCuVectorAddVec(), UnitTestEstimateDiagGmm(), UnitTestEstimateLda(), kaldi::UnitTestEstimateMmieDiagGmm(), kaldi::UnitTestLbfgs(), kaldi::UnitTestLinearCgd(), kaldi::UnitTestSparseVectorAddToVec(), HldaAccsDiagGmm::Update(), kaldi::UpdateEbwDiagGmm(), EbwAmSgmm2Updater::UpdatePhoneVectorsInternal(), MleAmSgmm2Updater::UpdatePhoneVectorsInternal(), PldaUnsupervisedAdaptor::UpdatePlda(), EbwAmSgmm2Updater::UpdateU(), MleAmSgmm2Updater::UpdateU(), EbwAmSgmm2Updater::UpdateW(), and MleSgmm2SpeakerAccs::UpdateWithU().

1021  {
1022  KALDI_ASSERT(dim_ == v.dim_);
1023  // remove __restrict__ if it causes compilation problems.
1024  register Real *__restrict__ data = data_;
1025  register OtherReal *__restrict__ other_data = v.data_;
1026  MatrixIndexT dim = dim_;
1027  if (alpha != 1.0)
1028  for (MatrixIndexT i = 0; i < dim; i++)
1029  data[i] += alpha * other_data[i];
1030  else
1031  for (MatrixIndexT i = 0; i < dim; i++)
1032  data[i] += other_data[i];
1033 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVec2 ( const Real  alpha,
const VectorBase< Real > &  v 
)
void AddVec2 ( const Real  alpha,
const VectorBase< OtherReal > &  v 
)

Add vector : *this = *this + alpha * rv^2 [element-wise squaring], with casting between floats and doubles.

Definition at line 1042 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, rnnlm::i, and KALDI_ASSERT.

1042  {
1043  KALDI_ASSERT(dim_ == v.dim_);
1044  // remove __restrict__ if it causes compilation problems.
1045  register Real *__restrict__ data = data_;
1046  register OtherReal *__restrict__ other_data = v.data_;
1047  MatrixIndexT dim = dim_;
1048  if (alpha != 1.0)
1049  for (MatrixIndexT i = 0; i < dim; i++)
1050  data[i] += alpha * other_data[i] * other_data[i];
1051  else
1052  for (MatrixIndexT i = 0; i < dim; i++)
1053  data[i] += other_data[i] * other_data[i];
1054 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecDivVec ( Real  alpha,
const VectorBase< Real > &  v,
const VectorBase< Real > &  r,
Real  beta 
)

Add element-by-element quotient of two vectors.

this <—- alpha*v/r + beta*this

Definition at line 1011 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, rnnlm::i, and KALDI_ASSERT.

Referenced by main(), and kaldi::UnitTestSimpleForVec().

1012  {
1013  KALDI_ASSERT((dim_ == v.dim_ && dim_ == rr.dim_));
1014  for (MatrixIndexT i = 0; i < dim_; i++) {
1015  data_[i] = alpha * v.data_[i]/rr.data_[i] + beta * data_[i] ;
1016  }
1017 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVecVec ( Real  alpha,
const VectorBase< Real > &  v,
const VectorBase< Real > &  r,
Real  beta 
)

Add element-by-element product of vectlrs:

Definition at line 977 of file kaldi-vector.cc.

References kaldi::cblas_Xgbmv(), VectorBase< Real >::data_, VectorBase< Real >::dim_, KALDI_ASSERT, and kaldi::kNoTrans.

Referenced by FmllrRawAccs::AccumulateFromPosteriors(), MleAmSgmm2Accs::CommitStatsForSpk(), kaldi::ComputeAmGmmFeatureDeriv(), kaldi::ComputeLocalCost(), OptimizeLbfgs< Real >::ComputeNewDirection(), kaldi::CuVectorUnitTestAddVecVec(), and kaldi::GetFeatDeriv().

978  {
979  KALDI_ASSERT(v.data_ != this->data_ && r.data_ != this->data_);
980  // We pretend that v is a band-diagonal matrix.
981  KALDI_ASSERT(dim_ == v.dim_ && dim_ == r.dim_);
982  cblas_Xgbmv(kNoTrans, dim_, dim_, 0, 0, alpha, v.data_, 1,
983  r.data_, 1, beta, this->data_, 1);
984 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void cblas_Xgbmv(MatrixTransposeType trans, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT num_below, MatrixIndexT num_above, float alpha, const float *Mdata, MatrixIndexT stride, const float *xdata, MatrixIndexT incX, float beta, float *ydata, MatrixIndexT incY)
void ApplyAbs ( )

Take absolute value of each of the elements.

Definition at line 801 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by kaldi::nnet2::PrintPriorDiagnostics(), kaldi::nnet3::PrintPriorDiagnostics(), and kaldi::UnitTestTopEigs().

801  {
802  for (MatrixIndexT i = 0; i < dim_; i++) { data_[i] = std::abs(data_[i]); }
803 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT ApplyCeiling ( Real  ceil_val)

Applies ceiling to all elements. Returns number of elements changed.

Definition at line 818 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by kaldi::CuVectorUnitTestApplyCeiling(), and FeatureTransformEstimate::EstimateInternal().

818  {
819  MatrixIndexT num_changed = 0;
820  for (MatrixIndexT i = 0; i < dim_; i++) {
821  if (data_[i] > ceil_val) {
822  data_[i] = ceil_val;
823  num_changed++;
824  }
825  }
826  return num_changed;
827 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT ApplyFloor ( const VectorBase< Real > &  floor_vec)

Applies floor to all elements. Returns number of elements floored.

Definition at line 831 of file kaldi-vector.cc.

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

831  {
832  KALDI_ASSERT(floor_vec.Dim() == dim_);
833  MatrixIndexT num_floored = 0;
834  for (MatrixIndexT i = 0; i < dim_; i++) {
835  if (data_[i] < floor_vec(i)) {
836  data_[i] = floor_vec(i);
837  num_floored++;
838  }
839  }
840  return num_floored;
841 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void ApplyLog ( )

Apply natural log to all elements.

Throw if any element of the vector is negative (but doesn't complain about zero; the log will be -infinity

Definition at line 777 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ERR, and kaldi::Log().

Referenced by AmSgmm2::ComponentLogLikes(), MfccComputer::Compute(), FbankComputer::Compute(), IvectorExtractor::GetAcousticAuxfWeight(), NnetChainCombiner::GetInitialParameters(), NnetCombiner::GetInitialParameters(), SpMatrix< Real >::Log(), PdfPrior::PdfPrior(), LogisticRegression::ScalePriors(), and MleSgmm2SpeakerAccs::UpdateWithU().

777  {
778  for (MatrixIndexT i = 0; i < dim_; i++) {
779  if (data_[i] < 0.0)
780  KALDI_ERR << "Trying to take log of a negative number.";
781  data_[i] = Log(data_[i]);
782  }
783 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * data_
data memory area
Definition: kaldi-vector.h:373
void ApplyLogAndCopy ( const VectorBase< Real > &  v)

Apply natural log to another vector and put result in *this.

Definition at line 786 of file kaldi-vector.cc.

References data_, VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, and kaldi::Log().

Referenced by kaldi::UnitTestSimple().

786  {
787  KALDI_ASSERT(dim_ == v.Dim());
788  for (MatrixIndexT i = 0; i < dim_; i++) {
789  data_[i] = Log(v(i));
790  }
791 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real ApplyLogSoftMax ( )

Applies log soft-max to vector and returns normalizer (log sum of exponentials).

This is the same as: $ x(i) = x(i) - log(\sum_i exp(x(i))) $

Definition at line 854 of file kaldi-vector.cc.

References data_, kaldi::Exp(), rnnlm::i, and kaldi::Log().

Referenced by kaldi::UnitTestSimpleForVec().

854  {
855  Real max = this->Max(), sum = 0.0;
856  for (MatrixIndexT i = 0; i < dim_; i++) {
857  sum += Exp((data_[i] -= max));
858  }
859  sum = Log(sum);
860  this->Add(-1.0 * sum);
861  return max + sum;
862 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
Real * data_
data memory area
Definition: kaldi-vector.h:373
void Add(Real c)
Add a constant to each element of a vector.
void ApplyPow ( Real  power)

Take all elements of vector to a power.

Definition at line 448 of file kaldi-vector.cc.

References data_, rnnlm::i, and KALDI_ERR.

Referenced by AccumDiagGmm::AccumulateForComponent(), AccumDiagGmm::AccumulateFromPosteriors(), SpMatrix< Real >::ApplyFloor(), SpMatrix< Real >::ApplyPow(), DiagGmm::ComponentLogLikelihood(), FbankComputer::Compute(), PlpComputer::Compute(), OnlineNaturalGradient::ComputeEt(), OnlinePreconditioner::ComputeEt(), kaldi::CuVectorUnitTestApplyPow(), Plda::GetNormalizationFactor(), Plda::LogLikelihoodRatio(), DiagGmm::LogLikelihoods(), DecodableAmDiagGmmUnmapped::LogLikelihoodZeroBased(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), kaldi::SlidingWindowCmnInternal(), Plda::SmoothWithinClassCovariance(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SolveQuadraticProblem(), MatrixBase< Real >::SymPosSemiDefEig(), UnitTestEstimateDiagGmm(), kaldi::UnitTestEstimateMmieDiagGmm(), kaldi::UnitTestPower(), and IvectorExtractorStats::UpdatePrior().

448  {
449  if (power == 1.0) return;
450  if (power == 2.0) {
451  for (MatrixIndexT i = 0; i < dim_; i++)
452  data_[i] = data_[i] * data_[i];
453  } else if (power == 0.5) {
454  for (MatrixIndexT i = 0; i < dim_; i++) {
455  if (!(data_[i] >= 0.0))
456  KALDI_ERR << "Cannot take square root of negative value "
457  << data_[i];
458  data_[i] = std::sqrt(data_[i]);
459  }
460  } else {
461  for (MatrixIndexT i = 0; i < dim_; i++) {
462  data_[i] = pow(data_[i], power);
463  if (data_[i] == HUGE_VAL) { // HUGE_VAL is what errno returns on error.
464  KALDI_ERR << "Could not raise element " << i << " to power "
465  << power << ": returned value = " << data_[i];
466  }
467  }
468  }
469 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * data_
data memory area
Definition: kaldi-vector.h:373
void ApplyPowAbs ( Real  power,
bool  include_sign = false 
)

Take the absolute value of all elements of a vector to a power.

Include the sign of the input element if include_sign == true. If power is negative and the input value is zero, the output is set zero.

Definition at line 475 of file kaldi-vector.cc.

References data_, rnnlm::i, and KALDI_ERR.

Referenced by kaldi::UnitTestPowerAbs().

475  {
476  if (power == 1.0)
477  for (MatrixIndexT i = 0; i < dim_; i++)
478  data_[i] = (include_sign && data_[i] < 0 ? -1 : 1) * std::abs(data_[i]);
479  if (power == 2.0) {
480  for (MatrixIndexT i = 0; i < dim_; i++)
481  data_[i] = (include_sign && data_[i] < 0 ? -1 : 1) * data_[i] * data_[i];
482  } else if (power == 0.5) {
483  for (MatrixIndexT i = 0; i < dim_; i++) {
484  data_[i] = (include_sign && data_[i] < 0 ? -1 : 1) * std::sqrt(std::abs(data_[i]));
485  }
486  } else if (power < 0.0) {
487  for (MatrixIndexT i = 0; i < dim_; i++) {
488  data_[i] = (data_[i] == 0.0 ? 0.0 : pow(std::abs(data_[i]), power));
489  data_[i] *= (include_sign && data_[i] < 0 ? -1 : 1);
490  if (data_[i] == HUGE_VAL) { // HUGE_VAL is what errno returns on error.
491  KALDI_ERR << "Could not raise element " << i << "to power "
492  << power << ": returned value = " << data_[i];
493  }
494  }
495  } else {
496  for (MatrixIndexT i = 0; i < dim_; i++) {
497  data_[i] = (include_sign && data_[i] < 0 ? -1 : 1) * pow(std::abs(data_[i]), power);
498  if (data_[i] == HUGE_VAL) { // HUGE_VAL is what errno returns on error.
499  KALDI_ERR << "Could not raise element " << i << "to power "
500  << power << ": returned value = " << data_[i];
501  }
502  }
503  }
504 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real ApplySoftMax ( )

Apply soft-max to vector and return normalizer (log sum of exponentials).

This is the same as: $ x(i) = exp(x(i)) / \sum_i exp(x(i)) $

Definition at line 844 of file kaldi-vector.cc.

References data_, kaldi::Exp(), rnnlm::i, and kaldi::Log().

Referenced by Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), IvectorExtractorStats::CommitStatsForWPoint(), FullGmm::ComponentPosteriors(), DiagGmm::ComponentPosteriors(), AmSgmm2::ComputeFmllrPreXform(), kaldi::CuVectorUnitTestApplySoftMax(), IvectorExtractor::GetIvectorDistWeight(), main(), EbwAmSgmm2Updater::UpdatePhoneVectorsInternal(), and kaldi::VectorToPosteriorEntry().

844  {
845  Real max = this->Max(), sum = 0.0;
846  for (MatrixIndexT i = 0; i < dim_; i++) {
847  sum += (data_[i] = Exp(data_[i] - max));
848  }
849  this->Scale(1.0 / sum);
850  return max + Log(sum);
851 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
void Scale(Real alpha)
Multiplies all elements by this constant.
Real * data_
data memory area
Definition: kaldi-vector.h:373
bool ApproxEqual ( const VectorBase< Real > &  other,
float  tol = 0.01 
) const

Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).

Definition at line 552 of file kaldi-vector.cc.

References VectorBase< Real >::AddVec(), data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, rnnlm::i, KALDI_ASSERT, KALDI_ERR, and VectorBase< Real >::Norm().

Referenced by kaldi::ApproxEqual(), AccumDiagGmm::AssertEqual(), kaldi::AssertEqual(), kaldi::CuVectorUnitTestCopyDiagFromPacked(), FmllrRawAccs::DataHasChanged(), FmllrDiagGmmAccs::DataHasChanged(), NnetDiscriminativeSupervision::operator==(), NnetChainSupervision::operator==(), and kaldi::UnitTestNorm().

552  {
553  if (dim_ != other.dim_) KALDI_ERR << "ApproxEqual: size mismatch "
554  << dim_ << " vs. " << other.dim_;
555  KALDI_ASSERT(tol >= 0.0);
556  if (tol != 0.0) {
557  Vector<Real> tmp(*this);
558  tmp.AddVec(-1.0, other);
559  return (tmp.Norm(2.0) <= static_cast<Real>(tol) * this->Norm(2.0));
560  } else { // Test for exact equality.
561  const Real *data = data_;
562  const Real *other_data = other.data_;
563  for (MatrixIndexT dim = dim_, i = 0; i < dim; i++)
564  if (data[i] != other_data[i]) return false;
565  return true;
566  }
567 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ERR
Definition: kaldi-error.h:127
Real Norm(Real p) const
Compute the p-th norm of the vector.
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
template void CopyColFromMat ( const MatrixBase< OtherReal > &  M,
MatrixIndexT  col 
)

Extracts a column of the matrix M.

Definition at line 652 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by kaldi::ComputeVadEnergy(), FmllrRawAccs::FmllrRawAccs(), kaldi::generate_features(), kaldi::IncreaseTransformDimension(), main(), OnlineTransform::OnlineTransform(), TestSgmm2PreXform(), kaldi::UnitTestAddDiagVecMat(), kaldi::UnitTestRow(), kaldi::UnitTestSimpleForVec(), kaldi::UnitTestSnipEdges(), and kaldi::UnitTestSpliceRows().

652  {
653  KALDI_ASSERT(col < mat.NumCols());
654  KALDI_ASSERT(dim_ == mat.NumRows());
655  for (MatrixIndexT i = 0; i < dim_; i++)
656  data_[i] = mat(i, col);
657  // can't do this very efficiently so don't really bother. could improve this though.
658 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyColsFromMat ( const MatrixBase< Real > &  M)

Performs a column stack of the matrix M.

Definition at line 375 of file kaldi-vector.cc.

References MatrixBase< Real >::Data(), data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

Referenced by kaldi::UnitTestSpliceRows().

375  {
376  KALDI_ASSERT(dim_ == mat.NumCols() * mat.NumRows());
377 
378  Real* inc_data = data_;
379  const MatrixIndexT cols = mat.NumCols(), rows = mat.NumRows(), stride = mat.Stride();
380  const Real *mat_inc_data = mat.Data();
381 
382  for (MatrixIndexT i = 0; i < cols; i++) {
383  for (MatrixIndexT j = 0; j < rows; j++) {
384  inc_data[j] = mat_inc_data[j*stride];
385  }
386  mat_inc_data++;
387  inc_data += rows;
388  }
389 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyDiagFromMat ( const MatrixBase< Real > &  M)

Extracts the diagonal of the matrix M.

Definition at line 670 of file kaldi-vector.cc.

References kaldi::cblas_Xcopy(), MatrixBase< Real >::Data(), data_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Stride().

Referenced by kaldi::CuVectorUnitTestCopyDiagFromMat(), kaldi::UnitTestAddDiagMat2(), kaldi::UnitTestAddDiagMatMat(), kaldi::UnitTestSpliceRows(), and kaldi::UnitTestTopEigs().

670  {
671  KALDI_ASSERT(dim_ == std::min(M.NumRows(), M.NumCols()));
672  cblas_Xcopy(dim_, M.Data(), M.Stride() + 1, data_, 1);
673 }
void cblas_Xcopy(const int N, const float *X, const int incX, float *Y, const int incY)
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyDiagFromPacked ( const PackedMatrix< Real > &  M)

Extracts the diagonal of a packed matrix M; works for Sp or Tp.

Definition at line 676 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ASSERT, and PackedMatrix< Real >::NumCols().

Referenced by VectorBase< BaseFloat >::CopyDiagFromSp(), VectorBase< BaseFloat >::CopyDiagFromTp(), DiagGmm::CopyFromFullGmm(), kaldi::CuVectorUnitTestCopyDiagFromPacked(), and SpMatrix< Real >::Eig().

676  {
677  KALDI_ASSERT(dim_ == M.NumCols());
678  for (MatrixIndexT i = 0; i < dim_; i++)
679  data_[i] = M(i, i);
680  // could make this more efficient.
681 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyDiagFromSp ( const SpMatrix< Real > &  M)
inline

Extracts the diagonal of a symmetric matrix.

Definition at line 287 of file kaldi-vector.h.

Referenced by kaldi::SolveQuadraticMatrixProblem(), kaldi::SolveQuadraticProblem(), and SpMatrix< Real >::TopEigs().

287 { CopyDiagFromPacked(M); }
void CopyDiagFromPacked(const PackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
void CopyDiagFromTp ( const TpMatrix< Real > &  M)
inline

Extracts the diagonal of a triangular matrix.

Definition at line 290 of file kaldi-vector.h.

290 { CopyDiagFromPacked(M); }
void CopyDiagFromPacked(const PackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
template void CopyFromPacked ( const PackedMatrix< OtherReal > &  M)

Copy data from a SpMatrix or TpMatrix (must match own size).

Definition at line 236 of file kaldi-vector.cc.

236  {
237  SubVector<OtherReal> v(M);
238  this->CopyFromVec(v);
239 }
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void CopyFromPtr ( const Real *  Data,
MatrixIndexT  sz 
)
protected

Load data into the vector: sz must match own size.

Load data into the vector.

Definition at line 248 of file kaldi-vector.cc.

References data_, and KALDI_ASSERT.

248  {
249  KALDI_ASSERT(dim_ == sz);
250  std::memcpy(this->data_, data, Dim() * sizeof(Real));
251 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
void CopyFromVec ( const VectorBase< Real > &  v)

Copy data from another vector (must match own size).

Copy data from another vector.

Definition at line 227 of file kaldi-vector.cc.

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

Referenced by OptimizeLbfgs< Real >::AcceptStep(), RegtreeMllrDiagGmmAccs::AccumulateForGaussian(), RegtreeMllrDiagGmmAccs::AccumulateForGmm(), AccumFullGmm::AccumulateFromPosteriors(), kaldi::ApplyAffineTransform(), Fmpe::ApplyC(), kaldi::ClusterGaussiansToUbm(), NnetChainCombiner::Combine(), NnetCombiner::Combine(), kaldi::nnet2::CombineNnets(), kaldi::nnet2::CombineNnetsA(), FullGmm::ComponentPosteriors(), DiagGmm::ComponentPosteriors(), SpectrogramComputer::Compute(), kaldi::ComputeAndSubtractMean(), OnlineNaturalGradient::ComputeEt(), OnlinePreconditioner::ComputeEt(), kaldi::ComputeFeatureNormalizingTransform(), OnlineGenericBaseFeature< C >::ComputeFeatures(), AmSgmm2::ComputePerFrameVars(), AmSgmm2::ComputePerSpkDerivedVars(), EbwAmSgmm2Updater::ComputePhoneVecStats(), OnlineCmvn::ComputeStatsForFrame(), FmllrRawAccs::ConvertToSimpleStats(), kaldi::ConvolveSignals(), DiagGmmNormal::CopyFromDiagGmm(), FullGmm::CopyFromDiagGmm(), DiagGmm::CopyFromDiagGmm(), FullGmmNormal::CopyFromFullGmm(), FullGmm::CopyFromFullGmm(), DiagGmm::CopyFromFullGmm(), DiagGmmNormal::CopyToDiagGmm(), FullGmmNormal::CopyToFullGmm(), CuVectorBase< Real >::CopyToVec(), kaldi::CuVectorUnitTestCopyCross2(), kaldi::Debias(), DiscriminativeExampleSplitter::DoExcise(), kaldi::EBWUpdateGaussian(), DecodableNnetSimple::EnsureFrameIsComputed(), OnlinePitchFeatureImpl::ExtractFrame(), kaldi::ExtractWaveformRemainder(), FastNnetCombiner::FastNnetCombiner(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), kaldi::FilterMatrixRows(), FullGmm::GetComponentMean(), FullGmm::GetCovarsAndMeans(), kaldi::GetFeatureMeanAndVariance(), OnlineGenericBaseFeature< C >::GetFrame(), OnlineMatrixFeature::GetFrame(), OnlineCmvn::GetFrame(), OnlineTransform::GetFrame(), OnlineCacheFeature::GetFrame(), IvectorExtractor::GetIvectorDistribution(), RegtreeFmllrDiagGmm::GetLogDets(), LogisticRegression::GetLogPosteriors(), FullGmm::GetMeans(), NnetChainCombiner::GetNormalizedWeights(), NnetCombiner::GetNormalizedWeights(), LogisticRegression::GetObjfAndGrad(), DecodableNnetSimple::GetOutputForFrame(), SentenceAveragingComponent::GetParams(), NnetChainCombiner::GetParamsDeriv(), NnetCombiner::GetParamsDeriv(), OnlineCmvn::GetState(), AmSgmm2::GetSubstateMean(), NnetChainCombiner::GetUnnormalizedWeightsDeriv(), NnetCombiner::GetUnnormalizedWeightsDeriv(), AmSgmm2::GetVarScaledSubstateSpeakerMean(), NnetChainCombiner::GetWeights(), NnetCombiner::GetWeights(), kaldi::GpsrBasic(), kaldi::GpsrBB(), kaldi::GpsrGradient(), kaldi::GpsrObjective(), FmllrDiagGmmAccs::InitSingleFrameStats(), kaldi::IsmoothStatsDiagGmm(), IvectorExtractor::IvectorExtractor(), kaldi::LinearCgd(), FullGmm::LogLikelihoods(), DiagGmm::LogLikelihoods(), DiagGmm::LogLikelihoodsPreselect(), DecodableAmDiagGmmUnmapped::LogLikelihoodZeroBased(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), DiagGmm::merged_components_logdet(), FullGmm::MergedComponentsLogdet(), DiagGmm::MergeKmeans(), SoftmaxComponent::MixUp(), Vector< BaseFloat >::operator=(), OnlinePreconditionerSimple::PreconditionDirections(), OnlineNaturalGradientSimple::PreconditionDirections(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), kaldi::nnet3::ProcessFile(), kaldi::nnet2::ProcessFile(), FullGmmNormal::Rand(), kaldi::RealFftInefficient(), OptimizeLbfgs< Real >::Restart(), kaldi::ReverseFrames(), DiagGmm::SetComponentInvVar(), FullGmm::SetInvCovars(), Sgmm2PerSpkDerivedVars::SetSpeakerVector(), FullGmm::SetWeights(), DiagGmm::SetWeights(), kaldi::SlidingWindowCmnInternal(), kaldi::SolveQuadraticProblem(), SpMatrix< Real >::TopEigs(), Plda::TransformIvector(), kaldi::UnitTestAddDiagVecMat(), kaldi::UnitTestComplexFft(), kaldi::UnitTestCuVectorCopyFromVec(), kaldi::UnitTestDiagGmm(), UnitTestEstimateLda(), UnitTestFullGmm(), kaldi::UnitTestIoCross(), kaldi::UnitTestPower(), kaldi::UnitTestPowerAbs(), UnitTestReadWave(), kaldi::UnitTestRealFft(), kaldi::UnitTestRegtreeFmllrDiagGmm(), kaldi::UnitTestSimpleForVec(), kaldi::UnitTestSplitRadixComplexFft(), kaldi::UnitTestSplitRadixComplexFft2(), kaldi::UnitTestSplitRadixRealFft(), kaldi::UnitTestSubvector(), FmllrRawAccs::Update(), kaldi::UpdateEbwDiagGmm(), kaldi::UpdateEbwWeightsDiagGmm(), MleSgmm2SpeakerAccs::UpdateNoU(), PldaUnsupervisedAdaptor::UpdatePlda(), IvectorExtractorStats::UpdateProjection(), IvectorExtractorStats::UpdateVariances(), IvectorExtractorStats::UpdateWeight(), MleSgmm2SpeakerAccs::UpdateWithU(), VectorRandomizer::Value(), Vector< BaseFloat >::Vector(), PerElementScaleComponent::Vectorize(), PerElementOffsetComponent::Vectorize(), ConstantFunctionComponent::Vectorize(), and NonlinearComponent::Write().

227  {
228  KALDI_ASSERT(Dim() == v.Dim());
229  if (data_ != v.data_) {
230  std::memcpy(this->data_, v.data_, dim_ * sizeof(Real));
231  }
232 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
void CopyFromVec ( const VectorBase< OtherReal > &  v)

Copy data from another vector of different type (double vs. float)

Definition at line 255 of file kaldi-vector.cc.

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

255  {
256  KALDI_ASSERT(dim_ == other.Dim());
257  Real * __restrict__ ptr = data_;
258  const OtherReal * __restrict__ other_ptr = other.Data();
259  for (MatrixIndexT i = 0; i < dim_; i++)
260  ptr[i] = other_ptr[i];
261 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyFromVec ( const CuVectorBase< OtherReal > &  v)

Copy from CuVector. This is defined in ../cudamatrix/cu-vector.h.

Definition at line 347 of file cu-vector.h.

347  {
348  cu.CopyToVec(this);
349 }
void CopyRowFromMat ( const MatrixBase< Real > &  M,
MatrixIndexT  row 
)

Extracts a row of the matrix M.

Could also do this with this->Copy(M[row]).

Definition at line 392 of file kaldi-vector.cc.

References data_, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

Referenced by RegtreeFmllrDiagGmmAccs::AccumulateForGaussian(), RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), DiagGmm::GetComponentMean(), DiagGmm::GetComponentVariance(), LdaEstimate::GetStats(), main(), UnitTestEstimateDiagGmm(), kaldi::UnitTestEstimateMmieDiagGmm(), kaldi::UnitTestPosteriors(), kaldi::UnitTestRow(), and kaldi::UnitTestSpliceRows().

392  {
393  KALDI_ASSERT(row < mat.NumRows());
394  KALDI_ASSERT(dim_ == mat.NumCols());
395  const Real *mat_row = mat.RowData(row);
396  memcpy(data_, mat_row, sizeof(Real)*dim_);
397 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowFromMat ( const MatrixBase< OtherReal > &  M,
MatrixIndexT  row 
)

Extracts a row of the matrix M with type conversion.

Definition at line 401 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

401  {
402  KALDI_ASSERT(row < mat.NumRows());
403  KALDI_ASSERT(dim_ == mat.NumCols());
404  const OtherReal *mat_row = mat.RowData(row);
405  for (MatrixIndexT i = 0; i < dim_; i++)
406  data_[i] = static_cast<Real>(mat_row[i]);
407 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
template void CopyRowFromSp ( const SpMatrix< OtherReal > &  S,
MatrixIndexT  row 
)

Extracts a row of the symmetric matrix S.

Definition at line 416 of file kaldi-vector.cc.

References PackedMatrix< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, PackedMatrix< Real >::NumCols(), and PackedMatrix< Real >::NumRows().

Referenced by CompressedAffineXformStats::CopyFromAffineXformStats().

416  {
417  KALDI_ASSERT(row < sp.NumRows());
418  KALDI_ASSERT(dim_ == sp.NumCols());
419 
420  const OtherReal *sp_data = sp.Data();
421 
422  sp_data += (row*(row+1)) / 2; // takes us to beginning of this row.
423  MatrixIndexT i;
424  for (i = 0; i < row; i++) // copy consecutive elements.
425  data_[i] = static_cast<Real>(*(sp_data++));
426  for(; i < dim_; ++i, sp_data += i)
427  data_[i] = static_cast<Real>(*sp_data);
428 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowsFromMat ( const MatrixBase< Real > &  M)

Performs a row stack of the matrix M.

Definition at line 333 of file kaldi-vector.cc.

References MatrixBase< Real >::Data(), data_, rnnlm::i, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::RowData(), and MatrixBase< Real >::Stride().

Referenced by BasisFmllrAccus::AccuGradientScatter(), FmllrSgmm2Accs::AccumulateForFmllrSubspace(), kaldi::CuVectorUnitTestCopyFromMat(), FmllrRawAccs::GetAuxf(), LinearTransform::GetGradient(), LinearTransform::GetParams(), kaldi::nnet1::MomentStatistics(), kaldi::nnet2::PerturbTrainingExample(), LogisticRegression::TrainParameters(), kaldi::UnitTestSpliceRows(), and LstmNonlinearityComponent::Vectorize().

333  {
334  KALDI_ASSERT(dim_ == mat.NumCols() * mat.NumRows());
335 
336  Real *inc_data = data_;
337  const MatrixIndexT cols = mat.NumCols(), rows = mat.NumRows();
338 
339  if (mat.Stride() == mat.NumCols()) {
340  memcpy(inc_data, mat.Data(), cols*rows*sizeof(Real));
341  } else {
342  for (MatrixIndexT i = 0; i < rows; i++) {
343  // copy the data to the propper position
344  memcpy(inc_data, mat.RowData(i), cols * sizeof(Real));
345  // set new copy position
346  inc_data += cols;
347  }
348  }
349 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowsFromMat ( const MatrixBase< OtherReal > &  M)

Definition at line 353 of file kaldi-vector.cc.

References data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::RowData().

353  {
354  KALDI_ASSERT(dim_ == mat.NumCols() * mat.NumRows());
355  Real *vec_data = data_;
356  const MatrixIndexT cols = mat.NumCols(),
357  rows = mat.NumRows();
358 
359  for (MatrixIndexT i = 0; i < rows; i++) {
360  const OtherReal *mat_row = mat.RowData(i);
361  for (MatrixIndexT j = 0; j < cols; j++) {
362  vec_data[j] = static_cast<Real>(mat_row[j]);
363  }
364  vec_data += cols;
365  }
366 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowsFromMat ( const CuMatrixBase< Real > &  M)

The following is implemented in ../cudamatrix/cu-matrix.cc.

Definition at line 2292 of file cu-matrix.cc.

References CuMatrixBase< Real >::Data(), data_, Timer::Elapsed(), KALDI_ASSERT, CuMatrixBase< Real >::Mat(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), CuMatrixBase< Real >::RowData(), and CuMatrixBase< Real >::Stride().

2292  {
2293  KALDI_ASSERT(dim_ == mat.NumCols() * mat.NumRows());
2294 #if HAVE_CUDA == 1
2295  if (CuDevice::Instantiate().Enabled()) {
2296  Timer tim;
2297  if (mat.Stride() == mat.NumCols()) {
2298  cudaMemcpy(data_, mat.Data(), sizeof(Real)*dim_, cudaMemcpyDeviceToHost);
2299  } else {
2300  // we could definitely do better than the following.
2301  Real* vec_data = data_;
2302  for (MatrixIndexT r = 0; r < mat.NumRows(); r++) {
2303  cudaMemcpy(vec_data, mat.RowData(r), sizeof(Real) * mat.NumCols(),
2304  cudaMemcpyDeviceToHost);
2305  vec_data += mat.NumCols();
2306  }
2307  }
2308  CuDevice::Instantiate().AccuProfile("CuVectorBase::CopyRowsFromMat", tim.Elapsed());
2309  } else
2310 #endif
2311  {
2312  CopyRowsFromMat(mat.Mat());
2313  }
2314 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
Real* Data ( )
inline

Returns a pointer to the start of the vector's data.

Definition at line 65 of file kaldi-vector.h.

Referenced by kaldi::AccCmvnStats(), SpMatrix< Real >::AddDiagVec(), MatrixBase< Real >::AddDiagVecMat(), SpMatrix< Real >::AddMat2Sp(), SpMatrix< Real >::AddMat2Vec(), MatrixBase< Real >::AddMatDiagVec(), VectorBase< Real >::AddMatSvec(), VectorBase< Real >::AddMatVec(), kaldi::AddOuterProductPlusMinus(), VectorBase< Real >::AddSpVec(), SparseVector< Real >::AddToVec(), VectorBase< Real >::AddVec(), SpMatrix< Real >::AddVec2(), SpMatrix< Real >::AddVec2Sp(), MatrixBase< Real >::AddVecToCols(), MatrixBase< Real >::AddVecToRows(), MatrixBase< Real >::AddVecVec(), SpMatrix< Real >::AddVecVec(), PitchInterpolator::Backtrace(), kaldi::ComplexFft(), kaldi::ComplexFftRecursive(), kaldi::ComplexFt(), kaldi::nnet3::ComponentDotProducts(), SpectrogramComputer::Compute(), MfccComputer::Compute(), FbankComputer::Compute(), PlpComputer::Compute(), PitchFrameInfo::ComputeBacktraces(), OnlineNaturalGradient::ComputeEt(), OnlinePreconditioner::ComputeEt(), kaldi::ComputeLpc(), kaldi::ComputeVadEnergy(), MatrixBase< Real >::CopyColFromVec(), MatrixBase< Real >::CopyColsFromVec(), CompressedMatrix::CopyColToVec(), MatrixBase< Real >::CopyDiagFromVec(), SparseVector< Real >::CopyElementsToVec(), SparseMatrix< Real >::CopyElementsToVec(), CuVectorBase< Real >::CopyFromVec(), PackedMatrix< Real >::CopyFromVec(), VectorBase< Real >::CopyFromVec(), MatrixBase< Real >::CopyRowFromVec(), MatrixBase< Real >::CopyRowsFromVec(), CuMatrixBase< Real >::CopyRowsFromVec(), CompressedMatrix::CopyRowToVec(), CuVectorBase< Real >::CopyToVec(), VectorBase< Real >::DivElements(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), FullGmm::GaussianSelection(), DiagGmm::GaussianSelection(), AmSgmm2::GaussianSelection(), FullGmm::GaussianSelectionPreselect(), DiagGmm::GaussianSelectionPreselect(), MatrixBase< Real >::LapackGesvd(), SoftmaxComponent::MixUp(), VectorBase< Real >::MulElements(), SpMatrix< Real >::Qr(), kaldi::nnet3::ReadVectorAsChar(), kaldi::RealFft(), kaldi::SelectLags(), AmSgmm2::SplitSubstatesInGroup(), SubVector< Real >::SubVector(), kaldi::nnet3::SummarizeVector(), SpMatrix< Real >::Tridiagonalize(), kaldi::UnitTestEigSp(), kaldi::UnitTestPldaEstimation(), kaldi::UnitTestSimpleForMat(), kaldi::UnitTestSplitRadixComplexFft(), kaldi::UnitTestSplitRadixRealFft(), kaldi::UnitTestSplitRadixRealFftSpeed(), kaldi::UnitTestTopEigs(), kaldi::VecSpVec(), kaldi::VecSvec(), kaldi::VecVec(), and kaldi::nnet3::WriteVectorAsChar().

65 { return data_; }
Real * data_
data memory area
Definition: kaldi-vector.h:373
const Real* Data ( ) const
inline

Returns a pointer to the start of the vector's data (const).

Definition at line 68 of file kaldi-vector.h.

68 { return data_; }
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT Dim ( ) const
inline

Returns the dimension of the vector.

Definition at line 59 of file kaldi-vector.h.

Referenced by kaldi::AccCmvnStats(), kaldi::AccCmvnStatsWrapper(), OnlineGenericBaseFeature< C >::AcceptWaveform(), OnlinePitchFeatureImpl::AcceptWaveform(), NnetLdaStatsAccumulator::AccStatsFromOutput(), LdaEstimate::Accumulate(), AccumFullGmm::AccumulateForComponent(), AccumDiagGmm::AccumulateForComponent(), FmllrRawAccs::AccumulateForGmm(), kaldi::AccumulateForUtterance(), AccumDiagGmm::AccumulateFromDiag(), MlltAccs::AccumulateFromPosteriors(), HldaAccsDiagGmm::AccumulateFromPosteriors(), AccumFullGmm::AccumulateFromPosteriors(), AccumDiagGmm::AccumulateFromPosteriors(), MleAmSgmm2Accs::AccumulateFromPosteriors(), MleSgmm2SpeakerAccs::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), AccumulateMultiThreadedClass::AccumulateMultiThreadedClass(), VectorRandomizer::AddData(), SpMatrix< Real >::AddDiagVec(), MatrixBase< Real >::AddDiagVecMat(), SpMatrix< Real >::AddMat2Vec(), MatrixBase< Real >::AddMatDiagVec(), Nnet::AddNnet(), kaldi::nnet3::AddNnetComponents(), kaldi::AddNoise(), kaldi::AddOuterProductPlusMinus(), PldaUnsupervisedAdaptor::AddStats(), SparseVector< Real >::AddToVec(), SpMatrix< Real >::AddVec2(), SpMatrix< Real >::AddVec2Sp(), MatrixBase< Real >::AddVecToCols(), MatrixBase< Real >::AddVecToRows(), kaldi::AddVectorsOfUnequalLength(), kaldi::AddVectorsWithOffset(), MatrixBase< Real >::AddVecVec(), SpMatrix< Real >::AddVecVec(), kaldi::nnet2::AppendDiscriminativeExamples(), kaldi::AppendVector(), kaldi::AppendVectorToFeats(), kaldi::ApplyAffineTransform(), VectorBase< Real >::ApplyFloor(), SpMatrix< Real >::ApplyFloor(), ApplyFmllrXform(), VectorBase< Real >::ApplyLogAndCopy(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), AssertEqual(), kaldi::nnet2::AverageConstPart(), MleAmSgmm2Accs::Check(), NnetDiscriminativeSupervision::CheckDim(), NnetChainSupervision::CheckDim(), IvectorExtractorStats::CheckDims(), MleSgmm2SpeakerAccs::Clear(), kaldi::nnet2::CombineNnets(), kaldi::nnet2::CombineNnetsA(), IvectorExtractorStats::CommitStatsForM(), MleAmSgmm2Accs::CommitStatsForSpk(), kaldi::ComplexFft(), kaldi::ComplexFftRecursive(), kaldi::ComplexFt(), kaldi::nnet3::ComponentDotProducts(), Nnet::ComponentDotProducts(), FullGmm::ComponentLogLikelihood(), DiagGmm::ComponentLogLikelihood(), AmSgmm2::ComponentLogLikes(), DiagGmm::ComponentPosteriors(), SpectrogramComputer::Compute(), MelBanks::Compute(), MfccComputer::Compute(), FbankComputer::Compute(), OfflineFeatureTpl< F >::Compute(), PlpComputer::Compute(), kaldi::ComputeAndProcessKaldiPitch(), PitchFrameInfo::ComputeBacktraces(), FastNnetCombiner::ComputeCurrentNnet(), kaldi::ComputeEarlyReverbEnergy(), OnlineNaturalGradient::ComputeEt(), OnlinePreconditioner::ComputeEt(), AmSgmm2::ComputeFmllrPreXform(), AmSgmm2::ComputeGammaI(), kaldi::ComputeGconsts(), FullGmm::ComputeGconsts(), DiagGmm::ComputeGconsts(), AmSgmm2::ComputeHsmFromModel(), kaldi::ComputeKaldiPitch(), kaldi::ComputeKaldiPitchFirstPass(), kaldi::ComputeLifterCoeffs(), kaldi::ComputeLocalCost(), kaldi::ComputeLpc(), kaldi::ComputeNccf(), kaldi::nnet2::ComputeObjfAndGradient(), FastNnetCombiner::ComputeObjfAndGradient(), AmSgmm2::ComputePerFrameVars(), AmSgmm2::ComputePerSpkDerivedVars(), kaldi::ComputePowerSpectrum(), OnlineNaturalGradient::ComputeWt1(), OnlinePreconditioner::ComputeWt1(), OnlineNaturalGradient::ComputeZt(), OnlinePreconditioner::ComputeZt(), MatrixBase< Real >::Cond(), kaldi::ConvolveSignals(), MatrixBase< Real >::CopyColFromVec(), MatrixBase< Real >::CopyColsFromVec(), CompressedMatrix::CopyColToVec(), MatrixBase< Real >::CopyDiagFromVec(), SparseVector< Real >::CopyElementsToVec(), SparseMatrix< Real >::CopyElementsToVec(), DiagGmmNormal::CopyFromDiagGmm(), DiagGmm::CopyFromDiagGmm(), CuVectorBase< Real >::CopyFromVec(), PackedMatrix< Real >::CopyFromVec(), VectorBase< Real >::CopyFromVec(), MatrixBase< Real >::CopyRowFromVec(), MatrixBase< Real >::CopyRowsFromVec(), CuMatrixBase< Real >::CopyRowsFromVec(), CompressedMatrix::CopyRowToVec(), DiagGmmNormal::CopyToDiagGmm(), FullGmmNormal::CopyToFullGmm(), CuVectorBase< Real >::CopyToVec(), kaldi::nnet1::CountCorrectFramesWeighted(), kaldi::cu::CpuBackpropLstmNonlinearity(), kaldi::CreateEigenvalueMatrix(), CuVector< BaseFloat >::CuVector(), FmllrRawAccs::DataHasChanged(), FmllrDiagGmmAccs::DataHasChanged(), kaldi::Debias(), DeltaFeatures::DeltaFeatures(), MatrixBase< Real >::DestructiveSvd(), Plda::Dim(), OnlineTransform::Dim(), kaldi::Dither(), VectorBase< Real >::DivElements(), DecodableNnet3SimpleOnline::DoNnetComputation(), DecodableNnetSimple::DoNnetComputation(), kaldi::EBWUpdateGaussian(), SpMatrix< Real >::Eig(), kaldi::ElementwiseProductOfFft(), Sgmm2PerSpkDerivedVars::Empty(), FeatureTransformEstimate::EstimateInternal(), kaldi::EstimateSgmm2FmllrSubspace(), Xent::Eval(), Mse::Eval(), OnlinePitchFeatureImpl::ExtractFrame(), kaldi::ExtractWaveformRemainder(), kaldi::ExtractWindow(), FastNnetCombiner::FastNnetCombiner(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), AmSgmm2::GaussianSelection(), DiagGmm::Generate(), FullGmm::GetComponentMean(), DiagGmm::GetComponentMean(), DiagGmm::GetComponentVariance(), kaldi::GetFeatureMeanAndVariance(), OnlineCmvn::GetFrame(), OnlineProcessPitch::GetFrame(), OnlineSpliceFrames::GetFrame(), OnlineDeltaFeature::GetFrame(), OnlineAppendFeature::GetFrame(), OnlinePitchFeatureImpl::GetFrame(), ParametricRelu::GetGradient(), LinearTransform::GetGradient(), AffineTransform::GetGradient(), RecurrentComponent::GetGradient(), ParallelComponent::GetGradient(), LstmProjected::GetGradient(), ConvolutionalComponent::GetGradient(), Convolutional2DComponent::GetGradient(), BlstmProjected::GetGradient(), AddShift::GetGradient(), Rescale::GetGradient(), EigenvalueDecomposition< Real >::GetImagEigenvalues(), NnetChainCombiner::GetInitialParameters(), NnetCombiner::GetInitialParameters(), OnlineIvectorEstimationStats::GetIvector(), DecodableNnetSimple::GetIvectorDim(), IvectorExtractor::GetIvectorDistribution(), Nnet::GetLearningRates(), kaldi::nnet3::GetLearningRates(), RegtreeFmllrDiagGmm::GetLogDets(), LogisticRegression::GetLogPosteriors(), NnetChainCombiner::GetNnetParameters(), NnetCombiner::GetNnetParameters(), kaldi::GetOccs(), ParametricRelu::GetParams(), LinearTransform::GetParams(), AffineTransform::GetParams(), RecurrentComponent::GetParams(), ParallelComponent::GetParams(), FramePoolingComponent::GetParams(), MultiBasisComponent::GetParams(), LstmProjected::GetParams(), ConvolutionalComponent::GetParams(), SentenceAveragingComponent::GetParams(), Convolutional2DComponent::GetParams(), BlstmProjected::GetParams(), AddShift::GetParams(), Rescale::GetParams(), NnetChainCombiner::GetParamsDeriv(), NnetCombiner::GetParamsDeriv(), IvectorExtractor::GetPriorAuxf(), EigenvalueDecomposition< Real >::GetRealEigenvalues(), kaldi::GetSplitTargets(), AmSgmm2::GetSubstateMean(), AmSgmm2::GetSubstateSpeakerMean(), AmSgmm2::GetVarScaledSubstateSpeakerMean(), NnetChainCombiner::GetWeights(), NnetCombiner::GetWeights(), NnetChainCombiner::GetWeightsDeriv(), NnetCombiner::GetWeightsDeriv(), kaldi::GpsrBasic(), kaldi::GpsrBasicAlpha(), kaldi::GpsrBB(), kaldi::GpsrCalcLinearCoeff(), kaldi::GpsrGradient(), kaldi::GpsrObjective(), AmNnet::Info(), AmNnetSimple::Info(), AmNnet::Init(), FramePoolingComponent::InitData(), InitRand(), kaldi::InitRand(), FmllrRawAccs::InitSingleFrameStats(), Sgmm2FmllrGlobalParams::IsEmpty(), OnlineIvectorEstimationStats::IvectorDim(), kaldi::nnet2::KlDivergence(), kaldi::nnet3::KlDivergence(), MatrixBase< Real >::LapackGesvd(), NnetDiscriminativeUpdater::LatticeComputations(), FullGmm::LogLikelihoods(), DiagGmm::LogLikelihoods(), FullGmm::LogLikelihoodsPreselect(), DiagGmm::LogLikelihoodsPreselect(), DecodableAmDiagGmmRegtreeFmllr::LogLikelihoodZeroBased(), DecodableAmDiagGmmUnmapped::LogLikelihoodZeroBased(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), TransitionModel::MapUpdate(), TransitionModel::MapUpdateShared(), DiagGmm::merged_components_logdet(), FullGmm::MergedComponentsLogdet(), kaldi::nnet3::MergeSupervision(), SoftmaxComponent::MixUp(), kaldi::MleDiagGmmUpdate(), TransitionModel::MleUpdate(), TransitionModel::MleUpdateShared(), kaldi::nnet1::MomentStatistics(), MatrixBase< Real >::MulColsVec(), VectorBase< Real >::MulElements(), MatrixBase< Real >::MulRowsVec(), FullGmm::NumGauss(), DiagGmmNormal::NumGauss(), DiagGmm::NumGauss(), OnlinePitchFeatureImpl::OnlinePitchFeatureImpl(), FisherComputationClass::operator()(), CuVector< BaseFloat >::operator=(), Vector< BaseFloat >::operator=(), OptimizeLbfgs< Real >::OptimizeLbfgs(), PdfPrior::PdfPrior(), kaldi::Preemphasize(), TransitionModel::Print(), NnetChainCombiner::PrintParams(), NnetCombiner::PrintParams(), kaldi::nnet2::PrintPriorDiagnostics(), kaldi::nnet3::PrintPriorDiagnostics(), kaldi::nnet3::PrintVectorPerUpdatableComponent(), IvectorExtractorStats::PriorDiagnostics(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), NnetChainTrainer::ProcessOutputs(), NnetDiscriminativeTrainer::ProcessOutputs(), kaldi::ProcessWindow(), NnetDiscriminativeUpdater::Propagate(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::unittest::RandFullGaussFeatures(), MleAmSgmm2Accs::Read(), FullGmm::Read(), VectorBase< Real >::Read(), Vector< Real >::Read(), PermuteComponent::Read(), kaldi::ReadData(), kaldi::RealFft(), kaldi::RealFftInefficient(), OnlinePitchFeatureImpl::RecomputeBacktraces(), ArbitraryResample::Resample(), LinearResample::Resample(), DiagGmmNormal::Resize(), FullGmm::Resize(), FullGmmNormal::Resize(), DiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), Nnet::ScaleComponents(), kaldi::nnet3::ScaleNnetComponents(), DiagGmm::SetComponentInvVar(), DiagGmm::SetComponentMean(), ArbitraryResample::SetIndexes(), Nnet::SetLearningRates(), kaldi::nnet3::SetLearningRates(), PitchFrameInfo::SetNccfPov(), AmNnetSimple::SetNnet(), Nnet::SetParams(), ParametricRelu::SetParams(), LinearTransform::SetParams(), AffineTransform::SetParams(), RecurrentComponent::SetParams(), ParallelComponent::SetParams(), ConvolutionalComponent::SetParams(), Convolutional2DComponent::SetParams(), LstmProjected::SetParams(), AddShift::SetParams(), BlstmProjected::SetParams(), Rescale::SetParams(), kaldi::nnet2::SetPriors(), kaldi::nnet3::SetPriors(), AmNnet::SetPriors(), AmNnetSimple::SetPriors(), LinearResample::SetRemainder(), Sgmm2PerSpkDerivedVars::SetSpeakerVector(), FullGmm::SetWeights(), DiagGmm::SetWeights(), kaldi::SolveQuadraticProblem(), kaldi::SortSvd(), AmSgmm2::SplitSubstates(), SubVector< Real >::SubVector(), kaldi::nnet3::SummarizeVector(), MatrixBase< Real >::SymPosSemiDefEig(), SpMatrix< Real >::TopEigs(), RegtreeFmllrDiagGmm::TransformFeature(), Plda::TransformIvector(), kaldi::nnet3::TruncateDerivWeights(), kaldi::TypeOneUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestBlockConvolution(), kaldi::UnitTestConvolution(), kaldi::UnitTestCuMatrixSetRandUniform(), kaldi::UnitTestDelay(), UnitTestEstimateLda(), UnitTestLinearResample(), UnitTestLinearResample2(), kaldi::UnitTestPieces(), kaldi::UnitTestPldaEstimation(), UnitTestReadWave(), kaldi::UnitTestReplaceValue(), kaldi::UnitTestSetRandUniform(), kaldi::UnitTestSimple(), UnitTestSimple(), kaldi::UnitTestSvdNodestroy(), AffineComponent::UnVectorize(), BlockAffineComponent::UnVectorize(), RepeatedAffineComponent::UnVectorize(), ConvolutionComponent::UnVectorize(), LstmNonlinearityComponent::UnVectorize(), CompositeComponent::UnVectorize(), kaldi::nnet3::UnVectorizeNnet(), MleSgmm2SpeakerAccs::Update(), MleSgmm2SpeakerAccs::UpdateNoU(), NnetChainTrainer::UpdateParamsWithMaxChange(), NnetTrainer::UpdateParamsWithMaxChange(), PldaUnsupervisedAdaptor::UpdatePlda(), OnlinePitchFeatureImpl::UpdateRemainder(), EbwAmSgmm2Updater::UpdateSubstateWeights(), MleSgmm2SpeakerAccs::UpdateWithU(), RegtreeFmllrDiagGmm::Validate(), kaldi::VecMatVec(), kaldi::VecSpVec(), kaldi::VecSvec(), Vector< BaseFloat >::Vector(), AffineComponent::Vectorize(), BlockAffineComponent::Vectorize(), RepeatedAffineComponent::Vectorize(), ConvolutionComponent::Vectorize(), LstmNonlinearityComponent::Vectorize(), CompositeComponent::Vectorize(), kaldi::nnet3::VectorizeNnet(), kaldi::VectorToPosteriorEntry(), kaldi::VecVec(), AccumDiagGmm::Write(), and kaldi::nnet3::WriteVectorAsChar().

59 { return dim_; }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void DivElements ( const VectorBase< Real > &  v)

Divide element-by-element by a vector.

Definition at line 988 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, rnnlm::i, and KALDI_ASSERT.

Referenced by DiagGmm::GetComponentMean(), main(), and kaldi::SolveQuadraticProblem().

988  {
989  KALDI_ASSERT(dim_ == v.dim_);
990  for (MatrixIndexT i = 0; i < dim_; i++) {
991  data_[i] /= v.data_[i];
992  }
993 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void DivElements ( const VectorBase< OtherReal > &  v)

Divide element-by-element by a vector of different type.

Definition at line 997 of file kaldi-vector.cc.

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

997  {
998  KALDI_ASSERT(dim_ == v.Dim());
999  const OtherReal *other_ptr = v.Data();
1000  for (MatrixIndexT i = 0; i < dim_; i++) {
1001  data_[i] /= other_ptr[i];
1002  }
1003 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool IsZero ( Real  cutoff = 1.0e-06) const

Returns true if matrix is all zeros.

Definition at line 292 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by MleAmSgmm2Accs::Check(), MleAmSgmm2Accs::CommitStatsForSpk(), main(), and kaldi::UnitTestSimpleForVec().

292  {
293  Real abs_max = 0.0;
294  for (MatrixIndexT i = 0; i < Dim(); i++)
295  abs_max = std::max(std::abs(data_[i]), abs_max);
296  return (abs_max <= cutoff);
297 }
int32 MatrixIndexT
Definition: matrix-common.h:96
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
KALDI_DISALLOW_COPY_AND_ASSIGN ( VectorBase< Real >  )
protected
Real LogSumExp ( Real  prune = -1.0) const

Returns log(sum(exp())) without exp overflow If prune > 0.0, ignores terms less than the max - prune.

[Note: in future, if prune = 0.0, it will take the max. For now, use -1 if you don't want it to prune.]

Definition at line 749 of file kaldi-vector.cc.

References data_, kaldi::Exp(), rnnlm::i, kaldi::kLogZeroDouble, kaldi::kLogZeroFloat, kaldi::kMinLogDiffDouble, kaldi::kMinLogDiffFloat, and kaldi::Log().

Referenced by IvectorExtractor::GetAcousticAuxfWeight(), LogisticRegression::GetLogPosteriors(), FullGmm::LogLikelihood(), DiagGmm::LogLikelihood(), DecodableAmDiagGmmRegtreeMllr::LogLikelihoodZeroBased(), main(), kaldi::UnitTestDiagGmm(), UnitTestFullGmm(), MleAmSgmm2Updater::UpdatePhoneVectorsInternal(), MleAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateWGetStats(), and MleSgmm2SpeakerAccs::UpdateWithU().

749  {
750  Real sum;
751  if (sizeof(sum) == 8) sum = kLogZeroDouble;
752  else sum = kLogZeroFloat;
753  Real max_elem = Max(), cutoff;
754  if (sizeof(Real) == 4) cutoff = max_elem + kMinLogDiffFloat;
755  else cutoff = max_elem + kMinLogDiffDouble;
756  if (prune > 0.0 && max_elem - prune > cutoff) // explicit pruning...
757  cutoff = max_elem - prune;
758 
759  double sum_relto_max_elem = 0.0;
760 
761  for (MatrixIndexT i = 0; i < dim_; i++) {
762  BaseFloat f = data_[i];
763  if (f >= cutoff)
764  sum_relto_max_elem += Exp(f - max_elem);
765  }
766  return max_elem + Log(sum_relto_max_elem);
767 }
double Exp(double x)
Definition: kaldi-math.h:83
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
static const double kMinLogDiffDouble
Definition: kaldi-math.h:124
static const float kMinLogDiffFloat
Definition: kaldi-math.h:125
Real * data_
data memory area
Definition: kaldi-vector.h:373
const float kLogZeroFloat
Definition: kaldi-math.h:128
const double kLogZeroDouble
Definition: kaldi-math.h:129
Real Max ( ) const

Returns the maximum value of any element, or -infinity for the empty vector.

Definition at line 570 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by NnetDiscriminativeSupervision::CheckDim(), NnetChainSupervision::CheckDim(), kaldi::nnet3::ComputeAccuracy(), kaldi::ComputeEarlyReverbEnergy(), kaldi::CuVectorUnitTestMax(), OptimizeLbfgs< Real >::DoStep(), FeatureTransformEstimate::EstimateInternal(), AmNnetSimple::Info(), kaldi::InitGmmFromRandomFrames(), SpMatrix< Real >::LimitCond(), main(), CuVectorBase< Real >::Max(), SpMatrix< Real >::MaxAbsEig(), kaldi::MaxAbsolute(), kaldi::nnet1::MomentStatistics(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), kaldi::nnet3::PrintPriorDiagnostics(), kaldi::nnet2::PrintPriorDiagnostics(), OnlineNaturalGradient::SelfTest(), OnlinePreconditioner::SelfTest(), kaldi::SolveQuadraticMatrixProblem(), kaldi::SolveQuadraticProblem(), SpMatrix< Real >::SymPosSemiDefEig(), kaldi::TestVectorToPosteriorEntry(), kaldi::UnitTestMaxAbsEig(), kaldi::UnitTestMaxMin(), kaldi::UnitTestSparseVectorMax(), kaldi::UnitTestVectorMax(), and IvectorExtractorStats::UpdatePrior().

570  {
571  Real ans = - std::numeric_limits<Real>::infinity();
572  const Real *data = data_;
573  MatrixIndexT i, dim = dim_;
574  for (i = 0; i + 4 <= dim; i += 4) {
575  Real a1 = data[i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
576  if (a1 > ans || a2 > ans || a3 > ans || a4 > ans) {
577  Real b1 = (a1 > a2 ? a1 : a2), b2 = (a3 > a4 ? a3 : a4);
578  if (b1 > ans) ans = b1;
579  if (b2 > ans) ans = b2;
580  }
581  }
582  for (; i < dim; i++)
583  if (data[i] > ans) ans = data[i];
584  return ans;
585 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real Max ( MatrixIndexT index) const

Returns the maximum value of any element, and the associated index.

Error if vector is empty.

Definition at line 588 of file kaldi-vector.cc.

References data_, rnnlm::i, and KALDI_ERR.

588  {
589  if (dim_ == 0) KALDI_ERR << "Empty vector";
590  Real ans = - std::numeric_limits<Real>::infinity();
591  MatrixIndexT index = 0;
592  const Real *data = data_;
593  MatrixIndexT i, dim = dim_;
594  for (i = 0; i + 4 <= dim; i += 4) {
595  Real a1 = data[i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
596  if (a1 > ans || a2 > ans || a3 > ans || a4 > ans) {
597  if (a1 > ans) { ans = a1; index = i; }
598  if (a2 > ans) { ans = a2; index = i + 1; }
599  if (a3 > ans) { ans = a3; index = i + 2; }
600  if (a4 > ans) { ans = a4; index = i + 3; }
601  }
602  }
603  for (; i < dim; i++)
604  if (data[i] > ans) { ans = data[i]; index = i; }
605  *index_out = index;
606  return ans;
607 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real Min ( ) const

Returns the minimum value of any element, or +infinity for the empty vector.

Definition at line 610 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by OnlinePitchFeatureImpl::AcceptWaveform(), kaldi::ApplyHessianXformToGradient(), kaldi::ApplyInvHessianXformToChange(), NnetDiscriminativeSupervision::CheckDim(), NnetChainSupervision::CheckDim(), kaldi::CuVectorUnitTestMin(), OptimizeLbfgs< Real >::DoStep(), kaldi::EBWUpdateGaussian(), kaldi::GetFeatureMeanAndVariance(), PldaEstimator::GetOutput(), AmNnet::Info(), AmNnetSimple::Info(), main(), SpMatrix< Real >::MaxAbsEig(), kaldi::MaxAbsolute(), CuVectorBase< Real >::Min(), MatrixBase< BaseFloat >::MinSingularValue(), kaldi::nnet1::MomentStatistics(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlinePitchFeatureImpl::RecomputeBacktraces(), OnlineNaturalGradient::SelfTest(), OnlinePreconditioner::SelfTest(), Nnet::SetLearningRates(), SpMatrix< Real >::SymPosSemiDefEig(), TestSgmm2FmllrAccsIO(), TestSgmm2FmllrSubspace(), kaldi::UnitTestFloorUnit(), kaldi::UnitTestMaxAbsEig(), kaldi::UnitTestMaxMin(), kaldi::UnitTestVectorMin(), and IvectorExtractorStats::UpdatePrior().

610  {
611  Real ans = std::numeric_limits<Real>::infinity();
612  const Real *data = data_;
613  MatrixIndexT i, dim = dim_;
614  for (i = 0; i + 4 <= dim; i += 4) {
615  Real a1 = data[i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
616  if (a1 < ans || a2 < ans || a3 < ans || a4 < ans) {
617  Real b1 = (a1 < a2 ? a1 : a2), b2 = (a3 < a4 ? a3 : a4);
618  if (b1 < ans) ans = b1;
619  if (b2 < ans) ans = b2;
620  }
621  }
622  for (; i < dim; i++)
623  if (data[i] < ans) ans = data[i];
624  return ans;
625 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real Min ( MatrixIndexT index) const

Returns the minimum value of any element, and the associated index.

Error if vector is empty.

Definition at line 628 of file kaldi-vector.cc.

References data_, rnnlm::i, and KALDI_ERR.

628  {
629  if (dim_ == 0) KALDI_ERR << "Empty vector";
630  Real ans = std::numeric_limits<Real>::infinity();
631  MatrixIndexT index = 0;
632  const Real *data = data_;
633  MatrixIndexT i, dim = dim_;
634  for (i = 0; i + 4 <= dim; i += 4) {
635  Real a1 = data[i], a2 = data[i+1], a3 = data[i+2], a4 = data[i+3];
636  if (a1 < ans || a2 < ans || a3 < ans || a4 < ans) {
637  if (a1 < ans) { ans = a1; index = i; }
638  if (a2 < ans) { ans = a2; index = i + 1; }
639  if (a3 < ans) { ans = a3; index = i + 2; }
640  if (a4 < ans) { ans = a4; index = i + 3; }
641  }
642  }
643  for (; i < dim; i++)
644  if (data[i] < ans) { ans = data[i]; index = i; }
645  *index_out = index;
646  return ans;
647 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * data_
data memory area
Definition: kaldi-vector.h:373
void MulElements ( const VectorBase< OtherReal > &  v)

Multipy element-by-element by another vector of different type.

Definition at line 962 of file kaldi-vector.cc.

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

962  {
963  KALDI_ASSERT(dim_ == v.Dim());
964  const OtherReal *other_ptr = v.Data();
965  for (MatrixIndexT i = 0; i < dim_; i++) {
966  data_[i] *= other_ptr[i];
967  }
968 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void MulTp ( const TpMatrix< Real > &  M,
const MatrixTransposeType  trans 
)

Multiplies this vector by lower-triangular marix: *this <– *this *M.

Definition at line 151 of file kaldi-vector.cc.

References kaldi::cblas_Xtpmv(), PackedMatrix< Real >::Data(), data_, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by VectorBase< Real >::AddTpVec(), FullGmm::Perturb(), FullGmm::Split(), kaldi::UnitTestCuVectorMulTp(), and kaldi::UnitTestMulTp().

152  {
153  KALDI_ASSERT(M.NumRows() == dim_);
154  cblas_Xtpmv(trans,M.Data(),M.NumRows(),data_,1);
155 }
void cblas_Xtpmv(MatrixTransposeType trans, const float *Mdata, const int num_rows, float *y, const int y_inc)
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real Norm ( Real  p) const

Compute the p-th norm of the vector.

Definition at line 508 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ASSERT, VectorBase< Real >::Norm(), and VectorBase< Real >::Scale().

Referenced by OptimizeLbfgs< Real >::AcceptStep(), VectorBase< Real >::ApproxEqual(), OptimizeLbfgs< Real >::ComputeHifNeeded(), IvectorExtractor::GetIvectorDistribution(), PldaEstimator::GetOutput(), kaldi::GpsrBasic(), kaldi::GpsrBB(), main(), VectorBase< Real >::Norm(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OptimizeLbfgs< Real >::Restart(), Plda::TransformIvector(), kaldi::UnitTestGpsr(), kaldi::UnitTestLbfgs(), kaldi::UnitTestLinearCgd(), kaldi::UnitTestNorm(), kaldi::UnitTestSimpleForVec(), IvectorExtractorStats::UpdatePrior(), and IvectorExtractTask::~IvectorExtractTask().

508  {
509  KALDI_ASSERT(p >= 0.0);
510  Real sum = 0.0;
511  if (p == 0.0) {
512  for (MatrixIndexT i = 0; i < dim_; i++)
513  if (data_[i] != 0.0) sum += 1.0;
514  return sum;
515  } else if (p == 1.0) {
516  for (MatrixIndexT i = 0; i < dim_; i++)
517  sum += std::abs(data_[i]);
518  return sum;
519  } else if (p == 2.0) {
520  for (MatrixIndexT i = 0; i < dim_; i++)
521  sum += data_[i] * data_[i];
522  return std::sqrt(sum);
523  } else if (p == std::numeric_limits<Real>::infinity()){
524  for (MatrixIndexT i = 0; i < dim_; i++)
525  sum = std::max(sum, std::abs(data_[i]));
526  return sum;
527  } else {
528  Real tmp;
529  bool ok = true;
530  for (MatrixIndexT i = 0; i < dim_; i++) {
531  tmp = pow(std::abs(data_[i]), p);
532  if (tmp == HUGE_VAL) // HUGE_VAL is what pow returns on error.
533  ok = false;
534  sum += tmp;
535  }
536  tmp = pow(sum, static_cast<Real>(1.0/p));
537  KALDI_ASSERT(tmp != HUGE_VAL); // should not happen here.
538  if (ok) {
539  return tmp;
540  } else {
541  Real maximum = this->Max(), minimum = this->Min(),
542  max_abs = std::max(maximum, -minimum);
543  KALDI_ASSERT(max_abs > 0); // Or should not have reached here.
544  Vector<Real> tmp(*this);
545  tmp.Scale(1.0 / max_abs);
546  return tmp.Norm(p) * max_abs;
547  }
548  }
549 }
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real operator() ( MatrixIndexT  i) const
inline

Indexing operator (const).

Definition at line 71 of file kaldi-vector.h.

71  {
72  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
73  static_cast<UnsignedMatrixIndexT>(dim_));
74  return *(data_ + i);
75  }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:182
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real& operator() ( MatrixIndexT  i)
inline

Indexing operator (non-const).

Definition at line 78 of file kaldi-vector.h.

78  {
79  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
80  static_cast<UnsignedMatrixIndexT>(dim_));
81  return *(data_ + i);
82  }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:182
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT RandCategorical ( ) const

This function returns a random index into this vector, chosen with probability proportional to the corresponding element.

Requires that this->Min() >= 0 and this->Sum() > 0.

Definition at line 310 of file kaldi-vector.cc.

References data_, rnnlm::i, KALDI_ASSERT, and kaldi::RandUniform().

Referenced by FullGmmNormal::Rand(), and kaldi::UnitTestRandCategorical().

310  {
311  kaldi::RandomState rstate;
312  Real sum = this->Sum();
313  KALDI_ASSERT(this->Min() >= 0.0 && sum > 0.0);
314  Real r = RandUniform(&rstate) * sum;
315  Real *data = this->data_;
316  MatrixIndexT dim = this->dim_;
317  Real running_sum = 0.0;
318  for (MatrixIndexT i = 0; i < dim; i++) {
319  running_sum += data[i];
320  if (r < running_sum) return i;
321  }
322  return dim_ - 1; // Should only happen if RandUniform()
323  // returns exactly 1, or due to roundoff.
324 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
Real Sum() const
Returns sum of the elements.
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
SubVector<Real> Range ( const MatrixIndexT  o,
const MatrixIndexT  l 
)
inline

Returns a sub-vector of a vector (a range of elements).

Parameters
o[in] Origin, 0 < o < Dim()
l[in] Length 0 < l < Dim()-o
Returns
A SubVector object that aliases the data of the Vector object. See SubVector class for details

Definition at line 89 of file kaldi-vector.h.

Referenced by OnlineGenericBaseFeature< C >::AcceptWaveform(), RegtreeFmllrDiagGmmAccs::AccumulateForGaussian(), RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), FmllrRawAccs::AccumulateFromPosteriors(), FmllrSgmm2Accs::AccumulateFromPosteriors(), VectorRandomizer::AddData(), kaldi::AddVectorsOfUnequalLength(), kaldi::AddVectorsWithOffset(), kaldi::AppendVector(), Fmpe::ApplyProjection(), Fmpe::ApplyProjectionReverse(), FmllrRawAccs::CommitSingleFrameStats(), FmllrDiagGmmAccs::CommitSingleFrameStats(), MelBanks::Compute(), PlpComputer::Compute(), BasisFmllrEstimate::ComputeAmDiagPrecond(), kaldi::ComputeEarlyReverbEnergy(), Sgmm2Project::ComputeLdaTransform(), FmllrRawAccs::DataHasChanged(), OnlinePitchFeatureImpl::ExtractFrame(), CompressedAffineXformStats::ExtractOneG(), kaldi::ExtractWindow(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), FmllrDiagGmmAccs::FmllrDiagGmmAccs(), kaldi::FmllrInnerUpdate(), ParametricRelu::GetGradient(), Nnet::GetGradient(), AffineTransform::GetGradient(), RecurrentComponent::GetGradient(), ParallelComponent::GetGradient(), LstmProjected::GetGradient(), ConvolutionalComponent::GetGradient(), Convolutional2DComponent::GetGradient(), BlstmProjected::GetGradient(), IvectorExtractor::GetIvectorDistribution(), Nnet::GetParams(), ParametricRelu::GetParams(), AffineTransform::GetParams(), RecurrentComponent::GetParams(), ParallelComponent::GetParams(), FramePoolingComponent::GetParams(), MultiBasisComponent::GetParams(), LstmProjected::GetParams(), ConvolutionalComponent::GetParams(), Convolutional2DComponent::GetParams(), BlstmProjected::GetParams(), kaldi::GpsrCalcLinearCoeff(), kaldi::GpsrGradient(), kaldi::GpsrObjective(), FramePoolingComponent::InitData(), FmllrRawAccs::InitSingleFrameStats(), main(), MelBanks::MelBanks(), kaldi::MergeFullGmm(), SoftmaxComponent::MixUp(), kaldi::RealFftInefficient(), Nnet::SetParams(), ParametricRelu::SetParams(), AffineTransform::SetParams(), RecurrentComponent::SetParams(), ParallelComponent::SetParams(), MultiBasisComponent::SetParams(), ConvolutionalComponent::SetParams(), Convolutional2DComponent::SetParams(), LstmProjected::SetParams(), BlstmProjected::SetParams(), FullGmm::Split(), DiagGmm::Split(), SpMatrix< Real >::TopEigs(), kaldi::UnitTestCuVectorSum(), UnitTestLinearResample(), kaldi::UnitTestRange(), UnitTestVectorRandomizer(), Nnet::UnVectorize(), RepeatedAffineComponent::UnVectorize(), AffineComponent::UnVectorize(), BlockAffineComponent::UnVectorize(), ConvolutionComponent::UnVectorize(), VectorRandomizer::Value(), RepeatedAffineComponent::Vectorize(), AffineComponent::Vectorize(), BlockAffineComponent::Vectorize(), and ConvolutionComponent::Vectorize().

89  {
90  return SubVector<Real>(*this, o, l);
91  }
const SubVector<Real> Range ( const MatrixIndexT  o,
const MatrixIndexT  l 
) const
inline

Returns a const sub-vector of a vector (a range of elements).

Parameters
o[in] Origin, 0 < o < Dim()
l[in] Length 0 < l < Dim()-o
Returns
A SubVector object that aliases the data of the Vector object. See SubVector class for details

Definition at line 98 of file kaldi-vector.h.

99  {
100  return SubVector<Real>(*this, o, l);
101  }
void Read ( std::istream &  in,
bool  binary,
bool  add = false 
)

Reads from C++ stream (option to add to existing contents).

Throws exception on failure

Definition at line 1063 of file kaldi-vector.cc.

References VectorBase< Real >::Dim(), KALDI_ERR, and Vector< Real >::Read().

Referenced by kaldi::operator>>().

1063  {
1064  if (add) {
1065  Vector<Real> tmp(Dim());
1066  tmp.Read(is, binary, false); // read without adding.
1067  if (this->Dim() != tmp.Dim()) {
1068  KALDI_ERR << "VectorBase::Read, size mismatch " << this->Dim()<<" vs. "<<tmp.Dim();
1069  }
1070  this->AddVec(1.0, tmp);
1071  return;
1072  } // now assume add == false.
1073 
1074  // In order to avoid rewriting this, we just declare a Vector and
1075  // use it to read the data, then copy.
1076  Vector<Real> tmp;
1077  tmp.Read(is, binary, false);
1078  if (tmp.Dim() != Dim())
1079  KALDI_ERR << "VectorBase<Real>::Read, size mismatch "
1080  << Dim() << " vs. " << tmp.Dim();
1081  CopyFromVec(tmp);
1082 }
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
#define KALDI_ERR
Definition: kaldi-error.h:127
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
void ReplaceValue ( Real  orig,
Real  changed 
)

Set each element to y = (x == orig ? changed : x).

Definition at line 953 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by kaldi::UnitTestCuVectorReplaceValue(), and kaldi::UnitTestReplaceValue().

953  {
954  Real *data = data_;
955  for (MatrixIndexT i = 0; i < dim_; i++)
956  if (data[i] == orig) data[i] = changed;
957 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void Scale ( Real  alpha)

Multiplies all elements by this constant.

Definition at line 940 of file kaldi-vector.cc.

References kaldi::cblas_Xscal(), and data_.

Referenced by FmllrSgmm2Accs::AccumulateForFmllrSubspace(), FmllrDiagGmmAccs::AccumulateForGmm(), FmllrRawAccs::AccumulateForGmm(), RegtreeMllrDiagGmmAccs::AccumulateForGmm(), RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), AccumAmDiagGmm::AccumulateForGmmTwofeats(), kaldi::AddNoise(), RegressionTree::BuildTree(), kaldi::ClusterGaussiansToUbm(), PlpComputer::Compute(), kaldi::ComputeAmGmmFeatureDeriv(), Fmpe::ComputeC(), kaldi::ComputeFeatureNormalizingTransform(), AmSgmm2::ComputeFmllrPreXform(), NnetChainCombiner::ComputeObjfAndDerivFromNnet(), NnetCombiner::ComputeObjfAndDerivFromNnet(), EbwAmSgmm2Updater::ComputePhoneVecStats(), FmllrRawAccs::ConvertToSimpleStats(), kaldi::CuVectorUnitTestScale(), kaldi::Debias(), DeltaFeatures::DeltaFeatures(), MatrixBase< Real >::DestructiveSvd(), kaldi::EBWUpdateGaussian(), BasisFmllrEstimate::EstimateFmllrBasis(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), kaldi::FmllrInnerUpdate(), kaldi::GenRandStats(), IvectorExtractor::GetAcousticAuxfVariance(), kaldi::GetFeatDeriv(), kaldi::GetFeatureMeanAndVariance(), PldaEstimator::GetOutput(), LdaEstimate::GetStats(), kaldi::GpsrBB(), NonlinearComponent::Info(), LstmNonlinearityComponent::Info(), ConstantFunctionComponent::InitFromConfig(), kaldi::unittest::InitRandDiagGmm(), kaldi::unittest::InitRandFullGmm(), kaldi::InitRandomGmm(), kaldi::IsmoothStatsDiagGmm(), main(), kaldi::MapDiagGmmUpdate(), FullGmm::Merge(), DiagGmm::merged_components_logdet(), FullGmm::MergedComponentsLogdet(), DiagGmm::MergeKmeans(), kaldi::MleDiagGmmUpdate(), kaldi::MleFullGmmUpdate(), TransitionModel::MleUpdate(), TransitionModel::MleUpdateShared(), VectorBase< Real >::Norm(), PdfPrior::PdfPrior(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), DiscriminativeObjectiveInfo::Print(), IvectorExtractorStats::PriorDiagnostics(), rand_diag_gmm(), kaldi::RealFft(), OnlinePitchFeatureImpl::RecomputeBacktraces(), DiagGmm::RemoveComponent(), FullGmm::RemoveComponent(), LdaEstimate::Scale(), IvectorExtractorUtteranceStats::Scale(), AccumFullGmm::Scale(), AccumDiagGmm::Scale(), VectorClusterable::Scale(), OnlineIvectorEstimationStats::Scale(), ShiftedDeltaFeatures::ShiftedDeltaFeatures(), kaldi::SlidingWindowCmnInternal(), AccumDiagGmm::SmoothStats(), kaldi::TestClusterKMeansVector(), kaldi::nnet3::TestNnetComponentUpdatable(), kaldi::TestVectorToPosteriorEntry(), SpMatrix< Real >::TopEigs(), Plda::TransformIvector(), kaldi::UnitTestAddDiagMat2(), kaldi::UnitTestAddDiagMatMat(), kaldi::UnitTestAddDiagVecMat(), kaldi::nnet2::UnitTestAmNnet(), kaldi::UnitTestComplexFft(), kaldi::UnitTestComplexFt(), kaldi::UnitTestCuVectorAddColSumMat(), kaldi::UnitTestCuVectorAddColSumMatLarge(), kaldi::UnitTestCuVectorAddRowSumMat(), kaldi::UnitTestCuVectorAddRowSumMatLarge(), kaldi::UnitTestCuVectorAddVec(), UnitTestEstimateDiagGmm(), UnitTestEstimateFullGmm(), UnitTestEstimateLda(), kaldi::UnitTestEstimateMmieDiagGmm(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), kaldi::UnitTestLbfgs(), kaldi::nnet2::UnitTestNnetDecodable(), kaldi::UnitTestNorm(), kaldi::UnitTestPldaEstimation(), kaldi::nnet2::UnitTestPreconditionDirectionsOnline(), kaldi::nnet3::UnitTestPreconditionDirectionsOnline(), kaldi::UnitTestRealFft(), kaldi::UnitTestSimpleForVec(), kaldi::UnitTestSplitRadixComplexFft(), kaldi::UnitTestSplitRadixRealFft(), kaldi::UnitTestVector(), HldaAccsDiagGmm::Update(), NnetChainTrainer::UpdateParamsWithMaxChange(), NnetTrainer::UpdateParamsWithMaxChange(), IvectorExtractorStats::UpdatePrior(), EbwAmSgmm2Updater::UpdateSubstateWeights(), EbwAmSgmm2Updater::UpdateU(), MleAmSgmm2Updater::UpdateU(), MleAmSgmm2Updater::UpdateW(), MleSgmm2SpeakerAccs::UpdateWithU(), VectorClusterable::VectorClusterable(), NonlinearComponent::Write(), and LstmNonlinearityComponent::Write().

940  {
941  cblas_Xscal(dim_, alpha, data_, 1);
942 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void cblas_Xscal(const int N, const float alpha, float *data, const int inc)
Real * data_
data memory area
Definition: kaldi-vector.h:373
void Set ( Real  f)

Set all members of a vector to a specified value.

Definition at line 327 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by VectorBase< Real >::AddColSumMat(), VectorBase< Real >::AddRowSumMat(), MatrixBase< Real >::AddVecToCols(), MatrixBase< Real >::AddVecToRows(), kaldi::nnet2::CombineNnetsA(), kaldi::ComputeLocalCost(), PitchInterpolator::Forward(), kaldi::nnet2::GetInitialModel(), FastNnetCombiner::GetInitialModel(), NnetChainCombiner::GetInitialParameters(), NnetCombiner::GetInitialParameters(), FastNnetCombiner::GetInitialParams(), kaldi::nnet2::GetInitialScaleParams(), LogisticRegression::GetLogPosteriors(), kaldi::nnet2::GetUpdateDirection(), kaldi::GpsrCalcLinearCoeff(), OnlinePreconditionerSimple::InitDefault(), OnlineNaturalGradientSimple::InitDefault(), OnlineNaturalGradient::InitDefault(), OnlinePreconditioner::InitDefault(), main(), kaldi::ReadData(), AmNnet::ResizeOutputLayer(), VectorClusterable::SetZero(), kaldi::SlidingWindowCmnInternal(), Plda::SmoothWithinClassCovariance(), VectorClusterable::Sub(), TestSgmm2FmllrAccsIO(), TestSgmm2FmllrSubspace(), TestSgmm2PreXform(), kaldi::TrainOneIter(), kaldi::nnet3::TruncateDerivWeights(), kaldi::UnitTestAddVecToCols(), kaldi::UnitTestAddVecToRows(), UnitTestRegressionTree(), and kaldi::UnitTestSimpleForVec().

327  {
328  // Why not use memset here?
329  for (MatrixIndexT i = 0; i < dim_; i++) { data_[i] = f; }
330 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void SetRandn ( )

Set vector to random normally-distributed noise.

Definition at line 300 of file kaldi-vector.cc.

References data_, rnnlm::i, kaldi::RandGauss(), and kaldi::RandGauss2().

Referenced by kaldi::CuVectorUnitTestAddVecVec(), ConstantFunctionComponent::InitFromConfig(), LogisticRegression::MixUp(), SoftmaxComponent::MixUp(), PitchInterpolator::MultiplyObsProb(), FullGmm::Perturb(), kaldi::nnet2::PerturbTrainingExample(), FullGmmNormal::Rand(), FullGmm::Split(), kaldi::TestClusterKMeansVector(), kaldi::TestClusterUtilsVector(), kaldi::TestVectorToPosteriorEntry(), SpMatrix< Real >::TopEigs(), kaldi::UnitTestAddDiagVecMat(), kaldi::UnitTestAddMatDiagVec(), kaldi::UnitTestAddVec2Sp(), kaldi::UnitTestAddVecToColsSpeed(), kaldi::UnitTestAddVecToRowsSpeed(), kaldi::UnitTestAddVecVec(), kaldi::nnet2::UnitTestAmNnet(), kaldi::UnitTestBackpropLstmNonlinearity(), kaldi::UnitTestBlockConvolution(), kaldi::UnitTestConvolution(), kaldi::UnitTestCuMatrixAddVecVec(), kaldi::UnitTestCuSpMatrixAddVec2(), kaldi::UnitTestCuVectorAddTp(), kaldi::UnitTestCuVectorCopyFromVec(), kaldi::UnitTestCuVectorMulTp(), kaldi::UnitTestCuVectorReplaceValue(), kaldi::UnitTestFloorCeiling(), kaldi::UnitTestLinearCgd(), UnitTestLinearResample(), UnitTestLinearResample2(), kaldi::UnitTestMulTp(), kaldi::nnet2::UnitTestNnetDecodable(), kaldi::UnitTestPldaEstimation(), kaldi::nnet2::UnitTestPreconditionDirectionsOnline(), kaldi::nnet3::UnitTestPreconditionDirectionsOnline(), kaldi::UnitTestReplaceValue(), kaldi::UnitTestSolve(), kaldi::UnitTestSparseVectorAddToVec(), kaldi::UnitTestSparseVectorMax(), kaldi::UnitTestSparseVectorSum(), kaldi::UnitTestSparseVectorVecSvec(), kaldi::nnet3::UnitTestSummarizeVector(), kaldi::UnitTestTriVecSolver(), kaldi::UnitTestVecmul(), kaldi::UnitTestVector(), kaldi::UnitTestVectorMax(), and kaldi::UnitTestVectorMin().

300  {
301  kaldi::RandomState rstate;
302  MatrixIndexT last = (Dim() % 2 == 1) ? Dim() - 1 : Dim();
303  for (MatrixIndexT i = 0; i < last; i += 2) {
304  kaldi::RandGauss2(data_ + i, data_ + i +1, &rstate);
305  }
306  if (Dim() != last) data_[last] = static_cast<Real>(kaldi::RandGauss(&rstate));
307 }
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
int32 MatrixIndexT
Definition: matrix-common.h:96
void RandGauss2(float *a, float *b, RandomState *state)
Definition: kaldi-math.cc:146
Real * data_
data memory area
Definition: kaldi-vector.h:373
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59
void SetZero ( )

Set vector to all zeros.

Definition at line 287 of file kaldi-vector.cc.

References data_.

Referenced by MinimumBayesRisk::AccStats(), MleSgmm2SpeakerAccs::Clear(), FmllrDiagGmmAccs::CommitSingleFrameStats(), MleAmSgmm2Accs::CommitStatsForSpk(), MfccComputer::Compute(), PlpComputer::Compute(), OptimizeLbfgs< Real >::ComputeNewDirection(), kaldi::ConvolveSignals(), SparseVector< Real >::CopyElementsToVec(), kaldi::DiagGmmToStats(), kaldi::EBWUpdateGaussian(), OnlinePitchFeatureImpl::ExtractFrame(), kaldi::FFTbasedBlockConvolveSignals(), FullGmm::GetComponentMean(), OnlineIvectorEstimationStats::GetIvector(), NnetChainCombiner::GetNnetParameters(), NnetCombiner::GetNnetParameters(), NnetChainCombiner::GetParamsDeriv(), NnetCombiner::GetParamsDeriv(), kaldi::GpsrBasic(), FmllrRawAccs::InitSingleFrameStats(), FmllrDiagGmmAccs::InitSingleFrameStats(), main(), FullGmm::Merge(), FullGmm::MergePreselect(), DeltaFeatures::Process(), ShiftedDeltaFeatures::Process(), AccumFullGmm::Read(), LdaEstimate::Read(), AccumFullGmm::SetZero(), FmllrRawAccs::SetZero(), AccumDiagGmm::SetZero(), CuMatrixBase< Real >::SetZeroAboveDiag(), kaldi::UnitTestCuVectorAddColSumMat(), kaldi::UnitTestCuVectorAddColSumMatLarge(), kaldi::UnitTestCuVectorAddRowSumMat(), kaldi::UnitTestCuVectorAddRowSumMatLarge(), and LdaEstimate::ZeroAccumulators().

287  {
288  std::memset(data_, 0, dim_ * sizeof(Real));
289 }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
void Sigmoid ( const VectorBase< Real > &  src)

Sets each element of *this to the sigmoid function of the corresponding element of "src".

Definition at line 915 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, kaldi::Exp(), rnnlm::i, and KALDI_ASSERT.

Referenced by MatrixBase< Real >::Sigmoid().

915  {
916  KALDI_ASSERT(dim_ == src.dim_);
917  for (MatrixIndexT i = 0; i < dim_; i++) {
918  Real x = src.data_[i];
919  // We aim to avoid floating-point overflow here.
920  if (x > 0.0) {
921  x = 1.0 / (1.0 + Exp(-x));
922  } else {
923  Real ex = Exp(x);
924  x = ex / (ex + 1.0);
925  }
926  data_[i] = x;
927  }
928 }
double Exp(double x)
Definition: kaldi-math.h:83
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT SizeInBytes ( ) const
inline

Returns the size in memory of the vector, in bytes.

Definition at line 62 of file kaldi-vector.h.

62 { return (dim_*sizeof(Real)); }
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
void Solve ( const TpMatrix< Real > &  M,
const MatrixTransposeType  trans 
)

If trans == kNoTrans, solves M x = b, where b is the value of *this at input and x is the value of *this at output.

If trans == kTrans, solves M' x = b. Does not test for M being singular or near-singular, so test it before calling this routine.

Definition at line 158 of file kaldi-vector.cc.

References kaldi::cblas_Xtpsv(), PackedMatrix< Real >::Data(), data_, KALDI_ASSERT, and PackedMatrix< Real >::NumRows().

Referenced by kaldi::UnitTestTriVecSolver().

159  {
160  KALDI_ASSERT(M.NumRows() == dim_);
161  cblas_Xtpsv(trans, M.Data(), M.NumRows(), data_, 1);
162 }
void cblas_Xtpsv(MatrixTransposeType trans, const float *Mdata, const int num_rows, float *y, const int y_inc)
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Real Sum ( ) const

Returns sum of the elements.

Definition at line 684 of file kaldi-vector.cc.

References data_, and rnnlm::i.

Referenced by OnlinePitchFeatureImpl::AcceptWaveform(), AccumAmDiagGmm::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriors(), FmllrDiagGmmAccs::AccumulateFromPosteriorsPreselect(), IvectorExtractorStats::AuxfPerFrame(), Nnet::Check(), kaldi::ClusterGaussiansToUbm(), IvectorExtractorStats::CommitStatsForWPoint(), kaldi::nnet3::ComputeAccuracy(), kaldi::ComputeCorrelation(), AmSgmm2::ComputeFmllrPreXform(), NnetChainCombiner::ComputeObjfAndDerivFromNnet(), NnetCombiner::ComputeObjfAndDerivFromNnet(), EbwAmSgmm2Updater::ComputePhoneVecStats(), kaldi::ComputeVadEnergy(), OnlineNaturalGradient::ComputeWt1(), OnlinePreconditioner::ComputeWt1(), kaldi::CuVectorUnitTestCopyDiagFromMat(), Xent::Eval(), Mse::Eval(), DiagGmm::Generate(), kaldi::GenRandStats(), IvectorExtractor::GetAcousticAuxf(), IvectorExtractor::GetAcousticAuxfVariance(), IvectorExtractor::GetAuxf(), IvectorExtractor::GetIvectorDistWeight(), LdaEstimate::GetStats(), AmNnet::Info(), AmNnetSimple::Info(), kaldi::unittest::InitRandDiagGmm(), kaldi::InitRandomGmm(), IvectorExtractorStats::IvectorVarianceDiagnostic(), kaldi::nnet3::KlDivergence(), kaldi::nnet2::KlDivergence(), AffineComponent::LimitRank(), main(), TransitionModel::MapUpdate(), TransitionModel::MapUpdateShared(), kaldi::MleFullGmmUpdate(), TransitionModel::MleUpdate(), TransitionModel::MleUpdateShared(), kaldi::nnet1::MomentStatistics(), IvectorExtractorUtteranceStats::NumFrames(), LimitRankClass::operator()(), PdfPrior::PdfPrior(), BackpropTruncationComponent::PrecomputeIndexes(), OnlineNaturalGradientSimple::PreconditionDirectionsCpu(), OnlinePreconditionerSimple::PreconditionDirectionsCpu(), OnlineNaturalGradient::PreconditionDirectionsInternal(), OnlinePreconditioner::PreconditionDirectionsInternal(), IvectorExtractorStats::PriorDiagnostics(), kaldi::ProcessWindow(), DiagGmm::RemoveComponent(), FullGmm::RemoveComponent(), MleAmSgmm2Updater::RenormalizeN(), MleAmSgmm2Updater::RenormalizeV(), OnlineNaturalGradient::ReorthogonalizeXt1(), OnlinePreconditioner::ReorthogonalizeXt1(), Xent::Report(), OnlineNaturalGradient::SelfTest(), OnlinePreconditioner::SelfTest(), CuVectorBase< Real >::Sum(), kaldi::nnet3::SummarizeVector(), LdaEstimate::TotCount(), kaldi::TraceMatMat(), kaldi::nnet2::UnitTestAmNnet(), kaldi::UnitTestDiagGmm(), kaldi::UnitTestEigSymmetric(), UnitTestFullGmm(), kaldi::nnet2::UnitTestNnetDecodable(), kaldi::UnitTestPosteriors(), kaldi::UnitTestRandCategorical(), kaldi::UnitTestRange(), kaldi::UnitTestResize(), kaldi::UnitTestSimpleForVec(), kaldi::UnitTestSparseVectorSum(), MleSgmm2SpeakerAccs::Update(), IvectorExtractorStats::Update(), kaldi::UpdateEbwWeightsDiagGmm(), EbwAmSgmm2Updater::UpdateM(), EbwAmSgmm2Updater::UpdateN(), MleSgmm2SpeakerAccs::UpdateNoU(), IvectorExtractorStats::UpdateProjections(), OnlinePitchFeatureImpl::UpdateRemainder(), EbwAmSgmm2Updater::UpdateSubstateWeights(), MleAmSgmm2Updater::UpdateSubstateWeights(), EbwAmSgmm2Updater::UpdateU(), MleAmSgmm2Updater::UpdateU(), IvectorExtractorStats::UpdateVariances(), EbwAmSgmm2Updater::UpdateVars(), MleAmSgmm2Updater::UpdateVars(), EbwAmSgmm2Updater::UpdateW(), MleAmSgmm2Updater::UpdateW(), IvectorExtractorStats::UpdateWeights(), MleSgmm2SpeakerAccs::UpdateWithU(), and MleAmSgmm2Accs::~MleAmSgmm2Accs().

684  {
685  double sum = 0.0;
686  for (MatrixIndexT i = 0; i < dim_; i++) { sum += data_[i]; }
687  return sum;
688 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
Real SumLog ( ) const

Returns sum of the logs of the elements.

More efficient than just taking log of each. Will return NaN if any elements are negative.

Definition at line 691 of file kaldi-vector.cc.

References data_, rnnlm::i, and kaldi::Log().

Referenced by Plda::LogLikelihoodRatio(), main(), and GaussClusterable::Objf().

691  {
692  double sum_log = 0.0;
693  double prod = 1.0;
694  for (MatrixIndexT i = 0; i < dim_; i++) {
695  prod *= data_[i];
696  // Possible future work (arnab): change these magic values to pre-defined
697  // constants
698  if (prod < 1.0e-10 || prod > 1.0e+10) {
699  sum_log += Log(prod);
700  prod = 1.0;
701  }
702  }
703  if (prod != 1.0) sum_log += Log(prod);
704  return sum_log;
705 }
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
double Log(double x)
Definition: kaldi-math.h:100
Real * data_
data memory area
Definition: kaldi-vector.h:373
void Tanh ( const VectorBase< Real > &  src)

Sets each element of *this to the tanh of the corresponding element of "src".

Definition at line 877 of file kaldi-vector.cc.

References data_, VectorBase< Real >::data_, VectorBase< Real >::dim_, kaldi::Exp(), rnnlm::i, and KALDI_ASSERT.

Referenced by MatrixBase< Real >::Tanh().

877  {
878  KALDI_ASSERT(dim_ == src.dim_);
879  for (MatrixIndexT i = 0; i < dim_; i++) {
880  Real x = src.data_[i];
881  if (x > 0.0) {
882  Real inv_expx = Exp(-x);
883  x = -1.0 + 2.0 / (1.0 + inv_expx * inv_expx);
884  } else {
885  Real expx = Exp(x);
886  x = 1.0 - 2.0 / (1.0 + expx * expx);
887  }
888  data_[i] = x;
889  }
890 }
double Exp(double x)
Definition: kaldi-math.h:83
int32 MatrixIndexT
Definition: matrix-common.h:96
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:375
Real * data_
data memory area
Definition: kaldi-vector.h:373
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Write ( std::ostream &  Out,
bool  binary 
) const

Writes to C++ stream (option to write in binary).

Definition at line 1202 of file kaldi-vector.cc.

References rnnlm::i, KALDI_ASSERT, KALDI_ERR, kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by main(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), UnitTestRegressionTree(), AmNnet::Write(), NnetExample::Write(), AmNnetSimple::Write(), AccumFullGmm::Write(), LdaEstimate::Write(), Sgmm2FmllrGlobalParams::Write(), Plda::Write(), FullGmm::Write(), TransitionModel::Write(), VectorClusterable::Write(), DiagGmm::Write(), DiscriminativeNnetExample::Write(), IvectorExtractor::Write(), CuVector< Real >::Write(), OnlineIvectorEstimationStats::Write(), IvectorExtractorStats::Write(), NonlinearComponent::Write(), LstmNonlinearityComponent::Write(), and kaldi::nnet3::WriteVectorAsChar().

1202  {
1203  if (!os.good()) {
1204  KALDI_ERR << "Failed to write vector to stream: stream not good";
1205  }
1206  if (binary) {
1207  std::string my_token = (sizeof(Real) == 4 ? "FV" : "DV");
1208  WriteToken(os, binary, my_token);
1209 
1210  int32 size = Dim(); // make the size 32-bit on disk.
1211  KALDI_ASSERT(Dim() == (MatrixIndexT) size);
1212  WriteBasicType(os, binary, size);
1213  os.write(reinterpret_cast<const char*>(Data()), sizeof(Real) * size);
1214  } else {
1215  os << " [ ";
1216  for (MatrixIndexT i = 0; i < Dim(); i++)
1217  os << (*this)(i) << " ";
1218  os << "]\n";
1219  }
1220  if (!os.good())
1221  KALDI_ERR << "Failed to write vector to stream";
1222 }
int32 MatrixIndexT
Definition: matrix-common.h:96
#define KALDI_ERR
Definition: kaldi-error.h:127
Real * Data()
Returns a pointer to the start of the vector's data.
Definition: kaldi-vector.h:65
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
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:59

Friends And Related Function Documentation

friend class CuVector< Real >
friend

Definition at line 349 of file kaldi-vector.h.

friend class CuVectorBase< Real >
friend

Definition at line 348 of file kaldi-vector.h.

friend class VectorBase< double >
friend

Definition at line 346 of file kaldi-vector.h.

friend class VectorBase< float >
friend

Definition at line 347 of file kaldi-vector.h.

Member Data Documentation


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