NnetIo Struct Reference

#include <nnet-example.h>

Collaboration diagram for NnetIo:

Public Member Functions

 NnetIo (const std::string &name, int32 t_begin, const MatrixBase< BaseFloat > &feats, int32 t_stride=1)
 This constructor creates NnetIo with name "name", indexes with n=0, x=0, and t values ranging from t_begin to (t_begin + t_stride * feats.NumRows() - 1) with a stride t_stride, and the provided features. More...
 
 NnetIo (const std::string &name, int32 t_begin, const GeneralMatrix &feats, int32 t_stride=1)
 This constructor creates NnetIo with name "name", indexes with n=0, x=0, and t values ranging from t_begin to (t_begin + t_stride * feats.NumRows() - 1) with a stride t_stride, and the provided features. More...
 
 NnetIo (const std::string &name, int32 dim, int32 t_begin, const Posterior &labels, int32 t_stride=1)
 This constructor sets "name" to the provided string, sets "indexes" with n=0, x=0, and t from t_begin to (t_begin + t_stride * labels.size() - 1) with a stride t_stride, and the labels as provided. More...
 
void Swap (NnetIo *other)
 
 NnetIo ()
 
void Write (std::ostream &os, bool binary) const
 
void Read (std::istream &is, bool binary)
 
bool operator== (const NnetIo &other) const
 

Public Attributes

std::string name
 the name of the input in the neural net; in simple setups it will just be "input". More...
 
std::vector< Indexindexes
 "indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of the "features" matrix. More...
 
GeneralMatrix features
 The features or labels. More...
 

Detailed Description

Definition at line 33 of file nnet-example.h.

Constructor & Destructor Documentation

◆ NnetIo() [1/4]

NnetIo ( const std::string &  name,
int32  t_begin,
const MatrixBase< BaseFloat > &  feats,
int32  t_stride = 1 
)

This constructor creates NnetIo with name "name", indexes with n=0, x=0, and t values ranging from t_begin to (t_begin + t_stride * feats.NumRows() - 1) with a stride t_stride, and the provided features.

t_begin should be the frame that feats.Row(0) represents.

Definition at line 58 of file nnet-example.cc.

References rnnlm::i, NnetIo::indexes, KALDI_ASSERT, and MatrixBase< Real >::NumRows().

60  :
61  name(name), features(feats) {
62  int32 num_rows = feats.NumRows();
63  KALDI_ASSERT(num_rows > 0);
64  indexes.resize(num_rows); // sets all n,t,x to zeros.
65  for (int32 i = 0; i < num_rows; i++)
66  indexes[i].t = t_begin + i * t_stride;
67 }
kaldi::int32 int32
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36

◆ NnetIo() [2/4]

NnetIo ( const std::string &  name,
int32  t_begin,
const GeneralMatrix feats,
int32  t_stride = 1 
)

This constructor creates NnetIo with name "name", indexes with n=0, x=0, and t values ranging from t_begin to (t_begin + t_stride * feats.NumRows() - 1) with a stride t_stride, and the provided features.

t_begin should be the frame that the first row of 'feats' represents.

Definition at line 69 of file nnet-example.cc.

References rnnlm::i, NnetIo::indexes, KALDI_ASSERT, and GeneralMatrix::NumRows().

71  :
72  name(name), features(feats) {
73  int32 num_rows = feats.NumRows();
74  KALDI_ASSERT(num_rows > 0);
75  indexes.resize(num_rows); // sets all n,t,x to zeros.
76  for (int32 i = 0; i < num_rows; i++)
77  indexes[i].t = t_begin + i * t_stride;
78 }
kaldi::int32 int32
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36

◆ NnetIo() [3/4]

NnetIo ( const std::string &  name,
int32  dim,
int32  t_begin,
const Posterior labels,
int32  t_stride = 1 
)

This constructor sets "name" to the provided string, sets "indexes" with n=0, x=0, and t from t_begin to (t_begin + t_stride * labels.size() - 1) with a stride t_stride, and the labels as provided.

t_begin should be the frame to which labels[0] corresponds.

Definition at line 86 of file nnet-example.cc.

References NnetIo::features, rnnlm::i, NnetIo::indexes, and KALDI_ASSERT.

90  :
91  name(name) {
92  int32 num_rows = labels.size();
93  KALDI_ASSERT(num_rows > 0);
94  SparseMatrix<BaseFloat> sparse_feats(dim, labels);
95  features = sparse_feats;
96  indexes.resize(num_rows); // sets all n,t,x to zeros.
97  for (int32 i = 0; i < num_rows; i++)
98  indexes[i].t = t_begin + i * t_stride;
99 }
kaldi::int32 int32
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36

◆ NnetIo() [4/4]

NnetIo ( )
inline

Definition at line 78 of file nnet-example.h.

References NnetIo::operator==(), NnetIo::Read(), and NnetIo::Write().

78 { }

Member Function Documentation

◆ operator==()

bool operator== ( const NnetIo other) const

Definition at line 46 of file nnet-example.cc.

References kaldi::ApproxEqual(), NnetIo::features, GeneralMatrix::GetMatrix(), NnetIo::indexes, NnetIo::name, GeneralMatrix::NumCols(), and GeneralMatrix::NumRows().

Referenced by NnetIo::NnetIo().

46  {
47  if (name != other.name) return false;
48  if (indexes != other.indexes) return false;
49  if (features.NumRows() != other.features.NumRows() ||
50  features.NumCols() != other.features.NumCols())
51  return false;
52  Matrix<BaseFloat> this_mat, other_mat;
53  features.GetMatrix(&this_mat);
54  other.features.GetMatrix(&other_mat);
55  return ApproxEqual(this_mat, other_mat);
56 }
void GetMatrix(Matrix< BaseFloat > *mat) const
Outputs the contents as a matrix.
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
MatrixIndexT NumCols() const
MatrixIndexT NumRows() const
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36
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:265

◆ Read()

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

Definition at line 38 of file nnet-example.cc.

References kaldi::nnet3::ExpectToken(), NnetIo::features, NnetIo::indexes, NnetIo::name, GeneralMatrix::Read(), kaldi::nnet3::ReadIndexVector(), and kaldi::ReadToken().

Referenced by NnetIo::NnetIo(), and NnetExample::Read().

38  {
39  ExpectToken(is, binary, "<NnetIo>");
40  ReadToken(is, binary, &name);
41  ReadIndexVector(is, binary, &indexes);
42  features.Read(is, binary);
43  ExpectToken(is, binary, "</NnetIo>");
44 }
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
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
void Read(std::istream &is, bool binary)
Note: if you write a compressed matrix in text form, it will be read as a regular full matrix...
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
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36

◆ Swap()

void Swap ( NnetIo other)

Definition at line 80 of file nnet-example.cc.

References NnetIo::features, NnetIo::indexes, NnetIo::name, and GeneralMatrix::Swap().

80  {
81  name.swap(other->name);
82  indexes.swap(other->indexes);
83  features.Swap(&(other->features));
84 }
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
void Swap(GeneralMatrix *other)
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36

◆ Write()

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

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

References NnetIo::features, NnetIo::indexes, KALDI_ASSERT, NnetIo::name, GeneralMatrix::NumRows(), GeneralMatrix::Write(), kaldi::nnet3::WriteIndexVector(), and kaldi::WriteToken().

Referenced by NnetIo::NnetIo(), and NnetExample::Write().

28  {
29  KALDI_ASSERT(features.NumRows() == static_cast<int32>(indexes.size()));
30  WriteToken(os, binary, "<NnetIo>");
31  WriteToken(os, binary, name);
32  WriteIndexVector(os, binary, indexes);
33  features.Write(os, binary);
34  WriteToken(os, binary, "</NnetIo>");
35  KALDI_ASSERT(static_cast<size_t>(features.NumRows()) == indexes.size());
36 }
void WriteIndexVector(std::ostream &os, bool binary, const std::vector< Index > &vec)
Definition: nnet-common.cc:126
kaldi::int32 int32
GeneralMatrix features
The features or labels.
Definition: nnet-example.h:46
std::vector< Index > indexes
"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of th...
Definition: nnet-example.h:42
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
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
Definition: nnet-example.h:36
void Write(std::ostream &os, bool binary) const

Member Data Documentation

◆ features

◆ indexes

std::vector<Index> indexes

"indexes" is a vector the same length as features.NumRows(), explaining the meaning of each row of the "features" matrix.

Note: the "n" values in the indexes will always be zero in individual examples, but in general nonzero after we aggregate the examples into the minibatch level.

Definition at line 42 of file nnet-example.h.

Referenced by kaldi::nnet3::ContainsSingleExample(), kaldi::nnet3::ExampleApproxEqual(), kaldi::nnet3::FilterExample(), kaldi::nnet3::GetChainComputationRequest(), kaldi::nnet3::GetComputationRequest(), kaldi::nnet3::GetDiscriminativeComputationRequest(), kaldi::nnet3::GetIoSizes(), kaldi::nnet3::GetNumNvalues(), main(), kaldi::nnet3::MergeIo(), NnetIo::NnetIo(), NnetIoStructureCompare::operator()(), NnetIo::operator==(), NnetIo::Read(), NnetIo::Swap(), NnetIo::Write(), and kaldi::nnet3::WriteExamples().

◆ name


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