Blame view

src/tree/clusterable-classes.cc 9.74 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  // tree/clusterable-classes.cc
  
  // Copyright 2009-2011  Microsoft Corporation;  Saarland University
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  #include <algorithm>
  #include <string>
  #include "base/kaldi-math.h"
  #include "itf/clusterable-itf.h"
  #include "tree/clusterable-classes.h"
  
  namespace kaldi {
  
  // ============================================================================
  // Implementations common to all Clusterable classes (may be overridden for
  // speed).
  // ============================================================================
  
  BaseFloat Clusterable::ObjfPlus(const Clusterable &other) const {
    Clusterable *copy = this->Copy();
    copy->Add(other);
    BaseFloat ans = copy->Objf();
    delete copy;
    return ans;
  }
  
  BaseFloat Clusterable::ObjfMinus(const Clusterable &other) const {
    Clusterable *copy = this->Copy();
    copy->Sub(other);
    BaseFloat ans = copy->Objf();
    delete copy;
    return ans;
  }
  
  BaseFloat Clusterable::Distance(const Clusterable &other) const {
    Clusterable *copy = this->Copy();
    copy->Add(other);
    BaseFloat ans = this->Objf() + other.Objf() - copy->Objf();
    if (ans < 0) {
      // This should not happen. Check if it is more than just rounding error.
      if (std::fabs(ans) > 0.01 * (1.0 + std::fabs(copy->Objf()))) {
        KALDI_WARN << "Negative number returned (badly defined Clusterable "
                   << "class?): ans= " << ans;
      }
      ans = 0;
    }
    delete copy;
    return ans;
  }
  
  // ============================================================================
  // Implementation of ScalarClusterable class.
  // ============================================================================
  
  BaseFloat ScalarClusterable::Objf() const {
    if (count_ == 0) {
      return 0;
    } else {
      KALDI_ASSERT(count_ > 0);
      return -(x2_ - x_ * x_ / count_);
    }
  }
  
  void ScalarClusterable::Add(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "scalar");
    const ScalarClusterable *other =
        static_cast<const ScalarClusterable*>(&other_in);
    x_ += other->x_;
    x2_ += other->x2_;
    count_ += other->count_;
  }
  
  void ScalarClusterable::Sub(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "scalar");
    const ScalarClusterable *other =
        static_cast<const ScalarClusterable*>(&other_in);
    x_ -= other->x_;
    x2_ -= other->x2_;
    count_ -= other->count_;
  }
  
  Clusterable* ScalarClusterable::Copy() const {
    ScalarClusterable *ans = new ScalarClusterable();
    ans->Add(*this);
    return ans;
  }
  
  void ScalarClusterable::Write(std::ostream &os, bool binary) const {
    WriteToken(os, binary, "SCL");  // magic string.
    WriteBasicType(os, binary, x_);
    WriteBasicType(os, binary, x2_);
    WriteBasicType(os, binary, count_);
  }
  
  Clusterable* ScalarClusterable::ReadNew(std::istream &is, bool binary) const {
    ScalarClusterable *sc = new ScalarClusterable();
    sc->Read(is, binary);
    return sc;
  }
  
  void ScalarClusterable::Read(std::istream &is, bool binary) {
    ExpectToken(is, binary, "SCL");
    ReadBasicType(is, binary, &x_);
    ReadBasicType(is, binary, &x2_);
    ReadBasicType(is, binary, &count_);
  }
  
  std::string ScalarClusterable::Info() {
    std::stringstream str;
    if (count_ == 0) {
      str << "[empty]";
    } else {
      str << "[mean " << (x_ / count_) << ", var " << (x2_ / count_ -
          (x_ * x_ / (count_ * count_))) << "]";
    }
    return str.str();
  }
  
  // ============================================================================
  // Implementation of GaussClusterable class.
  // ============================================================================
  
  void GaussClusterable::AddStats(const VectorBase<BaseFloat> &vec,
                                  BaseFloat weight) {
    count_ += weight;
    stats_.Row(0).AddVec(weight, vec);
    stats_.Row(1).AddVec2(weight, vec);
  }
  
  void GaussClusterable::Add(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "gauss");
    const GaussClusterable *other =
        static_cast<const GaussClusterable*>(&other_in);
    count_ += other->count_;
    stats_.AddMat(1.0, other->stats_);
  }
  
  void GaussClusterable::Sub(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "gauss");
    const GaussClusterable *other =
        static_cast<const GaussClusterable*>(&other_in);
    count_ -= other->count_;
    stats_.AddMat(-1.0, other->stats_);
  }
  
  Clusterable* GaussClusterable::Copy() const {
    KALDI_ASSERT(stats_.NumRows() == 2);
    GaussClusterable *ans = new GaussClusterable(stats_.NumCols(), var_floor_);
    ans->Add(*this);
    return ans;
  }
  
  void GaussClusterable::Scale(BaseFloat f) {
    KALDI_ASSERT(f >= 0.0);
    count_ *= f;
    stats_.Scale(f);
  }
  
  void GaussClusterable::Write(std::ostream &os, bool binary) const {
    WriteToken(os, binary, "GCL");  // magic string.
    WriteBasicType(os, binary, count_);
    WriteBasicType(os, binary, var_floor_);
    stats_.Write(os, binary);
  }
  
  Clusterable* GaussClusterable::ReadNew(std::istream &is, bool binary) const {
    GaussClusterable *gc = new GaussClusterable();
    gc->Read(is, binary);
    return gc;
  }
  
  void GaussClusterable::Read(std::istream &is, bool binary) {
    ExpectToken(is, binary, "GCL");  // magic string.
    ReadBasicType(is, binary, &count_);
    ReadBasicType(is, binary, &var_floor_);
    stats_.Read(is, binary);
  }
  
  BaseFloat GaussClusterable::Objf() const {
    if (count_ <= 0.0) {
      if (count_ < -0.1) {
        KALDI_WARN << "GaussClusterable::Objf(), count is negative " << count_;
      }
      return 0.0;
    } else {
      size_t dim = stats_.NumCols();
      Vector<double> vars(dim);
      double objf_per_frame = 0.0;
      for (size_t d = 0; d < dim; d++) {
        double mean(stats_(0, d) / count_), var = stats_(1, d) / count_ - mean
            * mean, floored_var = std::max(var, var_floor_);
        vars(d) = floored_var;
        objf_per_frame += -0.5 * var / floored_var;
      }
      objf_per_frame += -0.5 * (vars.SumLog() + M_LOG_2PI * dim);
      if (KALDI_ISNAN(objf_per_frame)) {
        KALDI_WARN << "GaussClusterable::Objf(), objf is NaN";
        return 0.0;
      }
      // KALDI_VLOG(2) << "count = " << count_ << ", objf_per_frame = "<< objf_per_frame
      //   << ", returning " << (objf_per_frame*count_) << ", floor = " << var_floor_;
      return objf_per_frame * count_;
    }
  }
  
  
  // ============================================================================
  // Implementation of VectorClusterable class.
  // ============================================================================
  
  void VectorClusterable::Add(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "vector");
    const VectorClusterable *other =
        static_cast<const VectorClusterable*>(&other_in);
    weight_ += other->weight_;
    stats_.AddVec(1.0, other->stats_);
    sumsq_ += other->sumsq_;
  }
  
  void VectorClusterable::Sub(const Clusterable &other_in) {
    KALDI_ASSERT(other_in.Type() == "vector");
    const VectorClusterable *other =
        static_cast<const VectorClusterable*>(&other_in);
    weight_ -= other->weight_;
    sumsq_ -= other->sumsq_;
    stats_.AddVec(-1.0, other->stats_);
    if (weight_ < 0.0) {
      if (weight_ < -0.1 && weight_ < -0.0001 * fabs(other->weight_)) {
        // a negative weight may indicate an algorithmic error if it is
        // encountered.
        KALDI_WARN << "Negative weight encountered " << weight_;
      }
      weight_ = 0.0;
    }
    if (weight_ == 0.0) {
      sumsq_ = 0.0;
      stats_.Set(0.0);
    }
  }
  
  Clusterable* VectorClusterable::Copy() const {
    VectorClusterable *ans = new VectorClusterable();
    ans->weight_ = weight_;
    ans->sumsq_ = sumsq_;
    ans->stats_ = stats_;
    return ans;
  }
  
  void VectorClusterable::Scale(BaseFloat f) {
    KALDI_ASSERT(f >= 0.0);
    weight_ *= f;
    stats_.Scale(f);
    sumsq_ *= f;
  }
  
  void VectorClusterable::Write(std::ostream &os, bool binary) const {
    WriteToken(os, binary, "VCL");  // magic string.
    WriteToken(os, binary, "<Weight>");
    WriteBasicType(os, binary, weight_);
    WriteToken(os, binary, "<Sumsq>");  
    WriteBasicType(os, binary, sumsq_);
    WriteToken(os, binary, "<Stats>");    
    stats_.Write(os, binary);
  }
  
  Clusterable* VectorClusterable::ReadNew(std::istream &is, bool binary) const {
    VectorClusterable *vc = new VectorClusterable();
    vc->Read(is, binary);
    return vc;
  }
  
  void VectorClusterable::Read(std::istream &is, bool binary) {
    ExpectToken(is, binary, "VCL");  // magic string.
    ExpectToken(is, binary, "<Weight>");
    ReadBasicType(is, binary, &weight_);
    ExpectToken(is, binary, "<Sumsq>");  
    ReadBasicType(is, binary, &sumsq_);
    ExpectToken(is, binary, "<Stats>");    
    stats_.Read(is, binary);
  }
  
  VectorClusterable::VectorClusterable(const Vector<BaseFloat> &vector,
                                       BaseFloat weight):
      weight_(weight), stats_(vector), sumsq_(0.0) {
    stats_.Scale(weight);
    KALDI_ASSERT(weight >= 0.0);
    sumsq_ = VecVec(vector, vector) * weight;
  }    
  
  
  BaseFloat VectorClusterable::Objf() const {
    double direct_sumsq;
    if (weight_ > std::numeric_limits<BaseFloat>::min()) {
      direct_sumsq = VecVec(stats_, stats_) / weight_;
    } else {
      direct_sumsq = 0.0;
    }
    // ans is a negated weighted sum of squared distances; it should not be
    // positive.
    double ans = -(sumsq_ - direct_sumsq); 
    if (ans > 0.0) {
      if (ans > 1.0) {
        KALDI_WARN << "Positive objective function encountered (treating as zero): "
                   << ans;
      }
      ans = 0.0;
    }
    return ans;
  }
  
  
  }  // end namespace kaldi.