kaldi-vector.h
Go to the documentation of this file.
1 // matrix/kaldi-vector.h
2 
3 // Copyright 2009-2012 Ondrej Glembek; Microsoft Corporation; Lukas Burget;
4 // Saarland University (Author: Arnab Ghoshal);
5 // Ariya Rastrow; Petr Schwarz; Yanmin Qian;
6 // Karel Vesely; Go Vivace Inc.; Arnab Ghoshal
7 // Wei Shi;
8 // 2015 Guoguo Chen
9 // 2017 Daniel Galvez
10 // 2019 Yiwen Shao
11 
12 // See ../../COPYING for clarification regarding multiple authors
13 //
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
22 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
23 // MERCHANTABLITY OR NON-INFRINGEMENT.
24 // See the Apache 2 License for the specific language governing permissions and
25 // limitations under the License.
26 
27 #ifndef KALDI_MATRIX_KALDI_VECTOR_H_
28 #define KALDI_MATRIX_KALDI_VECTOR_H_ 1
29 
30 #include "matrix/matrix-common.h"
31 
32 namespace kaldi {
33 
36 
40 template<typename Real>
41 class VectorBase {
42  public:
44  void SetZero();
45 
47  bool IsZero(Real cutoff = 1.0e-06) const; // replace magic number
48 
50  void Set(Real f);
51 
53  void SetRandn();
54 
56  void SetRandUniform();
57 
62 
64  inline MatrixIndexT Dim() const { return dim_; }
65 
67  inline MatrixIndexT SizeInBytes() const { return (dim_*sizeof(Real)); }
68 
70  inline Real* Data() { return data_; }
71 
73  inline const Real* Data() const { return data_; }
74 
76  inline Real operator() (MatrixIndexT i) const {
77  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
78  static_cast<UnsignedMatrixIndexT>(dim_));
79  return *(data_ + i);
80  }
81 
83  inline Real & operator() (MatrixIndexT i) {
84  KALDI_PARANOID_ASSERT(static_cast<UnsignedMatrixIndexT>(i) <
85  static_cast<UnsignedMatrixIndexT>(dim_));
86  return *(data_ + i);
87  }
88 
95  return SubVector<Real>(*this, o, l);
96  }
97 
104  const MatrixIndexT l) const {
105  return SubVector<Real>(*this, o, l);
106  }
107 
109  void CopyFromVec(const VectorBase<Real> &v);
110 
112  template<typename OtherReal>
114 
116  template<typename OtherReal>
117  void CopyFromVec(const VectorBase<OtherReal> &v);
118 
120  template<typename OtherReal>
121  void CopyFromVec(const CuVectorBase<OtherReal> &v);
122 
125  void Floor(const VectorBase<Real> &v, Real floor_val, MatrixIndexT *floored_count = nullptr);
126 
129  void Ceiling(const VectorBase<Real> &v, Real ceil_val, MatrixIndexT *ceiled_count = nullptr);
130 
131  void Pow(const VectorBase<Real> &v, Real power);
132 
136  void ApplyLog();
137 
139  void ApplyLogAndCopy(const VectorBase<Real> &v);
140 
142  void ApplyExp();
143 
145  void ApplyAbs();
146 
149  inline void ApplyFloor(Real floor_val, MatrixIndexT *floored_count = nullptr) {
150  this->Floor(*this, floor_val, floored_count);
151  };
152 
155  inline void ApplyCeiling(Real ceil_val, MatrixIndexT *ceiled_count = nullptr) {
156  this->Ceiling(*this, ceil_val, ceiled_count);
157  };
158 
160  MatrixIndexT ApplyFloor(const VectorBase<Real> &floor_vec);
161 
164  Real ApplySoftMax();
165 
169  Real ApplyLogSoftMax();
170 
172  void Tanh(const VectorBase<Real> &src);
173 
176  void Sigmoid(const VectorBase<Real> &src);
177 
179  inline void ApplyPow(Real power) {
180  this->Pow(*this, power);
181  };
182 
186  void ApplyPowAbs(Real power, bool include_sign=false);
187 
189  Real Norm(Real p) const;
190 
192  bool ApproxEqual(const VectorBase<Real> &other, float tol = 0.01) const;
193 
195  void InvertElements();
196 
199  template<typename OtherReal>
200  void AddVec(const Real alpha, const VectorBase<OtherReal> &v);
201 
203  void AddVec2(const Real alpha, const VectorBase<Real> &v);
204 
207  template<typename OtherReal>
208  void AddVec2(const Real alpha, const VectorBase<OtherReal> &v);
209 
212  void AddMatVec(const Real alpha, const MatrixBase<Real> &M,
213  const MatrixTransposeType trans, const VectorBase<Real> &v,
214  const Real beta); // **beta previously defaulted to 0.0**
215 
218  void AddMatSvec(const Real alpha, const MatrixBase<Real> &M,
219  const MatrixTransposeType trans, const VectorBase<Real> &v,
220  const Real beta); // **beta previously defaulted to 0.0**
221 
222 
225  void AddSpVec(const Real alpha, const SpMatrix<Real> &M,
226  const VectorBase<Real> &v, const Real beta); // **beta previously defaulted to 0.0**
227 
230  void AddTpVec(const Real alpha, const TpMatrix<Real> &M,
231  const MatrixTransposeType trans, const VectorBase<Real> &v,
232  const Real beta); // **beta previously defaulted to 0.0**
233 
235  void ReplaceValue(Real orig, Real changed);
236 
238  void MulElements(const VectorBase<Real> &v);
240  template<typename OtherReal>
241  void MulElements(const VectorBase<OtherReal> &v);
242 
244  void DivElements(const VectorBase<Real> &v);
246  template<typename OtherReal>
247  void DivElements(const VectorBase<OtherReal> &v);
248 
250  void Add(Real c);
251 
253  // this <-- alpha * v .* r + beta*this .
254  void AddVecVec(Real alpha, const VectorBase<Real> &v,
255  const VectorBase<Real> &r, Real beta);
256 
259  void AddVecDivVec(Real alpha, const VectorBase<Real> &v,
260  const VectorBase<Real> &r, Real beta);
261 
263  void Scale(Real alpha);
264 
266  void MulTp(const TpMatrix<Real> &M, const MatrixTransposeType trans);
267 
273  void Solve(const TpMatrix<Real> &M, const MatrixTransposeType trans);
274 
276  void CopyRowsFromMat(const MatrixBase<Real> &M);
277  template<typename OtherReal>
278  void CopyRowsFromMat(const MatrixBase<OtherReal> &M);
279 
281  void CopyRowsFromMat(const CuMatrixBase<Real> &M);
282 
284  void CopyColsFromMat(const MatrixBase<Real> &M);
285 
288  void CopyRowFromMat(const MatrixBase<Real> &M, MatrixIndexT row);
290  template<typename OtherReal>
292 
294  template<typename OtherReal>
295  void CopyRowFromSp(const SpMatrix<OtherReal> &S, MatrixIndexT row);
296 
298  template<typename OtherReal>
300 
302  void CopyDiagFromMat(const MatrixBase<Real> &M);
303 
305  void CopyDiagFromPacked(const PackedMatrix<Real> &M);
306 
307 
309  inline void CopyDiagFromSp(const SpMatrix<Real> &M) { CopyDiagFromPacked(M); }
310 
312  inline void CopyDiagFromTp(const TpMatrix<Real> &M) { CopyDiagFromPacked(M); }
313 
315  Real Max() const;
316 
319  Real Max(MatrixIndexT *index) const;
320 
322  Real Min() const;
323 
326  Real Min(MatrixIndexT *index) const;
327 
329  Real Sum() const;
330 
334  Real SumLog() const;
335 
337  void AddRowSumMat(Real alpha, const MatrixBase<Real> &M, Real beta = 1.0);
338 
340  void AddColSumMat(Real alpha, const MatrixBase<Real> &M, Real beta = 1.0);
341 
345  void AddDiagMat2(Real alpha, const MatrixBase<Real> &M,
346  MatrixTransposeType trans = kNoTrans, Real beta = 1.0);
347 
351  void AddDiagMatMat(Real alpha, const MatrixBase<Real> &M, MatrixTransposeType transM,
352  const MatrixBase<Real> &N, MatrixTransposeType transN,
353  Real beta = 1.0);
354 
359  Real LogSumExp(Real prune = -1.0) const;
360 
363  void Read(std::istream &in, bool binary, bool add = false);
364 
366  void Write(std::ostream &Out, bool binary) const;
367 
368  friend class VectorBase<double>;
369  friend class VectorBase<float>;
370  friend class CuVectorBase<Real>;
371  friend class CuVector<Real>;
372  protected:
377 
379  explicit VectorBase(): data_(NULL), dim_(0) {
381  }
382 
383 // Took this out since it is not currently used, and it is possible to create
384 // objects where the allocated memory is not the same size as dim_ : Arnab
385 // /// Initializer from a pointer and a size; keeps the pointer internally
386 // /// (ownership or non-ownership depends on the child class).
387 // explicit VectorBase(Real* data, MatrixIndexT dim)
388 // : data_(data), dim_(dim) {}
389 
390  // Arnab : made this protected since it is unsafe too.
392  void CopyFromPtr(const Real* Data, MatrixIndexT sz);
393 
395  Real* data_;
399 }; // class VectorBase
400 
405 template<typename Real>
406 class Vector: public VectorBase<Real> {
407  public:
409  Vector(): VectorBase<Real>() {}
410 
413  explicit Vector(const MatrixIndexT s,
414  MatrixResizeType resize_type = kSetZero)
415  : VectorBase<Real>() { Resize(s, resize_type); }
416 
419  template<typename OtherReal>
420  explicit Vector(const CuVectorBase<OtherReal> &cu);
421 
423  Vector(const Vector<Real> &v) : VectorBase<Real>() { // (cannot be explicit)
424  Resize(v.Dim(), kUndefined);
425  this->CopyFromVec(v);
426  }
427 
429  explicit Vector(const VectorBase<Real> &v) : VectorBase<Real>() {
430  Resize(v.Dim(), kUndefined);
431  this->CopyFromVec(v);
432  }
433 
435  template<typename OtherReal>
436  explicit Vector(const VectorBase<OtherReal> &v): VectorBase<Real>() {
437  Resize(v.Dim(), kUndefined);
438  this->CopyFromVec(v);
439  }
440 
441 // Took this out since it is unsafe : Arnab
442 // /// Constructor from a pointer and a size; copies the data to a location
443 // /// it owns.
444 // Vector(const Real* Data, const MatrixIndexT s): VectorBase<Real>() {
445 // Resize(s);
446  // CopyFromPtr(Data, s);
447 // }
448 
449 
451  void Swap(Vector<Real> *other);
452 
454  ~Vector() { Destroy(); }
455 
458  void Read(std::istream &in, bool binary, bool add = false);
459 
467  void Resize(MatrixIndexT length, MatrixResizeType resize_type = kSetZero);
468 
470  void RemoveElement(MatrixIndexT i);
471 
473  Vector<Real> &operator = (const Vector<Real> &other) {
474  Resize(other.Dim(), kUndefined);
475  this->CopyFromVec(other);
476  return *this;
477  }
478 
480  Vector<Real> &operator = (const VectorBase<Real> &other) {
481  Resize(other.Dim(), kUndefined);
482  this->CopyFromVec(other);
483  return *this;
484  }
485  private:
490  void Init(const MatrixIndexT dim);
491 
493  void Destroy();
494 
495 };
496 
497 
500 template<typename Real>
501 class SubVector : public VectorBase<Real> {
502  public:
506  SubVector(const VectorBase<Real> &t, const MatrixIndexT origin,
507  const MatrixIndexT length) : VectorBase<Real>() {
508  // following assert equiv to origin>=0 && length>=0 &&
509  // origin+length <= rt.dim_
510  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(origin)+
511  static_cast<UnsignedMatrixIndexT>(length) <=
512  static_cast<UnsignedMatrixIndexT>(t.Dim()));
513  this->data_ = const_cast<Real*> (t.Data()+origin);
514  this->dim_ = length;
515  }
516 
520  this->data_ = const_cast<Real*> (M.Data());
521  this->dim_ = (M.NumRows()*(M.NumRows()+1))/2;
522  }
523 
525  SubVector(const SubVector &other) : VectorBase<Real> () {
526  // this copy constructor needed for Range() to work in base class.
527  this->data_ = other.data_;
528  this->dim_ = other.dim_;
529  }
530 
534  SubVector(const Real *data, MatrixIndexT length) : VectorBase<Real> () {
535  this->data_ = const_cast<Real*>(data);
536  this->dim_ = length;
537  }
538 
541  this->data_ = const_cast<Real*>(matrix.RowData(row));
542  this->dim_ = matrix.NumCols();
543  }
544 
546 
547  private:
549  SubVector & operator = (const SubVector &other) {}
550 };
551 
557 template<typename Real>
558 std::ostream & operator << (std::ostream & out, const VectorBase<Real> & v);
559 
562 template<typename Real>
563 std::istream & operator >> (std::istream & in, VectorBase<Real> & v);
564 
567 template<typename Real>
568 std::istream & operator >> (std::istream & in, Vector<Real> & v);
570 
573 
574 
575 template<typename Real>
577  const VectorBase<Real> &b, Real tol = 0.01) {
578  return a.ApproxEqual(b, tol);
579 }
580 
581 template<typename Real>
583  float tol = 0.01) {
584  KALDI_ASSERT(a.ApproxEqual(b, tol));
585 }
586 
587 
589 template<typename Real>
590 Real VecVec(const VectorBase<Real> &v1, const VectorBase<Real> &v2);
591 
592 template<typename Real, typename OtherReal>
593 Real VecVec(const VectorBase<Real> &v1, const VectorBase<OtherReal> &v2);
594 
595 
598 template<typename Real>
599 Real VecMatVec(const VectorBase<Real> &v1, const MatrixBase<Real> &M,
600  const VectorBase<Real> &v2);
601 
603 
604 
605 } // namespace kaldi
606 
607 // we need to include the implementation
608 #include "matrix/kaldi-vector-inl.h"
609 
610 
611 
612 #endif // KALDI_MATRIX_KALDI_VECTOR_H_
void CopyColsFromMat(const MatrixBase< Real > &M)
Performs a column stack of the matrix M.
bool ApproxEqual(const VectorBase< Real > &other, float tol=0.01) const
Returns true if ((*this)-other).Norm(2.0) <= tol * (*this).Norm(2.0).
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void SetRandUniform()
Sets to numbers uniformly distributed on (0,1)
const Real * Data() const
Returns a pointer to the start of the vector&#39;s data (const).
Definition: kaldi-vector.h:73
Packed symetric matrix class.
Definition: matrix-common.h:62
void ApplyExp()
Apply exponential to each value in vector.
Vector(const VectorBase< Real > &v)
Copy-constructor from base-class, needed to copy from SubVector.
Definition: kaldi-vector.h:429
void CopyDiagFromTp(const TpMatrix< Real > &M)
Extracts the diagonal of a triangular matrix.
Definition: kaldi-vector.h:312
MatrixResizeType
Definition: matrix-common.h:37
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Sigmoid(const VectorBase< Real > &src)
Sets each element of *this to the sigmoid function of the corresponding element of "src"...
bool IsZero(Real cutoff=1.0e-06) const
Returns true if matrix is all zeros.
void CopyFromPacked(const PackedMatrix< OtherReal > &M)
Copy data from a SpMatrix or TpMatrix (must match own size).
void CopyDiagFromMat(const MatrixBase< Real > &M)
Extracts the diagonal of the matrix M.
Vector(const Vector< Real > &v)
Copy constructor. The need for this is controversial.
Definition: kaldi-vector.h:423
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
void Tanh(const VectorBase< Real > &src)
Sets each element of *this to the tanh of the corresponding element of "src".
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 *t...
void ApplyCeiling(Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
Definition: kaldi-vector.h:155
void ApplyLogAndCopy(const VectorBase< Real > &v)
Apply natural log to another vector and put result in *this.
SubVector(const PackedMatrix< Real > &M)
This constructor initializes the vector to point at the contents of this packed matrix (SpMatrix or T...
Definition: kaldi-vector.h:519
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
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.
Real SumLog() const
Returns sum of the logs of the elements.
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
~Vector()
Destructor. Deallocates memory.
Definition: kaldi-vector.h:454
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)...
SubVector(const VectorBase< Real > &t, const MatrixIndexT origin, const MatrixIndexT length)
Constructor from a Vector or SubVector.
Definition: kaldi-vector.h:506
Real VecMatVec(const VectorBase< Real > &v1, const MatrixBase< Real > &M, const VectorBase< Real > &v2)
Returns .
void MulTp(const TpMatrix< Real > &M, const MatrixTransposeType trans)
Multiplies this vector by lower-triangular matrix: *this <– *this *M.
void ApplyLog()
Apply natural log to all elements.
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.
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...
Real ApplySoftMax()
Apply soft-max to vector and return normalizer (log sum of exponentials).
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
MatrixIndexT NumRows() const
void Pow(const VectorBase< Real > &v, Real power)
~VectorBase()
Destructor; does not deallocate memory, this is handled by child classes.
Definition: kaldi-vector.h:376
Real Norm(Real p) const
Compute the p-th norm of the vector.
void CopyRowFromMat(const MatrixBase< Real > &M, MatrixIndexT row)
Extracts a row of the matrix M.
void AddVec2(const Real alpha, const VectorBase< Real > &v)
Add vector : *this = *this + alpha * rv^2 [element-wise squaring].
void AddVecVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element product of vectors:
void ApplyFloor(Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
Definition: kaldi-vector.h:149
VectorBase()
Empty initializer, corresponds to vector of zero size.
Definition: kaldi-vector.h:379
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
void CopyDiagFromPacked(const PackedMatrix< Real > &M)
Extracts the diagonal of a packed matrix M; works for Sp or Tp.
void Floor(const VectorBase< Real > &v, Real floor_val, MatrixIndexT *floored_count=nullptr)
Applies floor to all elements.
int32 MatrixIndexT
Definition: matrix-common.h:98
MatrixIndexT dim_
dimension of vector
Definition: kaldi-vector.h:397
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
void MulElements(const VectorBase< Real > &v)
Multiply element-by-element by another vector.
void Read(std::istream &in, bool binary, bool add=false)
Reads from C++ stream (option to add to existing contents).
SubVector(const Real *data, MatrixIndexT length)
Constructor from a pointer to memory and a length.
Definition: kaldi-vector.h:534
void ApplyPowAbs(Real power, bool include_sign=false)
Take the absolute value of all elements of a vector to a power.
Vector(const VectorBase< OtherReal > &v)
Type conversion constructor.
Definition: kaldi-vector.h:436
SubVector(const SubVector &other)
Copy constructor.
Definition: kaldi-vector.h:525
KALDI_DISALLOW_COPY_AND_ASSIGN(VectorBase)
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
Packed symetric matrix class.
Definition: matrix-common.h:63
Real * Data()
Returns a pointer to the start of the vector&#39;s data.
Definition: kaldi-vector.h:70
void CopyFromPtr(const Real *Data, MatrixIndexT sz)
Load data into the vector: sz must match own size.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
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:92
SubVector(const MatrixBase< Real > &matrix, MatrixIndexT row)
This operation does not preserve const-ness, so be careful.
Definition: kaldi-vector.h:540
Real operator()(MatrixIndexT i) const
Indexing operator (const).
Definition: kaldi-vector.h:76
Real Sum() const
Returns sum of the elements.
void SetRandn()
Set vector to random normally-distributed noise.
void AddVecDivVec(Real alpha, const VectorBase< Real > &v, const VectorBase< Real > &r, Real beta)
Add element-by-element quotient of two vectors.
#define KALDI_ASSERT_IS_FLOATING_TYPE(F)
Definition: kaldi-utils.h:137
std::istream & operator>>(std::istream &is, Matrix< Real > &M)
Real * data_
data memory area
Definition: kaldi-vector.h:395
Vector()
Constructor that takes no arguments. Initializes to empty.
Definition: kaldi-vector.h:409
Matrix for CUDA computing.
Definition: matrix-common.h:69
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
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.
A class representing a vector.
Definition: kaldi-vector.h:406
void InvertElements()
Invert all elements.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Set(Real f)
Set all members of a vector to a specified value.
void ApplyPow(Real power)
Take all elements of vector to a power.
Definition: kaldi-vector.h:179
~SubVector()
Destructor (does nothing; no pointers are owned here).
Definition: kaldi-vector.h:545
MatrixTransposeType
Definition: matrix-common.h:32
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
void DivElements(const VectorBase< Real > &v)
Divide element-by-element by a vector.
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
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 kNoT...
const SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l) const
Returns a const sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:103
Real ApplyLogSoftMax()
Applies log soft-max to vector and returns normalizer (log sum of exponentials).
void ReplaceValue(Real orig, Real changed)
Set each element to y = (x == orig ? changed : x).
void Ceiling(const VectorBase< Real > &v, Real ceil_val, MatrixIndexT *ceiled_count=nullptr)
Applies ceiling to all elements.
Vector(const MatrixIndexT s, MatrixResizeType resize_type=kSetZero)
Constructor with specific size.
Definition: kaldi-vector.h:413
MatrixIndexT RandCategorical() const
This function returns a random index into this vector, chosen with probability proportional to the co...
MatrixIndexT SizeInBytes() const
Returns the size in memory of the vector, in bytes.
Definition: kaldi-vector.h:67
void ApplyAbs()
Take absolute value of each of the elements.
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
void Add(Real c)
Add a constant to each element of a vector.
void AddColSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of columns of M) + beta * *this.
void SetZero()
Set vector to all zeros.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
void CopyDiagFromSp(const SpMatrix< Real > &M)
Extracts the diagonal of a symmetric matrix.
Definition: kaldi-vector.h:309
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
void CopyRowFromSp(const SpMatrix< OtherReal > &S, MatrixIndexT row)
Extracts a row of the symmetric matrix S.
Vector for CUDA computing.
Definition: matrix-common.h:72
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:94