PermuteComponent Class Reference

PermuteComponent does a permutation of the dimensions (by default, a fixed random permutation, but it may be specified). More...

#include <nnet-component.h>

Inheritance diagram for PermuteComponent:
Collaboration diagram for PermuteComponent:

Public Member Functions

void Init (int32 dim)
 
void Init (const std::vector< int32 > &reorder)
 
 PermuteComponent (int32 dim)
 
 PermuteComponent (const std::vector< int32 > &reorder)
 
 PermuteComponent ()
 
virtual int32 InputDim () const
 Get size of input vectors. More...
 
virtual int32 OutputDim () const
 Get size of output vectors. More...
 
virtual ComponentCopy () const
 Copy component (deep copy). More...
 
virtual void InitFromString (std::string args)
 Initialize, typically from a line of a config file. More...
 
virtual void Read (std::istream &is, bool binary)
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual std::string Type () const
 
virtual bool BackpropNeedsInput () const
 
virtual bool BackpropNeedsOutput () const
 
virtual void Propagate (const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Perform forward pass propagation Input->Output. More...
 
virtual void Backprop (const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, Component *to_update, CuMatrix< BaseFloat > *in_deriv) const
 Perform backward pass propagation of the derivative, and also either update the model (if to_update == this) or update another model or compute the model derivative (otherwise). More...
 
- Public Member Functions inherited from Component
 Component ()
 
virtual int32 Index () const
 Returns the index in the sequence of layers in the neural net; intended only to be used in debugging information. More...
 
virtual void SetIndex (int32 index)
 
virtual std::vector< int32Context () const
 Return a vector describing the temporal context this component requires for each frame of output, as a sorted list. More...
 
void Propagate (const ChunkInfo &in_info, const ChunkInfo &out_info, const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out) const
 A non-virtual propagate function that first resizes output if necessary. More...
 
virtual std::string Info () const
 
virtual ~Component ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (PermuteComponent)
 

Private Attributes

std::vector< int32reorder_
 

Additional Inherited Members

- Static Public Member Functions inherited from Component
static ComponentReadNew (std::istream &is, bool binary)
 Read component from stream. More...
 
static ComponentNewFromString (const std::string &initializer_line)
 Initialize the Component from one line that will contain first the type, e.g. More...
 
static ComponentNewComponentOfType (const std::string &type)
 Return a new Component of the given type e.g. More...
 

Detailed Description

PermuteComponent does a permutation of the dimensions (by default, a fixed random permutation, but it may be specified).

Useful in conjunction with block-diagonal transforms.

Definition at line 1320 of file nnet-component.h.

Constructor & Destructor Documentation

◆ PermuteComponent() [1/3]

PermuteComponent ( int32  dim)
inline

Definition at line 1324 of file nnet-component.h.

1324 { Init(dim); }

◆ PermuteComponent() [2/3]

PermuteComponent ( const std::vector< int32 > &  reorder)
inline

Definition at line 1325 of file nnet-component.h.

1325 { Init(reorder); }

◆ PermuteComponent() [3/3]

PermuteComponent ( )
inline

Definition at line 1327 of file nnet-component.h.

1327 { } // e.g. prior to Read() or Init()

Member Function Documentation

◆ Backprop()

void Backprop ( const ChunkInfo in_info,
const ChunkInfo out_info,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_value,
const CuMatrixBase< BaseFloat > &  out_deriv,
Component to_update,
CuMatrix< BaseFloat > *  in_deriv 
) const
virtual

Perform backward pass propagation of the derivative, and also either update the model (if to_update == this) or update another model or compute the model derivative (otherwise).

Note: in_value and out_value are the values of the input and output of the component, and these may be dummy variables if respectively BackpropNeedsInput() or BackpropNeedsOutput() return false for that component (not all components need these).

num_chunks lets us treat the input matrix as contiguous-in-time chunks of equal size; it only matters if splicing is involved.

Implements Component.

Definition at line 2350 of file nnet-component.cc.

References CuMatrixBase< Real >::CopyCols(), KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), AffineComponent::OutputDim(), and CuMatrix< Real >::Resize().

2356  {
2357  in_deriv->Resize(out_deriv.NumRows(), out_deriv.NumCols());
2358  KALDI_ASSERT(out_deriv.NumCols() == OutputDim());
2359  // Note: if we were actually using this component type we could make the
2360  // CuArray a member variable for efficiency.
2361  CuArray<int32> cu_reorder(reorder_);
2362  in_deriv->CopyCols(out_deriv, cu_reorder);
2363 }
virtual int32 OutputDim() const
Get size of output vectors.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< int32 > reorder_

◆ BackpropNeedsInput()

virtual bool BackpropNeedsInput ( ) const
inlinevirtual

Reimplemented from Component.

Definition at line 1337 of file nnet-component.h.

1337 { return false; }

◆ BackpropNeedsOutput()

virtual bool BackpropNeedsOutput ( ) const
inlinevirtual

Reimplemented from Component.

Definition at line 1338 of file nnet-component.h.

References KALDI_DISALLOW_COPY_AND_ASSIGN, and Component::Propagate().

1338 { return false; }

◆ Copy()

Component * Copy ( ) const
virtual

Copy component (deep copy).

Implements Component.

Definition at line 290 of file nnet-component.cc.

References PermuteComponent::reorder_.

290  {
291  PermuteComponent *ans = new PermuteComponent();
292  ans->reorder_ = reorder_;
293  return ans;
294 }
std::vector< int32 > reorder_

◆ Init() [1/2]

void Init ( int32  dim)

Definition at line 2316 of file nnet-component.cc.

References rnnlm::i, and KALDI_ASSERT.

2316  {
2317  KALDI_ASSERT(dim > 0);
2318  reorder_.resize(dim);
2319  for (int32 i = 0; i < dim; i++) reorder_[i] = i;
2320  std::random_shuffle(reorder_.begin(), reorder_.end());
2321 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< int32 > reorder_

◆ Init() [2/2]

void Init ( const std::vector< int32 > &  reorder)

Definition at line 295 of file nnet-component.cc.

References rnnlm::i, and KALDI_ASSERT.

295  {
296  reorder_ = reorder;
297  KALDI_ASSERT(!reorder.empty());
298  std::vector<int32> indexes(reorder);
299  std::sort(indexes.begin(), indexes.end());
300  for (int32 i = 0; i < static_cast<int32>(indexes.size()); i++)
301  KALDI_ASSERT(i == indexes[i] && "Not a permutation");
302 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< int32 > reorder_

◆ InitFromString()

void InitFromString ( std::string  args)
virtual

Initialize, typically from a line of a config file.

The "args" will contain any parameters that need to be passed to the Component, e.g. dimensions.

Implements Component.

Definition at line 2323 of file nnet-component.cc.

References AffineComponentPreconditionedOnline::Init(), KALDI_ERR, kaldi::nnet2::ParseFromString(), and AffineComponentPreconditionedOnline::Type().

2323  {
2324  std::string orig_args(args);
2325  int32 dim;
2326  bool ok = ParseFromString("dim", &args, &dim);
2327  if (!ok || !args.empty() || dim <= 0)
2328  KALDI_ERR << "Invalid initializer for layer of type "
2329  << Type() << ": \"" << orig_args << "\"";
2330  Init(dim);
2331 }
kaldi::int32 int32
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
virtual std::string Type() const
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ InputDim()

virtual int32 InputDim ( ) const
inlinevirtual

Get size of input vectors.

Implements Component.

Definition at line 1329 of file nnet-component.h.

1329 { return reorder_.size(); }
std::vector< int32 > reorder_

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( PermuteComponent  )
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Get size of output vectors.

Implements Component.

Definition at line 1330 of file nnet-component.h.

References kaldi::cu::Copy().

1330 { return reorder_.size(); }
std::vector< int32 > reorder_

◆ Propagate()

void Propagate ( const ChunkInfo in_info,
const ChunkInfo out_info,
const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
virtual

Perform forward pass propagation Input->Output.

Each row is one frame or training example. Interpreted as "num_chunks" equally sized chunks of frames; this only matters for layers that do things like context splicing. Typically this variable will either be 1 (when we're processing a single contiguous chunk of data) or will be the same as in.NumFrames(), but other values are possible if some layers do splicing.

Implements Component.

Definition at line 2333 of file nnet-component.cc.

References ChunkInfo::CheckSize(), CuMatrixBase< Real >::CopyCols(), rnnlm::i, KALDI_ASSERT, and ChunkInfo::NumChunks().

2336  {
2337  in_info.CheckSize(in);
2338  out_info.CheckSize(*out);
2339  KALDI_ASSERT(in_info.NumChunks() == out_info.NumChunks());
2340 
2341  std::vector<int32> reverse_reorder(reorder_.size());
2342  for (size_t i = 0; i < reorder_.size(); i++)
2343  reverse_reorder[reorder_[i]] = i;
2344  // Note: if we were actually using this component type we could make the
2345  // CuArray a member variable for efficiency.
2346  CuArray<int32> cu_reverse_reorder(reverse_reorder);
2347  out->CopyCols(in, cu_reverse_reorder);
2348 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< int32 > reorder_

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)
virtual

Implements Component.

Definition at line 2303 of file nnet-component.cc.

References kaldi::nnet2::ExpectOneOrTwoTokens(), kaldi::ExpectToken(), and kaldi::ReadIntegerVector().

2303  {
2304  ExpectOneOrTwoTokens(is, binary, "<PermuteComponent>", "<Reorder>");
2305  ReadIntegerVector(is, binary, &reorder_);
2306  ExpectToken(is, binary, "</PermuteComponent>");
2307 }
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
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
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
std::vector< int32 > reorder_

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

Implements Component.

Definition at line 1336 of file nnet-component.h.

1336 { return "PermuteComponent"; }

◆ Write()

void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

Definition at line 2309 of file nnet-component.cc.

References kaldi::WriteIntegerVector(), and kaldi::WriteToken().

2309  {
2310  WriteToken(os, binary, "<PermuteComponent>");
2311  WriteToken(os, binary, "<Reorder>");
2312  WriteIntegerVector(os, binary, reorder_);
2313  WriteToken(os, binary, "</PermuteComponent>");
2314 }
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
std::vector< int32 > reorder_
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198

Member Data Documentation

◆ reorder_

std::vector<int32> reorder_
private

Definition at line 1354 of file nnet-component.h.

Referenced by PermuteComponent::Copy().


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