A class representing a vector. More...
#include <kaldi-vector.h>
Public Member Functions | |
Vector () | |
Constructor that takes no arguments. Initializes to empty. More... | |
Vector (const MatrixIndexT s, MatrixResizeType resize_type=kSetZero) | |
Constructor with specific size. More... | |
template<typename OtherReal > | |
Vector (const CuVectorBase< OtherReal > &cu) | |
Copy constructor from CUDA vector This is defined in ../cudamatrix/cu-vector.h. More... | |
Vector (const Vector< Real > &v) | |
Copy constructor. The need for this is controversial. More... | |
Vector (const VectorBase< Real > &v) | |
Copy-constructor from base-class, needed to copy from SubVector. More... | |
template<typename OtherReal > | |
Vector (const VectorBase< OtherReal > &v) | |
Type conversion constructor. More... | |
void | Swap (Vector< Real > *other) |
Swaps the contents of *this and *other. Shallow swap. More... | |
~Vector () | |
Destructor. Deallocates memory. More... | |
void | Read (std::istream &in, bool binary, bool add=false) |
Read function using C++ streams. More... | |
void | Resize (MatrixIndexT length, MatrixResizeType resize_type=kSetZero) |
Set vector to a specified size (can be zero). More... | |
void | RemoveElement (MatrixIndexT i) |
Remove one element and shifts later elements down. More... | |
Vector< Real > & | operator= (const Vector< Real > &other) |
Assignment operator. More... | |
Vector< Real > & | operator= (const VectorBase< Real > &other) |
Assignment operator that takes VectorBase. More... | |
Public Member Functions inherited from VectorBase< Real > | |
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... | |
void | SetRandUniform () |
Sets to numbers uniformly distributed on (0,1) 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 | Floor (const VectorBase< Real > &v, Real floor_val, MatrixIndexT *floored_count=nullptr) |
Applies floor to all elements. More... | |
void | Ceiling (const VectorBase< Real > &v, Real ceil_val, MatrixIndexT *ceiled_count=nullptr) |
Applies ceiling to all elements. More... | |
void | Pow (const VectorBase< Real > &v, Real power) |
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... | |
void | ApplyFloor (Real floor_val, MatrixIndexT *floored_count=nullptr) |
Applies floor to all elements. More... | |
void | ApplyCeiling (Real ceil_val, MatrixIndexT *ceiled_count=nullptr) |
Applies ceiling to all elements. 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) |
Multiply element-by-element by another vector. More... | |
template<typename OtherReal > | |
void | MulElements (const VectorBase< OtherReal > &v) |
Multiply 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 vectors: 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 matrix: *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) |
Private Member Functions | |
void | Init (const MatrixIndexT dim) |
Init assumes the current contents of the class are invalid (i.e. More... | |
void | Destroy () |
Destroy function, called internally. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from VectorBase< Real > | |
~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 inherited from VectorBase< Real > | |
Real * | data_ |
data memory area More... | |
MatrixIndexT | dim_ |
dimension of vector More... | |
A class representing a vector.
This class provides a way to work with vectors in kaldi. It encapsulates basic operations and memory optimizations.
Definition at line 406 of file kaldi-vector.h.
|
inline |
Constructor that takes no arguments. Initializes to empty.
Definition at line 409 of file kaldi-vector.h.
|
inlineexplicit |
Constructor with specific size.
Sets to all-zero by default if set_zero == false, memory contents are undefined.
Definition at line 413 of file kaldi-vector.h.
|
explicit |
Copy constructor from CUDA vector This is defined in ../cudamatrix/cu-vector.h.
Definition at line 398 of file cu-vector.h.
Copy constructor. The need for this is controversial.
Definition at line 423 of file kaldi-vector.h.
|
inlineexplicit |
Copy-constructor from base-class, needed to copy from SubVector.
Definition at line 429 of file kaldi-vector.h.
|
inlineexplicit |
Type conversion constructor.
Definition at line 436 of file kaldi-vector.h.
|
inline |
Destructor. Deallocates memory.
Definition at line 454 of file kaldi-vector.h.
|
private |
Destroy function, called internally.
Deallocates memory and sets object to empty vector.
we need to free the data block if it was defined
Definition at line 279 of file kaldi-vector.cc.
|
inlineprivate |
Init assumes the current contents of the class are invalid (i.e.
junk or has already been freed), and it sets the vector to newly allocated memory with the specified dimension. dim == 0 is acceptable. The memory contents pointed to by data_ will be undefined.
Definition at line 167 of file kaldi-vector.cc.
Assignment operator.
Definition at line 473 of file kaldi-vector.h.
|
inline |
Assignment operator that takes VectorBase.
Definition at line 480 of file kaldi-vector.h.
Read function using C++ streams.
Can also add to existing contents of matrix.
Definition at line 1109 of file kaldi-vector.cc.
Referenced by FramePoolingComponent::InitData(), main(), kaldi::operator>>(), PdfPrior::PdfPrior(), AccumFullGmm::Read(), LdaEstimate::Read(), AccumDiagGmm::Read(), Plda::Read(), RestrictedAttentionComponent::Read(), CuVector< float >::Read(), VectorBase< float >::Read(), Vector< float >::Read(), IvectorExtractorStats::Read(), PermuteComponent::Read(), kaldi::nnet3::ReadVectorAsChar(), kaldi::UnitTestIo(), kaldi::UnitTestIoCross(), and UnitTestReadWave().
void RemoveElement | ( | MatrixIndexT | i | ) |
Remove one element and shifts later elements down.
Definition at line 269 of file kaldi-vector.cc.
Referenced by kaldi::UnitTestRemoveRow().
void Resize | ( | MatrixIndexT | length, |
MatrixResizeType | resize_type = kSetZero |
||
) |
Set vector to a specified size (can be zero).
The value of the new data depends on resize_type: -if kSetZero, the new data will be zero -if kUndefined, the new data will be undefined -if kCopyData, the new data will be the same as the old data in any shared positions, and zero elsewhere. This function takes time proportional to the number of data elements.
Definition at line 190 of file kaldi-vector.cc.
Referenced by OnlineSpeexEncoder::AcceptWaveform(), OnlineGenericBaseFeature< C >::AcceptWaveform(), RestrictedAttentionComponent::Add(), kaldi::AppendVector(), ApplyFmllrXform(), Plda::ApplyTransform(), kaldi::ComplexFftRecursive(), AmSgmm2::ComponentLogLikes(), DiagGmm::ComponentPosteriors(), kaldi::ComputeAndSubtractMean(), Plda::ComputeDerivedVars(), IvectorExtractor::ComputeDerivedVars(), AmSgmm2::ComputeFmllrPreXform(), AmSgmm2::ComputeGammaI(), kaldi::ComputePca(), BasisFmllrEstimate::ComputeTransform(), kaldi::ComputeVadEnergy(), FmllrRawAccs::ConvertToSimpleStats(), kaldi::ConvolveSignals(), OnlineSpeexDecoder::Decode(), kaldi::EstPca(), Xent::Eval(), kaldi::ExtractObjectRange(), kaldi::ExtractWindow(), kaldi::FFTbasedBlockConvolveSignals(), kaldi::FFTbasedConvolveSignals(), FmllrRawAccs::FmllrRawAccs(), TcpServer::GetChunk(), kaldi::GetEqualLoudnessVector(), kaldi::GetFeatureMeanAndVariance(), Nnet::GetGradient(), kaldi::nnet2::GetInitialScaleParams(), LogisticRegression::GetLogPosteriors(), kaldi::GetOccs(), Nnet::GetParams(), SingleUtteranceNnet2DecoderThreaded::GetRemainingWaveform(), MleAmSgmm2Accs::GetStateOccupancies(), LdaEstimate::GetStats(), OnlineSpeexDecoder::GetWaveform(), LdaEstimate::Init(), OnlinePreconditionerSimple::InitDefault(), OnlineNaturalGradientSimple::InitDefault(), TransitionModel::InitStats(), OnlineGenericBaseFeature< C >::InputFinished(), IvectorExtractor::IvectorExtractor(), IvectorExtractorStats::IvectorExtractorStats(), AffineComponent::LimitRank(), FullGmm::LogLikelihoods(), DiagGmm::LogLikelihoods(), FullGmm::LogLikelihoodsPreselect(), DiagGmm::LogLikelihoodsPreselect(), main(), MleSgmm2SpeakerAccs::MleSgmm2SpeakerAccs(), OnlineIvectorFeature::OnlineIvectorFeature(), LimitRankClass::operator()(), IvectorExtractTask::operator()(), OnlinePaSource::Read(), MleAmSgmm2Accs::Read(), kaldi::ReadData(), kaldi::nnet3::ReadVectorAsChar(), LinearResample::Resample(), DiagGmmNormal::Resize(), FullGmmNormal::Resize(), AccumFullGmm::Resize(), AccumDiagGmm::Resize(), Sgmm2PerFrameDerivedVars::Resize(), MleAmSgmm2Accs::ResizeAccumulators(), kaldi::nnet3::RunNnetComputation(), kaldi::SelectLags(), RestrictedAttentionComponent::StoreStats(), CuVector< float >::Swap(), kaldi::TypeThreeUsage(), UnitTestEstimateLda(), kaldi::UnitTestIo(), UnitTestLinearResample(), UnitTestReadWave(), kaldi::UnitTestRegtreeFmllrDiagGmm(), kaldi::UnitTestResize(), kaldi::nnet3::UnitTestSummarizeVector(), MleSgmm2SpeakerAccs::UpdateNoU(), MleSgmm2SpeakerAccs::UpdateWithU(), and NonlinearComponent::Write().
void Swap | ( | Vector< Real > * | other | ) |
Swaps the contents of *this and *other. Shallow swap.
Definition at line 1297 of file kaldi-vector.cc.
Referenced by BatchedXvectorComputer::OutputXvector(), kaldi::nnet3::PrintParameterStats(), Vector< float >::Resize(), and CuVector< float >::Swap().