SentenceAveragingComponent Class Reference

Deprecated!!!, keeping it as Katka Zmolikova used it in JSALT 2015. More...

#include <nnet-sentence-averaging-component.h>

Inheritance diagram for SentenceAveragingComponent:
Collaboration diagram for SentenceAveragingComponent:

Public Member Functions

 SentenceAveragingComponent (int32 dim_in, int32 dim_out)
 
 ~SentenceAveragingComponent ()
 
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 SetTrainOptions (const NnetTrainOptions &opts)
 Set the training options to the component,. 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...
 
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

Nnet nnet_
 
float learn_rate_factor_
 

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

Deprecated!!!, keeping it as Katka Zmolikova used it in JSALT 2015.

Definition at line 180 of file nnet-sentence-averaging-component.h.

Constructor & Destructor Documentation

◆ SentenceAveragingComponent()

SentenceAveragingComponent ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 182 of file nnet-sentence-averaging-component.h.

182  :
183  UpdatableComponent(dim_in, dim_out), learn_rate_factor_(100.0)
184  { }
UpdatableComponent(int32 input_dim, int32 output_dim)

◆ ~SentenceAveragingComponent()

Definition at line 185 of file nnet-sentence-averaging-component.h.

186  { }

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 267 of file nnet-sentence-averaging-component.h.

References CuMatrixBase< Real >::ColRange(), CuMatrixBase< Real >::CopyFromMat(), and CuMatrixBase< Real >::NumCols().

270  {
271  if (in_diff == NULL) return;
272  int32 num_inputs = in.NumCols(),
273  nnet_outputs = nnet_.OutputDim();
274  in_diff->CopyFromMat(out_diff.ColRange(nnet_outputs, num_inputs));
275  }
kaldi::int32 int32
int32 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143

◆ Copy()

Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 188 of file nnet-sentence-averaging-component.h.

188 { return new SentenceAveragingComponent(*this); }

◆ GetGradient()

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

Get gradient reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 225 of file nnet-sentence-averaging-component.h.

References KALDI_ERR.

225  {
226  KALDI_ERR << "Unimplemented!";
227  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ GetParams()

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

Get the trainable parameters reshaped as a vector,.

Implements UpdatableComponent.

Definition at line 229 of file nnet-sentence-averaging-component.h.

References VectorBase< Real >::CopyFromVec(), VectorBase< Real >::Dim(), and KALDI_ASSERT.

229  {
230  KALDI_ASSERT(params->Dim() == NumParams());
231  Vector<BaseFloat> params_aux;
232  nnet_.GetParams(&params_aux);
233  params->CopyFromVec(params_aux);
234  }
void GetParams(Vector< BaseFloat > *params) const
Get the network weights in a supervector,.
Definition: nnet-nnet.cc:237
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 189 of file nnet-sentence-averaging-component.h.

References Component::kSentenceAveragingComponent.

◆ Info()

std::string Info ( ) const
inlinevirtual

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

Reimplemented from Component.

Definition at line 240 of file nnet-sentence-averaging-component.h.

240  {
241  return std::string("nested_network {\n") + nnet_.Info() + "}\n";
242  }
std::string Info() const
Create string with human readable description of the nnet,.
Definition: nnet-nnet.cc:386

◆ InfoGradient()

std::string InfoGradient ( ) const
inlinevirtual

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

Reimplemented from Component.

Definition at line 244 of file nnet-sentence-averaging-component.h.

244  {
245  return std::string("nested_gradient {\n") + nnet_.InfoGradient() + "}\n";
246  }
std::string InfoGradient(bool header=true) const
Create string with per-component gradient statistics,.
Definition: nnet-nnet.cc:407

◆ InitData()

void InitData ( std::istream &  is)
inlinevirtual

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

Implements UpdatableComponent.

Definition at line 191 of file nnet-sentence-averaging-component.h.

References Component::InputDim(), KALDI_ASSERT, KALDI_ERR, Component::OutputDim(), kaldi::ReadBasicType(), and kaldi::ReadToken().

191  {
192  // define options
193  std::string nested_nnet_filename;
194  std::string nested_nnet_proto;
195  // parse config
196  std::string token;
197  while (is >> std::ws, !is.eof()) {
198  ReadToken(is, false, &token);
199  if (token == "<NestedNnetFilename>") ReadToken(is, false, &nested_nnet_filename);
200  else if (token == "<NestedNnetProto>") ReadToken(is, false, &nested_nnet_proto);
201  else if (token == "<LearnRateFactor>") ReadBasicType(is, false, &learn_rate_factor_);
202  else KALDI_ERR << "Unknown token " << token << " Typo in config?";
203  }
204  // initialize (read already prepared nnet from file)
205  KALDI_ASSERT((nested_nnet_proto != "") ^ (nested_nnet_filename != "")); // xor,
206  if (nested_nnet_filename != "") nnet_.Read(nested_nnet_filename);
207  if (nested_nnet_proto != "") nnet_.Init(nested_nnet_proto);
208  // check dims of nested nnet
211  }
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
int32 InputDim() const
Dimensionality on network input (input feature dim.),.
Definition: nnet-nnet.cc:148
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 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143
int32 InputDim() const
Get the dimension of the input,.
#define KALDI_ERR
Definition: kaldi-error.h:147
void Read(const std::string &rxfilename)
Read Nnet from &#39;rxfilename&#39;,.
Definition: nnet-nnet.cc:333
void Init(const std::string &proto_file)
Initialize the Nnet from the prototype,.
Definition: nnet-nnet.cc:301
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 OutputDim() const
Get the dimension of the output,.

◆ NumParams()

int32 NumParams ( ) const
inlinevirtual

Number of trainable parameters,.

Implements UpdatableComponent.

Definition at line 223 of file nnet-sentence-averaging-component.h.

223 { return nnet_.NumParams(); }
int32 NumParams() const
Get the number of parameters in the network,.
Definition: nnet-nnet.cc:210

◆ 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 248 of file nnet-sentence-averaging-component.h.

References CuVectorBase< Real >::AddRowSumMat(), CuMatrixBase< Real >::ColRange(), CuMatrixBase< Real >::NumCols(), and CuMatrixBase< Real >::NumRows().

249  {
250  // Get NN output
251  CuMatrix<BaseFloat> out_nnet;
252  nnet_.Propagate(in, &out_nnet);
253  // Get the average row (averaging over the time axis):
254  // averaging corresponds to extraction of a 'constant vector'
255  // code for single sentence,
256  int32 num_inputs = in.NumCols(),
257  nnet_outputs = nnet_.OutputDim(),
258  num_frames = out_nnet.NumRows();
259 
260  CuVector<BaseFloat> average_row(nnet_outputs);
261  average_row.AddRowSumMat(1.0/num_frames, out_nnet, 0.0);
262  // Forwarding sentence codes along with input features
263  out->ColRange(0, nnet_outputs).AddVecToRows(1.0, average_row, 0.0);
264  out->ColRange(nnet_outputs, num_inputs).CopyFromMat(in);
265  }
void Propagate(const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
Perform forward pass through the network,.
Definition: nnet-nnet.cc:70
kaldi::int32 int32
int32 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143

◆ ReadData()

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

Reads the component content.

Reimplemented from Component.

Definition at line 213 of file nnet-sentence-averaging-component.h.

References Component::InputDim(), KALDI_ASSERT, and Component::OutputDim().

213  {
214  nnet_.Read(is, binary);
217  }
int32 InputDim() const
Dimensionality on network input (input feature dim.),.
Definition: nnet-nnet.cc:148
int32 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143
int32 InputDim() const
Get the dimension of the input,.
void Read(const std::string &rxfilename)
Read Nnet from &#39;rxfilename&#39;,.
Definition: nnet-nnet.cc:333
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 OutputDim() const
Get the dimension of the output,.

◆ SetParams()

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

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

Implements UpdatableComponent.

Definition at line 236 of file nnet-sentence-averaging-component.h.

References KALDI_ERR.

236  {
237  KALDI_ERR << "Unimplemented!";
238  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ SetTrainOptions()

void SetTrainOptions ( const NnetTrainOptions opts)
inlinevirtual

Set the training options to the component,.

Reimplemented from UpdatableComponent.

Definition at line 297 of file nnet-sentence-averaging-component.h.

References NnetTrainOptions::learn_rate, and UpdatableComponent::SetTrainOptions().

297  {
299  // Pass the train options to the nnet
300  NnetTrainOptions o(opts);
301  o.learn_rate *= learn_rate_factor_;
303  }
virtual void SetTrainOptions(const NnetTrainOptions &opts)
Set the training options to the component,.
NnetTrainOptions opts_
Option-class with training hyper-parameters,.
void SetTrainOptions(const NnetTrainOptions &opts)
Set hyper-parameters of the training (pushes to all UpdatableComponents),.
Definition: nnet-nnet.cc:508

◆ Update()

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

Compute gradient and update parameters,.

Implements UpdatableComponent.

Definition at line 277 of file nnet-sentence-averaging-component.h.

References CuVectorBase< Real >::AddRowSumMat(), CuMatrixBase< Real >::AddVecToRows(), CuMatrixBase< Real >::ColRange(), and CuMatrixBase< Real >::NumRows().

278  {
279  // get useful dims,
280  int32 nnet_outputs = nnet_.OutputDim(),
281  num_frames = diff.NumRows();
282  // Passing the derivative into the nested network. The loss derivative is averaged:
283  // single frame from nested network influenced all the frames in the main network,
284  // so to get the derivative w.r.t. single frame from nested network we sum derivatives
285  // of all frames from main network (and scale by 1/Nframes constant).
286  //
287  // In fact all the frames from nested network influenced all the input frames to main nnet,
288  // so the loss derivarive w.r.t. nested network output is same for all frames in sentence.
289  CuVector<BaseFloat> average_diff(nnet_outputs);
290  average_diff.AddRowSumMat(1.0 / num_frames, diff.ColRange(0, nnet_outputs), 0.0);
291  CuMatrix<BaseFloat> nnet_out_diff(num_frames, nnet_outputs);
292  nnet_out_diff.AddVecToRows(1.0, average_diff, 0.0);
293  //
294  nnet_.Backpropagate(nnet_out_diff, NULL);
295  }
void Backpropagate(const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
Perform backward pass through the network,.
Definition: nnet-nnet.cc:96
kaldi::int32 int32
int32 OutputDim() const
Dimensionality of network outputs (posteriors | bn-features | etc.),.
Definition: nnet-nnet.cc:143

◆ WriteData()

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

Writes the component content.

Reimplemented from Component.

Definition at line 219 of file nnet-sentence-averaging-component.h.

219  {
220  nnet_.Write(os, binary);
221  }
void Write(const std::string &wxfilename, bool binary) const
Write Nnet to &#39;wxfilename&#39;,.
Definition: nnet-nnet.cc:367

Member Data Documentation

◆ learn_rate_factor_

float learn_rate_factor_
private

Definition at line 307 of file nnet-sentence-averaging-component.h.

◆ nnet_

Nnet nnet_
private

Definition at line 306 of file nnet-sentence-averaging-component.h.


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