kaldi通用底层矩阵运算库——CBLAS
matrix/cblas-wrappers.h
该头文件对CBLAS与CLAPACK的接口进行了简单的封装(将不同数据类型的多个接口封装为一个)。
比如
cblas_scopy和cblas_dcopy封装为cblas_Xcopy
clapack_sgetri和clapack_dgetri封装为clapack_Xgetri
上述接口的声明位于matrix/kaldi-blas.h中
tools/ATLAS_headers/include/clapack.h
matrix/kaldi-blas.h
该头文件根据不同的实现(ATLAS、CLAPACK、MKL、OPENBLAS)处理BLAS、LAPACK等的#include。
matrix/kaldi-vector.h
该头文件声明了几个Kaldi向量类,其运算的实现基于CBLAS和CLAPACK,具体依赖的库可以是:
- ATLAS
- CLAPACK
- MKL
- OPENBLAS
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,具体依赖的库可以是:
- ATLAS
- CLAPACK
- MKL
- OPENBLAS
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); }; |
kaldi通用底层矩阵运算库——CBLAS的更多相关文章
- kaldi通用底层矩阵运算库——CUDA
cudamatrix/cublas-wrappers.h 该头文件对cuBLAS的接口进行了简单的封装(函数名的简化和部分kaldi函数的封装). 比如 cublasSgemm_v2封装为cublas ...
- Python底层socket库
Python底层socket库将Unix关于网络通信的系统调用对象化处理,是底层函数的高级封装,socket()函数返回一个套接字,它的方法实现了各种套接字系统调用.read与write与Python ...
- C++矩阵运算库armadillo配置笔记
前言 最近在用C++实现神经网络模型,优化算法需要用到矩阵操作,一开始我用的是boost的ublas库,但用着用着感觉很不习惯,接口不够友好.于是上网搜索矩阵运算哪家强,大神们都推荐armadillo ...
- C++矩阵运算库推荐
最近在几个地方都看到有人问C++下用什么矩阵运算库比较好,顺便做了个调查,做一些相关的推荐吧.主要针对稠密矩阵,有时间会再写一个稀疏矩阵的推荐. Armadillo:C++下的Matlab替代品 地址 ...
- uTenux——重新整理底层驱动库
重新整理底层驱动库 1. 整理chip.h 在chip.h文件中的07----13的宏定义设置位如下,这样我们就不用在工程配中定义sam3s4c这个宏了,为我们以后通用少了一件麻烦事. //#if d ...
- .net通用底层搭建
.net通用底层搭建 之前写过几篇,有朋友说看不懂,有朋友说写的有点乱,自己看了下,的确是需要很认真的看才能看懂整套思路. 于是写下了这篇. 1.这个底层,使用的是ado.net,微软企业库 2.实体 ...
- YARN底层基础库
YARN基础库是其他一切模块的基础,它的设计直接决定了YARN的稳定性和扩展性,YARN借用了MRV1的一些底层基础库,比如RPC库等,但因为引入了很多新的软件设计方式,所以它的基础库更多,包括直 ...
- C++通用框架和库
C++通用框架和库 来源 https://www.cnblogs.com/skyus/articles/8524408.html 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应 ...
- Duanxx的Design abroad: C++矩阵运算库Eigen 概要
一.概要 这两天想起来要做神经网络的作业了,要求用C++完毕神经网络的算法. 摆在面前的第一个问题就是,神经网络算法中大量用到了矩阵运算.可是C++不像matlab那样对矩阵运算有非常好的支持.本来准 ...
随机推荐
- 自反ACL(第三组)
一.实验拓扑 二.配置过程 此处我用了学号后两位来划分网段,注意:先把网络做通再配ACL 1)网络连通测试 内网可以telnet外网 ----------- 外网可以telnet内网 2)ACL配置( ...
- zabbix proxy
ProxyMode=0 Server=zabbix-server的IPHostname=zabbix proxy(跟在zabbixWED网页建立代理proxies名称一样) LogFile=/data ...
- MySQL 系列
阅读目录 第一篇:初识数据库 第二篇:库操作相关 第三篇:表相关操作 第四篇:记录相关操作 第五篇:数据备份.pymysql模块 第六篇:视图.触发器.事务.存储过程.函数 第七篇:ORM框架SQLA ...
- 毕业设计(3)基于MicroPython的篮球计时计分器模型的设计与实现
前言 我们身边有不少人都喜欢看篮球比赛或者经常打篮球.说起篮球,肯定要提到NBA(美国职业篮球联赛),现在也正是NBA 18-19赛季常规赛的时候.最近3月5日,韦少22分帮助雷霆终结了四连败,哈登4 ...
- FineUIMvc的分部视图连接器 --VS-- FineUIPro的用户控件连接器
[FineUIPro]用户控件 在FineUIPro中,我们可以将公用的部分定义为一个用户控件,然后添加到页面中. 一个简单的示例:http://pro.fineui.com/#/usercontro ...
- springboot 配置文件
– Spring Boot使用一个全局的配置文件 • application.properties • application.yml – 配置文件放在src/main/resources目录或者类路 ...
- SSM(Spring + Springmvc + Mybatis)框架面试题
JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...
- MYSQL 双主配置
MYSQL1. 版本号:5.7.243. 部署方式:双主部署,两台机器即是主又是备 ,双向拷贝,可以同时写入.4. 安装部署路径: a) /home/softb) 配置路径 /etc/my.cnfc) ...
- 一入OI深似海 1 —— 偶然遇见,命运选择
记得还是三年级(还是四年级?人老了都忘了)吧, 那时候我还是个愣头青, 学校的编程班在每个班强制招人, 我就屁颠屁颠被赶去去上课了. 一开始的课完全听不懂, 导致没过多久我同班的人一个个走了, 我为了 ...
- Supervisor安装与使用
一.简介 1.supervisor是什么 superviosr是一个Linux/Unix系统上进程监控和管理的工具,它由python编写,可以用pip安装.supervisor能将一个普通的命令行进程 ...