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

FixedScaleComponent applies a fixed per-element scale; it's similar to the Rescale component in the nnet1 setup (and only needed for nnet1 model conversion). More...

#include <nnet-component.h>

Inheritance diagram for FixedScaleComponent:
Collaboration diagram for FixedScaleComponent:

Public Member Functions

 FixedScaleComponent ()
 
virtual std::string Type () const
 
virtual std::string Info () const
 
void Init (const CuVectorBase< BaseFloat > &scales)
 
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 (FixedScaleComponent)
 

Protected Attributes

CuVector< BaseFloatscales_
 

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

FixedScaleComponent applies a fixed per-element scale; it's similar to the Rescale component in the nnet1 setup (and only needed for nnet1 model conversion).

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

Constructor & Destructor Documentation

FixedScaleComponent ( )
inline

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

Referenced by FixedScaleComponent::Copy().

1503 { }

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

References CuMatrixBase< Real >::MulColsVec(), and FixedScaleComponent::scales_.

3382  {
3383  *in_deriv = out_deriv;
3384  in_deriv->MulColsVec(scales_);
3385 }
void MulColsVec(const CuVectorBase< Real > &scale)
scale i'th column by scale[i]
Definition: cu-matrix.cc:750
virtual bool BackpropNeedsInput ( ) const
inlinevirtual

Reimplemented from Component.

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

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

Reimplemented from Component.

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

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

Copy component (deep copy).

Implements Component.

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

References FixedScaleComponent::FixedScaleComponent(), and FixedScaleComponent::scales_.

3387  {
3389  ans->scales_ = scales_;
3390  return ans;
3391 }
std::string Info ( ) const
virtual

Reimplemented from Component.

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

References CuVectorBase< Real >::Dim(), Component::Info(), FixedScaleComponent::scales_, CuVectorBase< Real >::Sum(), and kaldi::VecVec().

3357  {
3358  std::stringstream stream;
3359  BaseFloat scales_size = static_cast<BaseFloat>(scales_.Dim()),
3360  scales_mean = scales_.Sum() / scales_size,
3361  scales_stddev = std::sqrt(VecVec(scales_, scales_) / scales_size
3362  - (scales_mean * scales_mean));
3363  stream << Component::Info() << ", scales-mean=" << scales_mean
3364  << ", scales-stddev=" << scales_stddev;
3365  return stream.str();
3366 }
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
virtual std::string Info() const
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:36
Real Sum() const
Definition: cu-vector.cc:268
void Init ( const CuVectorBase< BaseFloat > &  scales)

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

References CuVectorBase< Real >::Dim(), KALDI_ASSERT, and FixedScaleComponent::scales_.

Referenced by kaldi::ConvertRescaleComponent(), FixedScaleComponent::InitFromString(), and kaldi::nnet2::UnitTestFixedScaleComponent().

3337  {
3338  KALDI_ASSERT(scales.Dim() != 0);
3339  scales_ = scales;
3340 }
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
#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 3342 of file nnet-component.cc.

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

3342  {
3343  std::string orig_args = args;
3344  std::string filename;
3345  bool ok = ParseFromString("scales", &args, &filename);
3346 
3347  if (!ok || !args.empty())
3348  KALDI_ERR << "Invalid initializer for layer of type "
3349  << Type() << ": \"" << orig_args << "\"";
3350 
3351  CuVector<BaseFloat> vec;
3352  ReadKaldiObject(filename, &vec);
3353  Init(vec);
3354 }
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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 CuVectorBase< BaseFloat > &scales)
virtual std::string Type() const
virtual int32 InputDim ( ) const
inlinevirtual

Get size of input vectors.

Implements Component.

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

References CuVectorBase< Real >::Dim(), and FixedScaleComponent::scales_.

Referenced by AffineComponent::CollapseWithNext().

1513 { return scales_.Dim(); }
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
KALDI_DISALLOW_COPY_AND_ASSIGN ( FixedScaleComponent  )
protected
virtual int32 OutputDim ( ) const
inlinevirtual

Get size of output vectors.

Implements Component.

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

References CuVectorBase< Real >::Dim(), and FixedScaleComponent::scales_.

1514 { return scales_.Dim(); }
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
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 3368 of file nnet-component.cc.

References CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::MulColsVec(), and FixedScaleComponent::scales_.

3371  {
3372  out->CopyFromMat(in);
3373  out->MulColsVec(scales_);
3374 }
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
void MulColsVec(const CuVectorBase< Real > &scale)
scale i'th column by scale[i]
Definition: cu-matrix.cc:750
void Read ( std::istream &  is,
bool  binary 
)
virtual

Implements Component.

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

References kaldi::nnet2::ExpectOneOrTwoTokens(), kaldi::ExpectToken(), CuVector< Real >::Read(), and FixedScaleComponent::scales_.

3401  {
3402  ExpectOneOrTwoTokens(is, binary, "<FixedScaleComponent>", "<Scales>");
3403  scales_.Read(is, binary);
3404  ExpectToken(is, binary, "</FixedScaleComponent>");
3405 }
void Read(std::istream &is, bool binary)
I/O.
Definition: cu-vector.cc:862
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
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 1504 of file nnet-component.h.

Referenced by FixedScaleComponent::InitFromString().

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

Write component to stream.

Implements Component.

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

References FixedScaleComponent::scales_, CuVector< Real >::Write(), and kaldi::WriteToken().

3394  {
3395  WriteToken(os, binary, "<FixedScaleComponent>");
3396  WriteToken(os, binary, "<Scales>");
3397  scales_.Write(os, binary);
3398  WriteToken(os, binary, "</FixedScaleComponent>");
3399 }
void Write(std::ostream &is, bool binary) const
Definition: cu-vector.cc:872
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

Friends And Related Function Documentation

friend class AffineComponent
friend

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

Member Data Documentation


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