ScalarClusterable Class Reference

ScalarClusterable clusters scalars with x^2 loss. More...

#include <clusterable-classes.h>

Inheritance diagram for ScalarClusterable:
Collaboration diagram for ScalarClusterable:

Public Member Functions

 ScalarClusterable ()
 
 ScalarClusterable (BaseFloat x)
 
virtual std::string Type () const
 Return a string that describes the inherited type. More...
 
virtual BaseFloat Objf () const
 Return the objective function associated with the stats [assuming ML estimation]. More...
 
virtual void SetZero ()
 Set stats to empty. More...
 
virtual void Add (const Clusterable &other_in)
 Add other stats. More...
 
virtual void Sub (const Clusterable &other_in)
 Subtract other stats. More...
 
virtual ClusterableCopy () const
 Return a copy of this object. More...
 
virtual BaseFloat Normalizer () const
 Return the normalizer (typically, count) associated with the stats. More...
 
virtual void Write (std::ostream &os, bool binary) const
 Write data to stream. More...
 
virtual ClusterableReadNew (std::istream &is, bool binary) const
 Read data from a stream and return the corresponding object (const function; it's a class member because we need access to the vtable so generic code can read derived types). More...
 
std::string Info ()
 
BaseFloat Mean ()
 
- Public Member Functions inherited from Clusterable
virtual void Scale (BaseFloat f)
 Scale the stats by a positive number f [not mandatory to supply this]. More...
 
virtual ~Clusterable ()
 
virtual BaseFloat ObjfPlus (const Clusterable &other) const
 Return the objective function of the combined object this + other. More...
 
virtual BaseFloat ObjfMinus (const Clusterable &other) const
 Return the objective function of the subtracted object this - other. More...
 
virtual BaseFloat Distance (const Clusterable &other) const
 Return the objective function decrease from merging the two clusters, negated to be a positive number (or zero). More...
 

Private Member Functions

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

Private Attributes

BaseFloat x_
 
BaseFloat x2_
 
BaseFloat count_
 

Detailed Description

ScalarClusterable clusters scalars with x^2 loss.

Definition at line 34 of file clusterable-classes.h.

Constructor & Destructor Documentation

◆ ScalarClusterable() [1/2]

ScalarClusterable ( )
inline

Definition at line 36 of file clusterable-classes.h.

◆ ScalarClusterable() [2/2]

ScalarClusterable ( BaseFloat  x)
inlineexplicit

Definition at line 37 of file clusterable-classes.h.

Member Function Documentation

◆ Add()

void Add ( const Clusterable other)
virtual

Add other stats.

Implements Clusterable.

Definition at line 78 of file clusterable-classes.cc.

References KALDI_ASSERT, Clusterable::Type(), and ScalarClusterable::x_.

Referenced by ScalarClusterable::Copy(), ScalarClusterable::SetZero(), kaldi::TestAddToClustersOptimized(), and GaussClusterable::Type().

78  {
79  KALDI_ASSERT(other_in.Type() == "scalar");
80  const ScalarClusterable *other =
81  static_cast<const ScalarClusterable*>(&other_in);
82  x_ += other->x_;
83  x2_ += other->x2_;
84  count_ += other->count_;
85 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Copy()

Clusterable * Copy ( ) const
virtual

Return a copy of this object.

Implements Clusterable.

Definition at line 96 of file clusterable-classes.cc.

References ScalarClusterable::Add().

Referenced by GaussClusterable::Normalizer(), ScalarClusterable::SetZero(), and kaldi::TestAddToClusters().

96  {
98  ans->Add(*this);
99  return ans;
100 }

◆ Info()

std::string Info ( )

Definition at line 122 of file clusterable-classes.cc.

Referenced by ScalarClusterable::Normalizer().

122  {
123  std::stringstream str;
124  if (count_ == 0) {
125  str << "[empty]";
126  } else {
127  str << "[mean " << (x_ / count_) << ", var " << (x2_ / count_ -
128  (x_ * x_ / (count_ * count_))) << "]";
129  }
130  return str.str();
131 }

◆ Mean()

BaseFloat Mean ( )
inline

Definition at line 53 of file clusterable-classes.h.

References ScalarClusterable::count_, and ScalarClusterable::x_.

Referenced by kaldi::TestAddToClusters().

53 { return (count_ != 0 ? x_/count_ : 0.0); }

◆ Normalizer()

virtual BaseFloat Normalizer ( ) const
inlinevirtual

Return the normalizer (typically, count) associated with the stats.

Implements Clusterable.

Definition at line 44 of file clusterable-classes.h.

References ScalarClusterable::count_, ScalarClusterable::Info(), ScalarClusterable::ReadNew(), and ScalarClusterable::Write().

44  {
45  return static_cast<BaseFloat>(count_);
46  }
float BaseFloat
Definition: kaldi-types.h:29

◆ Objf()

BaseFloat Objf ( ) const
virtual

Return the objective function associated with the stats [assuming ML estimation].

Implements Clusterable.

Definition at line 69 of file clusterable-classes.cc.

References KALDI_ASSERT.

Referenced by kaldi::TestDistance(), kaldi::TestObjfMinus(), kaldi::TestObjfPlus(), kaldi::TestSumObjfAndSumNormalizer(), ScalarClusterable::Type(), and GaussClusterable::Type().

69  {
70  if (count_ == 0) {
71  return 0;
72  } else {
73  KALDI_ASSERT(count_ > 0);
74  return -(x2_ - x_ * x_ / count_);
75  }
76 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Read()

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

Definition at line 115 of file clusterable-classes.cc.

References kaldi::ExpectToken(), and kaldi::ReadBasicType().

Referenced by ScalarClusterable::ReadNew().

115  {
116  ExpectToken(is, binary, "SCL");
117  ReadBasicType(is, binary, &x_);
118  ReadBasicType(is, binary, &x2_);
119  ReadBasicType(is, binary, &count_);
120 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
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:191

◆ ReadNew()

Clusterable * ReadNew ( std::istream &  os,
bool  binary 
) const
virtual

Read data from a stream and return the corresponding object (const function; it's a class member because we need access to the vtable so generic code can read derived types).

Implements Clusterable.

Definition at line 109 of file clusterable-classes.cc.

References ScalarClusterable::Read().

Referenced by ScalarClusterable::Normalizer(), and GaussClusterable::Normalizer().

109  {
111  sc->Read(is, binary);
112  return sc;
113 }

◆ SetZero()

◆ Sub()

void Sub ( const Clusterable other)
virtual

Subtract other stats.

Implements Clusterable.

Definition at line 87 of file clusterable-classes.cc.

References KALDI_ASSERT, Clusterable::Type(), and ScalarClusterable::x_.

Referenced by ScalarClusterable::SetZero(), and GaussClusterable::Type().

87  {
88  KALDI_ASSERT(other_in.Type() == "scalar");
89  const ScalarClusterable *other =
90  static_cast<const ScalarClusterable*>(&other_in);
91  x_ -= other->x_;
92  x2_ -= other->x2_;
93  count_ -= other->count_;
94 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

Return a string that describes the inherited type.

Implements Clusterable.

Definition at line 38 of file clusterable-classes.h.

References ScalarClusterable::Objf().

38 { return "scalar"; }

◆ Write()

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

Write data to stream.

Implements Clusterable.

Definition at line 102 of file clusterable-classes.cc.

References kaldi::WriteBasicType(), and kaldi::WriteToken().

Referenced by ScalarClusterable::Normalizer(), and GaussClusterable::Normalizer().

102  {
103  WriteToken(os, binary, "SCL"); // magic string.
104  WriteBasicType(os, binary, x_);
105  WriteBasicType(os, binary, x2_);
106  WriteBasicType(os, binary, count_);
107 }
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
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Member Data Documentation

◆ count_

◆ x2_

BaseFloat x2_
private

Definition at line 56 of file clusterable-classes.h.

Referenced by ScalarClusterable::SetZero().

◆ x_


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