All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AddShift Class Reference

Adds shift to all the lines of the matrix (can be used for global mean normalization) More...

#include <nnet-various.h>

Inheritance diagram for AddShift:
Collaboration diagram for AddShift:

Public Member Functions

 AddShift (int32 dim_in, int32 dim_out)
 
 ~AddShift ()
 
ComponentCopy () const
 Copy component (deep copy),. More...
 
ComponentType GetType () const
 Get Type Identification of the component,. More...
 
void InitData (std::istream &is)
 Initialize the content of the component by the 'line' from the prototype,. More...
 
void ReadData (std::istream &is, bool binary)
 Reads the component content. More...
 
void WriteData (std::ostream &os, bool binary) const
 Writes the component content. More...
 
int32 NumParams () const
 Number of trainable parameters,. More...
 
void GetGradient (VectorBase< BaseFloat > *gradient) const
 Get gradient reshaped as a vector,. More...
 
void GetParams (VectorBase< BaseFloat > *params) const
 Get the trainable parameters reshaped as a vector,. More...
 
void SetParams (const VectorBase< BaseFloat > &params)
 Set the trainable parameters from, reshaped as a vector,. More...
 
std::string Info () const
 Print some additional info (after <ComponentName> and the dims),. More...
 
std::string InfoGradient () const
 Print some additional info about gradient (after <...> and dims),. More...
 
void PropagateFnc (const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
 Abstract interface for propagation/backpropagation. More...
 
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...) More...
 
void Update (const CuMatrixBase< BaseFloat > &input, const CuMatrixBase< BaseFloat > &diff)
 Compute gradient and update parameters,. More...
 
void SetLearnRateCoef (BaseFloat c)
 Set the learn-rate coefficient,. More...
 
- Public Member Functions inherited from UpdatableComponent
 UpdatableComponent (int32 input_dim, int32 output_dim)
 
virtual ~UpdatableComponent ()
 
bool IsUpdatable () const
 Check if contains trainable parameters,. More...
 
virtual void SetTrainOptions (const NnetTrainOptions &opts)
 Set the training options to the component,. More...
 
const NnetTrainOptionsGetTrainOptions () const
 Get the training options from the component,. More...
 
virtual void SetBiasLearnRateCoef (BaseFloat val)
 Set the learn-rate coefficient for bias,. More...
 
- Public Member Functions inherited from Component
 Component (int32 input_dim, int32 output_dim)
 Generic interface of a component,. More...
 
virtual ~Component ()
 
virtual bool IsMultistream () const
 Check if component has 'Recurrent' interface (trainable and recurrent),. More...
 
int32 InputDim () const
 Get the dimension of the input,. More...
 
int32 OutputDim () const
 Get the dimension of the output,. More...
 
void Propagate (const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
 Perform forward-pass propagation 'in' -> 'out',. More...
 
void Backpropagate (const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
 Perform backward-pass propagation 'out_diff' -> 'in_diff'. More...
 
void Write (std::ostream &os, bool binary) const
 Write the component to a stream,. More...
 

Protected Attributes

CuVector< BaseFloatshift_data_
 
CuVector< BaseFloatshift_data_grad_
 
- Protected Attributes inherited from UpdatableComponent
NnetTrainOptions opts_
 Option-class with training hyper-parameters,. More...
 
BaseFloat learn_rate_coef_
 Scalar applied to learning rate for weight matrices (to be used in ::Update method),. More...
 
BaseFloat bias_learn_rate_coef_
 Scalar applied to learning rate for bias (to be used in ::Update method),. More...
 
- Protected Attributes inherited from Component
int32 input_dim_
 Data members,. More...
 
int32 output_dim_
 Dimension of the output of the Component,. More...
 

Additional Inherited Members

- Public Types inherited from Component
enum  ComponentType {
  kUnknown = 0x0, kUpdatableComponent = 0x0100, kAffineTransform, kLinearTransform,
  kConvolutionalComponent, kConvolutional2DComponent, kLstmProjected, kBlstmProjected,
  kRecurrentComponent, kActivationFunction = 0x0200, kSoftmax, kHiddenSoftmax,
  kBlockSoftmax, kSigmoid, kTanh, kParametricRelu,
  kDropout, kLengthNormComponent, kTranform = 0x0400, kRbm,
  kSplice, kCopy, kTranspose, kBlockLinearity,
  kAddShift, kRescale, kKlHmm = 0x0800, kSentenceAveragingComponent,
  kSimpleSentenceAveragingComponent, kAveragePoolingComponent, kAveragePooling2DComponent, kMaxPoolingComponent,
  kMaxPooling2DComponent, kFramePoolingComponent, kParallelComponent, kMultiBasisComponent
}
 Component type identification mechanism,. More...
 
- Static Public Member Functions inherited from Component
static const char * TypeToMarker (ComponentType t)
 Converts component type to marker,. More...
 
static ComponentType MarkerToType (const std::string &s)
 Converts marker to component type (case insensitive),. More...
 
static ComponentInit (const std::string &conf_line)
 Initialize component from a line in config file,. More...
 
static ComponentRead (std::istream &is, bool binary)
 Read the component from a stream (static method),. More...
 
- Static Public Attributes inherited from Component
static const struct key_value kMarkerMap []
 The table with pairs of Component types and markers (defined in nnet-component.cc),. More...
 

Detailed Description

Adds shift to all the lines of the matrix (can be used for global mean normalization)

Definition at line 291 of file nnet-various.h.

Constructor & Destructor Documentation

AddShift ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 293 of file nnet-various.h.

Referenced by AddShift::Copy().

293  :
294  UpdatableComponent(dim_in, dim_out),
295  shift_data_(dim_in)
296  { }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
UpdatableComponent(int32 input_dim, int32 output_dim)
~AddShift ( )
inline

Definition at line 298 of file nnet-various.h.

299  { }

Member Function Documentation

void BackpropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
const CuMatrixBase< BaseFloat > &  out,
const CuMatrixBase< BaseFloat > &  out_diff,
CuMatrixBase< BaseFloat > *  in_diff 
)
inlinevirtual

Backward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 373 of file nnet-various.h.

References CuMatrixBase< Real >::CopyFromMat().

376  {
377  // the derivative of additive constant is zero...
378  in_diff->CopyFromMat(out_diff);
379  }
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 301 of file nnet-various.h.

References AddShift::AddShift().

301 { return new AddShift(*this); }
AddShift(int32 dim_in, int32 dim_out)
Definition: nnet-various.h:293
void GetGradient ( VectorBase< BaseFloat > *  gradient) const
inlinevirtual

Get gradient reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 339 of file nnet-various.h.

References CuVectorBase< Real >::CopyToVec(), VectorBase< Real >::Dim(), KALDI_ASSERT, AddShift::NumParams(), and AddShift::shift_data_grad_.

339  {
340  KALDI_ASSERT(gradient->Dim() == NumParams());
341  shift_data_grad_.CopyToVec(gradient);
342  }
CuVector< BaseFloat > shift_data_grad_
Definition: nnet-various.h:396
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:337
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:839
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
void GetParams ( VectorBase< BaseFloat > *  params) const
inlinevirtual

Get the trainable parameters reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 344 of file nnet-various.h.

References CuVectorBase< Real >::CopyToVec(), VectorBase< Real >::Dim(), KALDI_ASSERT, AddShift::NumParams(), and AddShift::shift_data_.

Referenced by kaldi::ConvertAddShiftComponent().

344  {
345  KALDI_ASSERT(params->Dim() == NumParams());
346  shift_data_.CopyToVec(params);
347  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:337
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:839
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 302 of file nnet-various.h.

References Component::kAddShift.

std::string Info ( ) const
inlinevirtual

Print some additional info (after <ComponentName> and the dims),.

Reimplemented from Component.

Definition at line 354 of file nnet-various.h.

References UpdatableComponent::learn_rate_coef_, kaldi::nnet1::MomentStatistics(), AddShift::shift_data_, and kaldi::nnet1::ToString().

354  {
355  return std::string("\n shift_data") +
357  ", lr-coef " + ToString(learn_rate_coef_);
358  }
std::string ToString(const T &t)
Convert basic type to a string (please don't overuse),.
Definition: nnet-utils.h:52
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
std::string MomentStatistics(const VectorBase< Real > &vec)
Get a string with statistics of the data in a vector, so we can print them easily.
Definition: nnet-utils.h:63
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
std::string InfoGradient ( ) const
inlinevirtual

Print some additional info about gradient (after <...> and dims),.

Reimplemented from Component.

Definition at line 360 of file nnet-various.h.

References UpdatableComponent::learn_rate_coef_, kaldi::nnet1::MomentStatistics(), AddShift::shift_data_grad_, and kaldi::nnet1::ToString().

360  {
361  return std::string("\n shift_data_grad") +
363  ", lr-coef " + ToString(learn_rate_coef_);
364  }
std::string ToString(const T &t)
Convert basic type to a string (please don't overuse),.
Definition: nnet-utils.h:52
std::string MomentStatistics(const VectorBase< Real > &vec)
Get a string with statistics of the data in a vector, so we can print them easily.
Definition: nnet-utils.h:63
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
CuVector< BaseFloat > shift_data_grad_
Definition: nnet-various.h:396
void InitData ( std::istream &  is)
inlinevirtual

Initialize the content of the component by the 'line' from the prototype,.

Implements UpdatableComponent.

Definition at line 304 of file nnet-various.h.

References Component::InputDim(), KALDI_ERR, kaldi::kSetZero, UpdatableComponent::learn_rate_coef_, kaldi::ReadBasicType(), kaldi::ReadToken(), CuVector< Real >::Resize(), CuVectorBase< Real >::Set(), and AddShift::shift_data_.

304  {
305  // define options
306  float init_param = 0.0;
307  // parse config
308  std::string token;
309  while (is >> std::ws, !is.eof()) {
310  ReadToken(is, false, &token);
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?"
314  << " (InitParam)";
315  }
316  // initialize
317  shift_data_.Resize(InputDim(), kSetZero); // set to zero
318  shift_data_.Set(init_param);
319  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
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...
Definition: io-funcs-inl.h:55
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
void Set(Real value)
Definition: cu-vector.cc:1024
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:892
#define KALDI_ERR
Definition: kaldi-error.h:127
int32 InputDim() const
Get the dimension of the input,.
int32 NumParams ( ) const
inlinevirtual

Number of trainable parameters,.

Implements UpdatableComponent.

Definition at line 337 of file nnet-various.h.

References CuVectorBase< Real >::Dim(), and AddShift::shift_data_.

Referenced by kaldi::ConvertAddShiftComponent(), AddShift::GetGradient(), AddShift::GetParams(), and AddShift::SetParams().

337 { return shift_data_.Dim(); }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
void PropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
)
inlinevirtual

Abstract interface for propagation/backpropagation.

Forward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 366 of file nnet-various.h.

References CuMatrixBase< Real >::AddVecToRows(), CuMatrixBase< Real >::CopyFromMat(), and AddShift::shift_data_.

367  {
368  // copy, add the shift,
369  out->CopyFromMat(in);
370  out->AddVecToRows(1.0, shift_data_, 1.0);
371  }
void AddVecToRows(Real alpha, const CuVectorBase< Real > &row, Real beta=1.0)
(for each row r of *this), r = alpha * row + beta * r
Definition: cu-matrix.cc:1112
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 321 of file nnet-various.h.

References kaldi::ExpectToken(), UpdatableComponent::learn_rate_coef_, kaldi::Peek(), CuVector< Real >::Read(), kaldi::ReadBasicType(), and AddShift::shift_data_.

321  {
322  // optional learning-rate coef,
323  if ('<' == Peek(is, binary)) {
324  ExpectToken(is, binary, "<LearnRateCoef>");
325  ReadBasicType(is, binary, &learn_rate_coef_);
326  }
327  // read the shift data
328  shift_data_.Read(is, binary);
329  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
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...
Definition: io-funcs-inl.h:55
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
void Read(std::istream &is, bool binary)
I/O.
Definition: cu-vector.cc:862
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:188
void SetLearnRateCoef ( BaseFloat  val)
inlinevirtual

Set the learn-rate coefficient,.

Reimplemented from UpdatableComponent.

Definition at line 392 of file nnet-various.h.

References UpdatableComponent::learn_rate_coef_.

392 { learn_rate_coef_ = c; }
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
void SetParams ( const VectorBase< BaseFloat > &  params)
inlinevirtual

Set the trainable parameters from, reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 349 of file nnet-various.h.

References CuVectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), KALDI_ASSERT, AddShift::NumParams(), and AddShift::shift_data_.

Referenced by main().

349  {
350  KALDI_ASSERT(params.Dim() == NumParams());
351  shift_data_.CopyFromVec(params);
352  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:970
int32 NumParams() const
Number of trainable parameters,.
Definition: nnet-various.h:337
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
void Update ( const CuMatrixBase< BaseFloat > &  input,
const CuMatrixBase< BaseFloat > &  diff 
)
inlinevirtual

Compute gradient and update parameters,.

Implements UpdatableComponent.

Definition at line 381 of file nnet-various.h.

References CuVectorBase< Real >::AddRowSumMat(), CuVectorBase< Real >::AddVec(), Component::InputDim(), kaldi::kSetZero, NnetTrainOptions::learn_rate, UpdatableComponent::learn_rate_coef_, UpdatableComponent::opts_, CuVector< Real >::Resize(), AddShift::shift_data_, and AddShift::shift_data_grad_.

382  {
383  // we use following hyperparameters from the option class,
384  const BaseFloat lr = opts_.learn_rate;
385  // gradient,
386  shift_data_grad_.Resize(InputDim(), kSetZero); // reset to zero,
387  shift_data_grad_.AddRowSumMat(1.0, diff, 0.0);
388  // update,
390  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
NnetTrainOptions opts_
Option-class with training hyper-parameters,.
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
CuVector< BaseFloat > shift_data_grad_
Definition: nnet-various.h:396
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:892
void AddRowSumMat(Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
Sum the rows of the matrix, add to vector.
Definition: cu-vector.cc:1166
float BaseFloat
Definition: kaldi-types.h:29
int32 InputDim() const
Get the dimension of the input,.
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
Definition: cu-vector.cc:1126
void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 331 of file nnet-various.h.

References UpdatableComponent::learn_rate_coef_, AddShift::shift_data_, CuVector< Real >::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

331  {
332  WriteToken(os, binary, "<LearnRateCoef>");
333  WriteBasicType(os, binary, learn_rate_coef_);
334  shift_data_.Write(os, binary);
335  }
CuVector< BaseFloat > shift_data_
Definition: nnet-various.h:395
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
void Write(std::ostream &is, bool binary) const
Definition: cu-vector.cc:872
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
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...
Definition: io-funcs-inl.h:34

Member Data Documentation

CuVector<BaseFloat> shift_data_grad_
protected

Definition at line 396 of file nnet-various.h.

Referenced by AddShift::GetGradient(), AddShift::InfoGradient(), and AddShift::Update().


The documentation for this class was generated from the following file: