34 orthonormal_constraint_(0.0),
35 use_natural_gradient_(true) { }
64 std::ostringstream stream;
68 stream <<
", time-offsets=";
70 if (
i != 0) stream <<
',';
79 stream <<
", has-bias=false";
84 stream <<
", use-natural-gradient=false";
102 std::string time_offsets;
104 int32 input_dim = -1, output_dim = -1;
106 bool ok = cfl->
GetValue(
"time-offsets", &time_offsets) &&
107 cfl->
GetValue(
"input-dim", &input_dim) &&
108 cfl->
GetValue(
"output-dim", &output_dim);
109 if (!ok || input_dim <= 0 || output_dim <= 0 ||
112 KALDI_ERR <<
"Bad initializer: there is a problem with " 113 "time-offsets, input-dim or output-dim (not defined?): " 119 KALDI_ERR <<
"Bad initializer: repeated time-offsets: " 126 BaseFloat param_stddev = -1, bias_mean = 0.0, bias_stddev = 1.0;
127 bool use_bias =
true;
128 cfl->
GetValue(
"param-stddev", ¶m_stddev);
129 cfl->
GetValue(
"bias-stddev", &bias_stddev);
130 cfl->
GetValue(
"bias-mean", &bias_mean);
131 cfl->
GetValue(
"use-bias", &use_bias);
133 if (param_stddev < 0.0) {
153 int32 rank_out = -1, rank_in = -1;
154 BaseFloat alpha_out = 4.0, alpha_in = 4.0,
155 num_samples_history = 2000.0;
158 cfl->
GetValue(
"rank-out", &rank_out);
159 cfl->
GetValue(
"alpha-in", &alpha_in);
160 cfl->
GetValue(
"alpha-out", &alpha_out);
161 cfl->
GetValue(
"num-samples-history", &num_samples_history);
163 int32 spliced_input_dim =
166 rank_in = std::min<int32>(20, (spliced_input_dim + 1) / 2);
169 rank_out = std::min<int32>(80, (output_dim + 1) / 2);
201 for (
int32 i = 0;
i < num_offsets;
i++) {
207 i * input_dim, input_dim);
214 const std::string &debug_info,
230 if (in_deriv != NULL) {
232 for (
int32 i = 0;
i < num_offsets;
i++) {
238 i * input_dim, input_dim);
247 if (to_update_in != NULL) {
273 for (
int32 i = 0;
i < num_offsets;
i++) {
280 i * input_dim, input_dim);
293 num_rows = out_deriv.
NumRows(),
294 input_dim = in_value.
NumCols(),
295 spliced_input_dim = num_offsets * input_dim,
296 augmented_input_dim =
302 augmented_input_dim);
305 in_value_temp.Range(0, num_rows, spliced_input_dim, 1).Set(1.0);
308 for (
int32 i = 0;
i < num_offsets;
i++) {
311 i * input_dim, input_dim),
316 in_value_temp_part.CopyFromMat(in_value_part);
345 0, spliced_input_dim);
348 in_value_precon_part,
kNoTrans, 1.0);
352 std::vector<Index> *input_indexes,
353 std::vector<Index> *output_indexes)
const {
354 using namespace time_height_convolution;
359 ConvolutionComputationIo io;
363 std::vector<Index> modified_input_indexes,
364 modified_output_indexes;
371 &modified_input_indexes,
372 &modified_output_indexes);
378 input_indexes->swap(modified_input_indexes);
379 output_indexes->swap(modified_output_indexes);
390 WriteToken(os, binary,
"<OrthonormalConstraint>");
392 WriteToken(os, binary,
"<UseNaturalGradient>");
399 WriteToken(os, binary,
"<NumSamplesHistory>");
418 ExpectToken(is, binary,
"<OrthonormalConstraint>");
422 int32 rank_in, rank_out;
432 if (token ==
"<AlphaInOut>") {
438 alpha_out = alpha_in;
459 const Index &output_index,
460 std::vector<Index> *desired_indexes)
const {
463 desired_indexes->resize(size);
464 for (
size_t i = 0;
i < size;
i++) {
465 (*desired_indexes)[
i].n = output_index.
n;
467 (*desired_indexes)[
i].x = output_index.
x;
474 const Index &output_index,
476 std::vector<Index> *used_inputs)
const {
479 Index index(output_index);
481 if (used_inputs != NULL) {
482 used_inputs->clear();
483 used_inputs->reserve(size);
485 for (
size_t i = 0;
i < size;
i++) {
487 if (input_index_set(index)) {
488 if (used_inputs != NULL) {
490 used_inputs->push_back(index);
502 int32 num_output_rows,
507 row_offset + (row_stride * num_output_rows) - (row_stride - 1));
510 input_matrix.
Data() + input_matrix.
Stride() * row_offset,
513 input_matrix.
Stride() * row_stride);
542 const std::vector<Index> &input_indexes,
543 const std::vector<Index> &output_indexes,
544 bool need_backprop)
const {
545 using namespace time_height_convolution;
549 ConvolutionComputationIo io;
557 std::vector<Index> modified_input_indexes,
558 modified_output_indexes;
560 &modified_input_indexes,
561 &modified_output_indexes);
563 modified_output_indexes == output_indexes);
571 for (
int32 i = 0;
i < num_offsets;
i++) {
576 required_input_t = io.start_t_out + time_offset,
577 input_t = (required_input_t - io.start_t_in) / io.t_step_in;
579 KALDI_ASSERT(required_input_t == io.start_t_in + io.t_step_in * input_t);
590 int32 n = io.reorder_t_in,
591 input_t_multiple = n * (input_t /
n), input_t_remainder = input_t % n;
593 int32 input_row_offset = input_t_multiple * io.num_images +
680 std::ostream &os,
bool binary)
const {
681 WriteToken(os, binary,
"<TdnnComponentPrecomputedIndexes>");
686 WriteToken(os, binary,
"</TdnnComponentPrecomputedIndexes>");
690 std::istream &is,
bool binary) {
692 "<TdnnComponentPrecomputedIndexes>",
697 ExpectToken(is, binary,
"</TdnnComponentPrecomputedIndexes>");
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
MatrixIndexT Stride() const
const std::string WholeLine()
virtual void Vectorize(VectorBase< BaseFloat > *params) const
Turns the parameters into vector form.
static void ModifyComputationIo(time_height_convolution::ConvolutionComputationIo *io)
void CopyColFromMat(const CuMatrixBase< Real > &mat, MatrixIndexT col)
Abstract base-class for neural-net components.
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...
std::vector< int32 > time_offsets_
void SetUpdatePeriod(int32 update_period)
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
void SetNumSamplesHistory(BaseFloat num_samples_history)
virtual void Write(std::ostream &os, bool binary) const
int32 GetVerboseLevel()
Get verbosity level, usually set via command line '–verbose=' switch.
An abstract representation of a set of Indexes.
TdnnComponent is a more memory-efficient alternative to manually splicing several frames of input and...
void ConsolidateMemory()
This virtual function relates to memory management, and avoiding fragmentation.
void InitLearningRatesFromConfig(ConfigLine *cfl)
virtual void Backprop(const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL...
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Keywords for search: natural gradient, naturalgradient, NG-SGD.
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
void CopyRowsFromVec(const CuVectorBase< Real > &v)
This function has two modes of operation.
virtual void Read(std::istream &is, bool binary)
Read function (used after we know the type of the Component); accepts input that is missing the token...
virtual void ReorderIndexes(std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
This function only does something interesting for non-simple Components.
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
struct Index is intended to represent the various indexes by which we number the rows of the matrices...
virtual void Read(std::istream &os, bool binary)
static CuSubMatrix< BaseFloat > GetInputPart(const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
This function is like ExpectToken but for two tokens, and it will either accept token1 and then token...
void GetIndexesForComputation(const ConvolutionComputationIo &io, const std::vector< Index > &orig_input_indexes, const std::vector< Index > &orig_output_indexes, std::vector< Index > *input_indexes, std::vector< Index > *output_indexes)
This function computes the reordered and possibly padded indexes corresponding to the computation in ...
virtual bool IsComputable(const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
This function only does something interesting for non-simple Components, and it exists to make it pos...
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
CuMatrix< BaseFloat > linear_params_
virtual int32 InputDim() const
Returns input-dimension of this component.
virtual void PerturbParams(BaseFloat stddev)
This function is to be used in testing.
BaseFloat learning_rate_
learning rate (typically 0.0..0.01)
virtual ComponentPrecomputedIndexes * PrecomputeIndexes(const MiscComputationInfo &misc_info, const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, bool need_backprop) const
This function must return NULL for simple Components.
void UpdateSimple(const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
std::string ReadUpdatableCommon(std::istream &is, bool binary)
virtual void InitFromConfig(ConfigLine *cfl)
Initialize, from a ConfigLine object.
virtual PrecomputedIndexes * Copy() const
virtual void UnVectorize(const VectorBase< BaseFloat > ¶ms)
Converts the parameters from vector form.
void PreconditionDirections(CuMatrixBase< BaseFloat > *X, BaseFloat *scale)
This call implements the main functionality of this class.
virtual std::string Info() const
Returns some text-form information about this component, for diagnostics.
void AddMatMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType transA, const CuMatrixBase< Real > &B, MatrixTransposeType transB, Real beta)
C = alpha * A(^T)*B(^T) + beta * C.
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
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.
void Swap(OnlineNaturalGradient *other)
Class UpdatableComponent is a Component which has trainable parameters; it extends the interface of C...
void SetAlpha(BaseFloat alpha)
bool use_natural_gradient_
OnlineNaturalGradient preconditioner_out_
const Real * Data() const
Return data pointer (const).
std::vector< int32 > row_offsets
Matrix for CUDA computing.
MatrixIndexT NumCols() const
OnlineNaturalGradient preconditioner_in_
virtual void * Propagate(const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Propagate function.
void GetComputationIo(const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, ConvolutionComputationIo *io)
This function takes lists of input and output indexes to a computation (e.g.
void UpdateNaturalGradient(const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
virtual void FreezeNaturalGradient(bool freeze)
freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natu...
This class is responsible for parsing input like hi-there xx=yyy a=b c empty= f-oo=Append(bar, sss) ba_z=123 bing='a b c' baz="a b c d='a b' e" and giving you access to the fields, in this case.
virtual std::string Info() const
Returns some text-form information about this component, for diagnostics.
bool is_gradient_
True if this component is to be treated as a gradient rather than as parameters.
#define KALDI_ASSERT(cond)
void WriteUpdatableCommon(std::ostream &is, bool binary) const
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
bool GetValue(const std::string &key, std::string *value)
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...
BaseFloat GetAlpha() const
virtual void Scale(BaseFloat scale)
This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when c...
virtual BaseFloat DotProduct(const UpdatableComponent &other) const
Computes dot-product between parameters of two instances of a Component.
virtual void Add(BaseFloat alpha, const Component &other)
This virtual function when called by – an UpdatableComponent adds the parameters of another updatabl...
void PrintParameterStats(std::ostringstream &os, const std::string &name, const CuVectorBase< BaseFloat > ¶ms, bool include_mean)
Print to 'os' some information about the mean and standard deviation of some parameters, used in Info() functions in nnet-simple-component.cc.
MatrixIndexT NumRows() const
Dimensions.
Provides a vector abstraction class.
int32 GetUpdatePeriod() const
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
BaseFloat GetNumSamplesHistory() const
CuVector< BaseFloat > bias_params_
virtual void GetInputIndexes(const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
This function only does something interesting for non-simple Components.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
BaseFloat orthonormal_constraint_
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).