DctComponent Class Reference

Discrete cosine transform. More...

#include <nnet-component.h>

Inheritance diagram for DctComponent:
Collaboration diagram for DctComponent:

Public Member Functions

 DctComponent ()
 
virtual std::string Type () const
 
virtual std::string Info () const
 
void Init (int32 dim, int32 dct_dim, bool reorder, int32 keep_dct_dim=0)
 
virtual void InitFromString (std::string args)
 Initialize, typically from a line of a config file. More...
 
virtual int32 InputDim () const
 Get size of input vectors. More...
 
virtual int32 OutputDim () const
 Get size of output vectors. More...
 
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...
 
virtual bool BackpropNeedsInput () const
 
virtual bool BackpropNeedsOutput () const
 
virtual ComponentCopy () const
 Copy component (deep copy). More...
 
virtual void Read (std::istream &is, bool binary)
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. 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 ~Component ()
 

Private Member Functions

void Reorder (CuMatrixBase< BaseFloat > *mat, bool reverse) const
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (DctComponent)
 

Private Attributes

int32 dim_
 
bool reorder_
 
CuMatrix< BaseFloatdct_mat_
 

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

Discrete cosine transform.

TODO: modify this Component so that it supports only keeping a subset

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

Constructor & Destructor Documentation

◆ DctComponent()

DctComponent ( )
inline

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

1363 { dim_ = 0; }

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 3086 of file nnet-component.cc.

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

3092  {
3093  KALDI_ASSERT(out_deriv.NumCols() == OutputDim());
3094 
3095  int32 dct_dim = dct_mat_.NumCols(),
3096  dct_keep_dim = dct_mat_.NumRows(),
3097  num_chunks = dim_ / dct_dim,
3098  num_rows = out_deriv.NumRows();
3099 
3100  in_deriv->Resize(num_rows, dim_);
3101 
3102  CuMatrix<BaseFloat> out_deriv_tmp;
3103  if (reorder_) {
3104  out_deriv_tmp = out_deriv;
3105  Reorder(&out_deriv_tmp, false);
3106  }
3107  for (int32 chunk = 0; chunk < num_chunks; chunk++) {
3108  CuSubMatrix<BaseFloat> in_deriv_mat(*in_deriv,
3109  0, num_rows, dct_dim * chunk, dct_dim),
3110  out_deriv_mat(reorder_ ? out_deriv_tmp : out_deriv,
3111  0, num_rows, dct_keep_dim * chunk, dct_keep_dim);
3112 
3113  // Note: in the reverse direction the DCT matrix is transposed. This is
3114  // normal when computing derivatives; the necessity for the transpose is
3115  // obvious if you consider what happens when the input and output dims
3116  // differ.
3117  in_deriv_mat.AddMatMat(1.0, out_deriv_mat, kNoTrans,
3118  dct_mat_, kNoTrans, 0.0);
3119  }
3120  if (reorder_)
3121  Reorder(in_deriv, true);
3122 }
virtual int32 OutputDim() const
Get size of output vectors.
kaldi::int32 int32
void Reorder(CuMatrixBase< BaseFloat > *mat, bool reverse) const
CuMatrix< BaseFloat > dct_mat_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ BackpropNeedsInput()

virtual bool BackpropNeedsInput ( ) const
inlinevirtual

Reimplemented from Component.

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

1387 { return false; }

◆ BackpropNeedsOutput()

virtual bool BackpropNeedsOutput ( ) const
inlinevirtual

Reimplemented from Component.

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

References kaldi::cu::Copy().

1388 { return false; }

◆ Copy()

Component * Copy ( ) const
virtual

Copy component (deep copy).

Implements Component.

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

References DctComponent::dct_mat_, DctComponent::dim_, and DctComponent::reorder_.

3124  {
3125  DctComponent *ans = new DctComponent();
3126  ans->dct_mat_ = dct_mat_;
3127  ans->dim_ = dim_;
3128  ans->reorder_ = reorder_;
3129  return ans;
3130 }
CuMatrix< BaseFloat > dct_mat_

◆ Info()

std::string Info ( ) const
virtual

Reimplemented from Component.

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

References Component::Info().

2986  {
2987  std::stringstream stream;
2988  stream << Component::Info() << ", dct_dim=" << dct_mat_.NumCols();
2989  if (dct_mat_.NumCols() != dct_mat_.NumRows())
2990  stream << ", dct_keep_dim=" << dct_mat_.NumRows();
2991 
2992  return stream.str();
2993 }
CuMatrix< BaseFloat > dct_mat_
virtual std::string Info() const

◆ Init()

void Init ( int32  dim,
int32  dct_dim,
bool  reorder,
int32  keep_dct_dim = 0 
)

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

References kaldi::ComputeDctMatrix(), and KALDI_ASSERT.

Referenced by kaldi::nnet2::UnitTestDctComponent().

2995  {
2996  int dct_keep_dim_ = (dct_keep_dim > 0) ? dct_keep_dim : dct_dim;
2997 
2998  KALDI_ASSERT(dim > 0 && dct_dim > 0);
2999  KALDI_ASSERT(dim % dct_dim == 0); // dct_dim must divide dim.
3000  KALDI_ASSERT(dct_dim >= dct_keep_dim_);
3001  dim_ = dim;
3002  dct_mat_.Resize(dct_keep_dim_, dct_dim);
3003  reorder_ = reorder;
3004  Matrix<BaseFloat> dct_mat(dct_keep_dim_, dct_dim);
3005  ComputeDctMatrix(&dct_mat);
3006  dct_mat_ = dct_mat;
3007 }
void ComputeDctMatrix(Matrix< Real > *M)
ComputeDctMatrix computes a matrix corresponding to the DCT, such that M * v equals the DCT of vector...
CuMatrix< BaseFloat > dct_mat_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ 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 3011 of file nnet-component.cc.

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

Referenced by kaldi::nnet2::UnitTestDctComponent().

3011  {
3012  std::string orig_args(args);
3013  int32 dim, dct_dim, dct_keep_dim = 0;
3014  bool reorder = false;
3015 
3016  bool ok = ParseFromString("dim", &args, &dim);
3017  ok = ParseFromString("dct-dim", &args, &dct_dim) && ok;
3018  ok = ParseFromString("reorder", &args, &reorder) && ok;
3019  ParseFromString("dct-keep-dim", &args, &dct_keep_dim);
3020 
3021  if (!ok || !args.empty() || dim <= 0 || dct_dim <= 0 || dct_keep_dim < 0)
3022  KALDI_ERR << "Invalid initializer for layer of type "
3023  << Type() << ": \"" << orig_args << "\"";
3024  Init(dim, dct_dim, reorder, dct_keep_dim);
3025 }
virtual std::string Type() const
void Init(int32 dim, int32 dct_dim, bool reorder, int32 keep_dct_dim=0)
kaldi::int32 int32
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ InputDim()

virtual int32 InputDim ( ) const
inlinevirtual

Get size of input vectors.

Implements Component.

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

1373 { return dim_; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( DctComponent  )
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Get size of output vectors.

Implements Component.

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

References Component::Propagate().

1374 { return dct_mat_.NumRows() * (dim_ / dct_mat_.NumCols()); }
CuMatrix< BaseFloat > dct_mat_

◆ 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 3053 of file nnet-component.cc.

References CuMatrixBase< Real >::AddMatMat(), ChunkInfo::CheckSize(), AffineComponent::InputDim(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, ChunkInfo::NumCols(), CuMatrixBase< Real >::NumCols(), ChunkInfo::NumRows(), and CuMatrixBase< Real >::NumRows().

3056  {
3057  KALDI_ASSERT(in.NumCols() == InputDim());
3058  int32 dct_dim = dct_mat_.NumCols(),
3059  dct_keep_dim = dct_mat_.NumRows(),
3060  num_rows = in.NumRows(),
3061  num_chunks = dim_ / dct_dim;
3062 
3063  in_info.CheckSize(in);
3064  out_info.CheckSize(*out);
3065  KALDI_ASSERT(num_rows == out_info.NumRows());
3066  KALDI_ASSERT(num_chunks * dct_keep_dim == out_info.NumCols());
3067 
3068  CuMatrix<BaseFloat> in_tmp;
3069  if (reorder_) {
3070  in_tmp = in;
3071  Reorder(&in_tmp, false);
3072  }
3073 
3074  for (int32 chunk = 0; chunk < num_chunks; chunk++) {
3075  CuSubMatrix<BaseFloat> in_mat(reorder_ ? in_tmp : in,
3076  0, num_rows, dct_dim * chunk, dct_dim),
3077  out_mat(*out,
3078  0, num_rows, dct_keep_dim * chunk, dct_keep_dim);
3079 
3080  out_mat.AddMatMat(1.0, in_mat, kNoTrans, dct_mat_, kTrans, 0.0);
3081  }
3082  if (reorder_)
3083  Reorder(out, true);
3084 }
kaldi::int32 int32
void Reorder(CuMatrixBase< BaseFloat > *mat, bool reverse) const
CuMatrix< BaseFloat > dct_mat_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
virtual int32 InputDim() const
Get size of input vectors.

◆ Read()

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

Implements Component.

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

References kaldi::nnet2::ExpectOneOrTwoTokens(), kaldi::ExpectToken(), AffineComponentPreconditionedOnline::Init(), KALDI_ASSERT, KALDI_ERR, kaldi::ReadBasicType(), and kaldi::ReadToken().

3147  {
3148  ExpectOneOrTwoTokens(is, binary, "<DctComponent>", "<Dim>");
3149  ReadBasicType(is, binary, &dim_);
3150 
3151  ExpectToken(is, binary, "<DctDim>");
3152  int32 dct_dim;
3153  ReadBasicType(is, binary, &dct_dim);
3154 
3155  ExpectToken(is, binary, "<Reorder>");
3156  ReadBasicType(is, binary, &reorder_);
3157 
3158  int32 dct_keep_dim = dct_dim;
3159  std::string token;
3160  ReadToken(is, binary, &token);
3161  if (token == "<DctKeepDim>") {
3162  ReadBasicType(is, binary, &dct_keep_dim);
3163  ExpectToken(is, binary, "</DctComponent>");
3164  } else if (token != "</DctComponent>") {
3165  KALDI_ERR << "Expected token \"</DctComponent>\", got instead \""
3166  << token << "\".";
3167  }
3168 
3169  KALDI_ASSERT(dct_dim > 0 && dim_ > 0 && dim_ % dct_dim == 0);
3170  Init(dim_, dct_dim, reorder_, dct_keep_dim);
3171  //idct_mat_.Resize(dct_keep_dim, dct_dim);
3172  //ComputeDctMatrix(&dct_mat_);
3173 }
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
void Init(int32 dim, int32 dct_dim, bool reorder, int32 keep_dct_dim=0)
kaldi::int32 int32
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 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
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)

◆ Reorder()

void Reorder ( CuMatrixBase< BaseFloat > *  mat,
bool  reverse 
) const
private

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

References CuVectorBase< Real >::CopyFromVec(), rnnlm::i, rnnlm::j, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and kaldi::swap().

3027  {
3028  // reorders into contiguous blocks of dize "dct_dim_", assuming that
3029  // such blocks were interlaced before. if reverse==true, does the
3030  // reverse.
3031  int32 dct_dim = dct_mat_.NumCols(),
3032  dct_keep_dim = dct_mat_.NumRows(),
3033  block_size_in = dim_ / dct_dim,
3034  block_size_out = dct_keep_dim;
3035 
3036  //This does not necesarily needs to be true anymore -- output must be reordered as well, but the dimension differs...
3037  //KALDI_ASSERT(mat->NumCols() == dim_);
3038  if (reverse) std::swap(block_size_in, block_size_out);
3039 
3040  CuVector<BaseFloat> temp(mat->NumCols());
3041  for (int32 i = 0; i < mat->NumRows(); i++) {
3042  CuSubVector<BaseFloat> row(*mat, i);
3043  int32 num_blocks_in = block_size_out;
3044  for (int32 b = 0; b < num_blocks_in; b++) {
3045  for (int32 j = 0; j < block_size_in; j++) {
3046  temp(j * block_size_out + b) = row(b * block_size_in + j);
3047  }
3048  }
3049  row.CopyFromVec(temp);
3050  }
3051 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
kaldi::int32 int32
CuMatrix< BaseFloat > dct_mat_

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

Implements Component.

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

1364 { return "DctComponent"; }

◆ Write()

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

Write component to stream.

Implements Component.

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

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

3132  {
3133  WriteToken(os, binary, "<DctComponent>");
3134  WriteToken(os, binary, "<Dim>");
3135  WriteBasicType(os, binary, dim_);
3136  WriteToken(os, binary, "<DctDim>");
3137  int32 dct_dim = dct_mat_.NumCols();
3138  WriteBasicType(os, binary, dct_dim);
3139  WriteToken(os, binary, "<Reorder>");
3140  WriteBasicType(os, binary, reorder_);
3141  WriteToken(os, binary, "<DctKeepDim>");
3142  int32 dct_keep_dim = dct_mat_.NumRows();
3143  WriteBasicType(os, binary, dct_keep_dim);
3144  WriteToken(os, binary, "</DctComponent>");
3145 }
kaldi::int32 int32
CuMatrix< BaseFloat > dct_mat_
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

◆ dct_mat_

CuMatrix<BaseFloat> dct_mat_
private

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

Referenced by DctComponent::Copy().

◆ dim_

int32 dim_
private

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

Referenced by DctComponent::Copy().

◆ reorder_

bool reorder_
private

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

Referenced by DctComponent::Copy().


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