21 #ifndef KALDI_NNET_NNET_VARIOUS_H_ 22 #define KALDI_NNET_NNET_VARIOUS_H_ 56 std::vector<std::vector<int32> > build_vector;
59 while (is >> std::ws, !is.eof()) {
61 if (token ==
"<ReadVector>") {
63 }
else if (token ==
"<BuildVector>") {
66 while (is >> std::ws, !is.eof()) {
67 std::string colon_sep_list_or_end;
68 ReadToken(is,
false, &colon_sep_list_or_end);
69 if (colon_sep_list_or_end ==
"</BuildVector>")
break;
72 build_vector.push_back(v);
75 KALDI_ERR <<
"Unknown token " << token <<
", a typo in config?" 76 <<
" (ReadVector|BuildVector)";
80 if (build_vector.size() > 0) {
94 void WriteData(std::ostream &os,
bool binary)
const {
99 std::ostringstream ostr;
101 std::string str = ostr.str();
102 str.erase(str.end()-1);
122 num_frames = out_diff.
NumRows();
127 for (
int32 i = 0;
i < offsets.size();
i++) {
128 int32 o_i = offsets.at(
i);
129 int32 n_rows = num_frames - abs(o_i),
130 src_row = std::max(-o_i, 0),
131 tgt_row = std::max(o_i, 0);
160 std::vector<std::vector<int32> > build_vector;
163 while (is >> std::ws, !is.eof()) {
165 if (token ==
"<ReadVector>") {
166 copy_from_indices_.Read(is,
false);
167 }
else if (token ==
"<BuildVector>") {
170 while (is >> std::ws, !is.eof()) {
171 std::string colon_sep_list_or_end;
172 ReadToken(is,
false, &colon_sep_list_or_end);
173 if (colon_sep_list_or_end ==
"</BuildVector>")
break;
174 std::vector<int32> v;
176 build_vector.push_back(v);
179 KALDI_ERR <<
"Unknown token " << token <<
", a typo in config?" 180 <<
" (ReadVector|BuildVector)";
184 if (build_vector.size() > 0) {
190 copy_from_indices_.Add(-1);
200 copy_from_indices_.Read(is, binary);
202 copy_from_indices_.Add(-1);
208 tmp.
Write(os, binary);
212 return std::string(
"\n min ") +
ToString(copy_from_indices_.Min()) +
213 ", max " +
ToString(copy_from_indices_.Max());
218 cu::Copy(in, copy_from_indices_,out);
225 static bool warning_displayed =
false;
226 if (!warning_displayed) {
228 << __func__ <<
"() Not implemented!";
230 warning_displayed =
true;
259 if (row_scales_.Dim() != in.
NumRows()) {
260 row_scales_.Resize(in.
NumRows());
265 row_scales_.AddColSumMat(1.0, l2_aux_, 0.0);
266 row_scales_.ApplyPow(0.5);
267 row_scales_.InvertElements();
306 float init_param = 0.0;
309 while (is >> std::ws, !is.eof()) {
311 if (token ==
"<InitParam>")
ReadBasicType(is,
false, &init_param);
312 else if (token ==
"<LearnRateCoef>")
ReadBasicType(is,
false, &learn_rate_coef_);
313 else KALDI_ERR <<
"Unknown token " << token <<
", a typo in config?" 318 shift_data_.Set(init_param);
323 if (
'<' ==
Peek(is, binary)) {
328 shift_data_.Read(is, binary);
334 shift_data_.Write(os, binary);
341 shift_data_grad_.CopyToVec(gradient);
346 shift_data_.CopyToVec(params);
351 shift_data_.CopyFromVec(params);
355 return std::string(
"\n shift_data") +
357 ", lr-coef " +
ToString(learn_rate_coef_);
361 return std::string(
"\n shift_data_grad") +
363 ", lr-coef " +
ToString(learn_rate_coef_);
387 shift_data_grad_.AddRowSumMat(1.0, diff, 0.0);
389 shift_data_.AddVec(-lr * learn_rate_coef_, shift_data_grad_);
419 float init_param = 0.0;
422 while (is >> std::ws, !is.eof()) {
424 if (token ==
"<InitParam>")
ReadBasicType(is,
false, &init_param);
425 else if (token ==
"<LearnRateCoef>")
ReadBasicType(is,
false, &learn_rate_coef_);
426 else KALDI_ERR <<
"Unknown token " << token <<
", a typo in config?" 431 scale_data_.Set(init_param);
436 if (
'<' ==
Peek(is, binary)) {
441 scale_data_.Read(is, binary);
447 scale_data_.Write(os, binary);
454 scale_data_grad_.CopyToVec(gradient);
459 scale_data_.CopyToVec(params);
464 scale_data_.CopyFromVec(params);
468 return std::string(
"\n scale_data") +
470 ", lr-coef " +
ToString(learn_rate_coef_);
474 return std::string(
"\n scale_data_grad") +
476 ", lr-coef " +
ToString(learn_rate_coef_);
503 scale_data_grad_.AddRowSumMat(1.0, gradient_aux, 0.0);
505 scale_data_.AddVec(-lr * learn_rate_coef_, scale_data_grad_);
518 #endif // KALDI_NNET_NNET_VARIOUS_H_ std::string ToString(const T &t)
Convert basic type to a string (please don't overuse),.
ComponentType GetType() const
Get Type Identification of the component,.
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
CuVector< BaseFloat > shift_data_
CuMatrix< BaseFloat > l2_aux_
auxiliary matrix for L2 norm computation,
void ReadData(std::istream &is, bool binary)
Reads the component content.
void SetLearnRateCoef(BaseFloat c)
Set the learn-rate coefficient,.
ComponentType GetType() const
Get Type Identification of the component,.
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 CopyToVec(std::vector< T > *dst) const
This function resizes *dst if needed.
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
void ReadData(std::istream &is, bool binary)
Reads the component content.
AddShift(int32 dim_in, int32 dim_out)
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
void ReadData(std::istream &is, bool binary)
Reads the component content.
void SetLearnRateCoef(BaseFloat c)
Set the learn-rate coefficient,.
Component * Copy() const
Copy component (deep copy),.
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
Class UpdatableComponent is a Component which has trainable parameters, it contains SGD training hype...
CuSubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Rearrange the matrix columns according to the indices in copy_from_indices_.
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
CuVector< BaseFloat > shift_data_grad_
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
int32 NumParams() const
Number of trainable parameters,.
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
ComponentType
Component type identification mechanism,.
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Rescale the data column-wise by a vector (can be used for global variance normalization) ...
void Update(const CuMatrixBase< BaseFloat > &input, const CuMatrixBase< BaseFloat > &diff)
Compute gradient and update parameters,.
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
void Add(const T &value)
Add a constant value.
Rescale the matrix-rows to have unit length (L2-norm).
CopyComponent(int32 dim_in, int32 dim_out)
ComponentType GetType() const
Get Type Identification of the component,.
CuVector< BaseFloat > row_scales_
normalization scale of each row,
static const char * TypeToMarker(ComponentType t)
Converts component type to marker,.
void SetParams(const VectorBase< BaseFloat > ¶ms)
Set the trainable parameters from, reshaped as a vector,.
Adds shift to all the lines of the matrix (can be used for global mean normalization) ...
ComponentType GetType() const
Get Type Identification of the component,.
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
void AddVecToRows(Real alpha, const CuVectorBase< Real > &row, Real beta=1.0)
(for each row r of *this), r = alpha * row + beta * r
CuVector< BaseFloat > scale_data_grad_
void SetZero()
Math operations, some calling kernels.
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
void InitData(std::istream &is)
Initialize the content of the component by the 'line' from the prototype,.
void MulElements(const CuMatrixBase< Real > &A)
Multiply two matrices elementwise: C = C .* A.
CuArray< int32 > frame_offsets_
void Read(std::istream &is, bool binary)
I/O.
Splices the time context of the input features in N, out k*N, FrameOffset o_1,o_2,...,o_k FrameOffset example 11frames: -5 -4 -3 -2 -1 0 1 2 3 4 5.
int32 InputDim() const
Get the dimension of the input,.
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
void InitData(std::istream &is)
Virtual interface for initialization and I/O,.
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'.
Splice(int32 dim_in, int32 dim_out)
This class is used for a piece of a CuMatrix.
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
MatrixIndexT Dim() const
Returns the dimension of the vector.
CuSubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
void WriteData(std::ostream &os, bool binary) const
Writes the component content.
void InitData(std::istream &is)
Initialize the content of the component by the 'line' from the prototype,.
void ReadData(std::istream &is, bool binary)
Reads the component content.
void Write(std::ostream &is, bool binary) const
void MulColsVec(const CuVectorBase< Real > &scale)
scale i'th column by scale[i]
void Splice(const CuMatrixBase< Real > &src, const CuArray< int32 > &frame_offsets, CuMatrixBase< Real > *tgt)
Splice concatenates frames of src as specified in frame_offsets into tgt.
void GetParams(VectorBase< BaseFloat > *params) const
Get the trainable parameters reshaped as a vector,.
Matrix for CUDA computing.
LengthNormComponent(int32 dim_in, int32 dim_out)
CuArray< int32 > copy_from_indices_
MatrixIndexT NumCols() const
void BackpropagateFnc(const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
Backward pass transformation (to be implemented by descending class...)
#define KALDI_ASSERT(cond)
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
void SetParams(const VectorBase< BaseFloat > ¶ms)
Set the trainable parameters from, reshaped as a vector,.
void Update(const CuMatrixBase< BaseFloat > &input, const CuMatrixBase< BaseFloat > &diff)
Compute gradient and update parameters,.
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
Rescale(int32 dim_in, int32 dim_out)
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
std::string InfoGradient() const
Print some additional info about gradient (after <...> and dims),.
Abstract class, building block of the network.
ComponentType GetType() const
Get Type Identification of the component,.
Component * Copy() const
Copy component (deep copy),.
void PropagateFnc(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
Abstract interface for propagation/backpropagation.
void GetGradient(VectorBase< BaseFloat > *gradient) const
Get gradient reshaped as a vector,.
int32 OutputDim() const
Get the dimension of the output,.
MatrixIndexT NumRows() const
Dimensions.
Provides a vector abstraction class.
MatrixIndexT Dim() const
Return the vector dimension.
Component * Copy() const
Copy component (deep copy),.
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
Component * Copy() const
Copy component (deep copy),.
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i'th row by scale[i]
std::string InfoGradient() const
Print some additional info about gradient (after <...> and dims),.
Component * Copy() const
Copy component (deep copy),.
CuVector< BaseFloat > scale_data_
void GetGradient(VectorBase< BaseFloat > *gradient) const
Get gradient reshaped as a vector,.
std::string Info() const
Print some additional info (after <ComponentName> and the dims),.
void InitData(std::istream &is)
Virtual interface for initialization and I/O,.
int32 NumParams() const
Number of trainable parameters,.
void Copy(const CuMatrixBase< Real > &src, const CuArray< int32 > ©_from_indices, CuMatrixBase< Real > *tgt)
Copies elements from src into tgt as given by copy_from_indices.