All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NnetComputer Class Reference
Collaboration diagram for NnetComputer:

Public Member Functions

 NnetComputer (const Nnet &nnet, const CuMatrixBase< BaseFloat > &input_feats, bool pad, Nnet *nnet_to_update=NULL)
 
void Propagate ()
 The forward-through-the-layers part of the computation. More...
 
void Backprop (CuMatrix< BaseFloat > *tmp_deriv)
 
BaseFloat ComputeLastLayerDeriv (const Posterior &pdf_post, CuMatrix< BaseFloat > *deriv) const
 Computes objf derivative at last layer, and returns objective function summed over labels and multiplied by utterance_weight. More...
 
CuMatrixBase< BaseFloat > & GetOutput ()
 

Private Attributes

const Nnetnnet_
 
std::vector< CuMatrix
< BaseFloat > > 
forward_data_
 
Nnetnnet_to_update_
 
std::vector< ChunkInfochunk_info_
 

Detailed Description

Definition at line 32 of file nnet-compute.cc.

Constructor & Destructor Documentation

NnetComputer ( const Nnet nnet,
const CuMatrixBase< BaseFloat > &  input_feats,
bool  pad,
Nnet nnet_to_update = NULL 
)

Definition at line 64 of file nnet-compute.cc.

References NnetComputer::chunk_info_, Nnet::ComputeChunkInfo(), NnetComputer::forward_data_, rnnlm::i, Nnet::InputDim(), KALDI_ERR, Nnet::LeftContext(), NnetComputer::nnet_, CuMatrixBase< Real >::NumCols(), Nnet::NumComponents(), CuMatrixBase< Real >::NumRows(), CuMatrixBase< Real >::Range(), CuMatrix< Real >::Resize(), Nnet::RightContext(), and CuMatrixBase< Real >::Row().

67  :
68  nnet_(nnet), nnet_to_update_(nnet_to_update) {
69  int32 dim = input_feats.NumCols();
70  if (dim != nnet.InputDim()) {
71  KALDI_ERR << "Feature dimension is " << dim << " but network expects "
72  << nnet.InputDim();
73  }
74  forward_data_.resize(nnet.NumComponents() + 1);
75 
76  int32 left_context = (pad ? nnet_.LeftContext() : 0),
77  right_context = (pad ? nnet_.RightContext() : 0);
78 
79  int32 num_rows = left_context + input_feats.NumRows() + right_context;
80  nnet.ComputeChunkInfo(num_rows, 1, &chunk_info_);
81 
82  CuMatrix<BaseFloat> &input(forward_data_[0]);
83  input.Resize(num_rows, dim);
84  input.Range(left_context, input_feats.NumRows(),
85  0, dim).CopyFromMat(input_feats);
86  for (int32 i = 0; i < left_context; i++)
87  input.Row(i).CopyFromVec(input_feats.Row(0));
88  int32 last_row = input_feats.NumRows() - 1;
89  for (int32 i = 0; i < right_context; i++)
90  input.Row(num_rows - i - 1).CopyFromVec(input_feats.Row(last_row));
91 }
const CuSubVector< Real > Row(MatrixIndexT i) const
Definition: cu-matrix.h:549
MatrixIndexT NumCols() const
Definition: cu-matrix.h:196
std::vector< CuMatrix< BaseFloat > > forward_data_
Definition: nnet-compute.cc:58
int32 LeftContext() const
Returns the left-context summed over all the Components...
Definition: nnet-nnet.cc:42
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
#define KALDI_ERR
Definition: kaldi-error.h:127
int32 RightContext() const
Returns the right-context summed over all the Components...
Definition: nnet-nnet.cc:56
std::vector< ChunkInfo > chunk_info_
Definition: nnet-compute.cc:61

Member Function Documentation

void Backprop ( CuMatrix< BaseFloat > *  tmp_deriv)

Definition at line 141 of file nnet-compute.cc.

References Component::Backprop(), NnetComputer::chunk_info_, NnetComputer::forward_data_, Nnet::GetComponent(), KALDI_ASSERT, NnetComputer::nnet_, NnetComputer::nnet_to_update_, and Nnet::NumComponents().

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

141  {
142  KALDI_ASSERT(nnet_to_update_ != NULL); // Or why do backprop?
143  // If later this reasoning changes, we can change this
144  // statement and add logic to make component_to_update, below,
145  // NULL if necessary.
146 
147  for (int32 c = nnet_.NumComponents() - 1; c >= 0; c--) {
148  const Component &component = nnet_.GetComponent(c);
149  Component *component_to_update = &(nnet_to_update_->GetComponent(c));
150  const CuMatrix<BaseFloat> &input = forward_data_[c],
151  &output = forward_data_[c+1],
152  &output_deriv = *tmp_deriv;
153  CuMatrix<BaseFloat> input_deriv;
154  component.Backprop(chunk_info_[c], chunk_info_[c+1], input, output, output_deriv,
155  component_to_update, &input_deriv);
156  *tmp_deriv = input_deriv;
157  }
158 }
std::vector< CuMatrix< BaseFloat > > forward_data_
Definition: nnet-compute.cc:58
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const Component & GetComponent(int32 c) const
Definition: nnet-nnet.cc:141
std::vector< ChunkInfo > chunk_info_
Definition: nnet-compute.cc:61
BaseFloat ComputeLastLayerDeriv ( const Posterior pdf_post,
CuMatrix< BaseFloat > *  deriv 
) const

Computes objf derivative at last layer, and returns objective function summed over labels and multiplied by utterance_weight.

[Note: utterance_weight will normally be 1.0].

Definition at line 111 of file nnet-compute.cc.

References NnetComputer::forward_data_, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_VLOG, kaldi::Log(), NnetComputer::nnet_, CuMatrixBase< Real >::NumCols(), Nnet::NumComponents(), CuMatrixBase< Real >::NumRows(), and CuMatrix< Real >::Resize().

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

112  {
113  // TODO: convert this to proper CUDA code, c.f. ComputeObjfAndDeriv
114  // in nnet-update.cc (I'm not sure, though, that this code is ever reached.)
115  int32 num_components = nnet_.NumComponents();
116  double tot_objf = 0.0, tot_weight = 0.0;
117  const CuMatrix<BaseFloat> &last_layer_output = forward_data_[num_components];
118  int32 num_frames = last_layer_output.NumRows(),
119  num_pdfs = last_layer_output.NumCols();
120  KALDI_ASSERT(pdf_post.size() == static_cast<size_t>(num_frames));
121  deriv->Resize(num_frames, num_pdfs); // will zero it.
122  for (int32 i = 0; i < deriv->NumRows(); i++) {
123  for (size_t j = 0; j < pdf_post[i].size(); j++) {
124  int32 label = pdf_post[i][j].first;
125  BaseFloat weight = pdf_post[i][j].second;
126  KALDI_ASSERT(label >= 0 && label < num_pdfs);
127  BaseFloat this_prob = last_layer_output(i, label);
128  KALDI_ASSERT(this_prob > 0.99e-20); // We floored to 1.0e-20 in SoftmaxLayer.
129  tot_objf += weight * Log(this_prob);
130  tot_weight += weight;
131  (*deriv)(i, label) += weight / this_prob; // could be "=", assuming the
132  // labels are all distinct.
133  }
134  }
135  KALDI_VLOG(4) << "Objective function is " << (tot_objf/tot_weight) <<
136  " per frame over " << tot_weight << " samples.";
137  return tot_objf;
138 }
std::vector< CuMatrix< BaseFloat > > forward_data_
Definition: nnet-compute.cc:58
float BaseFloat
Definition: kaldi-types.h:29
double Log(double x)
Definition: kaldi-math.h:100
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
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
CuMatrixBase<BaseFloat>& GetOutput ( )
inline

Definition at line 54 of file nnet-compute.cc.

References NnetComputer::forward_data_.

Referenced by kaldi::nnet2::NnetComputation(), and kaldi::nnet2::NnetComputationChunked().

54 { return forward_data_.back(); }
std::vector< CuMatrix< BaseFloat > > forward_data_
Definition: nnet-compute.cc:58
void Propagate ( )

The forward-through-the-layers part of the computation.

This is the forward part of the computation.

Definition at line 95 of file nnet-compute.cc.

References Component::BackpropNeedsInput(), Component::BackpropNeedsOutput(), NnetComputer::chunk_info_, NnetComputer::forward_data_, Nnet::GetComponent(), NnetComputer::nnet_, NnetComputer::nnet_to_update_, Nnet::NumComponents(), and Component::Propagate().

Referenced by kaldi::nnet2::NnetComputation(), kaldi::nnet2::NnetComputationChunked(), and kaldi::nnet2::NnetGradientComputation().

95  {
96  for (int32 c = 0; c < nnet_.NumComponents(); c++) {
97  const Component &component = nnet_.GetComponent(c);
98  CuMatrix<BaseFloat> &input = forward_data_[c],
99  &output = forward_data_[c+1];
100  component.Propagate(chunk_info_[c], chunk_info_[c+1], input, &output);
101  const Component *prev_component = (c == 0 ? NULL : &(nnet_.GetComponent(c-1)));
102  bool will_do_backprop = (nnet_to_update_ != NULL),
103  keep_last_output = will_do_backprop &&
104  ((c>0 && prev_component->BackpropNeedsOutput()) ||
105  component.BackpropNeedsInput());
106  if (!keep_last_output)
107  forward_data_[c].Resize(0, 0); // We won't need this data; save memory.
108  }
109 }
std::vector< CuMatrix< BaseFloat > > forward_data_
Definition: nnet-compute.cc:58
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
const Component & GetComponent(int32 c) const
Definition: nnet-nnet.cc:141
std::vector< ChunkInfo > chunk_info_
Definition: nnet-compute.cc:61

Member Data Documentation

std::vector<ChunkInfo> chunk_info_
private
Nnet* nnet_to_update_
private

Definition at line 59 of file nnet-compute.cc.

Referenced by NnetComputer::Backprop(), and NnetComputer::Propagate().


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