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

Convolutional1dComponent implements convolution over frequency axis. More...

#include <nnet-component.h>

Inheritance diagram for Convolutional1dComponent:
Collaboration diagram for Convolutional1dComponent:

Public Member Functions

 Convolutional1dComponent ()
 
 Convolutional1dComponent (const Convolutional1dComponent &component)
 
 Convolutional1dComponent (const CuMatrixBase< BaseFloat > &filter_params, const CuVectorBase< BaseFloat > &bias_params, BaseFloat learning_rate)
 
int32 InputDim () const
 Get size of input vectors. More...
 
int32 OutputDim () const
 Get size of output vectors. More...
 
void Init (BaseFloat learning_rate, int32 input_dim, int32 output_dim, int32 patch_dim, int32 patch_step, int32 patch_stride, BaseFloat param_stddev, BaseFloat bias_stddev, bool appended_conv)
 
void Init (BaseFloat learning_rate, int32 patch_dim, int32 patch_step, int32 patch_stride, std::string matrix_filename, bool appended_conv)
 
void Resize (int32 input_dim, int32 output_dim)
 
std::string Info () const
 
void InitFromString (std::string args)
 Initialize, typically from a line of a config file. More...
 
std::string Type () const
 
bool BackpropNeedsInput () const
 
bool BackpropNeedsOutput () const
 
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...
 
void Scale (BaseFloat scale)
 This new virtual function scales the parameters by this amount. More...
 
virtual void Add (BaseFloat alpha, const UpdatableComponent &other)
 This new virtual function adds the parameters of another updatable component, times some constant, to the current parameters. 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_in, 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...
 
void SetZero (bool treat_as_gradient)
 Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set the learning rate to 1 and make any other changes necessary (there's a variable we have to set for the MixtureProbComponent). More...
 
void Read (std::istream &is, bool binary)
 
void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual BaseFloat DotProduct (const UpdatableComponent &other) const
 Here, "other" is a component of the same specific type. More...
 
ComponentCopy () const
 Copy component (deep copy). More...
 
void PerturbParams (BaseFloat stddev)
 We introduce a new virtual function that only applies to class UpdatableComponent. More...
 
void SetParams (const VectorBase< BaseFloat > &bias, const MatrixBase< BaseFloat > &filter)
 
const CuVector< BaseFloat > & BiasParams ()
 
const CuMatrix< BaseFloat > & LinearParams ()
 
int32 GetParameterDim () const
 The following new virtual function returns the total dimension of the parameters in this class. More...
 
void Update (const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 
- Public Member Functions inherited from UpdatableComponent
 UpdatableComponent (const UpdatableComponent &other)
 
void Init (BaseFloat learning_rate)
 
 UpdatableComponent (BaseFloat learning_rate)
 
 UpdatableComponent ()
 
virtual ~UpdatableComponent ()
 
void SetLearningRate (BaseFloat lrate)
 Sets the learning rate of gradient descent. More...
 
BaseFloat LearningRate () const
 Gets the learning rate of gradient descent. More...
 
virtual void Vectorize (VectorBase< BaseFloat > *params) const
 Turns the parameters into vector form. More...
 
virtual void UnVectorize (const VectorBase< BaseFloat > &params)
 Converts the parameters from vector form. 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< int32 > Context () 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

const Convolutional1dComponentoperator= (const Convolutional1dComponent &other)
 

Static Private Member Functions

static void ReverseIndexes (const std::vector< int32 > &forward_indexes, int32 input_dim, std::vector< std::vector< int32 > > *backward_indexes)
 
static void RearrangeIndexes (const std::vector< std::vector< int32 > > &in, std::vector< std::vector< int32 > > *out)
 

Private Attributes

int32 patch_dim_
 
int32 patch_step_
 
int32 patch_stride_
 
CuMatrix< BaseFloatfilter_params_
 
CuVector< BaseFloatbias_params_
 
bool appended_conv_
 
bool is_gradient_
 

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...
 
- Protected Attributes inherited from UpdatableComponent
BaseFloat learning_rate_
 learning rate (0.0..0.01) More...
 

Detailed Description

Convolutional1dComponent implements convolution over frequency axis.

We assume the input featrues are spliced, i.e. each frame is in fact a set of stacked frames, where we can form patches which span over several frequency bands and whole time axis. A patch is the instance of a filter on a group of frequency bands and whole time axis. Shifts of the filter generate patches.

The convolution is done over whole axis with same filter coefficients, i.e. we don't use separate filters for different 'regions' of frequency axis. Due to convolution, same weights are used repeateadly, the final gradient is a sum of all position-specific gradients (the sum was found better than averaging).

In order to have a fast implementations, the filters are represented in vectorized form, where each rectangular filter corresponds to a row in a matrix, where all the filters are stored. The features are then re-shaped to a set of matrices, where one matrix corresponds to single patch-position, where all the filters get applied.

The type of convolution is controled by hyperparameters: patch_dim_ ... frequency axis size of the patch patch_step_ ... size of shift in the convolution patch_stride_ ... shift for 2nd dim of a patch (i.e. frame length before splicing) For instance, for a convolutional component after raw input, if the input is 36-dim fbank feature with delta of order 2 and spliced using +/- 5 frames of contexts, the convolutional component takes the input as a 36 x 33 image. The patch_stride_ should be configured 36. If patch_step_ and patch_dim_ are configured 1 and 7, the Convolutional1dComponent creates a 2D filter of 7 x 33, such that the convolution is actually done only along the frequency axis. Specifically, the convolutional output along the frequency axis is (36 - 7) / 1 + 1 = 30, and the convolutional output along the temporal axis is 33 - 33 + 1 = 1, resulting in an output image of 30 x 1, which is called a feature map in ConvNet. Then if the output-dim is set 3840, the constructor would know there should be 3840 / 30 = 128 distinct filters, which will create 128 feature maps of 30 x 1 for one frame of input. The feature maps are vectorized as a 3840-dim row vector in the output matrix of this component. For details on progatation of Convolutional1dComponent, check the function definition.

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

Constructor & Destructor Documentation

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

3635  :
3636  UpdatableComponent(component),
3637  filter_params_(component.filter_params_),
3638  bias_params_(component.bias_params_),
3639  appended_conv_(component.appended_conv_),
3640  is_gradient_(component.is_gradient_) {}
Convolutional1dComponent ( const CuMatrixBase< BaseFloat > &  filter_params,
const CuVectorBase< BaseFloat > &  bias_params,
BaseFloat  learning_rate 
)

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

References Convolutional1dComponent::appended_conv_, CuVectorBase< Real >::Dim(), Convolutional1dComponent::is_gradient_, KALDI_ASSERT, and CuMatrixBase< Real >::NumRows().

3644  :
3645  UpdatableComponent(learning_rate),
3646  filter_params_(filter_params),
3647  bias_params_(bias_params) {
3648  KALDI_ASSERT(filter_params.NumRows() == bias_params.Dim() &&
3649  bias_params.Dim() != 0);
3650  appended_conv_ = false;
3651  is_gradient_ = false;
3652 }
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

Member Function Documentation

void Add ( BaseFloat  alpha,
const UpdatableComponent other 
)
virtual

This new virtual function adds the parameters of another updatable component, times some constant, to the current parameters.

Implements UpdatableComponent.

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

References CuMatrixBase< Real >::AddMat(), CuVectorBase< Real >::AddVec(), Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, and KALDI_ASSERT.

3900  {
3901  const Convolutional1dComponent *other =
3902  dynamic_cast<const Convolutional1dComponent*>(&other_in);
3903  KALDI_ASSERT(other != NULL);
3904  filter_params_.AddMat(alpha, other->filter_params_);
3905  bias_params_.AddVec(alpha, other->bias_params_);
3906 }
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:939
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
Definition: cu-vector.cc:1126
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.

Buffer for backpropagation: derivatives in the domain of 'patches_', 1row = vectorized rectangular feature patches, 1col = dim over speech frames,

Implements Component.

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

References CuMatrixBase< Real >::AddCols(), Convolutional1dComponent::appended_conv_, CuMatrixBase< Real >::ColRange(), rnnlm::d, Convolutional1dComponent::filter_params_, Convolutional1dComponent::InputDim(), kaldi::kNoTrans, kaldi::kSetZero, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, Convolutional1dComponent::RearrangeIndexes(), CuMatrix< Real >::Resize(), Convolutional1dComponent::ReverseIndexes(), and Convolutional1dComponent::Update().

3972  {
3973  in_deriv->Resize(out_deriv.NumRows(), InputDim());
3974  Convolutional1dComponent *to_update = dynamic_cast<Convolutional1dComponent*>(to_update_in);
3975  int32 num_splice = InputDim() / patch_stride_;
3976  int32 num_patches = 1 + (patch_stride_ - patch_dim_) / patch_step_;
3977  int32 num_filters = filter_params_.NumRows();
3978  int32 num_frames = out_deriv.NumRows();
3979  int32 filter_dim = filter_params_.NumCols();
3980 
3986  CuMatrix<BaseFloat> patches_deriv(num_frames, filter_dim * num_patches, kSetZero);
3987 
3988  //
3989  // backpropagate to vector of matrices
3990  // (corresponding to position of a filter)
3991  //
3992  std::vector<CuSubMatrix<BaseFloat>* > patch_deriv_batch, out_deriv_batch,
3993  filter_params_batch;
3994 
3995  CuSubMatrix<BaseFloat>* filter_params_elem = new CuSubMatrix<BaseFloat>(
3996  filter_params_, 0, filter_params_.NumRows(), 0, filter_params_.NumCols());
3997 
3998  // form batch in vector container
3999  for (int32 p = 0; p < num_patches; p++) {
4000  // form batch in vector container. for filter_params_batch, all elements
4001  // point to the same copy filter_params_elem
4002  patch_deriv_batch.push_back(new CuSubMatrix<BaseFloat>(patches_deriv.ColRange(
4003  p * filter_dim, filter_dim)));
4004  out_deriv_batch.push_back(new CuSubMatrix<BaseFloat>(out_deriv.ColRange(
4005  p * num_filters, num_filters)));
4006  filter_params_batch.push_back(filter_params_elem);
4007  }
4008  AddMatMatBatched<BaseFloat>(1.0, patch_deriv_batch, out_deriv_batch, kNoTrans,
4009  filter_params_batch, kNoTrans, 0.0);
4010 
4011  // release memory
4012  delete filter_params_elem;
4013  for (int32 p = 0; p < num_patches; p++) {
4014  delete patch_deriv_batch[p];
4015  delete out_deriv_batch[p];
4016  }
4017 
4018  // sum the derivatives into in_deriv
4019  std::vector<int32> column_map(filter_dim * num_patches);
4020  for (int32 patch = 0, index = 0; patch < num_patches; patch++) {
4021  int32 fstride = patch * patch_step_;
4022  for (int32 splice = 0; splice < num_splice; splice++) {
4023  int32 cstride = splice * patch_stride_;
4024  for (int32 d = 0; d < patch_dim_; d++, index++) {
4025  if (appended_conv_)
4026  column_map[index] = (fstride + d) * num_splice + splice;
4027  else
4028  column_map[index] = fstride + cstride + d;
4029  }
4030  }
4031  }
4032  std::vector<std::vector<int32> > reversed_column_map;
4033  ReverseIndexes(column_map, InputDim(), &reversed_column_map);
4034  std::vector<std::vector<int32> > rearranged_column_map;
4035  RearrangeIndexes(reversed_column_map, &rearranged_column_map);
4036  for (int32 p = 0; p < rearranged_column_map.size(); p++) {
4037  CuArray<int32> cu_cols(rearranged_column_map[p]);
4038  in_deriv->AddCols(patches_deriv, cu_cols);
4039  }
4040 
4041  if (to_update != NULL) {
4042  // Next update the model (must do this 2nd so the derivatives we propagate
4043  // are accurate, in case this == to_update_in.)
4044  to_update->Update(in_value, out_deriv);
4045  }
4046 }
static void ReverseIndexes(const std::vector< int32 > &forward_indexes, int32 input_dim, std::vector< std::vector< int32 > > *backward_indexes)
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Definition: cu-matrix.cc:47
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
static void RearrangeIndexes(const std::vector< std::vector< int32 > > &in, std::vector< std::vector< int32 > > *out)
int32 InputDim() const
Get size of input vectors.
void AddCols(const CuMatrixBase< Real > &src, const CuArray< MatrixIndexT > &indices)
Add column indices[r] of src to column r.
Definition: cu-matrix.cc:2481
bool BackpropNeedsInput ( ) const
inlinevirtual

Reimplemented from Component.

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

1743 { return true; }
bool BackpropNeedsOutput ( ) const
inlinevirtual

Reimplemented from Component.

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

1744 { return false; }
const CuVector<BaseFloat>& BiasParams ( )
inline

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

References Convolutional1dComponent::bias_params_.

1767 { return bias_params_; }
Component * Copy ( ) const
virtual

Copy component (deep copy).

Implements Component.

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, Convolutional1dComponent::Convolutional1dComponent(), Convolutional1dComponent::filter_params_, Convolutional1dComponent::is_gradient_, UpdatableComponent::learning_rate_, Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, and Convolutional1dComponent::patch_stride_.

4127  {
4129  ans->learning_rate_ = learning_rate_;
4130  ans->patch_dim_ = patch_dim_;
4131  ans->patch_step_ = patch_step_;
4132  ans->patch_stride_ = patch_stride_;
4133  ans->filter_params_ = filter_params_;
4134  ans->bias_params_ = bias_params_;
4135  ans->appended_conv_ = appended_conv_;
4136  ans->is_gradient_ = is_gradient_;
4137  return ans;
4138 }
BaseFloat learning_rate_
learning rate (0.0..0.01)
BaseFloat DotProduct ( const UpdatableComponent other) const
virtual

Here, "other" is a component of the same specific type.

This function computes the dot product in parameters, and is computed while automatically adjusting learning rates; typically, one of the two will actually contain the gradient.

Implements UpdatableComponent.

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

References Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, kaldi::kTrans, kaldi::TraceMatMat(), and kaldi::VecVec().

4120  {
4121  const Convolutional1dComponent *other =
4122  dynamic_cast<const Convolutional1dComponent*>(&other_in);
4123  return TraceMatMat(filter_params_, other->filter_params_, kTrans)
4124  + VecVec(bias_params_, other->bias_params_);
4125 }
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
int32 GetParameterDim ( ) const
virtual

The following new virtual function returns the total dimension of the parameters in this class.

E.g. used for L-BFGS update

Reimplemented from UpdatableComponent.

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

References Convolutional1dComponent::filter_params_, CuMatrixBase< Real >::NumCols(), and CuMatrixBase< Real >::NumRows().

4157  {
4158  return (filter_params_.NumCols() + 1) * filter_params_.NumRows();
4159 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
std::string Info ( ) const
virtual

Reimplemented from UpdatableComponent.

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, CuVectorBase< Real >::Dim(), Convolutional1dComponent::filter_params_, Convolutional1dComponent::InputDim(), kaldi::kTrans, UpdatableComponent::LearningRate(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Convolutional1dComponent::OutputDim(), Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, kaldi::TraceMatMat(), Convolutional1dComponent::Type(), and kaldi::VecVec().

3732  {
3733  std::stringstream stream;
3734  BaseFloat filter_params_size = static_cast<BaseFloat>(filter_params_.NumRows())
3735  * static_cast<BaseFloat>(filter_params_.NumCols());
3736  BaseFloat filter_stddev =
3738  filter_params_size),
3739  bias_stddev = std::sqrt(VecVec(bias_params_, bias_params_) /
3740  bias_params_.Dim());
3741 
3742  int32 num_splice = InputDim() / patch_stride_;
3743  int32 filter_dim = num_splice * patch_dim_;
3744  int32 num_patches = 1 + (patch_stride_ - patch_dim_) / patch_step_;
3745  int32 num_filters = OutputDim() / num_patches;
3746 
3747  stream << Type() << ", input-dim=" << InputDim()
3748  << ", output-dim=" << OutputDim()
3749  << ", num-splice=" << num_splice
3750  << ", num-patches=" << num_patches
3751  << ", num-filters=" << num_filters
3752  << ", filter-dim=" << filter_dim
3753  << ", filter-params-stddev=" << filter_stddev
3754  << ", bias-params-stddev=" << bias_stddev
3755  << ", appended-conv=" << appended_conv_
3756  << ", learning-rate=" << LearningRate();
3757  return stream.str();
3758 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
BaseFloat LearningRate() const
Gets the learning rate of gradient descent.
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
int32 OutputDim() const
Get size of output vectors.
int32 InputDim() const
Get size of input vectors.
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
void Init ( BaseFloat  learning_rate,
int32  input_dim,
int32  output_dim,
int32  patch_dim,
int32  patch_step,
int32  patch_stride,
BaseFloat  param_stddev,
BaseFloat  bias_stddev,
bool  appended_conv 
)

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, UpdatableComponent::Init(), KALDI_ASSERT, Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, CuVector< Real >::Resize(), CuMatrix< Real >::Resize(), CuVectorBase< Real >::Scale(), CuMatrixBase< Real >::Scale(), CuVectorBase< Real >::SetRandn(), and CuMatrixBase< Real >::SetRandn().

Referenced by Convolutional1dComponent::InitFromString(), and kaldi::nnet2::UnitTestConvolutional1dComponent().

3673  {
3674  UpdatableComponent::Init(learning_rate);
3675  patch_dim_ = patch_dim;
3676  patch_step_ = patch_step;
3677  patch_stride_ = patch_stride;
3678  appended_conv_ = appended_conv;
3679  int32 num_splice = input_dim / patch_stride;
3680  int32 filter_dim = num_splice * patch_dim;
3681  int32 num_patches = 1 + (patch_stride - patch_dim) / patch_step;
3682  int32 num_filters = output_dim / num_patches;
3683  KALDI_ASSERT(input_dim % patch_stride == 0);
3684  KALDI_ASSERT((patch_stride - patch_dim) % patch_step == 0);
3685  KALDI_ASSERT(output_dim % num_patches == 0);
3686 
3687  filter_params_.Resize(num_filters, filter_dim);
3688  bias_params_.Resize(num_filters);
3689  KALDI_ASSERT(param_stddev >= 0.0 && bias_stddev >= 0.0);
3691  filter_params_.Scale(param_stddev);
3693  bias_params_.Scale(bias_stddev);
3694 }
void Scale(Real value)
Definition: cu-vector.cc:1105
void Scale(Real value)
Definition: cu-matrix.cc:608
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:892
void Init(BaseFloat learning_rate)
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Definition: cu-matrix.cc:47
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Init ( BaseFloat  learning_rate,
int32  patch_dim,
int32  patch_step,
int32  patch_stride,
std::string  matrix_filename,
bool  appended_conv 
)

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, CuVectorBase< Real >::CopyColFromMat(), CuMatrixBase< Real >::CopyFromMat(), Convolutional1dComponent::filter_params_, UpdatableComponent::Init(), KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, CuMatrixBase< Real >::Range(), kaldi::ReadKaldiObject(), CuVector< Real >::Resize(), and CuMatrix< Real >::Resize().

3700  {
3701  UpdatableComponent::Init(learning_rate);
3702  patch_dim_ = patch_dim;
3703  patch_step_ = patch_step;
3704  patch_stride_ = patch_stride;
3705  appended_conv_ = appended_conv;
3706  CuMatrix<BaseFloat> mat;
3707  ReadKaldiObject(matrix_filename, &mat);
3708  KALDI_ASSERT(mat.NumCols() >= 2);
3709  int32 filter_dim = mat.NumCols() - 1, num_filters = mat.NumRows();
3710  filter_params_.Resize(num_filters, filter_dim);
3711  bias_params_.Resize(num_filters);
3712  filter_params_.CopyFromMat(mat.Range(0, num_filters, 0, filter_dim));
3713  bias_params_.CopyColFromMat(mat, filter_dim);
3714 }
void CopyColFromMat(const CuMatrixBase< Real > &mat, MatrixIndexT col)
Definition: cu-vector.cc:79
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:892
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
void Init(BaseFloat learning_rate)
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Definition: cu-matrix.cc:47
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 3761 of file nnet-component.cc.

References Convolutional1dComponent::Init(), Convolutional1dComponent::InputDim(), KALDI_ASSERT, KALDI_ERR, UpdatableComponent::learning_rate_, Convolutional1dComponent::OutputDim(), and kaldi::nnet2::ParseFromString().

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

3761  {
3762  std::string orig_args(args);
3763  bool ok = true, appended_conv = false;
3764  BaseFloat learning_rate = learning_rate_;
3765  std::string matrix_filename;
3766  int32 input_dim = -1, output_dim = -1;
3767  int32 patch_dim = -1, patch_step = -1, patch_stride = -1;
3768  ParseFromString("learning-rate", &args, &learning_rate);
3769  ParseFromString("appended-conv", &args, &appended_conv);
3770  ok = ok && ParseFromString("patch-dim", &args, &patch_dim);
3771  ok = ok && ParseFromString("patch-step", &args, &patch_step);
3772  ok = ok && ParseFromString("patch-stride", &args, &patch_stride);
3773  if (ParseFromString("matrix", &args, &matrix_filename)) {
3774  // initialize from prefined parameter matrix
3775  Init(learning_rate, patch_dim, patch_step, patch_stride,
3776  matrix_filename, appended_conv);
3777  if (ParseFromString("input-dim", &args, &input_dim))
3778  KALDI_ASSERT(input_dim == InputDim() &&
3779  "input-dim mismatch vs. matrix.");
3780  if (ParseFromString("output-dim", &args, &output_dim))
3781  KALDI_ASSERT(output_dim == OutputDim() &&
3782  "output-dim mismatch vs. matrix.");
3783  } else {
3784  // initialize from configuration
3785  ok = ok && ParseFromString("input-dim", &args, &input_dim);
3786  ok = ok && ParseFromString("output-dim", &args, &output_dim);
3787  BaseFloat param_stddev = 1.0 / std::sqrt(input_dim), bias_stddev = 1.0;
3788  ParseFromString("param-stddev", &args, &param_stddev);
3789  ParseFromString("bias-stddev", &args, &bias_stddev);
3790  Init(learning_rate, input_dim, output_dim, patch_dim,
3791  patch_step, patch_stride, param_stddev, bias_stddev, appended_conv);
3792  }
3793  if (!args.empty())
3794  KALDI_ERR << "Could not process these elements in initializer: " << args;
3795  if (!ok)
3796  KALDI_ERR << "Bad initializer " << orig_args;
3797 }
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat learning_rate_
learning rate (0.0..0.01)
#define KALDI_ERR
Definition: kaldi-error.h:127
int32 OutputDim() const
Get size of output vectors.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Init(BaseFloat learning_rate, int32 input_dim, int32 output_dim, int32 patch_dim, int32 patch_step, int32 patch_stride, BaseFloat param_stddev, BaseFloat bias_stddev, bool appended_conv)
int32 InputDim() const
Get size of input vectors.
const CuMatrix<BaseFloat>& LinearParams ( )
inline

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

References Convolutional1dComponent::filter_params_.

1768 { return filter_params_; }
const Convolutional1dComponent& operator= ( const Convolutional1dComponent other)
private
int32 OutputDim ( ) const
virtual
void PerturbParams ( BaseFloat  stddev)
virtual

We introduce a new virtual function that only applies to class UpdatableComponent.

This is used in testing.

Implements UpdatableComponent.

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

References CuMatrixBase< Real >::AddMat(), CuVectorBase< Real >::AddVec(), Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, CuVectorBase< Real >::SetRandn(), and CuMatrixBase< Real >::SetRandn().

4140  {
4141  CuMatrix<BaseFloat> temp_filter_params(filter_params_);
4142  temp_filter_params.SetRandn();
4143  filter_params_.AddMat(stddev, temp_filter_params);
4144 
4145  CuVector<BaseFloat> temp_bias_params(bias_params_);
4146  temp_bias_params.SetRandn();
4147  bias_params_.AddVec(stddev, temp_bias_params);
4148 }
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:939
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
Definition: cu-vector.cc:1126
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.

Buffer of reshaped inputs: 1row = vectorized rectangular feature patches 1col = dim over speech frames,

Implements Component.

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

References CuMatrixBase< Real >::AddVecToRows(), Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, ChunkInfo::CheckSize(), CuMatrixBase< Real >::ColRange(), CuMatrixBase< Real >::CopyCols(), rnnlm::d, Convolutional1dComponent::filter_params_, Convolutional1dComponent::InputDim(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, kaldi::kUndefined, ChunkInfo::NumChunks(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, and Convolutional1dComponent::patch_stride_.

3822  {
3823  in_info.CheckSize(in);
3824  out_info.CheckSize(*out);
3825  KALDI_ASSERT(in_info.NumChunks() == out_info.NumChunks());
3826 
3827  // dims
3828  int32 num_splice = InputDim() / patch_stride_;
3829  int32 num_patches = 1 + (patch_stride_ - patch_dim_) / patch_step_;
3830  int32 num_filters = filter_params_.NumRows();
3831  int32 num_frames = in.NumRows();
3832  int32 filter_dim = filter_params_.NumCols();
3833 
3838  CuMatrix<BaseFloat> patches(num_frames, filter_dim * num_patches, kUndefined);
3839  // column_map is indexed by the column-index of "patches",
3840  // and the value is the corresponding column-index of "in".
3841  std::vector<int32> column_map(filter_dim * num_patches);
3842 
3843  // build-up a column selection map
3844  for (int32 patch = 0, index = 0; patch < num_patches; patch++) {
3845  int32 fstride = patch * patch_step_;
3846  for (int32 splice = 0; splice < num_splice; splice++) {
3847  int32 cstride = splice * patch_stride_;
3848  for (int32 d = 0; d < patch_dim_; d++, index++) {
3849  if (appended_conv_)
3850  column_map[index] = (fstride + d) * num_splice + splice;
3851  else
3852  column_map[index] = fstride + cstride + d;
3853  }
3854  }
3855  }
3856  CuArray<int32> cu_cols(column_map);
3857  patches.CopyCols(in, cu_cols);
3858 
3859  //
3860  // compute filter activations
3861  //
3862 
3863  std::vector<CuSubMatrix<BaseFloat>* > tgt_batch, patch_batch, filter_params_batch;
3864 
3865  CuSubMatrix<BaseFloat>* filter_params_elem = new CuSubMatrix<BaseFloat>(
3866  filter_params_, 0, filter_params_.NumRows(), 0, filter_params_.NumCols());
3867 
3868  // form batch in vector container
3869  for (int32 p = 0; p < num_patches; p++) {
3870  // form batch in vector container. for filter_params_batch, all elements
3871  // point to the same copy filter_params_elem
3872  tgt_batch.push_back(new CuSubMatrix<BaseFloat>(out->ColRange(p * num_filters,
3873  num_filters)));
3874  patch_batch.push_back(new CuSubMatrix<BaseFloat>(
3875  patches.ColRange(p * filter_dim, filter_dim)));
3876  filter_params_batch.push_back(filter_params_elem);
3877 
3878  tgt_batch[p]->AddVecToRows(1.0, bias_params_, 0.0); // add bias
3879  }
3880 
3881  // apply all filters
3882  AddMatMatBatched<BaseFloat>(1.0, tgt_batch, patch_batch, kNoTrans,
3883  filter_params_batch, kTrans, 1.0);
3884 
3885  // release memory
3886  delete filter_params_elem;
3887  for (int32 p = 0; p < num_patches; p++) {
3888  delete tgt_batch[p];
3889  delete patch_batch[p];
3890  }
3891 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 InputDim() const
Get size of input vectors.
void Read ( std::istream &  is,
bool  binary 
)
virtual

Implements Component.

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, kaldi::nnet2::ExpectOneOrTwoTokens(), kaldi::ExpectToken(), Convolutional1dComponent::filter_params_, Convolutional1dComponent::is_gradient_, KALDI_ASSERT, UpdatableComponent::learning_rate_, Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, CuVector< Real >::Read(), CuMatrix< Real >::Read(), kaldi::ReadBasicType(), kaldi::ReadToken(), and Convolutional1dComponent::Type().

4059  {
4060  std::ostringstream ostr_beg, ostr_end;
4061  ostr_beg << "<" << Type() << ">"; // e.g. "<Convolutional1dComponent>"
4062  ostr_end << "</" << Type() << ">"; // e.g. "</Convolutional1dComponent>"
4063  // might not see the "<Convolutional1dComponent>" part because
4064  // of how ReadNew() works.
4065  ExpectOneOrTwoTokens(is, binary, ostr_beg.str(), "<LearningRate>");
4066  ReadBasicType(is, binary, &learning_rate_);
4067  ExpectToken(is, binary, "<PatchDim>");
4068  ReadBasicType(is, binary, &patch_dim_);
4069  ExpectToken(is, binary, "<PatchStep>");
4070  ReadBasicType(is, binary, &patch_step_);
4071  ExpectToken(is, binary, "<PatchStride>");
4072  ReadBasicType(is, binary, &patch_stride_);
4073  // back-compatibility
4074  std::string tok;
4075  ReadToken(is, binary, &tok);
4076  if (tok == "<AppendedConv>") {
4077  ReadBasicType(is, binary, &appended_conv_);
4078  ExpectToken(is, binary, "<FilterParams>");
4079  } else {
4080  appended_conv_ = false;
4081  KALDI_ASSERT(tok == "<FilterParams>");
4082  }
4083  filter_params_.Read(is, binary);
4084  ExpectToken(is, binary, "<BiasParams>");
4085  bias_params_.Read(is, binary);
4086  ReadToken(is, binary, &tok);
4087  if (tok == "<IsGradient>") {
4088  ReadBasicType(is, binary, &is_gradient_);
4089  ExpectToken(is, binary, ostr_end.str());
4090  } else {
4091  is_gradient_ = false;
4092  KALDI_ASSERT(tok == ostr_end.str());
4093  }
4094 }
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 Read(std::istream &is, bool binary)
I/O.
Definition: cu-vector.cc:862
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:188
BaseFloat learning_rate_
learning rate (0.0..0.01)
void Read(std::istream &is, bool binary)
I/O functions.
Definition: cu-matrix.cc:459
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
void RearrangeIndexes ( const std::vector< std::vector< int32 > > &  in,
std::vector< std::vector< int32 > > *  out 
)
staticprivate

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

References rnnlm::i, and rnnlm::j.

Referenced by Convolutional1dComponent::Backprop().

3949  {
3950  int32 D = in.size();
3951  int32 L = 0;
3952  for (int32 i = 0; i < D; i++)
3953  if (in[i].size() > L)
3954  L = in[i].size();
3955  out->resize(L);
3956  for (int32 i = 0; i < L; i++)
3957  (*out)[i].resize(D, -1);
3958  for (int32 i = 0; i < D; i++) {
3959  for (int32 j = 0; j < in[i].size(); j++) {
3960  (*out)[j][i] = in[i][j];
3961  }
3962  }
3963 }
void Resize ( int32  input_dim,
int32  output_dim 
)

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

References Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, KALDI_ASSERT, Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, CuVector< Real >::Resize(), and CuMatrix< Real >::Resize().

3718  {
3719  KALDI_ASSERT(input_dim > 0 && output_dim > 0);
3720  int32 num_splice = input_dim / patch_stride_;
3721  int32 filter_dim = num_splice * patch_dim_;
3722  int32 num_patches = 1 + (patch_stride_ - patch_dim_) / patch_step_;
3723  int32 num_filters = output_dim / num_patches;
3724  KALDI_ASSERT(input_dim % patch_stride_ == 0);
3725  KALDI_ASSERT((patch_stride_ - patch_dim_) % patch_step_ == 0);
3726  KALDI_ASSERT(output_dim % num_patches == 0);
3727  filter_params_.Resize(num_filters, filter_dim);
3728  bias_params_.Resize(num_filters);
3729 }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:892
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
Definition: cu-matrix.cc:47
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void ReverseIndexes ( const std::vector< int32 > &  forward_indexes,
int32  input_dim,
std::vector< std::vector< int32 > > *  backward_indexes 
)
staticprivate

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

References rnnlm::i, rnnlm::j, and KALDI_ASSERT.

Referenced by Convolutional1dComponent::Backprop().

3922  {
3923  int32 i, size = forward_indexes.size();
3924  int32 reserve_size = 2 + size / input_dim;
3925  backward_indexes->resize(input_dim);
3926  std::vector<std::vector<int32> >::iterator iter = backward_indexes->begin(),
3927  end = backward_indexes->end();
3928  for (; iter != end; ++iter)
3929  iter->reserve(reserve_size);
3930  for (int32 j = 0; j < forward_indexes.size(); j++) {
3931  i = forward_indexes[j];
3932  KALDI_ASSERT(i < input_dim);
3933  (*backward_indexes)[i].push_back(j);
3934  }
3935 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Scale ( BaseFloat  scale)
virtual

This new virtual function scales the parameters by this amount.

Implements UpdatableComponent.

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

References Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, CuVectorBase< Real >::Scale(), and CuMatrixBase< Real >::Scale().

3894  {
3895  filter_params_.Scale(scale);
3896  bias_params_.Scale(scale);
3897 }
void Scale(Real value)
Definition: cu-vector.cc:1105
void Scale(Real value)
Definition: cu-matrix.cc:608
void SetParams ( const VectorBase< BaseFloat > &  bias,
const MatrixBase< BaseFloat > &  filter 
)

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

References Convolutional1dComponent::bias_params_, CuVectorBase< Real >::Dim(), Convolutional1dComponent::filter_params_, KALDI_ASSERT, and CuMatrixBase< Real >::NumRows().

4151  {
4152  bias_params_ = bias;
4153  filter_params_ = filter;
4155 }
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void SetZero ( bool  treat_as_gradient)
virtual

Set parameters to zero, and if treat_as_gradient is true, we'll be treating this as a gradient so set the learning rate to 1 and make any other changes necessary (there's a variable we have to set for the MixtureProbComponent).

Implements UpdatableComponent.

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

References Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, Convolutional1dComponent::is_gradient_, UpdatableComponent::SetLearningRate(), CuVectorBase< Real >::SetZero(), and CuMatrixBase< Real >::SetZero().

4048  {
4049  if (treat_as_gradient) {
4050  SetLearningRate(1.0);
4051  }
4054  if (treat_as_gradient) {
4055  is_gradient_ = true;
4056  }
4057 }
void SetZero()
Math operations, some calling kernels.
Definition: cu-matrix.cc:474
void SetZero()
Math operations.
Definition: cu-vector.cc:988
void SetLearningRate(BaseFloat lrate)
Sets the learning rate of gradient descent.
std::string Type ( ) const
inlinevirtual

Implements Component.

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

Referenced by Convolutional1dComponent::Info(), Convolutional1dComponent::Read(), and Convolutional1dComponent::Write().

1742 { return "Convolutional1dComponent"; }
void Update ( const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)

Buffer of reshaped inputs: 1row = vectorized rectangular feature patches 1col = dim over speech frames,

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

References CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::AddMatBlocks(), CuVectorBase< Real >::AddRowSumMat(), CuVectorBase< Real >::AddVec(), Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, CuMatrixBase< Real >::ColRange(), CuMatrixBase< Real >::CopyCols(), rnnlm::d, Convolutional1dComponent::filter_params_, Convolutional1dComponent::InputDim(), kaldi::kNoTrans, kaldi::kSetZero, kaldi::kTrans, kaldi::kUndefined, UpdatableComponent::learning_rate_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, CuVector< Real >::Resize(), and CuMatrix< Real >::Resize().

Referenced by Convolutional1dComponent::Backprop().

4163  {
4164  // useful dims
4165  int32 num_patches = 1 + (patch_stride_ - patch_dim_) / patch_step_;
4166  int32 num_filters = filter_params_.NumRows();
4167  int32 filter_dim = filter_params_.NumCols();
4168  int32 num_frames = in_value.NumRows();
4169  int32 num_splice = InputDim() / patch_stride_;
4170  CuMatrix<BaseFloat> filters_grad;
4171  CuVector<BaseFloat> bias_grad;
4172 
4177  CuMatrix<BaseFloat> patches(num_frames, filter_dim * num_patches, kUndefined);
4178  std::vector<int32> column_map(filter_dim * num_patches);
4179  for (int32 patch = 0, index = 0; patch < num_patches; patch++) {
4180  int32 fstride = patch * patch_step_;
4181  for (int32 splice = 0; splice < num_splice; splice++) {
4182  int32 cstride = splice * patch_stride_;
4183  for (int32 d = 0; d < patch_dim_; d++, index++) {
4184  if (appended_conv_)
4185  column_map[index] = (fstride + d) * num_splice + splice;
4186  else
4187  column_map[index] = fstride + cstride + d;
4188  }
4189  }
4190  }
4191  CuArray<int32> cu_cols(column_map);
4192  patches.CopyCols(in_value, cu_cols);
4193 
4194  //
4195  // calculate the gradient
4196  //
4197  filters_grad.Resize(num_filters, filter_dim, kSetZero); // reset
4198  bias_grad.Resize(num_filters, kSetZero); // reset
4199 
4200  //
4201  // use all the patches
4202  //
4203 
4204  // create a single large matrix holding the smaller matrices
4205  // from the vector container filters_grad_batch along the rows
4206  CuMatrix<BaseFloat> filters_grad_blocks_batch(
4207  num_patches * filters_grad.NumRows(), filters_grad.NumCols());
4208 
4209  std::vector<CuSubMatrix<BaseFloat>* > filters_grad_batch, diff_patch_batch,
4210  patch_batch;
4211  for (int32 p = 0; p < num_patches; p++) {
4212  // form batch in vector container
4213  filters_grad_batch.push_back(new CuSubMatrix<BaseFloat>(
4214  filters_grad_blocks_batch.RowRange(
4215  p * filters_grad.NumRows(),
4216  filters_grad.NumRows())));
4217  diff_patch_batch.push_back(new CuSubMatrix<BaseFloat>(out_deriv.ColRange(
4218  p * num_filters, num_filters)));
4219  patch_batch.push_back(new CuSubMatrix<BaseFloat>(patches.ColRange(
4220  p * filter_dim, filter_dim)));
4221  }
4222 
4223  AddMatMatBatched<BaseFloat>(1.0, filters_grad_batch, diff_patch_batch,
4224  kTrans, patch_batch, kNoTrans, 1.0);
4225 
4226  // add the row blocks together to filters_grad
4227  filters_grad.AddMatBlocks(1.0, filters_grad_blocks_batch);
4228 
4229  // create a matrix holding the col blocks sum of out_deriv
4230  CuMatrix<BaseFloat> out_deriv_col_blocks_sum(out_deriv.NumRows(), num_filters);
4231 
4232  // add the col blocks together to out_deriv_col_blocks_sum
4233  out_deriv_col_blocks_sum.AddMatBlocks(1.0, out_deriv);
4234 
4235  bias_grad.AddRowSumMat(1.0, out_deriv_col_blocks_sum, 1.0);
4236 
4237  // release memory
4238  for (int32 p = 0; p < num_patches; p++) {
4239  delete filters_grad_batch[p];
4240  delete diff_patch_batch[p];
4241  delete patch_batch[p];
4242  }
4243 
4244  //
4245  // update
4246  //
4247  filter_params_.AddMat(learning_rate_, filters_grad);
4248  bias_params_.AddVec(learning_rate_, bias_grad);
4249 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
CuSubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: cu-matrix.h:544
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
BaseFloat learning_rate_
learning rate (0.0..0.01)
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:939
int32 InputDim() const
Get size of input vectors.
void AddVec(Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
Definition: cu-vector.cc:1126
void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

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

References Convolutional1dComponent::appended_conv_, Convolutional1dComponent::bias_params_, Convolutional1dComponent::filter_params_, Convolutional1dComponent::is_gradient_, UpdatableComponent::learning_rate_, Convolutional1dComponent::patch_dim_, Convolutional1dComponent::patch_step_, Convolutional1dComponent::patch_stride_, Convolutional1dComponent::Type(), CuVector< Real >::Write(), CuMatrixBase< Real >::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

4096  {
4097  std::ostringstream ostr_beg, ostr_end;
4098  ostr_beg << "<" << Type() << ">"; // e.g. "<Convolutional1dComponent>"
4099  ostr_end << "</" << Type() << ">"; // e.g. "</Convolutional1dComponent>"
4100  WriteToken(os, binary, ostr_beg.str());
4101  WriteToken(os, binary, "<LearningRate>");
4102  WriteBasicType(os, binary, learning_rate_);
4103  WriteToken(os, binary, "<PatchDim>");
4104  WriteBasicType(os, binary, patch_dim_);
4105  WriteToken(os, binary, "<PatchStep>");
4106  WriteBasicType(os, binary, patch_step_);
4107  WriteToken(os, binary, "<PatchStride>");
4108  WriteBasicType(os, binary, patch_stride_);
4109  WriteToken(os, binary, "<AppendedConv>");
4110  WriteBasicType(os, binary, appended_conv_);
4111  WriteToken(os, binary, "<FilterParams>");
4112  filter_params_.Write(os, binary);
4113  WriteToken(os, binary, "<BiasParams>");
4114  bias_params_.Write(os, binary);
4115  WriteToken(os, binary, "<IsGradient>");
4116  WriteBasicType(os, binary, is_gradient_);
4117  WriteToken(os, binary, ostr_end.str());
4118 }
void Write(std::ostream &is, bool binary) const
Definition: cu-vector.cc:872
BaseFloat learning_rate_
learning rate (0.0..0.01)
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
void Write(std::ostream &os, bool binary) const
Definition: cu-matrix.cc:467

Member Data Documentation


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