Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nl-crf.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44744ad0309ca163814eb9f423862d820c0a72fa (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks 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 3 of the License, or      //
//    (at your option) any later version.                                    //
//                                                                           //
//    ModelBlocks 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 ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#ifndef _NL_CRF__
#define _NL_CRF__

#include "nl-safeids.h"
#include "nl-probmodel.h"
#include <cassert>
#include <math.h>

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  CRFModeledRV<Y>
//
////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2> 
class CRF3DModeledRV : public Y {

 private:

  // Static data members...
  static bool               bModel;      // whether model defined yet
  static int                cardGlb;     // global dependencies (used in all potentials)
  static int                cardOff;     // offset positions in site var sequence
  static int                cardSh;      // clique shapes at each offset
  static int                cardCnd;     // possible condition clique configs incl non-site vars in high bits
  static int                bitsVal;     // size in bits of val part of clique config
  static int                bitsValSite; // size in bits of each site var in val clique config
  static SafeArray5D<Id<int>,int,int,int,int,float> aaaaaPotentials;  // the model
/*   static SafeArray3D<int>   aaaCnds;          // calc features only once per frame */

 public:

  // Static extraction methods...
  static const float& getPotential ( Id<int> glb, int off, int sh, int configCnd, int configVal )
    { assert(bModel); return aaaaaPotentials.set(glb,off,sh,configCnd,configVal); }

  // Static specification methods...
  static void   init         ( int g, int o, int s, int c, int v, int b )
    { cardGlb=g; cardOff=o; cardSh=s; cardCnd=c; bitsVal=v; bitsValSite=b; }
  static float& setPotential ( Id<int> glb, int off, int sh, int configCnd, int configVal )
    { if(!bModel){aaaaaPotentials.init(cardGlb,cardOff,cardSh,cardCnd,1<<bitsVal,1.0); bModel=true;}
      return aaaaaPotentials.set(glb,off,sh,configCnd,configVal); }
  static void updateObservCliques ( const X1&, const X2& ) ;

  // Static input / output methods...
  static bool readModelFields ( char*[], int ) ;

  // Extraction methods...
  Prob getProb ( const X1&, const X2& ) const ;

  // Input / output methods...
  void writeObservCliqueConfigs ( FILE*, int, const char*, const X1&, const X2&, bool ) const ;
};

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2> bool               CRF3DModeledRV<Y,X1,X2>::bModel      = false;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::cardGlb     = 0;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::cardOff     = 0;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::cardSh      = 0;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::cardCnd     = 0;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::bitsVal     = 0;
template <class Y,class X1,class X2> int                CRF3DModeledRV<Y,X1,X2>::bitsValSite = 0;
template <class Y,class X1,class X2> SafeArray5D<Id<int>,int,int,int,int,float> CRF3DModeledRV<Y,X1,X2>::aaaaaPotentials;
/* template <class Y,class X1,class X2> SafeArray3D<int>   CRF3DModeledRV<Y,X1,X2>::aaaCnds; */

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2> 
Prob CRF3DModeledRV<Y,X1,X2>::getProb( const X1& x1, const X2& x2 ) const {

  SafeArray2D<int,int,int>    aaCnds  ( cardOff, cardSh ) ;
  SafeArray2D<int,int,double> aaTrell ( cardOff, 1<<bitsVal, 0.0 ) ;
  double prob = 1.0;

  // For each offset...
  for ( int off=0; off<cardOff; off++ )
    // For each shape...
    for ( int sh=0; sh<cardSh; sh++ )
      // Update clique config for condition...
      aaCnds.set(off,sh) = Y::getCliqueConfigCnd ( x1, x2, off, sh ) ;

  // For each offset...
  for ( int off=0; off<cardOff; off++ ) {
    // For each shape...
    for ( int sh=0; sh<cardSh; sh++ )
      // Multiply phi for feature (that is, exp lambda) into numerator...
      prob *= getPotential(Y::getGlobalDependency(x1,x2),off,sh,
                           aaCnds.get(off,sh),
                           Y::getCliqueConfigVal(off,sh));

    // If first column in trellis...
    if ( 0==off ) {
      // For each trellis value...
      for ( int configVal=0; configVal<(1<<bitsVal); configVal++ ) {
        // Add weight of each shape at current offset...
        float prod=1.0;
        for ( int sh=0; sh<cardSh; sh++ )
          prod *= getPotential(Y::getGlobalDependency(x1,x2),off,sh,
                               aaCnds.get(off,sh),
                               configVal) ;
        aaTrell.set(off,configVal) = prod ;
      }
    // If subsequent column in trellis...
    } else {
      // For each trellis transition (overlap = all but one)...
      for ( int configRghtValSite=0; configRghtValSite<(1<<bitsValSite); configRghtValSite++ )
        for ( int configValOverlap=0; configValOverlap<(1<<(bitsVal-bitsValSite)); configValOverlap++ ) {
          int configRghtVal = (configValOverlap<<bitsValSite)+configRghtValSite;
          // For each possible preceding trellis node... 
          for ( int configLeftValSite=0; configLeftValSite<(1<<bitsValSite); configLeftValSite++ ) {
            int configLeftVal = (configLeftValSite<<(bitsVal-bitsValSite))+configValOverlap;
            // Add product of result and previous trellis cell to current trellis cell...
            aaTrell.set(off,configRghtVal) += aaTrell.get(off-1,configLeftVal) ;
          }
          // Multiply weight of each shape...
          float prod=1.0;
          for ( int sh=0; sh<cardSh; sh++ )
            prod *= getPotential(Y::getGlobalDependency(x1,x2),off,sh,
                                 aaCnds.get(off,sh),
                                 configRghtVal);
          aaTrell.set(off,configRghtVal) *= prod;
        }
    }
  } // END EACH OFFSET

  // Calc total prob mass: sum of all possible forward scores in trellis...
  double probZ = 0.0;
  for ( int i=0; i<(1<<bitsVal); i++ )
    probZ += aaTrell.get(cardOff-1,i);
  // Normalize prob by total prob mass...
  return prob/probZ;
}

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2> 
bool CRF3DModeledRV<Y,X1,X2>::readModelFields ( char* aps[], int numFields ) {
  if ( 7==numFields )
    setPotential ( X1(string(aps[1])),                  // globals
                   atoi(aps[2]),                        // offsets
                   atoi(aps[3]),                        // shapes
                   atoi(aps[4]),                        // cnds
                   atoi(aps[5]) ) = exp(atof(aps[6])) ; // vals
  else return false;
  return true;
}

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2> 
void CRF3DModeledRV<Y,X1,X2>::writeObservCliqueConfigs ( FILE* pf, int frame, const char* psMdl,
                                                         const X1& x1, const X2& x2, bool bObsVal ) const {
  fprintf ( pf, "%04d> %s ", frame, psMdl );
  // For each shape (feature slope)...
  for ( int sh=0; sh<cardSh; sh++ ) {
    // Print clique config condition at each offset...
    for ( int off=0; off<cardOff; off++ )
      fprintf ( pf, "%c", intToTetraHex(Y::getCliqueConfigCnd(x1,x2,off,sh)) );
    if (sh<cardSh-1) printf(",");   // commas between shapes
  }
  printf(" : ");  // cond/val delimiter
  // Print clique config value at each offset...
  if ( bObsVal )
    for ( int off=0; off<cardOff; off++ )
      fprintf ( pf, "%c", intToTetraHex(Y::getCliqueConfigVal(off,0)) );
  else fprintf ( pf, "_" ) ;
  printf("\n");
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//  CRF4DModeledRV<Y>
//
////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2,class X3> 
class CRF4DModeledRV : public Y {

 private:

  // Static data members...
  static bool               bModel;      // whether model defined yet
  static int                cardGlb;     // global dependencies (used in all potentials)
  static int                cardOff;     // offset positions in site var sequence
  static int                cardSh;      // clique shapes at each offset
  static int                cardCnd;     // possible condition clique configs incl non-site vars in high bits
  static int                bitsVal;     // size in bits of val part of clique config
  static int                bitsValSite; // size in bits of each site var in val clique config
  static SafeArray5D<Id<int>,int,int,int,int,float> aaaaaPotentials;  // the model
/*   static SafeArray3D<int>   aaaCnds;          // calc features only once per frame */

 public:

  // Static extraction methods...
  static const float& getPotential ( Id<int> glb, int off, int sh, int configCnd, int configVal )
    { assert(bModel); return aaaaaPotentials.set(glb,off,sh,configCnd,configVal); }

  // Static specification methods...
  static void   init         ( int g, int o, int s, int c, int v, int b )
    { cardGlb=g; cardOff=o; cardSh=s; cardCnd=c; bitsVal=v; bitsValSite=b; }
  static float& setPotential ( Id<int> glb, int off, int sh, int configCnd, int configVal )
    { if(!bModel){aaaaaPotentials.init(cardGlb,cardOff,cardSh,cardCnd,1<<bitsVal,1.0); bModel=true;}
      return aaaaaPotentials.set(glb,off,sh,configCnd,configVal); }

  // Static input / output methods...
  static bool readModelFields ( char*[], int ) ;

  // Extraction methods...
  Prob getProb ( const X1&, const X2&, const X3& ) const ;

  // Input / output methods...
  void writeObservCliqueConfigs ( FILE*, int, const char*, const X1&, const X2&, const X3&, bool ) const ;
};

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2,class X3> bool CRF4DModeledRV<Y,X1,X2,X3>::bModel   = false;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::cardGlb     = 0;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::cardOff     = 0;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::cardSh      = 0;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::cardCnd     = 0;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::bitsVal     = 0;
template <class Y,class X1,class X2,class X3> int  CRF4DModeledRV<Y,X1,X2,X3>::bitsValSite = 0;
template <class Y,class X1,class X2,class X3> SafeArray5D<Id<int>,int,int,int,int,float> 
               CRF4DModeledRV<Y,X1,X2,X3>::aaaaaPotentials;
/* template <class Y,class X1,class X2> SafeArray3D<int>   CRF4DModeledRV<Y,X1,X2>::aaaCnds; */

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2,class X3> 
Prob CRF4DModeledRV<Y,X1,X2,X3>::getProb( const X1& x1, const X2& x2, const X3& x3 ) const {

  SafeArray2D<int,int,int>    aaCnds  ( cardOff, cardSh ) ;
  SafeArray2D<int,int,double> aaTrell ( cardOff, 1<<bitsVal, 0.0 ) ;
  double prob = 1.0;

  // For each offset...
  for ( int off=0; off<cardOff; off++ )
    // For each shape...
    for ( int sh=0; sh<cardSh; sh++ )
      // Update clique config for condition...
      aaCnds.set(off,sh) = Y::getCliqueConfigCnd ( x1, x2, x3, off, sh ) ;

  // For each offset...
  for ( int off=0; off<cardOff; off++ ) {
    // For each shape...
    for ( int sh=0; sh<cardSh; sh++ )
      // Multiply phi for feature (that is, exp lambda) into numerator...
      prob *= getPotential(Y::getGlobalDependency(x1,x2,x3),off,sh,
                           aaCnds.get(off,sh),
                           Y::getCliqueConfigVal(off,sh));

    // If first column in trellis...
    if ( 0==off ) {
      // For each trellis value...
      for ( int configVal=0; configVal<(1<<bitsVal); configVal++ ) {
        // Add weight of each shape at current offset...
        float prod=1.0;
        for ( int sh=0; sh<cardSh; sh++ )
          prod *= getPotential(Y::getGlobalDependency(x1,x2,x3),off,sh,
                               aaCnds.get(off,sh),
                               configVal) ;
        aaTrell.set(off,configVal) = prod ;
      }
    // If subsequent column in trellis...
    } else {
      // For each trellis transition (overlap = all but one)...
      for ( int configRghtValSite=0; configRghtValSite<(1<<bitsValSite); configRghtValSite++ )
        for ( int configValOverlap=0; configValOverlap<(1<<(bitsVal-bitsValSite)); configValOverlap++ ) {
          int configRghtVal = (configValOverlap<<bitsValSite)+configRghtValSite;
          // For each possible preceding trellis node... 
          for ( int configLeftValSite=0; configLeftValSite<(1<<bitsValSite); configLeftValSite++ ) {
            int configLeftVal = (configLeftValSite<<(bitsVal-bitsValSite))+configValOverlap;
            // Add product of result and previous trellis cell to current trellis cell...
            aaTrell.set(off,configRghtVal) += aaTrell.get(off-1,configLeftVal) ;
          }
          // Multiply weight of each shape...
          float prod=1.0;
          for ( int sh=0; sh<cardSh; sh++ )
            prod *= getPotential(Y::getGlobalDependency(x1,x2,x3),off,sh,
                                 aaCnds.get(off,sh),
                                 configRghtVal);
          aaTrell.set(off,configRghtVal) *= prod;
        }
    }
  } // END EACH OFFSET

  // Calc total prob mass: sum of all possible forward scores in trellis...
  double probZ = 0.0;
  for ( int i=0; i<(1<<bitsVal); i++ )
    probZ += aaTrell.get(cardOff-1,i);
  // Normalize prob by total prob mass...
  return prob/probZ;
}

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2,class X3> 
bool CRF4DModeledRV<Y,X1,X2,X3>::readModelFields ( char* aps[], int numFields ) {
  if ( 7==numFields )
    setPotential ( X1(string(aps[1])),                  // globals
                   atoi(aps[2]),                        // offsets
                   atoi(aps[3]),                        // shapes
                   atoi(aps[4]),                        // cnds
                   atoi(aps[5]) ) = exp(atof(aps[6])) ; // vals
  else return false;
  return true;
}

////////////////////////////////////////////////////////////////////////////////

template <class Y,class X1,class X2, class X3> 
void CRF4DModeledRV<Y,X1,X2,X3>::writeObservCliqueConfigs ( FILE* pf, int frame, const char* psMdl,
                                                            const X1& x1, const X2& x2, 
                                                            const X3& x3, bool bObsVal ) const {
  fprintf ( pf, "%04d> %s ", frame, psMdl );
  // For each shape (feature slope)...
  for ( int sh=0; sh<cardSh; sh++ ) {
    // Print clique config condition at each offset...
    for ( int off=0; off<cardOff; off++ )
      fprintf ( pf, "%c", intToTetraHex(Y::getCliqueConfigCnd(x1,x2,x3,off,sh)) );
    if (sh<cardSh-1) printf(",");   // commas between shapes
  }
  printf(" : ");  // cond/val delimiter
  // Print clique config value at each offset...
  if ( bObsVal )
    for ( int off=0; off<cardOff; off++ )
      fprintf ( pf, "%c", intToTetraHex(Y::getCliqueConfigVal(off,0)) );
  else fprintf ( pf, "_" ) ;
  printf("\n");
}

#endif //_NL_CRF__