LinearTransform Class Reference

#include <nnet-linear-transform.h>

Inheritance diagram for LinearTransform:
Collaboration diagram for LinearTransform:

Public Member Functions

 LinearTransform (int32 dim_in, int32 dim_out)
 
 ~LinearTransform ()
 
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...
 
void SetLinearity (const MatrixBase< BaseFloat > &l)
 
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...
 
const CuMatrixBase< BaseFloat > & GetLinearity ()
 Accessors to the component parameters. More...
 
void SetLinearity (const CuMatrixBase< BaseFloat > &linearity)
 
const CuMatrixBase< BaseFloat > & GetLinearityCorr ()
 
- 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 SetLearnRateCoef (BaseFloat val)
 Set the learn-rate coefficient,. 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...
 

Private Attributes

CuMatrix< BaseFloatlinearity_
 
CuMatrix< BaseFloatlinearity_corr_
 

Additional Inherited Members

- Public Types inherited from Component
enum  ComponentType {
  kUnknown = 0x0, kUpdatableComponent = 0x0100, kAffineTransform, kLinearTransform,
  kConvolutionalComponent, 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, kMaxPoolingComponent, 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...
 
- 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...
 

Detailed Description

Definition at line 33 of file nnet-linear-transform.h.

Constructor & Destructor Documentation

◆ LinearTransform()

LinearTransform ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 35 of file nnet-linear-transform.h.

Referenced by LinearTransform::Copy().

35  :
36  UpdatableComponent(dim_in, dim_out),
37  linearity_(dim_out, dim_in),
38  linearity_corr_(dim_out, dim_in)
39  { }
CuMatrix< BaseFloat > linearity_corr_
UpdatableComponent(int32 input_dim, int32 output_dim)

◆ ~LinearTransform()

~LinearTransform ( )
inline

Definition at line 41 of file nnet-linear-transform.h.

42  { }

Member Function Documentation

◆ BackpropagateFnc()

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 161 of file nnet-linear-transform.h.

References CuMatrixBase< Real >::AddMatMat(), kaldi::kNoTrans, and LinearTransform::linearity_.

164  {
165  // multiply error derivative by weights
166  in_diff->AddMatMat(1.0, out_diff, kNoTrans, linearity_, kNoTrans, 0.0);
167  }

◆ Copy()

Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 44 of file nnet-linear-transform.h.

References LinearTransform::LinearTransform().

44 { return new LinearTransform(*this); }
LinearTransform(int32 dim_in, int32 dim_out)

◆ GetGradient()

void GetGradient ( VectorBase< BaseFloat > *  gradient) const
inlinevirtual

Get gradient reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 123 of file nnet-linear-transform.h.

References VectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::Dim(), KALDI_ASSERT, LinearTransform::linearity_corr_, and LinearTransform::NumParams().

123  {
124  KALDI_ASSERT(gradient->Dim() == NumParams());
125  gradient->CopyRowsFromMat(linearity_corr_);
126  }
int32 NumParams() const
Number of trainable parameters,.
CuMatrix< BaseFloat > linearity_corr_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetLinearity()

const CuMatrixBase<BaseFloat>& GetLinearity ( )
inline

Accessors to the component parameters.

Definition at line 194 of file nnet-linear-transform.h.

References LinearTransform::linearity_.

194 { return linearity_; }

◆ GetLinearityCorr()

const CuMatrixBase<BaseFloat>& GetLinearityCorr ( )
inline

Definition at line 202 of file nnet-linear-transform.h.

References LinearTransform::linearity_corr_.

202 { return linearity_corr_; }
CuMatrix< BaseFloat > linearity_corr_

◆ GetParams()

void GetParams ( VectorBase< BaseFloat > *  params) const
inlinevirtual

Get the trainable parameters reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 128 of file nnet-linear-transform.h.

References VectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::Dim(), KALDI_ASSERT, LinearTransform::linearity_, and LinearTransform::NumParams().

128  {
129  KALDI_ASSERT(params->Dim() == NumParams());
130  params->CopyRowsFromMat(linearity_);
131  }
int32 NumParams() const
Number of trainable parameters,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetType()

ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 45 of file nnet-linear-transform.h.

References Component::kLinearTransform.

◆ Info()

std::string Info ( ) const
inlinevirtual

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

Reimplemented from Component.

Definition at line 144 of file nnet-linear-transform.h.

References UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_, kaldi::nnet1::MomentStatistics(), and kaldi::nnet1::ToString().

144  {
145  return std::string("\n linearity") +
147  ", lr-coef " + ToString(learn_rate_coef_);
148  }
std::string ToString(const T &t)
Convert basic type to a string (please don&#39;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),.

◆ InfoGradient()

std::string InfoGradient ( ) const
inlinevirtual

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

Reimplemented from Component.

Definition at line 149 of file nnet-linear-transform.h.

References UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_corr_, kaldi::nnet1::MomentStatistics(), and kaldi::nnet1::ToString().

149  {
150  return std::string("\n linearity_grad") +
152  ", lr-coef " + ToString(learn_rate_coef_);
153  }
std::string ToString(const T &t)
Convert basic type to a string (please don&#39;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),.
CuMatrix< BaseFloat > linearity_corr_

◆ InitData()

void InitData ( std::istream &  is)
inlinevirtual

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

Implements UpdatableComponent.

Definition at line 47 of file nnet-linear-transform.h.

References Input::Close(), Component::InputDim(), KALDI_ERR, KALDI_LOG, UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_, Component::OutputDim(), kaldi::nnet1::RandGauss(), kaldi::ReadBasicType(), kaldi::ReadToken(), and Input::Stream().

47  {
48  // define options
49  float param_stddev = 0.1;
50  std::string read_matrix_file;
51  // parse config
52  std::string token;
53  while (is >> std::ws, !is.eof()) {
54  ReadToken(is, false, &token);
55  if (token == "<ParamStddev>") ReadBasicType(is, false, &param_stddev);
56  else if (token == "<ReadMatrix>") ReadToken(is, false, &read_matrix_file);
57  else if (token == "<LearnRateCoef>") ReadBasicType(is, false, &learn_rate_coef_);
58  else KALDI_ERR << "Unknown token " << token << ", a typo in config?"
59  << " (ParamStddev|ReadMatrix|LearnRateCoef)";
60  }
61 
62  if (read_matrix_file != "") { // load from file,
63  bool binary;
64  Input in(read_matrix_file, &binary);
65  linearity_.Read(in.Stream(), binary);
66  in.Close();
67  // check dims,
68  if (OutputDim() != linearity_.NumRows() ||
69  InputDim() != linearity_.NumCols()) {
70  KALDI_ERR << "Dimensionality mismatch! Expected matrix"
71  << " r=" << OutputDim() << " c=" << InputDim()
72  << ", loaded matrix " << read_matrix_file
73  << " with r=" << linearity_.NumRows()
74  << " c=" << linearity_.NumCols();
75  }
76  KALDI_LOG << "Loaded <LinearTransform> matrix from file : "
77  << read_matrix_file;
78  return;
79  }
80 
81  //
82  // Initialize trainable parameters,
83  //
84  // Gaussian with given std_dev (mean = 0),
85  linearity_.Resize(OutputDim(), InputDim());
86  RandGauss(0.0, param_stddev, &linearity_);
87  }
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 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
int32 InputDim() const
Get the dimension of the input,.
#define KALDI_ERR
Definition: kaldi-error.h:147
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...
Definition: nnet-utils.h:164
int32 OutputDim() const
Get the dimension of the output,.
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ NumParams()

int32 NumParams ( ) const
inlinevirtual

Number of trainable parameters,.

Implements UpdatableComponent.

Definition at line 119 of file nnet-linear-transform.h.

References LinearTransform::linearity_.

Referenced by LinearTransform::GetGradient(), LinearTransform::GetParams(), and LinearTransform::SetParams().

119  {
120  return linearity_.NumRows()*linearity_.NumCols();
121  }

◆ PropagateFnc()

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 155 of file nnet-linear-transform.h.

References CuMatrixBase< Real >::AddMatMat(), kaldi::kNoTrans, kaldi::kTrans, and LinearTransform::linearity_.

156  {
157  // multiply by weights^t
158  out->AddMatMat(1.0, in, kNoTrans, linearity_, kTrans, 0.0);
159  }

◆ ReadData()

void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 89 of file nnet-linear-transform.h.

References kaldi::ExpectToken(), Component::input_dim_, KALDI_ASSERT, KALDI_ERR, UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_, Component::output_dim_, kaldi::Peek(), kaldi::PeekToken(), kaldi::ReadBasicType(), and kaldi::ReadToken().

89  {
90  // Read all the '<Tokens>' in arbitrary order,
91  while ('<' == Peek(is, binary)) {
92  int first_char = PeekToken(is, binary);
93  switch (first_char) {
94  case 'L': ExpectToken(is, binary, "<LearnRateCoef>");
95  ReadBasicType(is, binary, &learn_rate_coef_);
96  break;
97  default:
98  std::string token;
99  ReadToken(is, false, &token);
100  KALDI_ERR << "Unknown token: " << token;
101  }
102  }
103  // Read the data (data follow the tokens),
104 
105  // weights
106  linearity_.Read(is, binary);
107 
108  KALDI_ASSERT(linearity_.NumRows() == output_dim_);
109  KALDI_ASSERT(linearity_.NumCols() == input_dim_);
110  }
int32 input_dim_
Data members,.
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 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
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:191
#define KALDI_ERR
Definition: kaldi-error.h:147
int PeekToken(std::istream &is, bool binary)
PeekToken will return the first character of the next token, or -1 if end of file.
Definition: io-funcs.cc:170
int32 output_dim_
Dimension of the output of the Component,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SetLinearity() [1/2]

void SetLinearity ( const MatrixBase< BaseFloat > &  l)
inline

Definition at line 138 of file nnet-linear-transform.h.

References KALDI_ASSERT, LinearTransform::linearity_, MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

Referenced by main().

138  {
139  KALDI_ASSERT(l.NumCols() == linearity_.NumCols());
140  KALDI_ASSERT(l.NumRows() == linearity_.NumRows());
141  linearity_.CopyFromMat(l);
142  }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SetLinearity() [2/2]

void SetLinearity ( const CuMatrixBase< BaseFloat > &  linearity)
inline

Definition at line 196 of file nnet-linear-transform.h.

References KALDI_ASSERT, LinearTransform::linearity_, CuMatrixBase< Real >::NumCols(), and CuMatrixBase< Real >::NumRows().

196  {
197  KALDI_ASSERT(linearity.NumRows() == linearity_.NumRows());
198  KALDI_ASSERT(linearity.NumCols() == linearity_.NumCols());
199  linearity_.CopyFromMat(linearity);
200  }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SetParams()

void SetParams ( const VectorBase< BaseFloat > &  params)
inlinevirtual

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

Implements UpdatableComponent.

Definition at line 133 of file nnet-linear-transform.h.

References VectorBase< Real >::Dim(), KALDI_ASSERT, LinearTransform::linearity_, and LinearTransform::NumParams().

133  {
134  KALDI_ASSERT(params.Dim() == NumParams());
135  linearity_.CopyRowsFromVec(params);
136  }
int32 NumParams() const
Number of trainable parameters,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Update()

void Update ( const CuMatrixBase< BaseFloat > &  input,
const CuMatrixBase< BaseFloat > &  diff 
)
inlinevirtual

Compute gradient and update parameters,.

Implements UpdatableComponent.

Definition at line 170 of file nnet-linear-transform.h.

References kaldi::kNoTrans, kaldi::kTrans, NnetTrainOptions::l1_penalty, NnetTrainOptions::l2_penalty, NnetTrainOptions::learn_rate, UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_, LinearTransform::linearity_corr_, NnetTrainOptions::momentum, CuMatrixBase< Real >::NumRows(), UpdatableComponent::opts_, and kaldi::cu::RegularizeL1().

171  {
172  // we use following hyperparameters from the option class
173  const BaseFloat lr = opts_.learn_rate;
174  const BaseFloat mmt = opts_.momentum;
175  const BaseFloat l2 = opts_.l2_penalty;
176  const BaseFloat l1 = opts_.l1_penalty;
177  // we will also need the number of frames in the mini-batch
178  const int32 num_frames = input.NumRows();
179  // compute gradient (incl. momentum)
180  linearity_corr_.AddMatMat(1.0, diff, kTrans, input, kNoTrans, mmt);
181  // l2 regularization
182  if (l2 != 0.0) {
183  linearity_.AddMat(-lr*l2*num_frames, linearity_);
184  }
185  // l1 regularization
186  if (l1 != 0.0) {
187  cu::RegularizeL1(&linearity_, &linearity_corr_, lr*l1*num_frames, lr);
188  }
189  // update
191  }
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),.
kaldi::int32 int32
CuMatrix< BaseFloat > linearity_corr_
float BaseFloat
Definition: kaldi-types.h:29
void RegularizeL1(CuMatrixBase< Real > *weight, CuMatrixBase< Real > *grad, Real l1, Real lr)
RegularizeL1 is a gradient step with l1 regularization added to the gradient.
Definition: cu-math.cc:37

◆ WriteData()

void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 112 of file nnet-linear-transform.h.

References UpdatableComponent::learn_rate_coef_, LinearTransform::linearity_, kaldi::WriteBasicType(), and kaldi::WriteToken().

112  {
113  WriteToken(os, binary, "<LearnRateCoef>");
114  WriteBasicType(os, binary, learn_rate_coef_);
115  if (!binary) os << "\n";
116  linearity_.Write(os, binary);
117  }
BaseFloat learn_rate_coef_
Scalar applied to learning rate for weight matrices (to be used in ::Update method),.
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

◆ linearity_

◆ linearity_corr_


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