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

#include <nnet-kl-hmm.h>

Inheritance diagram for KlHmm:
Collaboration diagram for KlHmm:

Public Member Functions

 KlHmm (int32 dim_in, int32 dim_out)
 
 ~KlHmm ()
 
ComponentCopy () const
 Copy component (deep copy),. More...
 
ComponentType GetType () const
 Get Type Identification of the component,. More...
 
void PropagateFnc (const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
 Abstract interface for propagation/backpropagation. More...
 
void BackpropagateFnc (const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrixBase< BaseFloat > *in_diff)
 Backward pass transformation (to be implemented by descending class...) More...
 
void ReadData (std::istream &is, bool binary)
 Reads the component content. More...
 
void WriteData (std::ostream &os, bool binary) const
 Writes the component content. More...
 
void SetStats (const Matrix< BaseFloat > mat)
 Set the statistics matrix. More...
 
void Accumulate (const Matrix< BaseFloat > &posteriors, const std::vector< int32 > &alignment)
 Accumulate the statistics for KL-HMM paramter estimation,. More...
 
- Public Member Functions inherited from Component
 Component (int32 input_dim, int32 output_dim)
 Generic interface of a component,. More...
 
virtual ~Component ()
 
virtual bool IsUpdatable () const
 Check if componeny has 'Updatable' interface (trainable components),. More...
 
virtual bool IsMultistream () const
 Check if component has 'Recurrent' interface (trainable and recurrent),. More...
 
int32 InputDim () const
 Get the dimension of the input,. More...
 
int32 OutputDim () const
 Get the dimension of the output,. More...
 
void Propagate (const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
 Perform forward-pass propagation 'in' -> 'out',. More...
 
void Backpropagate (const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
 Perform backward-pass propagation 'out_diff' -> 'in_diff'. More...
 
void Write (std::ostream &os, bool binary) const
 Write the component to a stream,. More...
 
virtual std::string Info () const
 Print some additional info (after <ComponentName> and the dims),. More...
 
virtual std::string InfoGradient () const
 Print some additional info about gradient (after <...> and dims),. More...
 

Private Attributes

Matrix< double > kl_stats_
 
CuMatrix< BaseFloatkl_inv_q_
 

Additional Inherited Members

- Public Types inherited from Component
enum  ComponentType {
  kUnknown = 0x0, kUpdatableComponent = 0x0100, kAffineTransform, kLinearTransform,
  kConvolutionalComponent, kConvolutional2DComponent, kLstmProjected, kBlstmProjected,
  kRecurrentComponent, kActivationFunction = 0x0200, kSoftmax, kHiddenSoftmax,
  kBlockSoftmax, kSigmoid, kTanh, kParametricRelu,
  kDropout, kLengthNormComponent, kTranform = 0x0400, kRbm,
  kSplice, kCopy, kTranspose, kBlockLinearity,
  kAddShift, kRescale, kKlHmm = 0x0800, kSentenceAveragingComponent,
  kSimpleSentenceAveragingComponent, kAveragePoolingComponent, kAveragePooling2DComponent, kMaxPoolingComponent,
  kMaxPooling2DComponent, kFramePoolingComponent, kParallelComponent, kMultiBasisComponent
}
 Component type identification mechanism,. More...
 
- Static Public Member Functions inherited from Component
static const char * TypeToMarker (ComponentType t)
 Converts component type to marker,. More...
 
static ComponentType MarkerToType (const std::string &s)
 Converts marker to component type (case insensitive),. More...
 
static ComponentInit (const std::string &conf_line)
 Initialize component from a line in config file,. More...
 
static ComponentRead (std::istream &is, bool binary)
 Read the component from a stream (static method),. More...
 
- Static Public Attributes inherited from Component
static const struct key_value kMarkerMap []
 The table with pairs of Component types and markers (defined in nnet-component.cc),. More...
 
- Protected Member Functions inherited from Component
virtual void InitData (std::istream &is)
 Virtual interface for initialization and I/O,. More...
 
- Protected Attributes inherited from Component
int32 input_dim_
 Data members,. More...
 
int32 output_dim_
 Dimension of the output of the Component,. More...
 

Detailed Description

Definition at line 37 of file nnet-kl-hmm.h.

Constructor & Destructor Documentation

KlHmm ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 39 of file nnet-kl-hmm.h.

Referenced by KlHmm::Copy().

39  :
40  Component(dim_in, dim_out),
41  kl_stats_(dim_out, dim_in, kSetZero)
42  { }
Component(int32 input_dim, int32 output_dim)
Generic interface of a component,.
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147
~KlHmm ( )
inline

Definition at line 44 of file nnet-kl-hmm.h.

45  { }

Member Function Documentation

void Accumulate ( const Matrix< BaseFloat > &  posteriors,
const std::vector< int32 > &  alignment 
)
inline

Accumulate the statistics for KL-HMM paramter estimation,.

Definition at line 133 of file nnet-kl-hmm.h.

References rnnlm::i, KALDI_ASSERT, KlHmm::kl_stats_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Row().

Referenced by main().

134  {
135  KALDI_ASSERT(posteriors.NumRows() == alignment.size());
136  KALDI_ASSERT(posteriors.NumCols() == kl_stats_.NumCols());
137  int32 num_frames = alignment.size();
138  for (int32 i = 0; i < num_frames; i++) {
139  // Casting float posterior to double (fixing numerical issue),
140  Vector<double> temp(posteriors.Row(i));
141  // Sum the postiors grouped by states from the alignment,
142  kl_stats_.Row(alignment[i]).AddVec(1, temp);
143  }
144  }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147
void BackpropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
const CuMatrixBase< BaseFloat > &  out,
const CuMatrixBase< BaseFloat > &  out_diff,
CuMatrixBase< BaseFloat > *  in_diff 
)
inlinevirtual

Backward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 105 of file nnet-kl-hmm.h.

References KALDI_ERR.

108  {
109  KALDI_ERR << "Unimplemented";
110  }
#define KALDI_ERR
Definition: kaldi-error.h:127
Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 47 of file nnet-kl-hmm.h.

References KlHmm::KlHmm().

47 { return new KlHmm(*this); }
KlHmm(int32 dim_in, int32 dim_out)
Definition: nnet-kl-hmm.h:39
ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 48 of file nnet-kl-hmm.h.

References Component::kKlHmm.

void PropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
)
inlinevirtual

Abstract interface for propagation/backpropagation.

Forward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 50 of file nnet-kl-hmm.h.

References CuVectorBase< Real >::AddColSumMat(), VectorBase< Real >::AddColSumMat(), CuMatrixBase< Real >::AddMatMat(), CuMatrixBase< Real >::AddVecToCols(), MatrixBase< Real >::ApplyFloor(), MatrixBase< Real >::ApplyLog(), kaldi::ApproxEqual(), CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::CopyToMat(), MatrixBase< Real >::InvertElements(), KALDI_ASSERT, KlHmm::kl_inv_q_, KlHmm::kl_stats_, kaldi::kNoTrans, kaldi::kSetZero, kaldi::kTrans, CuMatrixBase< Real >::MulElements(), MatrixBase< Real >::MulRowsVec(), MatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), CuMatrixBase< Real >::NumRows(), CuMatrix< Real >::Resize(), and CuMatrixBase< Real >::Scale().

51  {
52  if (kl_inv_q_.NumRows() == 0) {
53  // Copy the CudaMatrix to a Matrix
54  Matrix<BaseFloat> in_tmp(in.NumRows(), in.NumCols());
55  in.CopyToMat(&in_tmp);
56  // Check if there are posteriors in the Matrix (check on first row),
57  BaseFloat post_sum = in_tmp.Row(0).Sum();
58  KALDI_ASSERT(ApproxEqual(post_sum, 1.0));
59  // Get a tmp Matrix of the stats
60  Matrix<BaseFloat> kl_stats_tmp(kl_stats_);
61  // Init a vector to get the sum of the rows (for normalization)
62  Vector<BaseFloat> row_sum(kl_stats_.NumRows(), kSetZero);
63  // Get the sum of the posteriors for normalization
64  row_sum.AddColSumMat(1, kl_stats_tmp);
65  // Apply floor to make sure there is no zero
66  row_sum.ApplyFloor(1e-20);
67  // Invert the sum (to normalize)
68  row_sum.InvertElements();
69  // Normalizing the statistics vector
70  kl_stats_tmp.MulRowsVec(row_sum);
71  // Apply floor before inversion and logarithm
72  kl_stats_tmp.ApplyFloor(1e-20);
73  // Apply invesion
74  kl_stats_tmp.InvertElements();
75  // Apply logarithm
76  kl_stats_tmp.ApplyLog();
77  // Inverted and logged values
79  // Holds now log (1/Q)
80  kl_inv_q_.CopyFromMat(kl_stats_tmp);
81  }
82  // Get the logarithm of the features for the Entropy calculation
83  // Copy the CudaMatrix to a Matrix
84  Matrix<BaseFloat> in_log_tmp(in.NumRows(), in.NumCols());
85  in.CopyToMat(&in_log_tmp);
86  // Flooring and log
87  in_log_tmp.ApplyFloor(1e-20);
88  in_log_tmp.ApplyLog();
89  CuMatrix<BaseFloat> log_in(in.NumRows(), in.NumCols());
90  log_in.CopyFromMat(in_log_tmp);
91  // P*logP
92  CuMatrix<BaseFloat> tmp_entropy(in);
93  tmp_entropy.MulElements(log_in);
94  // Getting the entropy (sum P*logP)
95  CuVector<BaseFloat> in_entropy(in.NumRows(), kSetZero);
96  in_entropy.AddColSumMat(1, tmp_entropy);
97  // sum P*log (1/Q)
98  out->AddMatMat(1, in, kNoTrans, kl_inv_q_, kTrans, 0);
99  // (sum P*logP) + (sum P*log(1/Q)
100  out->AddVecToCols(1, in_entropy);
101  // return the negative KL-divergence
102  out->Scale(-1);
103  }
void Scale(Real value)
Definition: cu-matrix.cc:608
void AddVecToCols(Real alpha, const CuVectorBase< Real > &col, Real beta=1.0)
(for each column c of *this), c = alpha * col + beta * c
Definition: cu-matrix.cc:1082
CuMatrix< BaseFloat > kl_inv_q_
Definition: nnet-kl-hmm.h:148
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
float BaseFloat
Definition: kaldi-types.h:29
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 CopyToMat(MatrixBase< OtherReal > *dst, MatrixTransposeType trans=kNoTrans) const
Definition: cu-matrix.cc:413
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:1141
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
static bool ApproxEqual(float a, float b, float relative_tolerance=0.001)
return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)).
Definition: kaldi-math.h:262
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147
void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 113 of file nnet-kl-hmm.h.

References Component::input_dim_, KALDI_ASSERT, KlHmm::kl_stats_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Component::output_dim_, and Matrix< Real >::Read().

113  {
114  kl_stats_.Read(is, binary);
117  }
int32 input_dim_
Data members,.
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
int32 output_dim_
Dimension of the output of the Component,.
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147
void SetStats ( const Matrix< BaseFloat mat)
inline

Set the statistics matrix.

Definition at line 125 of file nnet-kl-hmm.h.

References MatrixBase< Real >::CopyFromMat(), Component::input_dim_, KALDI_ASSERT, KlHmm::kl_stats_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Component::output_dim_, and Matrix< Real >::Resize().

Referenced by main().

125  {
126  KALDI_ASSERT(mat.NumRows() == output_dim_);
127  KALDI_ASSERT(mat.NumCols() == input_dim_);
128  kl_stats_.Resize(mat.NumRows(), mat.NumCols());
129  kl_stats_.CopyFromMat(mat);
130  }
int32 input_dim_
Data members,.
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
int32 output_dim_
Dimension of the output of the Component,.
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147
void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 120 of file nnet-kl-hmm.h.

References KlHmm::kl_stats_, and MatrixBase< Real >::Write().

Referenced by main().

120  {
121  kl_stats_.Write(os, binary);
122  }
void Write(std::ostream &out, bool binary) const
write to stream.
Matrix< double > kl_stats_
Definition: nnet-kl-hmm.h:147

Member Data Documentation

CuMatrix<BaseFloat> kl_inv_q_
private

Definition at line 148 of file nnet-kl-hmm.h.

Referenced by KlHmm::PropagateFnc().

Matrix<double> kl_stats_
private

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