chaine 3.13.1__cp312-cp312-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-312-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,237 @@
1
+ /*
2
+ * Online training with averaged perceptron.
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
+ /**
50
+ * Training parameters (configurable with crfsuite_params_t interface).
51
+ */
52
+ typedef struct
53
+ {
54
+ int max_iterations;
55
+ floatval_t epsilon;
56
+ } training_option_t;
57
+
58
+ /**
59
+ * Internal data structure for updating (averaging) feature weights.
60
+ */
61
+ typedef struct
62
+ {
63
+ floatval_t *w;
64
+ floatval_t *ws;
65
+ floatval_t c;
66
+ floatval_t cs;
67
+ } update_data;
68
+
69
+ static void update_weights(void *instance, int fid, floatval_t value)
70
+ {
71
+ update_data *ud = (update_data *)instance;
72
+ ud->w[fid] += ud->c * value;
73
+ ud->ws[fid] += ud->cs * value;
74
+ }
75
+
76
+ static int diff(int *x, int *y, int n)
77
+ {
78
+ int i, d = 0;
79
+ for (i = 0; i < n; ++i)
80
+ {
81
+ if (x[i] != y[i])
82
+ {
83
+ ++d;
84
+ }
85
+ }
86
+ return d;
87
+ }
88
+
89
+ static int exchange_options(crfsuite_params_t *params, training_option_t *opt, int mode)
90
+ {
91
+ BEGIN_PARAM_MAP(params, mode)
92
+ DDX_PARAM_INT(
93
+ "max_iterations", opt->max_iterations, 100,
94
+ "The maximum number of iterations.")
95
+ DDX_PARAM_FLOAT(
96
+ "epsilon", opt->epsilon, 0.,
97
+ "The stopping criterion (the ratio of incorrect label predictions).")
98
+ END_PARAM_MAP()
99
+
100
+ return 0;
101
+ }
102
+
103
+ void crfsuite_train_averaged_perceptron_init(crfsuite_params_t *params)
104
+ {
105
+ exchange_options(params, NULL, 0);
106
+ }
107
+
108
+ int crfsuite_train_averaged_perceptron(
109
+ encoder_t *gm,
110
+ dataset_t *trainset,
111
+ dataset_t *testset,
112
+ crfsuite_params_t *params,
113
+ logging_t *lg,
114
+ floatval_t **ptr_w)
115
+ {
116
+ int n, i, c, ret = 0;
117
+ int *viterbi = NULL;
118
+ floatval_t *w = NULL;
119
+ floatval_t *ws = NULL;
120
+ floatval_t *wa = NULL;
121
+ const int N = trainset->num_instances;
122
+ const int K = gm->num_features;
123
+ const int T = gm->cap_items;
124
+ training_option_t opt;
125
+ update_data ud;
126
+ clock_t begin = clock();
127
+
128
+ /* Initialize the variable. */
129
+ memset(&ud, 0, sizeof(ud));
130
+
131
+ /* Obtain parameter values. */
132
+ exchange_options(params, &opt, -1);
133
+
134
+ /* Allocate arrays. */
135
+ w = (floatval_t *)calloc(sizeof(floatval_t), K);
136
+ ws = (floatval_t *)calloc(sizeof(floatval_t), K);
137
+ wa = (floatval_t *)calloc(sizeof(floatval_t), K);
138
+ viterbi = (int *)calloc(sizeof(int), T);
139
+ if (w == NULL || ws == NULL || wa == NULL || viterbi == NULL)
140
+ {
141
+ ret = CRFSUITEERR_OUTOFMEMORY;
142
+ goto error_exit;
143
+ }
144
+
145
+ /* Show the parameters. */
146
+ logging(lg, "Start training with AP");
147
+
148
+ c = 1;
149
+ ud.w = w;
150
+ ud.ws = ws;
151
+
152
+ /* Loop for epoch. */
153
+ for (i = 0; i < opt.max_iterations; ++i)
154
+ {
155
+ floatval_t norm = 0., loss = 0.;
156
+ clock_t iteration_begin = clock();
157
+
158
+ /* Shuffle the instances. */
159
+ dataset_shuffle(trainset);
160
+
161
+ /* Loop for each instance. */
162
+ for (n = 0; n < N; ++n)
163
+ {
164
+ int d = 0;
165
+ floatval_t score;
166
+ const crfsuite_instance_t *inst = dataset_get(trainset, n);
167
+
168
+ /* Set the feature weights to the encoder. */
169
+ gm->set_weights(gm, w, 1.);
170
+ gm->set_instance(gm, inst);
171
+
172
+ /* Tag the sequence with the current model. */
173
+ gm->viterbi(gm, viterbi, &score);
174
+
175
+ /* Compute the number of different labels. */
176
+ d = diff(inst->labels, viterbi, inst->num_items);
177
+ if (0 < d)
178
+ {
179
+ /*
180
+ For every feature k on the correct path:
181
+ w[k] += 1; ws[k] += c;
182
+ */
183
+ ud.c = inst->weight;
184
+ ud.cs = c * inst->weight;
185
+ gm->features_on_path(gm, inst, inst->labels, update_weights, &ud);
186
+
187
+ /*
188
+ For every feature k on the Viterbi path:
189
+ w[k] -= 1; ws[k] -= c;
190
+ */
191
+ ud.c = -inst->weight;
192
+ ud.cs = -c * inst->weight;
193
+ gm->features_on_path(gm, inst, viterbi, update_weights, &ud);
194
+
195
+ /* We define the loss as the ratio of wrongly predicted labels. */
196
+ loss += d / (floatval_t)inst->num_items * inst->weight;
197
+ }
198
+
199
+ ++c;
200
+ }
201
+
202
+ /* Perform averaging to wa. */
203
+ veccopy(wa, w, K);
204
+ vecasub(wa, 1. / c, ws, K);
205
+
206
+ /* Output the progress. */
207
+ logging(lg, "Iteration %d, training loss: %f", i + 1, loss);
208
+
209
+ /* Holdout evaluation if necessary. */
210
+ if (testset != NULL)
211
+ {
212
+ holdout_evaluation(gm, testset, wa, lg);
213
+ }
214
+
215
+ /* Convergence test. */
216
+ if (loss / N < opt.epsilon)
217
+ {
218
+ logging(lg, "Loss has converged, terminating training");
219
+ break;
220
+ }
221
+ }
222
+
223
+ free(viterbi);
224
+ free(ws);
225
+ free(w);
226
+ *ptr_w = wa;
227
+ return ret;
228
+
229
+ error_exit:
230
+ free(viterbi);
231
+ free(wa);
232
+ free(ws);
233
+ free(w);
234
+ *ptr_w = NULL;
235
+
236
+ return ret;
237
+ }