chaine 3.13.1__cp310-cp310-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of chaine might be problematic. Click here for more details.

Files changed (68) hide show
  1. chaine/__init__.py +2 -0
  2. chaine/_core/crf.cpp +19854 -0
  3. chaine/_core/crf.cpython-310-darwin.so +0 -0
  4. chaine/_core/crf.pyx +271 -0
  5. chaine/_core/crfsuite/COPYING +27 -0
  6. chaine/_core/crfsuite/README +183 -0
  7. chaine/_core/crfsuite/include/crfsuite.h +1077 -0
  8. chaine/_core/crfsuite/include/crfsuite.hpp +649 -0
  9. chaine/_core/crfsuite/include/crfsuite_api.hpp +406 -0
  10. chaine/_core/crfsuite/include/os.h +65 -0
  11. chaine/_core/crfsuite/lib/cqdb/COPYING +28 -0
  12. chaine/_core/crfsuite/lib/cqdb/include/cqdb.h +518 -0
  13. chaine/_core/crfsuite/lib/cqdb/src/cqdb.c +639 -0
  14. chaine/_core/crfsuite/lib/cqdb/src/lookup3.c +1271 -0
  15. chaine/_core/crfsuite/lib/cqdb/src/main.c +184 -0
  16. chaine/_core/crfsuite/lib/crf/src/crf1d.h +354 -0
  17. chaine/_core/crfsuite/lib/crf/src/crf1d_context.c +788 -0
  18. chaine/_core/crfsuite/lib/crf/src/crf1d_encode.c +1020 -0
  19. chaine/_core/crfsuite/lib/crf/src/crf1d_feature.c +382 -0
  20. chaine/_core/crfsuite/lib/crf/src/crf1d_model.c +1085 -0
  21. chaine/_core/crfsuite/lib/crf/src/crf1d_tag.c +582 -0
  22. chaine/_core/crfsuite/lib/crf/src/crfsuite.c +500 -0
  23. chaine/_core/crfsuite/lib/crf/src/crfsuite_internal.h +233 -0
  24. chaine/_core/crfsuite/lib/crf/src/crfsuite_train.c +302 -0
  25. chaine/_core/crfsuite/lib/crf/src/dataset.c +115 -0
  26. chaine/_core/crfsuite/lib/crf/src/dictionary.c +127 -0
  27. chaine/_core/crfsuite/lib/crf/src/holdout.c +83 -0
  28. chaine/_core/crfsuite/lib/crf/src/json.c +1497 -0
  29. chaine/_core/crfsuite/lib/crf/src/json.h +120 -0
  30. chaine/_core/crfsuite/lib/crf/src/logging.c +85 -0
  31. chaine/_core/crfsuite/lib/crf/src/logging.h +49 -0
  32. chaine/_core/crfsuite/lib/crf/src/params.c +370 -0
  33. chaine/_core/crfsuite/lib/crf/src/params.h +84 -0
  34. chaine/_core/crfsuite/lib/crf/src/quark.c +180 -0
  35. chaine/_core/crfsuite/lib/crf/src/quark.h +46 -0
  36. chaine/_core/crfsuite/lib/crf/src/rumavl.c +1178 -0
  37. chaine/_core/crfsuite/lib/crf/src/rumavl.h +144 -0
  38. chaine/_core/crfsuite/lib/crf/src/train_arow.c +409 -0
  39. chaine/_core/crfsuite/lib/crf/src/train_averaged_perceptron.c +237 -0
  40. chaine/_core/crfsuite/lib/crf/src/train_l2sgd.c +491 -0
  41. chaine/_core/crfsuite/lib/crf/src/train_lbfgs.c +323 -0
  42. chaine/_core/crfsuite/lib/crf/src/train_passive_aggressive.c +442 -0
  43. chaine/_core/crfsuite/lib/crf/src/vecmath.h +360 -0
  44. chaine/_core/crfsuite/swig/crfsuite.cpp +1 -0
  45. chaine/_core/crfsuite_api.pxd +67 -0
  46. chaine/_core/liblbfgs/COPYING +22 -0
  47. chaine/_core/liblbfgs/README +71 -0
  48. chaine/_core/liblbfgs/include/lbfgs.h +745 -0
  49. chaine/_core/liblbfgs/lib/arithmetic_ansi.h +142 -0
  50. chaine/_core/liblbfgs/lib/arithmetic_sse_double.h +303 -0
  51. chaine/_core/liblbfgs/lib/arithmetic_sse_float.h +312 -0
  52. chaine/_core/liblbfgs/lib/lbfgs.c +1531 -0
  53. chaine/_core/tagger_wrapper.hpp +58 -0
  54. chaine/_core/trainer_wrapper.cpp +32 -0
  55. chaine/_core/trainer_wrapper.hpp +26 -0
  56. chaine/crf.py +505 -0
  57. chaine/logging.py +214 -0
  58. chaine/optimization/__init__.py +10 -0
  59. chaine/optimization/metrics.py +129 -0
  60. chaine/optimization/spaces.py +394 -0
  61. chaine/optimization/trial.py +103 -0
  62. chaine/optimization/utils.py +119 -0
  63. chaine/training.py +184 -0
  64. chaine/typing.py +18 -0
  65. chaine/validation.py +43 -0
  66. chaine-3.13.1.dist-info/METADATA +348 -0
  67. chaine-3.13.1.dist-info/RECORD +68 -0
  68. chaine-3.13.1.dist-info/WHEEL +5 -0
@@ -0,0 +1,144 @@
1
+ /*
2
+ * RumAVL - Threaded AVL Tree Implementation
3
+ *
4
+ * Copyright (c) 2005-2007 Jesse Long <jpl@unknown.za.net>
5
+ * All rights reserved.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a
8
+ * copy of this software and associated documentation files (the "Software"),
9
+ * to deal in the Software without restriction, including without limitation
10
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ * and/or sell copies of the Software, and to permit persons to whom the
12
+ * Software is furnished to do so, subject to the following conditions:
13
+ *
14
+ * 1. The above copyright notice and this permission notice shall be
15
+ * included in all copies or substantial portions of the Software.
16
+ * 2. The origin of the Software must not be misrepresented; you must not
17
+ * claim that you wrote the original Software.
18
+ * 3. Altered source versions of the Software must be plainly marked as
19
+ * such, and must not be misrepresented as being the original Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
+ * DEALINGS IN THE SOFTWARE.
28
+ */
29
+
30
+ /*
31
+ * Please see the `README' file, the documentation in the `doc' directory and
32
+ * the `rumavl.c' source file for more information.
33
+ */
34
+
35
+ #ifndef RUMAVL_H
36
+ #define RUMAVL_H 1
37
+
38
+ #ifdef __cplusplus
39
+ extern "C"
40
+ {
41
+ #endif
42
+
43
+ #include <stddef.h> /* size_t */
44
+
45
+ /*----------------------------------------------------------------------------
46
+ * DATA TYPES
47
+ *--------------------------------------------------------------------------*/
48
+
49
+ /* Opaque context handle for the tree */
50
+ typedef struct rumavl RUMAVL;
51
+
52
+ /* Node type - used for iterating */
53
+ typedef struct rumavl_node RUMAVL_NODE;
54
+
55
+ /*----------------------------------------------------------------------------
56
+ * FUNDEMENTAL FUNCTIONS
57
+ *--------------------------------------------------------------------------*/
58
+
59
+ /* Create a new RumAVL tree */
60
+ RUMAVL *rumavl_new(size_t reclen,
61
+ int (*cmp)(const void *, const void *, size_t, void *),
62
+ void *(*alloc)(void *, size_t, void *),
63
+ void *udata);
64
+
65
+ /* Destroy a RumAVL tree */
66
+ void rumavl_destroy(RUMAVL *tree);
67
+
68
+ /* This function returns the size of each record in a tree */
69
+ size_t rumavl_record_size(RUMAVL *tree);
70
+
71
+ /* Get a pointer to the udata pointer */
72
+ void **rumavl_udata(RUMAVL *tree);
73
+
74
+ /* Insert a record into a tree, overwriting an existing record necessary */
75
+ int rumavl_set(RUMAVL *tree, const void *record);
76
+ /* Insert a record into a tree, never overwrites an existing record */
77
+ int rumavl_insert(RUMAVL *tree, const void *record);
78
+
79
+ /* Retrieve record from tree, or NULL */
80
+ void *rumavl_find(RUMAVL *tree, const void *find);
81
+
82
+ /* Remove record from tree */
83
+ int rumavl_delete(RUMAVL *tree, const void *record);
84
+
85
+ /*----------------------------------------------------------------------------
86
+ * ITERATOR FUNCTIONS
87
+ *--------------------------------------------------------------------------*/
88
+
89
+ /* Get a pointer to the node containing a specific record */
90
+ RUMAVL_NODE *rumavl_node_find(RUMAVL *tree, const void *find, void **record);
91
+
92
+ /* Get the next node in sequence after a specific node, in a specific
93
+ * direction, or get the first node on either end of a tree */
94
+ RUMAVL_NODE *rumavl_node_next(RUMAVL *tree, RUMAVL_NODE *node, int dir,
95
+ void **record);
96
+ /* Possible directions */
97
+ #define RUMAVL_DESC (-1)
98
+ #define RUMAVL_ASC (+1)
99
+
100
+ /* Get a record held by a specific node */
101
+ void *rumavl_node_record(RUMAVL_NODE *node);
102
+
103
+ /* Pass each record in a tree to a user defined callback function */
104
+ extern int rumavl_foreach(RUMAVL *tree, int dir,
105
+ int (*cbfn)(RUMAVL *, void *, void *), void *udata);
106
+
107
+ /*----------------------------------------------------------------------------
108
+ * CALLBACK FUNCTIONS
109
+ *
110
+ * Functions giving you more control over the actions of this library.
111
+ *--------------------------------------------------------------------------*/
112
+
113
+ int (**rumavl_owcb(RUMAVL *tree))(RUMAVL *, RUMAVL_NODE *, void *,
114
+ const void *, void *);
115
+ int (**rumavl_delcb(RUMAVL *tree))(RUMAVL *, RUMAVL_NODE *, void *, void *);
116
+
117
+ /*----------------------------------------------------------------------------
118
+ * MEMORY MANAGEMENT
119
+ *
120
+ * The rumavl_mem struct is used to define how a RUMAVL object allocates
121
+ * and frees memory.
122
+ *--------------------------------------------------------------------------*/
123
+ void *(**rumavl_alloc(RUMAVL *tree))(void *ptr, size_t size, void *udata);
124
+
125
+ /*----------------------------------------------------------------------------
126
+ * ERROR CODES
127
+ *
128
+ * The functions returning int's will return these errors
129
+ *--------------------------------------------------------------------------*/
130
+
131
+ #define RUMAVL_ERR_INVAL (-1) /* Invalid argument */
132
+ #define RUMAVL_ERR_NOMEM (-2) /* Insufficient memory */
133
+ #define RUMAVL_ERR_NOENT (-3) /* Entry does not exist */
134
+ #define RUMAVL_ERR_EORNG (-5) /* No nodes left in range */
135
+ #define RUMAVL_ERR_EXIST (-6) /* Entry already exists */
136
+
137
+ /* returns static string describing error number */
138
+ extern const char *rumavl_strerror(int errno);
139
+
140
+ #ifdef __cplusplus
141
+ }
142
+ #endif
143
+
144
+ #endif /* ifndef RUMAVL_H */
@@ -0,0 +1,409 @@
1
+ /*
2
+ * Online training with Adaptive Regularization of Weights (AROW).
3
+ *
4
+ * Copyright (c) 2007-2010, Naoaki Okazaki
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ * * Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the names of the authors nor the names of its contributors
15
+ * may be used to endorse or promote products derived from this
16
+ * software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ /* $Id$ */
32
+
33
+ #ifdef HAVE_CONFIG_H
34
+ #include <config.h>
35
+ #endif /*HAVE_CONFIG_H*/
36
+
37
+ #include <os.h>
38
+
39
+ #include <stdio.h>
40
+ #include <stdlib.h>
41
+ #include <time.h>
42
+
43
+ #include <crfsuite.h>
44
+ #include "crfsuite_internal.h"
45
+ #include "logging.h"
46
+ #include "params.h"
47
+ #include "vecmath.h"
48
+
49
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
50
+
51
+ /**
52
+ * Training parameters (configurable with crfsuite_params_t interface).
53
+ */
54
+ typedef struct
55
+ {
56
+ floatval_t variance;
57
+ floatval_t gamma;
58
+ int max_iterations;
59
+ floatval_t epsilon;
60
+ } training_option_t;
61
+
62
+ /**
63
+ * Internal data structure for computing the sparse vector F(x, y) - F(x, y').
64
+ */
65
+ typedef struct
66
+ {
67
+ /* An array of feature indices relevant to the instance. */
68
+ int *actives;
69
+ int num_actives;
70
+ int cap_actives;
71
+ char *used;
72
+
73
+ /* Coefficient for collecting feature weights. */
74
+ floatval_t c;
75
+ /* The difference vector [K]. */
76
+ floatval_t *delta;
77
+ /* The number of features. */
78
+ int K;
79
+ } delta_t;
80
+
81
+ static int delta_init(delta_t *dc, const int K)
82
+ {
83
+ memset(dc, 0, sizeof(*dc));
84
+ dc->used = (char *)calloc(K, sizeof(char));
85
+ dc->delta = (floatval_t *)calloc(K, sizeof(floatval_t));
86
+ dc->K = K;
87
+ if (dc->delta == NULL || dc->used == NULL)
88
+ {
89
+ return 1;
90
+ }
91
+ return 0;
92
+ }
93
+
94
+ static void delta_finish(delta_t *dc)
95
+ {
96
+ free(dc->actives);
97
+ free(dc->used);
98
+ free(dc->delta);
99
+ memset(dc, 0, sizeof(*dc));
100
+ }
101
+
102
+ static void delta_reset(delta_t *dc)
103
+ {
104
+ int i;
105
+ for (i = 0; i < dc->num_actives; ++i)
106
+ {
107
+ int k = dc->actives[i];
108
+ dc->delta[k] = 0;
109
+ }
110
+ dc->num_actives = 0;
111
+ }
112
+
113
+ static void delta_collect(void *instance, int fid, floatval_t value)
114
+ {
115
+ delta_t *dc = (delta_t *)instance;
116
+
117
+ /* Expand the active feature list if necessary. */
118
+ if (dc->cap_actives <= dc->num_actives)
119
+ {
120
+ ++dc->cap_actives;
121
+ dc->cap_actives *= 2;
122
+ dc->actives = (int *)realloc(dc->actives, sizeof(int) * dc->cap_actives);
123
+ }
124
+
125
+ dc->actives[dc->num_actives++] = fid;
126
+ dc->delta[fid] += dc->c * value;
127
+ }
128
+
129
+ static void delta_finalize(delta_t *dc)
130
+ {
131
+ int i, j = 0, k;
132
+
133
+ /* Collapse the duplicated indices. */
134
+ for (i = 0; i < dc->num_actives; ++i)
135
+ {
136
+ k = dc->actives[i];
137
+ if (!dc->used[k])
138
+ {
139
+ dc->actives[j++] = k;
140
+ dc->used[k] = 1;
141
+ }
142
+ }
143
+ dc->num_actives = j; /* This is the distinct number of indices. */
144
+
145
+ /* Reset the used flag. */
146
+ for (i = 0; i < dc->num_actives; ++i)
147
+ {
148
+ k = dc->actives[i];
149
+ dc->used[k] = 0;
150
+ }
151
+ }
152
+
153
+ static floatval_t delta_norm2(delta_t *dc)
154
+ {
155
+ int i;
156
+ floatval_t norm2 = 0.;
157
+
158
+ for (i = 0; i < dc->num_actives; ++i)
159
+ {
160
+ int k = dc->actives[i];
161
+ norm2 += dc->delta[k] * dc->delta[k];
162
+ }
163
+ return norm2;
164
+ }
165
+
166
+ static void delta_add(delta_t *dc, floatval_t *w, floatval_t *ws, const floatval_t tau, const floatval_t u)
167
+ {
168
+ int i;
169
+ const floatval_t tauu = tau * u;
170
+
171
+ for (i = 0; i < dc->num_actives; ++i)
172
+ {
173
+ int k = dc->actives[i];
174
+ w[k] += tau * dc->delta[k];
175
+ ws[k] += tauu * dc->delta[k];
176
+ }
177
+ }
178
+
179
+ static int diff(int *x, int *y, int n)
180
+ {
181
+ int i, d = 0;
182
+ for (i = 0; i < n; ++i)
183
+ {
184
+ if (x[i] != y[i])
185
+ {
186
+ ++d;
187
+ }
188
+ }
189
+ return d;
190
+ }
191
+
192
+ static floatval_t cost_insensitive(floatval_t err, floatval_t d)
193
+ {
194
+ return err + 1.;
195
+ }
196
+
197
+ static floatval_t cost_sensitive(floatval_t err, floatval_t d)
198
+ {
199
+ return err + sqrt(d);
200
+ }
201
+
202
+ static floatval_t tau0(floatval_t cost, floatval_t norm, floatval_t c)
203
+ {
204
+ return cost / norm;
205
+ }
206
+
207
+ static floatval_t tau1(floatval_t cost, floatval_t norm, floatval_t c)
208
+ {
209
+ return MIN(c, cost / norm);
210
+ }
211
+
212
+ static floatval_t tau2(floatval_t cost, floatval_t norm, floatval_t c)
213
+ {
214
+ return cost / (norm + 0.5 / c);
215
+ }
216
+
217
+ static int exchange_options(crfsuite_params_t *params, training_option_t *opt, int mode)
218
+ {
219
+ BEGIN_PARAM_MAP(params, mode)
220
+ DDX_PARAM_FLOAT(
221
+ "variance", opt->variance, 1.,
222
+ "The initial variance of every feature weight.")
223
+ DDX_PARAM_FLOAT(
224
+ "gamma", opt->gamma, 1.,
225
+ "Tradeoff parameter.")
226
+ DDX_PARAM_INT(
227
+ "max_iterations", opt->max_iterations, 100,
228
+ "The maximum number of iterations.")
229
+ DDX_PARAM_FLOAT(
230
+ "epsilon", opt->epsilon, 0.,
231
+ "The stopping criterion (the mean loss).")
232
+ END_PARAM_MAP()
233
+
234
+ return 0;
235
+ }
236
+
237
+ void crfsuite_train_arow_init(crfsuite_params_t *params)
238
+ {
239
+ exchange_options(params, NULL, 0);
240
+ }
241
+
242
+ int crfsuite_train_arow(
243
+ encoder_t *gm,
244
+ dataset_t *trainset,
245
+ dataset_t *testset,
246
+ crfsuite_params_t *params,
247
+ logging_t *lg,
248
+ floatval_t **ptr_w)
249
+ {
250
+ int n, i, j, k, ret = 0;
251
+ int *viterbi = NULL;
252
+ floatval_t beta;
253
+ floatval_t *mean = NULL, *cov = NULL, *prod = NULL;
254
+ const int N = trainset->num_instances;
255
+ const int K = gm->num_features;
256
+ const int T = gm->cap_items;
257
+ training_option_t opt;
258
+ delta_t dc;
259
+ clock_t begin = clock();
260
+
261
+ /* Initialize the variable. */
262
+ if (delta_init(&dc, K) != 0)
263
+ {
264
+ ret = CRFSUITEERR_OUTOFMEMORY;
265
+ goto error_exit;
266
+ }
267
+
268
+ /* Obtain parameter values. */
269
+ exchange_options(params, &opt, -1);
270
+
271
+ /* Allocate arrays. */
272
+ mean = (floatval_t *)calloc(sizeof(floatval_t), K);
273
+ cov = (floatval_t *)calloc(sizeof(floatval_t), K);
274
+ prod = (floatval_t *)calloc(sizeof(floatval_t), K);
275
+ viterbi = (int *)calloc(sizeof(int), T);
276
+ if (mean == NULL || cov == NULL || prod == NULL || viterbi == NULL)
277
+ {
278
+ ret = CRFSUITEERR_OUTOFMEMORY;
279
+ goto error_exit;
280
+ }
281
+
282
+ /* Initialize the covariance vector (diagnal matrix). */
283
+ vecset(cov, opt.variance, K);
284
+
285
+ logging(lg, "Start training with AROW");
286
+ beta = 1.0 / opt.gamma;
287
+
288
+ /* Loop for epoch. */
289
+ for (i = 0; i < opt.max_iterations; ++i)
290
+ {
291
+ floatval_t norm = 0., sum_loss = 0.;
292
+ clock_t iteration_begin = clock();
293
+
294
+ /* Shuffle the instances. */
295
+ dataset_shuffle(trainset);
296
+
297
+ /* Loop for each instance. */
298
+ for (n = 0; n < N; ++n)
299
+ {
300
+ int d = 0;
301
+ floatval_t sv;
302
+ const crfsuite_instance_t *inst = dataset_get(trainset, n);
303
+
304
+ /* Set the feature weights to the encoder. */
305
+ gm->set_weights(gm, mean, 1.);
306
+ gm->set_instance(gm, inst);
307
+
308
+ /* Tag the sequence with the current model. */
309
+ gm->viterbi(gm, viterbi, &sv);
310
+
311
+ /* Compute the number of different labels. */
312
+ d = diff(inst->labels, viterbi, inst->num_items);
313
+ if (0 < d)
314
+ {
315
+ floatval_t alpha, frac;
316
+ floatval_t sc;
317
+ floatval_t cost;
318
+
319
+ /*
320
+ Compute the cost of this instance.
321
+ */
322
+ gm->score(gm, inst->labels, &sc);
323
+ cost = sv - sc + (double)d;
324
+
325
+ /* Initialize delta[k] = 0. */
326
+ delta_reset(&dc);
327
+
328
+ /*
329
+ For every feature k on the correct path:
330
+ delta[k] += 1;
331
+ */
332
+ dc.c = inst->weight;
333
+ gm->features_on_path(gm, inst, inst->labels, delta_collect, &dc);
334
+
335
+ /*
336
+ For every feature k on the Viterbi path:
337
+ delta[k] -= 1;
338
+ */
339
+ dc.c = -inst->weight;
340
+ gm->features_on_path(gm, inst, viterbi, delta_collect, &dc);
341
+
342
+ delta_finalize(&dc);
343
+
344
+ /* Compute prod[k] = delta[k] * delta[k]. */
345
+ for (j = 0; j < dc.num_actives; ++j)
346
+ {
347
+ k = dc.actives[j];
348
+ prod[k] = dc.delta[k] * dc.delta[k];
349
+ }
350
+
351
+ /*
352
+ Compute alpha.
353
+ */
354
+ frac = opt.gamma;
355
+ for (j = 0; j < dc.num_actives; ++j)
356
+ {
357
+ k = dc.actives[j];
358
+ frac += prod[k] * cov[k];
359
+ }
360
+ alpha = cost / frac;
361
+
362
+ /*
363
+ Update.
364
+ */
365
+ for (j = 0; j < dc.num_actives; ++j)
366
+ {
367
+ k = dc.actives[j];
368
+ mean[k] += alpha * cov[k] * dc.delta[k];
369
+ cov[k] = 1.0 / ((1.0 / cov[k]) + prod[k] / opt.gamma);
370
+ }
371
+
372
+ sum_loss += cost * inst->weight;
373
+ }
374
+ }
375
+
376
+ /* Output the progress. */
377
+ logging(lg, "Iteration %d, training loss: %f", i + 1, sum_loss);
378
+
379
+ /* Holdout evaluation if necessary. */
380
+ if (testset != NULL)
381
+ {
382
+ holdout_evaluation(gm, testset, mean, lg);
383
+ }
384
+
385
+ /* Convergence test. */
386
+ if (sum_loss / N <= opt.epsilon)
387
+ {
388
+ logging(lg, "Loss has converged, terminating training");
389
+ break;
390
+ }
391
+ }
392
+
393
+ free(viterbi);
394
+ free(prod);
395
+ free(cov);
396
+ *ptr_w = mean;
397
+ delta_finish(&dc);
398
+ return ret;
399
+
400
+ error_exit:
401
+ free(viterbi);
402
+ free(prod);
403
+ free(cov);
404
+ free(mean);
405
+ *ptr_w = NULL;
406
+ delta_finish(&dc);
407
+
408
+ return ret;
409
+ }