matrix/cblas-wrappers.h html
該頭文件對CBLAS與CLAPACK的接口進行了簡單的封裝(將不一樣數據類型的多個接口封裝爲一個)。 app
好比 ide
cblas_scopy和cblas_dcopy封裝爲cblas_Xcopy 函數
clapack_sgetri和clapack_dgetri封裝爲clapack_Xgetri 優化
上述接口的聲明位於matrix/kaldi-blas.h中 this
tools/ATLAS_headers/include/clapack.h spa
matrix/kaldi-blas.h 3d
該頭文件根據不一樣的實現(ATLAS、CLAPACK、MKL、OPENBLAS)處理BLAS、LAPACK等的#include。 htm
matrix/kaldi-vector.h blog
該頭文件聲明瞭幾個Kaldi向量類,其運算的實現基於CBLAS和CLAPACK,具體依賴的庫能夠是:
class VectorBase
向量抽象類。該類對基礎運算與內存優化進行了封裝,只提供向量運算。不涉及尺寸縮放和構造函數。
尺寸縮放和構造函數由派生類Vector和SubVector負責。
向量初始化
void SetZero();
向量信息
bool IsZero(Real cutoff = 1.0e-06) const;
inline MatrixIndexT Dim() const { return dim_; }
向量的讀取與轉換
inline Real* Data() { return data_; }
inline Real operator() (MatrixIndexT i) const
SubVector<Real> Range(const MatrixIndexT o, const MatrixIndexT l)
向量的拷貝函數
void CopyFromVec(const VectorBase<Real> &v);
void CopyFromPacked(const PackedMatrix<OtherReal> &M);
向量的運算
void ApplyLog();
void AddVec(const Real alpha, const VectorBase<OtherReal> &v);
IO
void Read(std::istream & in, bool binary, bool add = false);
void Write(std::ostream &Out, bool binary) const;
class Vector
該類表示普通Kaldi向量,並實現尺寸縮放和通常的構造函數。
多種構造函數
Vector(): VectorBase<Real>() {}
explicit Vector(const MatrixIndexT s,
MatrixResizeType resize_type = kSetZero)
: VectorBase<Real>() { Resize(s, resize_type); }
template<typename OtherReal>
explicit Vector(const CuVectorBase<OtherReal> &cu);
重載賦值運算符
Vector<Real> &operator = (const Vector<Real> &other) {
Resize(other.Dim(), kUndefined);
this->CopyFromVec(other);
return *this;
}
Vector<Real> &operator = (const VectorBase<Real> &other) {
Resize(other.Dim(), kUndefined);
this->CopyFromVec(other);
return *this;
}
Utils
void Swap(Vector<Real> *other);
void Resize(MatrixIndexT length, MatrixResizeType resize_type = kSetZero);
void RemoveElement(MatrixIndexT i);
SubVector
該類表示一個不佔有實際數據的泛化向量或向量索引,能夠表示高級向量的子向量或矩陣的行。實現多種用於索引的構造函數。
多種構造函數
SubVector(const VectorBase<Real> &t, const MatrixIndexT origin,
const MatrixIndexT length) : VectorBase<Real>()
SubVector(const PackedMatrix<Real> &M)
SubVector(const SubVector &other) : VectorBase<Real> ()
matrix/kaldi-matrix.h
該頭文件聲明瞭幾個Kaldi矩陣類,其運算的實現基於CBLAS和CLAPACK,具體依賴的庫能夠是:
class MatrixBase
矩陣抽象類。該類對基礎運算與內存優化進行了封裝,只提供矩陣運算。不涉及尺寸縮放和構造函數。
尺寸縮放和構造函數由派生類Matrix和SubMatrix負責。
矩陣信息
inline MatrixIndexT NumRows() const { return num_rows_; }、
inline MatrixIndexT Stride() const { return stride_; }
向量的讀取與轉換
inline const Real* Data() const
inline Real* Data() { return data_; }
inline Real* RowData(MatrixIndexT i)
矩陣的初始化
void SetZero();
void Set(Real);
矩陣的拷貝函數
void CopyFromMat(const MatrixBase<OtherReal> & M,
MatrixTransposeType trans = kNoTrans);
void CopyFromMat(const CompressedMatrix &M);
矩陣運算
若向量維數等於矩陣維數,則將其看做是對矩陣逐行拼接獲得的向量
若向量維數等於矩陣列數,則將*this的全部行都設定爲v
v.Dim() == NumRows() * NumCols(),
void CopyRowsFromVec(const VectorBase<Real> &v);
與CopyRowsFromVec相似,只不過矩陣爲列序
void CopyColsFromVec(const VectorBase<Real> &v);
*this矩陣爲方陣,且維數等於v的維數;將*this的對角向量設定爲v
void CopyDiagFromVec(const VectorBase<Real> &v);
矩陣全部元素之和
Real Sum() const;
矩陣的跡
Real Trace(bool check_square = true) const;
*this與A的元素級相乘
void MulElements(const MatrixBase<Real> &A);
*this/A,元素級相除
void DivElements(const MatrixBase<Real> &A);
alpha* *this,縮放*this的全部元素
void Scale(Real alpha);
(*this) = (*this) * diag(scale)
*this爲列序,for i: *this[i][j]*=scale[j]
void MulColsVec(const VectorBase<Real> &scale);
(*this) = diag(scale) * (*this)
*this爲行序,for j: *this[i][j]*=scale[i]
void MulRowsVec(const VectorBase<Real> &scale);
矩陣的逆
void Invert(Real *log_det = NULL, Real *det_sign = NULL,
bool inverse_needed = true);
矩陣的轉置,只適用於方陣,Matrix才支持普通矩陣
void Transpose();
對矩陣全部元素進行floor()運算
void ApplyFloor(Real floor_val);
對矩陣全部元素進行ceil()運算
void ApplyCeiling(Real ceiling_val);
對矩陣全部元素進行log()運算
void ApplyLog();
對矩陣全部元素進行exp()運算
void ApplyExp();
對矩陣全部元素進行pow()運算
void ApplyPow(Real power);
對矩陣全部元素進行sigmoid()運算
void Sigmoid(const MatrixBase<Real> &src);
對矩陣全部元素進行tanh()運算
void Tanh(const MatrixBase<Real> &src);
梯度從sigmoid激勵傳播到sigmoid激活
*this = diff * value * (1.0 - value)
void DiffSigmoid(const MatrixBase<Real> &value,
const MatrixBase<Real> &diff);
梯度從tanh激勵傳播到tanh激活
*this = diff * (1.0 - value^2).
void DiffTanh(const MatrixBase<Real> &value,
const MatrixBase<Real> &diff);
*this += alpha * M
void AddMat(const Real alpha, const MatrixBase<Real> &M,
MatrixTransposeType transA = kNoTrans);
*this = alpha A B + beta * *this
若transA == kNoTrans,則
*this的行數=A的行數
若transA == kTrans,則A=A^T
*this的行數=A的列數
若transB == kNoTrans,則
*this的列數=B的列數
若transB == kTrans,則B=B^T
*this的列數=B的行數
void AddMatMat(const Real alpha,
const MatrixBase<Real>& A, MatrixTransposeType transA,
const MatrixBase<Real>& B, MatrixTransposeType transB,
const Real beta);
*this = beta* *this + alpha*A*B*C.
void AddMatMatMat(const Real alpha,
const MatrixBase<Real>& A, MatrixTransposeType transA,
const MatrixBase<Real>& B, MatrixTransposeType transB,
const MatrixBase<Real>& C, MatrixTransposeType transC,
const Real beta);
class Matrix
該類表示普通Kaldi矩陣,並實現尺寸縮放和通常的構造函數。
多種構造函數
Matrix(const MatrixIndexT r, const MatrixIndexT c,
MatrixResizeType resize_type = kSetZero,
MatrixStrideType stride_type = kDefaultStride):
MatrixBase<Real>() { Resize(r, c, resize_type, stride_type); }
explicit Matrix(const CuMatrixBase<OtherReal> &cu,
MatrixTransposeType trans = kNoTrans);
重載賦值運算符
Matrix<Real> &operator = (const MatrixBase<Real> &other) {
if (MatrixBase<Real>::NumRows() != other.NumRows() ||
MatrixBase<Real>::NumCols() != other.NumCols())
Resize(other.NumRows(), other.NumCols(), kUndefined);
MatrixBase<Real>::CopyFromMat(other);
return *this;
}
Matrix<Real> &operator = (const Matrix<Real> &other) {
if (MatrixBase<Real>::NumRows() != other.NumRows() ||
MatrixBase<Real>::NumCols() != other.NumCols())
Resize(other.NumRows(), other.NumCols(), kUndefined);
MatrixBase<Real>::CopyFromMat(other);
return *this;
Utils
void Swap(Matrix<Real> *other);
void RemoveRow(MatrixIndexT i);
void Resize(const MatrixIndexT r,
const MatrixIndexT c,
MatrixResizeType resize_type = kSetZero,
MatrixStrideType stride_type = kDefaultStride);
class SubMatrix
該類表示一個不佔有實際數據的泛化矩陣或矩陣索引,能夠表示其餘矩陣的矩陣。實現多種用於索引的構造函數。
繼承於MatrixBase,用於對矩陣的子矩陣(塊矩陣)進行運算。
template<typename Real> class SubMatrix : public MatrixBase<Real> { public: 多種構造函數 // 將矩陣的一部分初始化爲一個SubMatrix,這都點相似於Matlab中的A(b:c, d:e)。 SubMatrix(const MatrixBase<Real>& T, const MatrixIndexT ro, // row offset, 0 < ro < NumRows() const MatrixIndexT r, // number of rows, r > 0 const MatrixIndexT co, // column offset, 0 < co < NumCols() const MatrixIndexT c); // number of columns, c > 0
// This initializer is mostly intended for use in CuMatrix and related // classes. Be careful! SubMatrix(Real *data, MatrixIndexT num_rows, MatrixIndexT num_cols, MatrixIndexT stride);
~SubMatrix<Real>() {}
/// This type of constructor is needed for Range() to work [in Matrix base /// class]. Cannot make it explicit. SubMatrix<Real> (const SubMatrix &other): MatrixBase<Real> (other.data_, other.num_cols_, other.num_rows_, other.stride_) {}
private: /// Disallow assignment. SubMatrix<Real> &operator = (const SubMatrix<Real> &other); }; |