21 #ifndef KALDI_NNET_NNET_UTILS_H_ 22 #define KALDI_NNET_NNET_UTILS_H_ 43 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
44 std::copy(v.begin(), v.end(), std::ostream_iterator<T>(os,
" "));
53 std::ostringstream os;
62 template <
typename Real>
68 Real mean = vec.
Sum() / vec.
Dim();
71 vec_no_mean = vec_aux;
73 Real variance = vec_aux.
Sum() / vec.
Dim();
79 Real skewness = vec_aux.
Sum() / pow(variance, 3.0/2.0) / vec.
Dim();
86 Real kurtosis = vec_aux.
Sum() / (variance * variance) / vec.
Dim() - 3.0;
88 std::ostringstream ostr;
89 ostr <<
" ( min " << vec.
Min() <<
", max " << vec.
Max()
91 <<
", stddev " << sqrt(variance)
92 <<
", skewness " << skewness
93 <<
", kurtosis " << kurtosis
101 template <
typename Real>
111 template <
typename Real>
121 template <
typename Real>
131 template <
typename Real>
133 Real sum = mat.
Sum();
141 template <
typename Real>
144 Real mean = mat.
Sum() / N;
147 Real var = pow_2.
Sum() / N - mean * mean;
149 KALDI_WARN <<
"Forcing the variance to be non-negative! " << var <<
"->0.0";
163 template <
typename Real>
168 for (
int32 r = 0; r < m.NumRows(); r++) {
169 for (
int32 c = 0; c < m.NumCols(); c++) {
187 template <
typename Real>
192 for (
int32 r = 0; r < m.NumRows(); r++) {
193 for (
int32 c = 0; c < m.NumCols(); c++) {
194 m(r, c) =
Rand(state) /
static_cast<Real
>(RAND_MAX);
199 m.Add(mu - (range / 2.0));
211 template <
typename Real>
216 for (
int32 i = 0;
i < v.Dim();
i++) {
217 v(
i) =
Rand(state) /
static_cast<Real
>(RAND_MAX);
221 v.Add(mu - (range / 2.0));
240 std::vector<int32>* out) {
244 for (
int32 i = 0;
i < in.size();
i++) {
246 int32 beg = 0, end = 0, step = 1;
247 switch (in[
i].size()) {
264 KALDI_ERR <<
"Something is wrong! (should be 1-3) : " 271 for (
int32 j = beg;
j <= end;
j += step) {
282 std::vector<int32> v;
291 template <
typename Real>
303 template <
typename Real>
317 #endif // KALDI_NNET_NNET_UTILS_H_ std::string ToString(const T &t)
Convert basic type to a string (please don't overuse),.
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
void PosteriorToPdfMatrix(const Posterior &post, const TransitionModel &model, CuMatrix< Real > *mat)
Wrapper of PosteriorToMatrixMapped with CuMatrix argument.
std::string MomentStatistics(const VectorBase< Real > &vec)
Get a string with statistics of the data in a vector, so we can print them easily.
void CopyToMat(MatrixBase< OtherReal > *dst, MatrixTransposeType trans=kNoTrans) const
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Base class which provides matrix operations not involving resizing or allocation. ...
void RandUniform(BaseFloat mu, BaseFloat range, CuMatrixBase< Real > *mat, struct RandomState *state=NULL)
Fill CuMatrix with random numbers (Uniform distribution): mu = the mean value, range = the 'width' of...
A class for storing matrices.
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, CuMatrix< Real > *mat)
Wrapper of PosteriorToMatrix with CuMatrix argument.
Real Min() const
Returns the minimum value of any element, or +infinity for the empty vector.
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Real ComputeStdDev(const CuMatrixBase< Real > &mat)
Get the standard deviation of values in the matrix.
void MulElements(const VectorBase< Real > &v)
Multiply element-by-element by another vector.
void MulElements(const CuMatrixBase< Real > &A)
Multiply two matrices elementwise: C = C .* A.
void BuildIntegerVector(const std::vector< std::vector< int32 > > &in, std::vector< int32 > *out)
Build 'integer vector' out of vector of 'matlab-like' representation: 'b, b:e, b:s:e'.
Real Max() const
Returns the maximum value of any element, or -infinity for the empty vector.
void RandGauss(BaseFloat mu, BaseFloat sigma, CuMatrixBase< Real > *mat, struct RandomState *state=NULL)
Fill CuMatrix with random numbers (Gaussian distribution): mu = the mean value, sigma = standard devi...
MatrixIndexT Dim() const
Returns the dimension of the vector.
int Rand(struct RandomState *state)
Real Sum() const
Returns sum of the elements.
Matrix for CUDA computing.
MatrixIndexT NumCols() const
A class representing a vector.
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
void CopyRowsFromMat(const MatrixBase< Real > &M)
Performs a row stack of the matrix M.
MatrixIndexT NumRows() const
Dimensions.
Provides a vector abstraction class.
void Add(Real c)
Add a constant to each element of a vector.
void CopyToVec(VectorBase< OtherReal > *dst) const
void CheckNanInf(const CuMatrixBase< Real > &mat, const char *msg="")
Check that matrix contains no nan or inf.
MatrixIndexT Dim() const
Dimensions.
Vector for CUDA computing.