Embeddings
Routines for embeddings.
ClosestToAverage
Bases: BaseEmbeddingsAggregator
The Closest to Average aggregation model chooses the output with the embedding that's closest to the average embedding.
This method takes a DataFrame
containing four columns: task
, worker
, output
, and embedding
.
Here the embedding
is a vector containing a representation of the output
which might be any
type of data such as text, images, NumPy arrays, etc. As a result, the method returns the output which
embedding is the closest one to the average embedding of the task responses.
Source code in crowdkit/aggregation/embeddings/closest_to_average.py
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 |
|
distance = attr.ib()
class-attribute
instance-attribute
A callable that takes two NumPy arrays (the task embedding and the aggregated embedding)
and returns a single float
number: the distance between these two vectors.
embeddings_and_outputs_ = attr.ib(init=False)
class-attribute
instance-attribute
The task embeddings and outputs. The pandas.DataFrame
data is indexed by task
and has the embedding
and output
columns.
scores_ = attr.ib(init=False)
class-attribute
instance-attribute
The task label scores. The pandas.DataFrame
data is indexed by task
so that result.loc[task, label]
is a score of label
for task
.
fit(data, aggregated_embeddings=None, true_embeddings=None)
Fits the model to the training data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
aggregated_embeddings
|
Series
|
The task aggregated embeddings.
The |
None
|
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
ClosestToAverage |
ClosestToAverage
|
self. |
Source code in crowdkit/aggregation/embeddings/closest_to_average.py
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 |
|
fit_predict(data, aggregated_embeddings=None)
Fits the model to the training data and returns the aggregated outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
aggregated_embeddings
|
Series
|
The task aggregated embeddings.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task embeddings and outputs.
The |
Source code in crowdkit/aggregation/embeddings/closest_to_average.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
fit_predict_scores(data, aggregated_embeddings=None)
Fits the model to the training data and returns the estimated scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
aggregated_embeddings
|
Series
|
The task aggregated embeddings.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task label scores.
The |
Source code in crowdkit/aggregation/embeddings/closest_to_average.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
HRRASA
Bases: BaseClassificationAggregator
The Hybrid Reliability and Representation Aware Sequence Aggregation (HRRASA) algorithm consists of four steps.
Step 1. Encode the worker answers into embeddings.
Step 2. Estimate the local workers' reliabilities that represent how well a
worker responds to one particular task. The local reliability of the worker \(k\) on the task \(i\) is
denoted by \(\gamma_i^k\) and is calculated by incorporating both types of representations:
\(\gamma_i^k = \lambda_{emb}\gamma_{i,emb}^k + \lambda_{seq}\gamma_{i,seq}^k, \; \lambda_{emb} + \lambda_{seq} = 1\),
where the \(\gamma_{i,emb}^k\) value is a reliability calculated on embedding
, and the \(\gamma_{i,seq}^k\)
value is a reliability calculated on output
.
The \(\gamma_{i,emb}^k\) value is calculated by the following equation: \(\gamma_{i,emb}^k = \frac{1}{|\mathcal{U}_i| - 1}\sum_{a_i^{k'} \in \mathcal{U}_i, k \neq k'} \exp\left(\frac{\|e_i^k-e_i^{k'}\|^2}{\|e_i^k\|^2\|e_i^{k'}\|^2}\right)\), where \(\mathcal{U_i}\) is a set of workers' responses on task \(i\).
The \(\gamma_{i,seq}^k\) value uses some similarity measure \(sim\) on the output
data,
e.g. GLEU similarity on texts:
\(\gamma_{i,seq}^k = \frac{1}{|\mathcal{U}_i| - 1}\sum_{a_i^{k'} \in \mathcal{U}_i, k \neq k'}sim(a_i^k, a_i^{k'})\).
Step 3. Estimate the global workers' reliabilities \(\beta\) by iteratively performing two steps: 1. For each task, estimate the aggregated embedding: \(\hat{e}_i = \frac{\sum_k \gamma_i^k \beta_k e_i^k}{\sum_k \gamma_i^k \beta_k}\). 2. For each worker, estimate the global reliability: \(\beta_k = \frac{\chi^2_{(\alpha/2, |\mathcal{V}_k|)}}{\sum_i\left(\|e_i^k - \hat{e}_i\|^2/\gamma_i^k\right)}\), where \(\mathcal{V}_k\) is a set of tasks completed by the worker \(k\).
Step 4. Estimate the aggregated result. It is the output which embedding is the closest one to
\(\hat{e}_i\). If calculate_ranks
is true, the method also calculates ranks for each worker response as
\(s_i^k = \beta_k \exp\left(-\frac{\|e_i^k - \hat{e}_i\|^2}{\|e_i^k\|^2\|\hat{e}_i\|^2}\right) + \gamma_i^k\).
Jiyi Li. Crowdsourced Text Sequence Aggregation based on Hybrid Reliability and Representation. In Proceedings of the 43rd International ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR '20), China (July 25–30, 2020), 1761-1764.
https://doi.org/10.1145/3397271.3401239
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from crowdkit.aggregation import HRRASA
>>> df = pd.DataFrame(
>>> [
>>> ['t1', 'p1', 'a', np.array([1.0, 0.0])],
>>> ['t1', 'p2', 'a', np.array([1.0, 0.0])],
>>> ['t1', 'p3', 'b', np.array([0.0, 1.0])]
>>> ],
>>> columns=['task', 'worker', 'output', 'embedding']
>>> )
>>> result = HRRASA().fit_predict(df)
Source code in crowdkit/aggregation/embeddings/hrrasa.py
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
alpha = attr.ib(default=0.05)
class-attribute
instance-attribute
The significance level of the chi-squared distribution quantiles in the \(eta\) parameter formula.
calculate_ranks = attr.ib(default=False)
class-attribute
instance-attribute
Specifies if the additional ranks_
attribute will be calculated (true) or not (false).
embeddings_and_outputs_ = attr.ib(init=False)
class-attribute
instance-attribute
The task embeddings and outputs.
The pandas.DataFrame
data is indexed by task
and has the embedding
and output
columns.
lambda_emb = attr.ib(default=0.5)
class-attribute
instance-attribute
The weight of reliability calculated on embeddings.
lambda_out = attr.ib(default=0.5)
class-attribute
instance-attribute
The weight of reliability calculated on outputs.
loss_history_ = attr.ib(init=False)
class-attribute
instance-attribute
A list of loss values during training.
n_iter = attr.ib(default=100)
class-attribute
instance-attribute
The maximum number of iterations.
tol = attr.ib(default=1e-09)
class-attribute
instance-attribute
The tolerance stopping criterion for iterative methods with a variable number of steps.
The algorithm converges when the loss change is less than the tol
parameter.
fit(data, true_embeddings=None)
Fits the model to the training data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
HRRASA |
HRRASA
|
self. |
Source code in crowdkit/aggregation/embeddings/hrrasa.py
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 |
|
fit_predict(data, true_embeddings=None)
Fits the model to the training data and returns the aggregated outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task embeddings and outputs.
The |
Source code in crowdkit/aggregation/embeddings/hrrasa.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
fit_predict_scores(data, true_embeddings=None)
Fits the model to the training data and returns the estimated scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task label scores.
The |
Source code in crowdkit/aggregation/embeddings/hrrasa.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
RASA
Bases: BaseEmbeddingsAggregator
The Reliability Aware Sequence Aggregation (RASA) algorithm consists of three steps.
Step 1. Encode the worker answers into embeddings.
Step 2. Estimate the global workers' reliabilities \(\beta\) by iteratively performing two steps: 1. For each task, estimate the aggregated embedding: \(\hat{e}_i = \frac{\sum_k \beta_k e_i^k}{\sum_k \beta_k}\) 2. For each worker, estimate the global reliability: \(\beta_k = \frac{\chi^2_{(\alpha/2, |\mathcal{V}_k|)}}{\sum_i\left(\|e_i^k - \hat{e}_i\|^2\right)}\), where \(\mathcal{V}_k\) is a set of tasks completed by the worker \(k\).
Step 3. Estimate the aggregated result. It is the output which embedding is the closest one to \(\hat{e}_i\).
Jiyi Li, Fumiyo Fukumoto. A Dataset of Crowdsourced Word Sequences: Collections and Answer Aggregation for Ground Truth Creation. In Proceedings of the First Workshop on Aggregating and Analysing Crowdsourced Annotations for NLP, Hong Kong, China (November 3, 2019), 24–28. https://doi.org/10.18653/v1/D19-5904
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from crowdkit.aggregation import RASA
>>> df = pd.DataFrame(
>>> [
>>> ['t1', 'p1', 'a', np.array([1.0, 0.0])],
>>> ['t1', 'p2', 'a', np.array([1.0, 0.0])],
>>> ['t1', 'p3', 'b', np.array([0.0, 1.0])]
>>> ],
>>> columns=['task', 'worker', 'output', 'embedding']
>>> )
>>> result = RASA().fit_predict(df)
Source code in crowdkit/aggregation/embeddings/rasa.py
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 |
|
alpha = attr.ib(default=0.05)
class-attribute
instance-attribute
The significance level of the chi-squared distribution quantiles in the \(eta\) parameter formula.
loss_history_ = attr.ib(init=False)
class-attribute
instance-attribute
A list of loss values during training.
n_iter = attr.ib(default=100)
class-attribute
instance-attribute
The maximum number of iterations.
tol = attr.ib(default=1e-09)
class-attribute
instance-attribute
The tolerance stopping criterion for iterative methods with a variable number of steps. The algorithm converges when the loss change is less than the tol
parameter.
fit(data, true_embeddings=None)
Fits the model to the training data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
RASA |
RASA
|
self. |
Source code in crowdkit/aggregation/embeddings/rasa.py
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 |
|
fit_predict(data, true_embeddings=None)
Fits the model to the training data and returns the aggregated outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task embeddings and outputs.
The |
Source code in crowdkit/aggregation/embeddings/rasa.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
fit_predict_scores(data, true_embeddings=None)
Fits the model to the training data and returns the estimated scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
DataFrame
|
The workers' outputs with their embeddings.
The |
required |
true_embeddings
|
Series
|
The embeddings of the true task responses.
The |
None
|
Returns:
Name | Type | Description |
---|---|---|
DataFrame |
DataFrame
|
The task label scores.
The |
Source code in crowdkit/aggregation/embeddings/rasa.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|