chaine 3.13.1__cp313-cp313-win_amd64.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.cp313-win_amd64.pyd +0 -0
  3. chaine/_core/crf.cpp +19854 -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 +4 -0
@@ -0,0 +1,323 @@
1
+ /*
2
+ * Batch training with L-BFGS.
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 <string.h>
42
+ #include <limits.h>
43
+ #include <time.h>
44
+
45
+ #include <crfsuite.h>
46
+ #include "crfsuite_internal.h"
47
+
48
+ #include "logging.h"
49
+ #include "params.h"
50
+ #include "vecmath.h"
51
+ #include <lbfgs.h>
52
+
53
+ /**
54
+ * Training parameters (configurable with crfsuite_params_t interface).
55
+ */
56
+ typedef struct
57
+ {
58
+ floatval_t c1;
59
+ floatval_t c2;
60
+ int memory;
61
+ floatval_t epsilon;
62
+ int stop;
63
+ floatval_t delta;
64
+ int max_iterations;
65
+ char *linesearch;
66
+ int linesearch_max_iterations;
67
+ } training_option_t;
68
+
69
+ /**
70
+ * Internal data structure for the callback function of lbfgs().
71
+ */
72
+ typedef struct
73
+ {
74
+ encoder_t *gm;
75
+ dataset_t *trainset;
76
+ dataset_t *testset;
77
+ logging_t *lg;
78
+ floatval_t c2;
79
+ floatval_t *best_w;
80
+ clock_t begin;
81
+ } lbfgs_internal_t;
82
+
83
+ static lbfgsfloatval_t lbfgs_evaluate(
84
+ void *instance,
85
+ const lbfgsfloatval_t *x,
86
+ lbfgsfloatval_t *g,
87
+ const int n,
88
+ const lbfgsfloatval_t step)
89
+ {
90
+ int i;
91
+ floatval_t f, norm = 0.;
92
+ lbfgs_internal_t *lbfgsi = (lbfgs_internal_t *)instance;
93
+ encoder_t *gm = lbfgsi->gm;
94
+ dataset_t *trainset = lbfgsi->trainset;
95
+
96
+ /* Compute the objective value and gradients. */
97
+ gm->objective_and_gradients_batch(gm, trainset, x, &f, g);
98
+
99
+ /* L2 regularization. */
100
+ if (0 < lbfgsi->c2)
101
+ {
102
+ const floatval_t c22 = lbfgsi->c2 * 2.;
103
+ for (i = 0; i < n; ++i)
104
+ {
105
+ g[i] += (c22 * x[i]);
106
+ norm += x[i] * x[i];
107
+ }
108
+ f += (lbfgsi->c2 * norm);
109
+ }
110
+
111
+ return f;
112
+ }
113
+
114
+ static int lbfgs_progress(
115
+ void *instance,
116
+ const lbfgsfloatval_t *x,
117
+ const lbfgsfloatval_t *g,
118
+ const lbfgsfloatval_t fx,
119
+ const lbfgsfloatval_t xnorm,
120
+ const lbfgsfloatval_t gnorm,
121
+ const lbfgsfloatval_t step,
122
+ int n,
123
+ int k,
124
+ int ls)
125
+ {
126
+ int i, num_active_features = 0;
127
+ clock_t duration, clk = clock();
128
+ lbfgs_internal_t *lbfgsi = (lbfgs_internal_t *)instance;
129
+ dataset_t *testset = lbfgsi->testset;
130
+ encoder_t *gm = lbfgsi->gm;
131
+ logging_t *lg = lbfgsi->lg;
132
+
133
+ /* Compute the duration required for this iteration. */
134
+ duration = clk - lbfgsi->begin;
135
+ lbfgsi->begin = clk;
136
+
137
+ /* Store the feature weight in case L-BFGS terminates with an error. */
138
+ for (i = 0; i < n; ++i)
139
+ {
140
+ lbfgsi->best_w[i] = x[i];
141
+ if (x[i] != 0.)
142
+ ++num_active_features;
143
+ }
144
+
145
+ /* Report the progress. */
146
+ logging(lg, "Iteration %d, training loss: %f", k, fx);
147
+
148
+ /* Send the tagger with the current parameters. */
149
+ if (testset != NULL)
150
+ {
151
+ holdout_evaluation(gm, testset, x, lg);
152
+ }
153
+
154
+ /* Continue. */
155
+ return 0;
156
+ }
157
+
158
+ static int exchange_options(crfsuite_params_t *params, training_option_t *opt, int mode)
159
+ {
160
+ BEGIN_PARAM_MAP(params, mode)
161
+ DDX_PARAM_FLOAT(
162
+ "c1", opt->c1, 0,
163
+ "Coefficient for L1 regularization.")
164
+ DDX_PARAM_FLOAT(
165
+ "c2", opt->c2, 1.0,
166
+ "Coefficient for L2 regularization.")
167
+ DDX_PARAM_INT(
168
+ "max_iterations", opt->max_iterations, INT_MAX,
169
+ "The maximum number of iterations for L-BFGS optimization.")
170
+ DDX_PARAM_INT(
171
+ "num_memories", opt->memory, 6,
172
+ "The number of limited memories for approximating the inverse hessian matrix.")
173
+ DDX_PARAM_FLOAT(
174
+ "epsilon", opt->epsilon, 1e-5,
175
+ "Epsilon for testing the convergence of the objective.")
176
+ DDX_PARAM_INT(
177
+ "period", opt->stop, 10,
178
+ "The duration of iterations to test the stopping criterion.")
179
+ DDX_PARAM_FLOAT(
180
+ "delta", opt->delta, 1e-5,
181
+ "The threshold for the stopping criterion; an L-BFGS iteration stops when the\n"
182
+ "improvement of the log likelihood over the last ${period} iterations is no\n"
183
+ "greater than this threshold.")
184
+ DDX_PARAM_STRING(
185
+ "linesearch", opt->linesearch, "MoreThuente",
186
+ "The line search algorithm used in L-BFGS updates:\n"
187
+ "{ 'MoreThuente': More and Thuente's method,\n"
188
+ " 'Backtracking': Backtracking method with regular Wolfe condition,\n"
189
+ " 'StrongBacktracking': Backtracking method with strong Wolfe condition\n"
190
+ "}\n")
191
+ DDX_PARAM_INT(
192
+ "max_linesearch", opt->linesearch_max_iterations, 20,
193
+ "The maximum number of trials for the line search algorithm.")
194
+ END_PARAM_MAP()
195
+
196
+ return 0;
197
+ }
198
+
199
+ void crfsuite_train_lbfgs_init(crfsuite_params_t *params)
200
+ {
201
+ exchange_options(params, NULL, 0);
202
+ }
203
+
204
+ int crfsuite_train_lbfgs(
205
+ encoder_t *gm,
206
+ dataset_t *trainset,
207
+ dataset_t *testset,
208
+ crfsuite_params_t *params,
209
+ logging_t *lg,
210
+ floatval_t **ptr_w)
211
+ {
212
+ int ret = 0, lbret;
213
+ floatval_t *w = NULL;
214
+ clock_t begin = clock();
215
+ const int N = trainset->num_instances;
216
+ const int L = trainset->data->labels->num(trainset->data->labels);
217
+ const int A = trainset->data->attrs->num(trainset->data->attrs);
218
+ const int K = gm->num_features;
219
+ lbfgs_internal_t lbfgsi;
220
+ lbfgs_parameter_t lbfgsparam;
221
+ training_option_t opt;
222
+
223
+ /* Initialize the variables. */
224
+ memset(&lbfgsi, 0, sizeof(lbfgsi));
225
+ memset(&opt, 0, sizeof(opt));
226
+ lbfgs_parameter_init(&lbfgsparam);
227
+
228
+ /* Allocate an array that stores the current weights. As per the liblbfgs
229
+ * documentation, this needs to be allocated with lbfgs_malloc. */
230
+ w = lbfgs_malloc(K);
231
+ if (w == NULL)
232
+ {
233
+ ret = CRFSUITEERR_OUTOFMEMORY;
234
+ goto error_exit;
235
+ }
236
+
237
+ /* Allocate an array that stores the best weights. */
238
+ lbfgsi.best_w = (floatval_t *)calloc(sizeof(floatval_t), K);
239
+ if (lbfgsi.best_w == NULL)
240
+ {
241
+ ret = CRFSUITEERR_OUTOFMEMORY;
242
+ goto error_exit;
243
+ }
244
+
245
+ /* Read the L-BFGS parameters. */
246
+ exchange_options(params, &opt, -1);
247
+ logging(lg, "Start training with L-BFGS");
248
+
249
+ /* Set parameters for L-BFGS. */
250
+ lbfgsparam.m = opt.memory;
251
+ lbfgsparam.epsilon = opt.epsilon;
252
+ lbfgsparam.past = opt.stop;
253
+ lbfgsparam.delta = opt.delta;
254
+ lbfgsparam.max_iterations = opt.max_iterations;
255
+ if (strcmp(opt.linesearch, "Backtracking") == 0)
256
+ {
257
+ lbfgsparam.linesearch = LBFGS_LINESEARCH_BACKTRACKING;
258
+ }
259
+ else if (strcmp(opt.linesearch, "StrongBacktracking") == 0)
260
+ {
261
+ lbfgsparam.linesearch = LBFGS_LINESEARCH_BACKTRACKING_STRONG_WOLFE;
262
+ }
263
+ else
264
+ {
265
+ lbfgsparam.linesearch = LBFGS_LINESEARCH_MORETHUENTE;
266
+ }
267
+ lbfgsparam.max_linesearch = opt.linesearch_max_iterations;
268
+
269
+ /* Set regularization parameters. */
270
+ if (0 < opt.c1)
271
+ {
272
+ lbfgsparam.orthantwise_c = opt.c1;
273
+ lbfgsparam.linesearch = LBFGS_LINESEARCH_BACKTRACKING;
274
+ }
275
+ else
276
+ {
277
+ lbfgsparam.orthantwise_c = 0;
278
+ }
279
+
280
+ /* Set other callback data. */
281
+ lbfgsi.gm = gm;
282
+ lbfgsi.trainset = trainset;
283
+ lbfgsi.testset = testset;
284
+ lbfgsi.c2 = opt.c2;
285
+ lbfgsi.lg = lg;
286
+
287
+ /* Call the L-BFGS solver. */
288
+ lbfgsi.begin = clock();
289
+ lbret = lbfgs(
290
+ K,
291
+ w,
292
+ NULL,
293
+ lbfgs_evaluate,
294
+ lbfgs_progress,
295
+ &lbfgsi,
296
+ &lbfgsparam);
297
+ if (lbret == LBFGS_CONVERGENCE)
298
+ {
299
+ logging(lg, "Loss has converged, terminating training");
300
+ }
301
+ else if (lbret == LBFGS_STOP)
302
+ {
303
+ logging(lg, "Terminated with the stopping criteria");
304
+ }
305
+ else if (lbret == LBFGSERR_MAXIMUMITERATION)
306
+ {
307
+ logging(lg, "Reached maximum number of iterations. terminating training");
308
+ }
309
+
310
+ /* Set the best_w array (allocated by us) as the result array, which the
311
+ * callee can safely `free`. */
312
+ *ptr_w = lbfgsi.best_w;
313
+
314
+ /* Exit with success. */
315
+ lbfgs_free(w);
316
+ return 0;
317
+
318
+ error_exit:
319
+ free(lbfgsi.best_w);
320
+ lbfgs_free(w);
321
+ *ptr_w = NULL;
322
+ return ret;
323
+ }