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

FixedLinearComponent is a linear transform that is supplied at network initialization time and is not trainable. More...

#include <nnet-component.h>

Inheritance diagram for FixedLinearComponent:
Collaboration diagram for FixedLinearComponent:

Public Member Functions

 FixedLinearComponent ()
 
virtual std::string Type () const
 
virtual std::string Info () const
 
void Init (const CuMatrixBase< BaseFloat > &matrix)
 
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< 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 ()
 

Protected Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (FixedLinearComponent)
 

Protected Attributes

CuMatrix< BaseFloatmat_
 

Friends

class AffineComponent
 

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

FixedLinearComponent is a linear transform that is supplied at network initialization time and is not trainable.

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

Constructor & Destructor Documentation

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

Referenced by FixedLinearComponent::Copy().

1415 { }

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

References CuMatrixBase< Real >::AddMatMat(), kaldi::kNoTrans, FixedLinearComponent::mat_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and CuMatrix< Real >::Resize().

3220  {
3221  in_deriv->Resize(out_deriv.NumRows(), mat_.NumCols());
3222  in_deriv->AddMatMat(1.0, out_deriv, kNoTrans, mat_, kNoTrans, 0.0);
3223 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
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
void AddMatMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType transA, const CuMatrixBase< Real > &B, MatrixTransposeType transB, Real beta)
C = alpha * A(^T)*B(^T) + beta * C.
Definition: cu-matrix.cc:1142
virtual bool BackpropNeedsInput ( ) const
inlinevirtual

Reimplemented from Component.

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

1439 { return false; }
virtual bool BackpropNeedsOutput ( ) const
inlinevirtual

Reimplemented from Component.

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

1440 { return false; }
Component * Copy ( ) const
virtual

Copy component (deep copy).

Implements Component.

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

References FixedLinearComponent::FixedLinearComponent(), FixedLinearComponent::Init(), and FixedLinearComponent::mat_.

3225  {
3227  ans->Init(mat_);
3228  return ans;
3229 }
std::string Info ( ) const
virtual

Reimplemented from Component.

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

References Component::Info(), kaldi::kTrans, FixedLinearComponent::mat_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and kaldi::TraceMatMat().

3193  {
3194  std::stringstream stream;
3195  BaseFloat mat_size = static_cast<BaseFloat>(mat_.NumRows())
3196  * static_cast<BaseFloat>(mat_.NumCols()),
3197  mat_stddev = std::sqrt(TraceMatMat(mat_, mat_, kTrans) /
3198  mat_size);
3199  stream << Component::Info() << ", params-stddev=" << mat_stddev;
3200  return stream.str();
3201 }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
virtual std::string Info() const
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.
void Init ( const CuMatrixBase< BaseFloat > &  matrix)
inline
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 3175 of file nnet-component.cc.

References FixedLinearComponent::Init(), KALDI_ASSERT, KALDI_ERR, CuMatrixBase< Real >::NumRows(), kaldi::nnet2::ParseFromString(), CuMatrix< Real >::Read(), Input::Stream(), and FixedLinearComponent::Type().

3175  {
3176  std::string orig_args = args;
3177  std::string filename;
3178  bool ok = ParseFromString("matrix", &args, &filename);
3179 
3180  if (!ok || !args.empty())
3181  KALDI_ERR << "Invalid initializer for layer of type "
3182  << Type() << ": \"" << orig_args << "\"";
3183 
3184  bool binary;
3185  Input ki(filename, &binary);
3186  CuMatrix<BaseFloat> mat;
3187  mat.Read(ki.Stream(), binary);
3188  KALDI_ASSERT(mat.NumRows() != 0);
3189  Init(mat);
3190 }
void Init(const CuMatrixBase< BaseFloat > &matrix)
bool ParseFromString(const std::string &name, std::string *string, int32 *param)
Functions used in Init routines.
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
virtual std::string Type() const
virtual int32 InputDim ( ) const
inlinevirtual

Get size of input vectors.

Implements Component.

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

References FixedLinearComponent::mat_, and CuMatrixBase< Real >::NumCols().

1425 { return mat_.NumCols(); }
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
KALDI_DISALLOW_COPY_AND_ASSIGN ( FixedLinearComponent  )
protected
virtual int32 OutputDim ( ) const
inlinevirtual

Get size of output vectors.

Implements Component.

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

References FixedLinearComponent::mat_, and CuMatrixBase< Real >::NumRows().

1426 { return mat_.NumRows(); }
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
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 3203 of file nnet-component.cc.

References CuMatrixBase< Real >::AddMatMat(), ChunkInfo::CheckSize(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, FixedLinearComponent::mat_, and ChunkInfo::NumChunks().

3206  {
3207  in_info.CheckSize(in);
3208  out_info.CheckSize(*out);
3209  KALDI_ASSERT(in_info.NumChunks() == out_info.NumChunks());
3210 
3211  out->AddMatMat(1.0, in, kNoTrans, mat_, kTrans, 0.0);
3212 }
void AddMatMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType transA, const CuMatrixBase< Real > &B, MatrixTransposeType transB, Real beta)
C = alpha * A(^T)*B(^T) + beta * C.
Definition: cu-matrix.cc:1142
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Read ( std::istream &  is,
bool  binary 
)
virtual

Implements Component.

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

References kaldi::nnet2::ExpectOneOrTwoTokens(), kaldi::ExpectToken(), FixedLinearComponent::mat_, and CuMatrix< Real >::Read().

3239  {
3240  ExpectOneOrTwoTokens(is, binary, "<FixedLinearComponent>", "<CuMatrix>");
3241  mat_.Read(is, binary);
3242  ExpectToken(is, binary, "</FixedLinearComponent>");
3243 }
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
void Read(std::istream &is, bool binary)
I/O functions.
Definition: cu-matrix.cc:459
static void ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
virtual std::string Type ( ) const
inlinevirtual

Implements Component.

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

Referenced by FixedLinearComponent::InitFromString().

1416 { return "FixedLinearComponent"; }
void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

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

References FixedLinearComponent::mat_, CuMatrixBase< Real >::Write(), and kaldi::WriteToken().

3232  {
3233  WriteToken(os, binary, "<FixedLinearComponent>");
3234  WriteToken(os, binary, "<CuMatrix>");
3235  mat_.Write(os, binary);
3236  WriteToken(os, binary, "</FixedLinearComponent>");
3237 }
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 Write(std::ostream &os, bool binary) const
Definition: cu-matrix.cc:467

Friends And Related Function Documentation

friend class AffineComponent
friend

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

Member Data Documentation


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