fill_mask
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline
¶
Bases: Pipeline
Masked language modeling prediction pipeline using any ModelWithLMHead
.
See the masked language modeling
examples for more information.
Example
>>> from mindnlp.transformers import pipeline
...
>>> fill_masker = pipeline(model="google-bert/bert-base-uncased")
>>> fill_masker("This is a simple [MASK].")
[{'score': 0.042, 'token': 3291, 'token_str': 'problem',
'sequence': 'this is a simple problem.'},
{'score': 0.031, 'token': 3160, 'token_str': 'question',
'sequence': 'this is a simple question.'},
{'score': 0.03, 'token': 8522, 'token_str': 'equation',
'sequence': 'this is a simple equation.'},
{'score': 0.027, 'token': 2028, 'token_str': 'one', 'sequence': 'this is a simple one.'},
{'score': 0.024, 'token': 3627, 'token_str': 'rule', 'sequence': 'this is a simple rule.'}]
Learn more about the basics of using a pipeline in the pipeline tutorial
This mask filling pipeline can currently be loaded from [pipeline
]
using the following task identifier:
"fill-mask"
.
The models that this pipeline can use are models that have been trained with a masked language modeling objective, which includes the bi-directional models in the library. See the up-to-date list of available models on huggingface.co/models.
This pipeline only works for inputs with exactly one token masked. Experimental: We added support for multiple masks. The returned values are raw model output, and correspond to disjoint probabilities where one might expect joint probabilities (See discussion).
This pipeline now supports tokenizer_kwargs.
Example
>>> from mindnlp.transformers import pipeline
...
>>> fill_masker = pipeline(model="google-bert/bert-base-uncased")
>>> tokenizer_kwargs = {"truncation": True}
>>> fill_masker(
... "This is a simple [MASK]. " + "...with a large amount of repeated text appended. " * 100,
... tokenizer_kwargs=tokenizer_kwargs,
... )
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 418 419 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.__call__(inputs, *args, **kwargs)
¶
Fill the masked token in the text(s) given as inputs.
PARAMETER | DESCRIPTION |
---|---|
args |
One or several texts (or one list of prompts) with masked tokens.
TYPE:
|
targets |
When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower).
TYPE:
|
top_k |
When passed, overrides the number of predictions to return.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
A list or a list of list of
|
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 418 419 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.ensure_exactly_one_mask_token(model_inputs)
¶
Ensure that there is exactly one mask token in the input tensor(s) provided to the FillMaskPipeline.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the FillMaskPipeline class.
|
model_inputs |
The input tensor(s) to the model. It can be either a single tensor or a list of tensors. Each tensor should have an 'input_ids' field.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method iterates through the input tensor(s) and checks if there is exactly one mask token present. If the 'model_inputs' parameter is a list, it iterates through each tensor in the list and ensures that the first 'input_ids' tensor has exactly one mask token. If 'model_inputs' is not a list, it assumes it is a single tensor and checks each 'input_ids' tensor to ensure that it has exactly one mask token.
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.get_masked_index(input_ids)
¶
This method returns the indices of the masked tokens in the input tensor.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the FillMaskPipeline class.
TYPE:
|
input_ids |
The input tensor containing token IDs. It should be compatible with the operations performed by the method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
np.ndarray: An array of indices representing the positions of the masked tokens in the input tensor. |
Source code in mindnlp/transformers/pipelines/fill_mask.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.get_target_ids(targets, top_k=None)
¶
Returns a list of target token IDs from the model vocabulary for the specified targets.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the FillMaskPipeline class.
TYPE:
|
targets |
A string or a list of strings representing the target tokens.
TYPE:
|
top_k |
The maximum number of target IDs to return. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
target_ids
|
A list of unique target token IDs from the model vocabulary.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no target is provided. |
Any Exception
|
If an error occurs while retrieving the model vocabulary. |
Note
- If a single target string is passed, it will be converted into a list containing that string.
- If a target token does not exist in the model vocabulary, it will be replaced with a meaningful token if possible.
- If a target token does not exist in the model vocabulary and cannot be replaced, it will be ignored and a warning will be logged.
Example
>>> pipeline = FillMaskPipeline()
>>> targets = ['apple', 'banana', 'orange']
>>> result = pipeline.get_target_ids(targets, top_k=2)
>>> print(result)
[135, 742]
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.postprocess(model_outputs, top_k=5, target_ids=None)
¶
This method takes 4 parameters: self, model_outputs, top_k, target_ids.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the FillMaskPipeline class.
TYPE:
|
model_outputs |
The dictionary containing the model outputs, including 'input_ids' and 'logits'.
TYPE:
|
top_k |
The maximum number of top predictions to consider. Defaults to 5.
TYPE:
|
target_ids |
The tensor containing the target token IDs. - If provided, only predictions for these target token IDs will be considered. - If not provided, all token IDs will be considered.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If target_ids is provided and its shape is less than top_k. |
IndexError
|
If masked_index is out of range. |
TypeError
|
If the input types are not as expected. |
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 |
|
mindnlp.transformers.pipelines.fill_mask.FillMaskPipeline.preprocess(inputs, return_tensors=None, tokenizer_kwargs=None, **preprocess_parameters)
¶
This method preprocesses the inputs using the tokenizer and returns the preprocessed model inputs.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the FillMaskPipeline class.
|
inputs |
The input data to be preprocessed.
|
return_tensors |
(Optional) Specifies the desired format of the returned tensors. Default is 'ms'. Allowed values are 'ms' (for model-specific tensors) or 'pt' (for PyTorch tensors).
DEFAULT:
|
tokenizer_kwargs |
(Optional) Additional keyword arguments to be passed to the tokenizer.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, GenericTensor]
|
Dict[str, GenericTensor]: A dictionary containing the preprocessed model inputs, with keys representing the input types and values representing the corresponding GenericTensor objects. |
Source code in mindnlp/transformers/pipelines/fill_mask.py
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 |
|