NnetChainSupervision Struct Reference

#include <nnet-chain-example.h>

Collaboration diagram for NnetChainSupervision:

Public Member Functions

 NnetChainSupervision ()
 
 NnetChainSupervision (const std::string &name, const chain::Supervision &supervision, const VectorBase< BaseFloat > &deriv_weights, int32 first_frame, int32 frame_skip)
 Initialize the object from an object of type chain::Supervision, and some extra information. More...
 
 NnetChainSupervision (const NnetChainSupervision &other)
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &is, bool binary)
 
void Swap (NnetChainSupervision *other)
 
void CheckDim () const
 
bool operator== (const NnetChainSupervision &other) const
 

Public Attributes

std::string name
 the name of the output in the neural net; in simple setups it will just be "output". More...
 
std::vector< Indexindexes
 The indexes that the output corresponds to. More...
 
chain::Supervision supervision
 The supervision object, containing the FST. More...
 
Vector< BaseFloatderiv_weights
 This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivative during training (but not during model combination, where the derivatives need to agree with the computed objf values for the optimization code to work). More...
 

Detailed Description

Definition at line 43 of file nnet-chain-example.h.

Constructor & Destructor Documentation

◆ NnetChainSupervision() [1/3]

◆ NnetChainSupervision() [2/3]

NnetChainSupervision ( const std::string &  name,
const chain::Supervision &  supervision,
const VectorBase< BaseFloat > &  deriv_weights,
int32  first_frame,
int32  frame_skip 
)

Initialize the object from an object of type chain::Supervision, and some extra information.

Note: you probably want to set 'name' to "output". 'first_frame' will often be zero but you can choose (just make it consistent with how you numbered your inputs), and 'frame_skip' would be 1 in a vanilla setup, but we plan to try setups where the output periodicity is slower than the input, so in this case it might be 2 or 3.

Definition at line 107 of file nnet-chain-example.cc.

References NnetChainSupervision::CheckDim(), rnnlm::i, NnetChainSupervision::indexes, rnnlm::j, and KALDI_ASSERT.

112  :
113  name(name),
116  // note: this will set the 'x' index to zero.
117  indexes.resize(supervision.num_sequences *
118  supervision.frames_per_sequence);
119  int32 k = 0, num_sequences = supervision.num_sequences,
120  frames_per_sequence = supervision.frames_per_sequence;
121  for (int32 i = 0; i < frames_per_sequence; i++) {
122  for (int32 j = 0; j < num_sequences; j++,k++) {
123  indexes[k].n = j;
124  indexes[k].t = i * frame_skip + first_frame;
125  }
126  }
127  KALDI_ASSERT(k == indexes.size());
128  CheckDim();
129 }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
kaldi::int32 int32
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< Index > indexes
The indexes that the output corresponds to.

◆ NnetChainSupervision() [3/3]

Definition at line 92 of file nnet-chain-example.cc.

References NnetChainSupervision::CheckDim().

92  :
93  name(other.name),
94  indexes(other.indexes),
95  supervision(other.supervision),
96  deriv_weights(other.deriv_weights) { CheckDim(); }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
std::vector< Index > indexes
The indexes that the output corresponds to.

Member Function Documentation

◆ CheckDim()

void CheckDim ( ) const

Definition at line 65 of file nnet-chain-example.cc.

References NnetChainSupervision::deriv_weights, rnnlm::i, NnetChainSupervision::indexes, rnnlm::j, KALDI_ASSERT, rnnlm::n, and NnetChainSupervision::supervision.

Referenced by kaldi::nnet3::MergeSupervision(), NnetChainSupervision::NnetChainSupervision(), NnetChainSupervision::Read(), NnetChainSupervision::Swap(), and NnetChainSupervision::Write().

65  {
66  if (supervision.frames_per_sequence == -1) {
67  // this object has not been set up.
68  KALDI_ASSERT(indexes.empty());
69  return;
70  }
71  KALDI_ASSERT(indexes.size() == supervision.num_sequences *
72  supervision.frames_per_sequence && !indexes.empty() &&
73  supervision.frames_per_sequence > 1);
74  int32 first_frame = indexes[0].t,
75  frame_skip = indexes[supervision.num_sequences].t - first_frame,
76  num_sequences = supervision.num_sequences,
77  frames_per_sequence = supervision.frames_per_sequence;
78  int32 k = 0;
79  for (int32 i = 0; i < frames_per_sequence; i++) {
80  for (int32 j = 0; j < num_sequences; j++,k++) {
81  int32 n = j, t = i * frame_skip + first_frame, x = 0;
82  Index index(n, t, x);
83  KALDI_ASSERT(indexes[k] == index);
84  }
85  }
86  if (deriv_weights.Dim() != 0) {
87  KALDI_ASSERT(deriv_weights.Dim() == indexes.size());
88  KALDI_ASSERT(deriv_weights.Min() >= 0.0);
89  }
90 }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
kaldi::int32 int32
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< Index > indexes
The indexes that the output corresponds to.

◆ operator==()

bool operator== ( const NnetChainSupervision other) const

Definition at line 39 of file nnet-chain-example.cc.

References NnetChainSupervision::deriv_weights, NnetChainSupervision::indexes, NnetChainSupervision::name, and NnetChainSupervision::supervision.

Referenced by NnetChainSupervision::NnetChainSupervision().

39  {
40  return name == other.name && indexes == other.indexes &&
41  supervision == other.supervision &&
42  deriv_weights.ApproxEqual(other.deriv_weights);
43 }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
std::vector< Index > indexes
The indexes that the output corresponds to.

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)

Definition at line 45 of file nnet-chain-example.cc.

References NnetChainSupervision::CheckDim(), NnetChainSupervision::deriv_weights, kaldi::nnet3::ExpectToken(), NnetChainSupervision::indexes, KALDI_ASSERT, NnetChainSupervision::name, kaldi::nnet3::ReadIndexVector(), kaldi::ReadToken(), kaldi::nnet3::ReadVectorAsChar(), and NnetChainSupervision::supervision.

Referenced by NnetChainSupervision::NnetChainSupervision(), and NnetChainExample::Read().

45  {
46  ExpectToken(is, binary, "<NnetChainSup>");
47  ReadToken(is, binary, &name);
48  ReadIndexVector(is, binary, &indexes);
49  supervision.Read(is, binary);
50  std::string token;
51  ReadToken(is, binary, &token);
52  // in the future this back-compatibility code can be reworked.
53  if (token != "</NnetChainSup>") {
54  KALDI_ASSERT(token == "<DW>" || token == "<DW2>");
55  if (token == "<DW>")
56  ReadVectorAsChar(is, binary, &deriv_weights);
57  else
58  deriv_weights.Read(is, binary);
59  ExpectToken(is, binary, "</NnetChainSup>");
60  }
61  CheckDim();
62 }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
void ReadIndexVector(std::istream &is, bool binary, std::vector< Index > *vec)
Definition: nnet-common.cc:143
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< Index > indexes
The indexes that the output corresponds to.
void ReadVectorAsChar(std::istream &is, bool binary, Vector< BaseFloat > *vec)

◆ Swap()

void Swap ( NnetChainSupervision other)

Definition at line 98 of file nnet-chain-example.cc.

References NnetChainSupervision::CheckDim(), NnetChainSupervision::deriv_weights, NnetChainSupervision::indexes, NnetChainSupervision::name, kaldi::RandInt(), and NnetChainSupervision::supervision.

Referenced by NnetChainSupervision::NnetChainSupervision().

98  {
99  name.swap(other->name);
100  indexes.swap(other->indexes);
101  supervision.Swap(&(other->supervision));
102  deriv_weights.Swap(&(other->deriv_weights));
103  if (RandInt(0, 5) == 0)
104  CheckDim();
105 }
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
std::vector< Index > indexes
The indexes that the output corresponds to.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ Write()

void Write ( std::ostream &  os,
bool  binary 
) const

Definition at line 28 of file nnet-chain-example.cc.

References NnetChainSupervision::CheckDim(), NnetChainSupervision::deriv_weights, NnetChainSupervision::indexes, NnetChainSupervision::name, NnetChainSupervision::supervision, kaldi::nnet3::WriteIndexVector(), and kaldi::WriteToken().

Referenced by NnetChainSupervision::NnetChainSupervision().

28  {
29  CheckDim();
30  WriteToken(os, binary, "<NnetChainSup>");
31  WriteToken(os, binary, name);
32  WriteIndexVector(os, binary, indexes);
33  supervision.Write(os, binary);
34  WriteToken(os, binary, "<DW2>");
35  deriv_weights.Write(os, binary);
36  WriteToken(os, binary, "</NnetChainSup>");
37 }
void WriteIndexVector(std::ostream &os, bool binary, const std::vector< Index > &vec)
Definition: nnet-common.cc:126
Vector< BaseFloat > deriv_weights
This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivat...
chain::Supervision supervision
The supervision object, containing the FST.
std::string name
the name of the output in the neural net; in simple setups it will just be "output".
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
std::vector< Index > indexes
The indexes that the output corresponds to.

Member Data Documentation

◆ deriv_weights

Vector<BaseFloat> deriv_weights

This is a vector of per-frame weights, required to be between 0 and 1, that is applied to the derivative during training (but not during model combination, where the derivatives need to agree with the computed objf values for the optimization code to work).

The reason for this is to more exactly handle edge effects and to ensure that no frames are 'double-counted'. The order of this vector corresponds to the order of the 'indexes' (i.e. all the first frames, then all the second frames, etc.) If this vector is empty it means we're not applying per-frame weights, so it's equivalent to a vector of all ones. This vector is written to disk compactly as unsigned char.

Definition at line 77 of file nnet-chain-example.h.

Referenced by NnetChainSupervision::CheckDim(), kaldi::nnet3::MergeSupervision(), NnetChainSupervision::operator==(), NnetChainTrainer::ProcessOutputs(), NnetChainSupervision::Read(), NnetChainSupervision::Swap(), and NnetChainSupervision::Write().

◆ indexes

std::vector<Index> indexes

The indexes that the output corresponds to.

The size of this vector will be equal to supervision.num_sequences * supervision.frames_per_sequence. Be careful about the order of these indexes– it is a little confusing. The indexes in the 'index' vector are ordered as: (frame 0 of each sequence); (frame 1 of each sequence); and so on. But in the 'supervision' object, the FST contains (sequence 0; sequence 1; ...). So reordering is needed when doing the numerator computation. We order 'indexes' in this way for efficiency in the denominator computation (it helps memory locality), as well as to avoid the need for the nnet to reorder things internally to match the requested output (for layers inside the neural net, the ordering is (frame 0; frame 1 ...) as this corresponds to the order you get when you sort a vector of Index).

Definition at line 60 of file nnet-chain-example.h.

Referenced by NnetChainSupervision::CheckDim(), kaldi::nnet3::GetChainComputationRequest(), kaldi::nnet3::MergeSupervision(), NnetChainSupervision::NnetChainSupervision(), NnetChainExampleStructureHasher::operator()(), NnetChainSupervision::operator==(), NnetChainSupervision::Read(), NnetChainSupervision::Swap(), and NnetChainSupervision::Write().

◆ name

◆ supervision


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