reformer
mindnlp.transformers.models.reformer.configuration_reformer
¶
Reformer model configuration
mindnlp.transformers.models.reformer.configuration_reformer.ReformerConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [ReformerModel]. It is used to instantiate a
Reformer model according to the specified arguments, defining the model architecture. Instantiating a configuration
with the defaults will yield a similar configuration to that of the ReFormer
google/reformer-crime-and-punishment architecture.
Configuration objects inherit from [PretrainedConfig] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig] for more information.
| PARAMETER | DESCRIPTION |
|---|---|
attention_head_size
|
Dimensionality of the projected key, query and value vectors
TYPE:
|
attn_layers
|
List of attention layer types in ascending order. It can be chosen between a LSHSelfAttention layer
( For more information on LSHSelfAttention layer, see LSH Self Attention. For more information on LocalSelfAttention layer, see Local Self Attention.
TYPE:
|
axial_pos_embds
|
Whether or not to use axial position embeddings. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
axial_norm_std
|
The standard deviation of the normal_initializer for initializing the weight matrices of the axial positional encodings.
TYPE:
|
axial_pos_shape
|
The position dims of the axial position encodings. During training, the product of the position dims has to be equal to the sequence length. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
axial_pos_embds_dim
|
The embedding dims of the axial position encodings. The sum of the embedding dims has to be equal to the hidden size. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
chunk_size_lm_head
|
The chunk size of the final language model feed forward head layer. A chunk size of 0 means that the feed forward layer is not chunked. A chunk size of n means that the feed forward layer processes n < sequence_length embeddings at a time. For more information on feed forward chunking, see How does Feed Forward Chunking work?.
TYPE:
|
eos_token_id
|
The token id for the end-of-sentence token.
TYPE:
|
feed_forward_size
|
Dimensionality of the feed_forward layer in the residual attention block.
TYPE:
|
hash_seed
|
Seed that can be used to make local sensitive hashing in
TYPE:
|
hidden_act
|
The non-linear activation function (function or string) in the feed forward layer in the residual attention
block. If string,
TYPE:
|
hidden_dropout_prob
|
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
hidden_size
|
Dimensionality of the output hidden states of the residual attention blocks.
TYPE:
|
initializer_range
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
is_decoder
|
Whether or not to use a causal mask in addition to the
TYPE:
|
layer_norm_eps
|
The epsilon used by the layer normalization layers.
TYPE:
|
local_chunk_length
|
Length of chunk which attends to itself in
TYPE:
|
local_num_chunks_before
|
Number of previous neighbouring chunks to attend to in
TYPE:
|
local_num_chunks_after
|
Number of following neighbouring chunks to attend to in
TYPE:
|
local_attention_probs_dropout_prob
|
The dropout ratio for the attention probabilities in
TYPE:
|
lsh_attn_chunk_length
|
Length of chunk which attends to itself in
TYPE:
|
lsh_num_chunks_before
|
Number of previous neighbouring chunks to attend to in
TYPE:
|
lsh_num_chunks_after
|
Number of following neighbouring chunks to attend to in
TYPE:
|
lsh_attention_probs_dropout_prob
|
The dropout ratio for the attention probabilities in
TYPE:
|
max_position_embeddings
|
The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
TYPE:
|
num_attention_heads
|
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
num_buckets
|
Number of buckets, the key query vectors can be "hashed into" using the locality sensitive hashing scheme.
Each query key vector is hashed into a hash in
TYPE:
|
num_hashes
|
Number of hashing rounds (e.g., number of random rotations) in Local Sensitive Hashing scheme. The higher
TYPE:
|
pad_token_id
|
The token id for the padding token.
TYPE:
|
vocab_size
|
\
Vocabulary size of the Reformer model. Defines the number of different tokens that can be represented by
the
TYPE:
|
tie_word_embeddings
|
Whether to tie input and output embeddings.
TYPE:
|
use_cache
|
Whether or not the model should return the last key/values attentions (not used by all models).
TYPE:
|
classifier_dropout
|
The dropout ratio for the classification head.
TYPE:
|
Example
>>> from transformers import ReformerConfig, ReformerModel
...
>>> # Initializing a Reformer configuration
>>> configuration = ReformerConfig()
...
>>> # Initializing a Reformer model (with random weights)
>>> model = ReformerModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp\transformers\models\reformer\configuration_reformer.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 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 | |
mindnlp.transformers.models.reformer.configuration_reformer.ReformerConfig.__init__(attention_head_size=64, attn_layers=['local', 'lsh', 'local', 'lsh', 'local', 'lsh'], axial_norm_std=1.0, axial_pos_embds=True, axial_pos_shape=[64, 64], axial_pos_embds_dim=[64, 192], chunk_size_lm_head=0, eos_token_id=2, feed_forward_size=512, hash_seed=None, hidden_act='relu', hidden_dropout_prob=0.05, hidden_size=256, initializer_range=0.02, is_decoder=False, layer_norm_eps=1e-12, local_num_chunks_before=1, local_num_chunks_after=0, local_attention_probs_dropout_prob=0.05, local_attn_chunk_length=64, lsh_attn_chunk_length=64, lsh_attention_probs_dropout_prob=0.0, lsh_num_chunks_before=1, lsh_num_chunks_after=0, max_position_embeddings=4096, num_attention_heads=12, num_buckets=None, num_hashes=1, pad_token_id=0, vocab_size=320, tie_word_embeddings=False, use_cache=True, classifier_dropout=None, **kwargs)
¶
Initializes a new instance of the ReformerConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
attention_head_size
|
The size of each attention head.
TYPE:
|
attn_layers
|
The list of attention layer types to be used.
TYPE:
|
axial_norm_std
|
Standard deviation for axial positional embeddings normalization.
TYPE:
|
axial_pos_embds
|
Whether to use axial positional embeddings.
TYPE:
|
axial_pos_shape
|
The shape of axial positional embeddings.
TYPE:
|
axial_pos_embds_dim
|
The dimensions of axial positional embeddings.
TYPE:
|
chunk_size_lm_head
|
Size of chunk for the language model head.
TYPE:
|
eos_token_id
|
The token ID for the end-of-sequence token.
TYPE:
|
feed_forward_size
|
The size of the feed-forward network.
TYPE:
|
hash_seed
|
The seed for hashing functions.
TYPE:
|
hidden_act
|
The activation function for hidden layers.
TYPE:
|
hidden_dropout_prob
|
The dropout probability for hidden layers.
TYPE:
|
hidden_size
|
The size of the hidden layers.
TYPE:
|
initializer_range
|
The range for weight initialization.
TYPE:
|
is_decoder
|
Whether the model is used as a decoder.
TYPE:
|
layer_norm_eps
|
Epsilon value for layer normalization.
TYPE:
|
local_num_chunks_before
|
Number of local attention chunks before.
TYPE:
|
local_num_chunks_after
|
Number of local attention chunks after.
TYPE:
|
local_attention_probs_dropout_prob
|
Dropout probability for local attention.
TYPE:
|
local_attn_chunk_length
|
Length of chunks for local attention.
TYPE:
|
lsh_attn_chunk_length
|
Length of chunks for LSH attention.
TYPE:
|
lsh_attention_probs_dropout_prob
|
Dropout probability for LSH attention.
TYPE:
|
lsh_num_chunks_before
|
Number of LSH attention chunks before.
TYPE:
|
lsh_num_chunks_after
|
Number of LSH attention chunks after.
TYPE:
|
max_position_embeddings
|
The maximum number of position embeddings.
TYPE:
|
num_attention_heads
|
The number of attention heads.
TYPE:
|
num_buckets
|
The number of buckets for hashing.
TYPE:
|
num_hashes
|
The number of hashes for LSH attention.
TYPE:
|
pad_token_id
|
The token ID for padding.
TYPE:
|
vocab_size
|
The size of the vocabulary.
TYPE:
|
tie_word_embeddings
|
Whether to tie word embeddings.
TYPE:
|
use_cache
|
Whether to cache intermediate values.
TYPE:
|
classifier_dropout
|
Dropout probability for classifier layers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\reformer\configuration_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer
¶
MindSpore REFORMER model.
mindnlp.transformers.models.reformer.modeling_reformer.AxialPositionEmbeddings
¶
Bases: Module
Constructs axial position embeddings. Useful for very long input sequences to save memory and time.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.EfficientAttentionMixin
¶
A few utilities for nn.Modules in Reformer, to be used as a mixin.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.LSHSelfAttention
¶
Bases: Module, EfficientAttentionMixin
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |
mindnlp.transformers.models.reformer.modeling_reformer.PositionEmbeddings
¶
Bases: Module
Constructs conventional position embeddings of shape [max_pos_embeddings, hidden_size].
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
201 202 203 204 205 206 207 208 209 210 211 212 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerClassificationHead
¶
Bases: Module
Head for sentence-level classification tasks.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEmbeddings
¶
Bases: Module
Construct the embeddings from word, position and token_type embeddings.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.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 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM
¶
Bases: ReformerPreTrainedModel
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, labels=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
labels (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Labels for computing the masked language modeling loss. Indices should be in [-100, 0, ...,
config.vocab_size] (see input_ids docstring) Tokens with indices set to -100 are ignored (masked),
the loss is only computed for the tokens with labels
Returns:
This example uses a false checkpoint since we don't have any available pretrained model for the masked language modeling task with the Reformer architecture.
Example:
>>> import torch
>>> from transformers import AutoTokenizer, ReformerForMaskedLM
>>> tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-reformer")
>>> model = ReformerForMaskedLM.from_pretrained("hf-internal-testing/tiny-random-reformer")
>>> # add mask_token
>>> tokenizer.add_special_tokens({"mask_token": "[MASK]"}) # doctest: +IGNORE_RESULT
>>> inputs = tokenizer("The capital of France is [MASK].", return_tensors="ms")
>>> # resize model's embedding matrix
>>> model.resize_token_embeddings(new_num_tokens=model.config.vocab_size + 1) # doctest: +IGNORE_RESULT
>>> with no_grad():
... logits = model(**inputs).logits
>>> # retrieve index of [MASK]
>>> mask_token_index = (inputs.input_ids == tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
>>> predicted_token_id = logits[0, mask_token_index].argmax(axis=-1)
>>> predicted_token = tokenizer.decode(predicted_token_id)
>>> labels = tokenizer("The capital of France is Paris.", return_tensors="ms")["input_ids"]
>>> # mask labels of non-[MASK] tokens
>>> labels = ops.where(
... inputs.input_ids == tokenizer.mask_token_id, labels[:, : inputs["input_ids"].shape[-1]], -100
... )
>>> outputs = model(**inputs, labels=labels)
>>> loss = round(outputs.loss.item(), 2)
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForQuestionAnswering
¶
Bases: ReformerPreTrainedModel
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForQuestionAnswering.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, start_positions=None, end_positions=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
start_positions (mindspore.Tensor of shape (batch_size,), optional):
Labels for position (index) of the start of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
end_positions (mindspore.Tensor of shape (batch_size,), optional):
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForSequenceClassification
¶
Bases: ReformerPreTrainedModel
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForSequenceClassification.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, labels=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [0, ...,
config.num_labels - 1]. If config.num_labels == 1 a regression loss is computed (Mean-Square loss), If
config.num_labels > 1 a classification loss is computed (Cross-Entropy).
Returns:
Example of single-label classification:
>>> import torch
>>> from transformers import AutoTokenizer, ReformerForSequenceClassification
>>> tokenizer = AutoTokenizer.from_pretrained("google/reformer-crime-and-punishment")
>>> model = ReformerForSequenceClassification.from_pretrained("google/reformer-crime-and-punishment")
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="ms")
>>> with no_grad():
... logits = model(**inputs).logits
>>> predicted_class_id = logits.argmax().item()
>>> label = model.config.id2label[predicted_class_id]
>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
>>> num_labels = len(model.config.id2label)
>>> model = ReformerForSequenceClassification.from_pretrained(
... "google/reformer-crime-and-punishment", num_labels=num_labels
... )
>>> labels = mindspore.tensor(1)
>>> loss = model(**inputs, labels=labels).loss
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerLayer
¶
Bases: Module
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel
¶
Bases: ReformerPreTrainedModel
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelOutput
dataclass
¶
Bases: ModelOutput
Output type of [ReformerModel].
| PARAMETER | DESCRIPTION |
|---|---|
last_hidden_state
|
Sequence of hidden-states at the last layer of the model.
TYPE:
|
past_buckets_states
|
List of Contains precomputed buckets and hidden-states that can be used (see
TYPE:
|
hidden_states
|
Tuple of Hidden-states of the model at the output of each layer plus the initial embedding outputs.
TYPE:
|
attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead
¶
Bases: ReformerPreTrainedModel
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, past_buckets_states=None, use_cache=None, output_hidden_states=None, output_attentions=None, return_dict=None, labels=None)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [-100, 0, ...,
config.vocab_size - 1]. All labels set to -100 are ignored (masked), the loss is only computed for
labels in [0, ..., config.vocab_size]
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHeadOutput
dataclass
¶
Bases: ModelOutput
Output type of [ReformerModelWithLMHead].
| PARAMETER | DESCRIPTION |
|---|---|
logits
|
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
TYPE:
|
past_buckets_states
|
List of Contains precomputed buckets and hidden-states that can be used (see
TYPE:
|
hidden_states
|
TTuple of Hidden-states of the model at the output of each layer plus the initial embedding outputs.
TYPE:
|
attentions
|
Tuple of Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TYPE:
|
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerPreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReverseSort
¶
Bases: Cell
After chunked attention is applied which sorted clusters, original ordering has to be restored. Since customized backward function is used for Reformer, the gradients of the output vectors have to be explicitly sorted here.
Source code in mindnlp\transformers\models\reformer\modeling_reformer.py
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 | |
mindnlp.transformers.models.reformer.tokenization_reformer
¶
Tokenization class for model Reformer.
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer
¶
Bases: PreTrainedTokenizer
Construct a Reformer tokenizer. Based on SentencePiece .
This tokenizer inherits from [PreTrainedTokenizer] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
SentencePiece file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
eos_token
|
The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence.
The token used is the
TYPE:
|
unk_token
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
additional_special_tokens
|
Additional special tokens used by the tokenizer.
TYPE:
|
sp_model_kwargs
|
Will be passed to the
TYPE:
|
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.vocab_size
property
¶
Returns the size of the vocabulary used by the ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The size of the vocabulary used by the ReformerTokenizer. |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__getstate__()
¶
Method 'getstate' in the class 'ReformerTokenizer'.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizer class. Represents the current instance of the ReformerTokenizer class. No restrictions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns a dictionary containing the state of the ReformerTokenizer instance with the 'sp_model' key set to None. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__init__(vocab_file, eos_token='</s>', unk_token='<unk>', additional_special_tokens=[], sp_model_kwargs=None, **kwargs)
¶
Initializes a new instance of the ReformerTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizer class.
|
vocab_file
|
Path to the vocabulary file.
TYPE:
|
eos_token
|
The end-of-sentence token. Defaults to ''.
TYPE:
|
unk_token
|
The unknown token. Defaults to '
TYPE:
|
additional_special_tokens
|
Additional special tokens to be added to the vocabulary. Defaults to an empty list.
TYPE:
|
sp_model_kwargs
|
Additional arguments to be passed to the SentencePieceProcessor forwardor. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__setstate__(d)
¶
setstate method in the class ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizer class.
TYPE:
|
d
|
A dictionary containing the state information to be set.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
229 230 231 232 233 234 235 236 237 238 239 240 241 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.get_vocab()
¶
Get the vocabulary of the ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the ReformerTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, int]
|
A dictionary of type Dict[str, int] mapping tokens to their corresponding IDs. The IDs are integers. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary to a specified directory.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizer class.
TYPE:
|
save_directory
|
The directory where the vocabulary will be saved.
TYPE:
|
filename_prefix
|
An optional prefix for the filename. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If the save_directory is not a valid directory. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast
¶
Tokenization class for model Reformer.
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" Reformer tokenizer (backed by HuggingFace's tokenizers library). Based on Unigram.
This tokenizer inherits from [PreTrainedTokenizerFast] which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
SentencePiece file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
eos_token
|
The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence.
The token used is the
TYPE:
|
unk_token
|
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
pad_token
|
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
additional_special_tokens
|
Additional special tokens used by the tokenizer.
TYPE:
|
Source code in mindnlp\transformers\models\reformer\tokenization_reformer_fast.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.can_save_slow_tokenizer: bool
property
¶
Method to check if the slow tokenizer can be saved.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the ReformerTokenizerFast class. This parameter refers to the current instance of the ReformerTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
A boolean value indicating whether the slow tokenizer can be saved. Returns True if the vocab_file exists, otherwise returns False.
TYPE:
|
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, eos_token='</s>', unk_token='<unk>', additional_special_tokens=[], **kwargs)
¶
init
Initializes the ReformerTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
The path to the vocabulary file. If not provided, the tokenizer will use a default vocabulary.
TYPE:
|
tokenizer_file
|
The path to the tokenizer file. If not provided, the tokenizer will use a default tokenizer.
TYPE:
|
eos_token
|
The end-of-sequence token. Defaults to ''.
TYPE:
|
unk_token
|
The unknown token. Defaults to '
TYPE:
|
additional_special_tokens
|
A list of additional special tokens to be added to the vocabulary.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer_fast.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary for a ReformerTokenizerFast instance.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the ReformerTokenizerFast class.
TYPE:
|
save_directory
|
The directory where the vocabulary will be saved.
TYPE:
|
filename_prefix
|
An optional prefix for the filename. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the fast tokenizer does not have the necessary information to save the vocabulary for a slow tokenizer. |
OSError
|
If the specified save_directory is not a valid directory. |
Source code in mindnlp\transformers\models\reformer\tokenization_reformer_fast.py
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 | |