File: EMTrainer.cc

package info (click to toggle)
torch 2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,488 kB
  • ctags: 3,217
  • sloc: cpp: 14,272; makefile: 201
file content (192 lines) | stat: -rw-r--r-- 4,892 bytes parent folder | download
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
// Copyright (C) 2002 Samy Bengio (bengio@idiap.ch)
//                
//
// This file is part of Torch. Release II.
// [The Ultimate Machine Learning Library]
//
// Torch is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Torch is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Torch; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include "EMTrainer.h"
#include "log_add.h"

namespace Torch {

EMTrainer::EMTrainer(Distribution *distribution_, SeqDataSet *data_) : Trainer(distribution_,(DataSet*)data_)
{
  distribution = distribution_;
  sdata = data_;

  addROption("end accuracy", &end_accuracy, 0.0001,"end accuracy",true);
  addIOption("max iter", &max_iter, 100, "maximum number of iterations",true);
}

void EMTrainer::train(List *measurers)
{
  int iter = 0;
  int n_train = sdata->n_examples;

  real prev_nll = INF;
  real nll = INF;

  DataSet **datas;
  Measurer ***mes;
  int *n_mes;
  int n_datas;

  message("EMTrainer: training");

  extractMeasurers(measurers, sdata, &datas, &mes, &n_mes, &n_datas);

  // first compute tot_n_frames;
  sdata->totNFrames();

  while (1) {
    distribution->eMIterInitialize();
    nll = 0;
    for (int t=0;t<n_train;t++) {
      data->setExample(t);
      distribution->eMForward(data->inputs);
      nll -= distribution->log_probability;
      distribution->eMAccPosteriors(data->inputs,LOG_ONE);

      for(int i = 0; i < n_mes[0]; i++)
        mes[0][i]->measureEx();
    }
    nll /= sdata->tot_n_frames;
    
    distribution->eMUpdate();

    for(int i = 0; i < n_mes[0]; i++)
      mes[0][i]->measureIter();

    // for each supplementary dataset given, simply compute
    // test llr (not used for training)
    for(int julie = 1; julie < n_datas; julie++) {
      SeqDataSet *dataset = (SeqDataSet*)datas[julie];

      for(int t=0;t<dataset->n_examples;t++) {
        dataset->setExample(t);
        distribution->eMForward(dataset->inputs);

        for(int i = 0; i < n_mes[julie]; i++)
          mes[julie][i]->measureEx();
      }
      for(int i = 0; i < n_mes[julie]; i++)
        mes[julie][i]->measureIter();
    }

    // stopping criterion
    if ((prev_nll == nll) || fabs((prev_nll - nll)/prev_nll) < end_accuracy) {
      print("\n");
      break;
    }
    prev_nll = nll;
    print(".");
    iter++;
    if ((iter >= max_iter) && (max_iter > 0)) {
      print("\n");
      warning("EMTrainer: you have reached the maximum number of iterations");
      break;
    }
  }
  for(int i=0;i<n_datas;i++) {
    for(int j=0;j<n_mes[i];j++)
      mes[i][j]->measureEnd();
  }
  deleteExtractedMeasurers(datas, mes, n_mes, n_datas);
}

void EMTrainer::test(List *measurers)
{
  DataSet **datas;
  Measurer ***mes;
  int *n_mes;
  int n_datas;

  message("emtrainer: testing");

  extractMeasurers(measurers, NULL, &datas, &mes, &n_mes, &n_datas);

  for(int andrea = 0; andrea < n_datas; andrea++)
  {
    DataSet *dataset = datas[andrea];

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->reset();

    distribution->eMIterInitialize();
    for(int t = 0; t < dataset->n_examples; t++)
    {
      dataset->setExample(t);
      distribution->eMForward(dataset->inputs);

      for(int i = 0; i < n_mes[andrea]; i++)
        mes[andrea][i]->measureEx();
    }

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->measureIter();

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->measureEnd();
  }

  deleteExtractedMeasurers(datas, mes, n_mes, n_datas);
}

void EMTrainer::decode(List *measurers)
{
  DataSet **datas;
  Measurer ***mes;
  int *n_mes;
  int n_datas;

  message("emtrainer: decoding");

  extractMeasurers(measurers, NULL, &datas, &mes, &n_mes, &n_datas);

  for(int andrea = 0; andrea < n_datas; andrea++)
  {
    DataSet *dataset = datas[andrea];

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->reset();

    distribution->eMIterInitialize();
    for(int t = 0; t < dataset->n_examples; t++)
    {
      dataset->setExample(t);
      distribution->decode(dataset->inputs);

      for(int i = 0; i < n_mes[andrea]; i++)
        mes[andrea][i]->measureEx();
    }

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->measureIter();

    for(int i = 0; i < n_mes[andrea]; i++)
      mes[andrea][i]->measureEnd();
  }

  deleteExtractedMeasurers(datas, mes, n_mes, n_datas);
}

EMTrainer::~EMTrainer()
{
}

}