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

#include <nnet-component.h>

Inheritance diagram for SumGroupComponent:
Collaboration diagram for SumGroupComponent:

Public Member Functions

virtual int32 InputDim () const
 Get size of input vectors. More...
virtual int32 OutputDim () const
 Get size of output vectors. More...
void Init (const std::vector< int32 > &sizes)
void GetSizes (std::vector< int32 > *sizes) const
virtual void InitFromString (std::string args)
 Initialize, typically from a line of a config file. More...
 SumGroupComponent ()
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...
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< 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 std::string Info () const
virtual ~Component ()

Private Member Functions


Private Attributes

CuArray< Int32Pairindexes_
CuArray< int32 > reverse_indexes_
int32 input_dim_
int32 output_dim_

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

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

Constructor & Destructor Documentation

SumGroupComponent ( )

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

Referenced by SumGroupComponent::Copy().

1284 { }

Member Function Documentation

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).

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 2451 of file

References CuMatrixBase< Real >::CopyCols(), SumGroupComponent::InputDim(), CuMatrixBase< Real >::NumRows(), CuMatrix< Real >::Resize(), and SumGroupComponent::reverse_indexes_.

2457  {
2458  in_deriv->Resize(out_deriv.NumRows(), InputDim());
2459  in_deriv->CopyCols(out_deriv, reverse_indexes_);
2460 }
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
MatrixIndexT NumRows() const
Definition: cu-matrix.h:195
void CopyCols(const CuMatrixBase< Real > &src, const CuArray< MatrixIndexT > &indexes)
Copies column r from column indexes[r] of src.
virtual int32 InputDim() const
Get size of input vectors.
virtual bool BackpropNeedsInput ( ) const

Reimplemented from Component.

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

1286 { return false; }
virtual bool BackpropNeedsOutput ( ) const

Reimplemented from Component.

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

1287 { return false; }
Component * Copy ( ) const

Copy component (deep copy).

Implements Component.

Definition at line 2395 of file

References SumGroupComponent::indexes_, SumGroupComponent::input_dim_, SumGroupComponent::output_dim_, SumGroupComponent::reverse_indexes_, and SumGroupComponent::SumGroupComponent().

2395  {
2396  SumGroupComponent *ans = new SumGroupComponent();
2397  ans->indexes_ = indexes_;
2398  ans->reverse_indexes_ = reverse_indexes_;
2399  ans->input_dim_ = input_dim_;
2400  ans->output_dim_ = output_dim_;
2401  return ans;
2402 }
void GetSizes ( std::vector< int32 > *  sizes) const

Definition at line 2418 of file

References CuArray< T >::CopyToVec(), rnnlm::i, SumGroupComponent::indexes_, and KALDI_ASSERT.

Referenced by SoftmaxComponent::MixUp(), and SumGroupComponent::Write().

2418  {
2419  std::vector<Int32Pair> indexes;
2420  indexes_.CopyToVec(&indexes);
2421  sizes->resize(indexes.size());
2422  for (size_t i = 0; i < indexes.size(); i++) {
2423  (*sizes)[i] = indexes[i].second - indexes[i].first;
2424  if (i == 0) { KALDI_ASSERT(indexes[i].first == 0); }
2425  else { KALDI_ASSERT(indexes[i].first == indexes[i-1].second); }
2426  KALDI_ASSERT(indexes[i].second > indexes[i].first);
2427  (*sizes)[i] = indexes[i].second - indexes[i].first;
2428  }
2429 }
void CopyToVec(std::vector< T > *dst) const
This function resizes *dst if needed.
Definition: cu-array-inl.h:133
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Init ( const std::vector< int32 > &  sizes)

Definition at line 2365 of file

References rnnlm::i, SumGroupComponent::indexes_, SumGroupComponent::input_dim_, rnnlm::j, KALDI_ASSERT, SumGroupComponent::output_dim_, and SumGroupComponent::reverse_indexes_.

Referenced by SumGroupComponent::InitFromString(), SoftmaxComponent::MixUp(), SumGroupComponent::Read(), and kaldi::nnet2::UnitTestSumGroupComponent().

2365  {
2366  KALDI_ASSERT(!sizes.empty());
2367  std::vector<Int32Pair> cpu_vec(sizes.size());
2368  std::vector<int32> reverse_cpu_vec;
2369  int32 cur_index = 0;
2370  for (size_t i = 0; i < sizes.size(); i++) {
2371  KALDI_ASSERT(sizes[i] > 0);
2372  cpu_vec[i].first = cur_index;
2373  cpu_vec[i].second = cur_index + sizes[i];
2374  cur_index += sizes[i];
2375  for (int32 j = cpu_vec[i].first; j < cpu_vec[i].second; j++)
2376  reverse_cpu_vec.push_back(i);
2377  }
2378  this->indexes_ = cpu_vec;
2379  this->reverse_indexes_ = reverse_cpu_vec;
2380  this->input_dim_ = cur_index;
2381  this->output_dim_ = sizes.size();
2382 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void InitFromString ( std::string  args)

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 2384 of file

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

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

2384  {
2385  std::string orig_args(args);
2386  std::vector<int32> sizes;
2387  bool ok = ParseFromString("sizes", &args, &sizes);
2389  if (!ok || !args.empty() || sizes.empty())
2390  KALDI_ERR << "Invalid initializer for layer of type "
2391  << Type() << ": \"" << orig_args << "\"";
2392  this->Init(sizes);
2393 }
virtual std::string Type() const
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
#define KALDI_ERR
Definition: kaldi-error.h:127
void Init(const std::vector< int32 > &sizes)
virtual int32 InputDim ( ) const

Get size of input vectors.

Implements Component.

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

References SumGroupComponent::input_dim_.

Referenced by SumGroupComponent::Backprop().

virtual int32 OutputDim ( ) const

Get size of output vectors.

Implements Component.

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

References SumGroupComponent::output_dim_.

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

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 2440 of file

References ChunkInfo::CheckSize(), SumGroupComponent::indexes_, KALDI_ASSERT, ChunkInfo::NumChunks(), and CuMatrixBase< Real >::SumColumnRanges().

2443  {
2444  in_info.CheckSize(in);
2445  out_info.CheckSize(*out);
2446  KALDI_ASSERT(in_info.NumChunks() == out_info.NumChunks());
2448  out->SumColumnRanges(in, indexes_);
2449 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void SumColumnRanges(const CuMatrixBase< Real > &src, const CuArray< Int32Pair > &indexes)
For each row r of this and for each column c, sets (*this)(r, c) to the sum src(r, j), where j ranges from indexes[c].first through indexes[c].second - 1.
void Read ( std::istream &  is,
bool  binary 

Implements Component.

Definition at line 2404 of file

References kaldi::nnet2::ExpectOneOrTwoTokens(), SumGroupComponent::Init(), KALDI_ERR, kaldi::ReadIntegerVector(), and kaldi::ReadToken().

2404  {
2405  ExpectOneOrTwoTokens(is, binary, "<SumGroupComponent>", "<Sizes>");
2406  std::vector<int32> sizes;
2407  ReadIntegerVector(is, binary, &sizes);
2409  std::string token;
2410  ReadToken(is, binary, &token);
2411  if (!(token == "<SumGroupComponent>" ||
2412  token == "</SumGroupComponent>")) {
2413  KALDI_ERR << "Expected </SumGroupComponent>, got " << token;
2414  }
2415  this->Init(sizes);
2416 }
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
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
#define KALDI_ERR
Definition: kaldi-error.h:127
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
void Init(const std::vector< int32 > &sizes)
virtual std::string Type ( ) const

Implements Component.

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

Referenced by SumGroupComponent::InitFromString().

1285 { return "SumGroupComponent"; }
void Write ( std::ostream &  os,
bool  binary 
) const

Write component to stream.

Implements Component.

Definition at line 2431 of file

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

2431  {
2432  WriteToken(os, binary, "<SumGroupComponent>");
2433  WriteToken(os, binary, "<Sizes>");
2434  std::vector<int32> sizes;
2435  this->GetSizes(&sizes);
2436  WriteIntegerVector(os, binary, sizes);
2437  WriteToken(os, binary, "</SumGroupComponent>");
2438 }
void GetSizes(std::vector< int32 > *sizes) const
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
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

int32 input_dim_
int32 output_dim_
CuArray<int32> reverse_indexes_

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