distilbert
mindnlp.transformers.models.distilbert.configuration_distilbert
¶
DistilBERT model configuration
mindnlp.transformers.models.distilbert.configuration_distilbert.DistilBertConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [DistilBertModel
] or a [TFDistilBertModel
]. It
is used to instantiate a DistilBERT 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 DistilBERT
distilbert-base-uncased 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 |
---|---|
vocab_size |
Vocabulary size of the DistilBERT model. Defines the number of different tokens that can be represented by
the
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:
|
sinusoidal_pos_embds |
Whether to use sinusoidal positional embeddings.
TYPE:
|
n_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
n_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
dim |
Dimensionality of the encoder layers and the pooler layer.
TYPE:
|
hidden_dim |
The size of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
TYPE:
|
dropout |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_dropout |
The dropout ratio for the attention probabilities.
TYPE:
|
activation |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
qa_dropout |
The dropout probabilities used in the question answering model [
TYPE:
|
seq_classif_dropout |
The dropout probabilities used in the sequence classification and the multiple choice model
[
TYPE:
|
Example
>>> from transformers import DistilBertConfig, DistilBertModel
...
>>> # Initializing a DistilBERT configuration
>>> configuration = DistilBertConfig()
...
>>> # Initializing a model (with random weights) from the configuration
>>> model = DistilBertModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/distilbert/configuration_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.configuration_distilbert.DistilBertConfig.__init__(vocab_size=30522, max_position_embeddings=512, sinusoidal_pos_embds=False, n_layers=6, n_heads=12, dim=768, hidden_dim=4 * 768, dropout=0.1, attention_dropout=0.1, activation='gelu', initializer_range=0.02, qa_dropout=0.1, seq_classif_dropout=0.2, pad_token_id=0, **kwargs)
¶
Initializes a new instance of the DistilBertConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance that the method is called on.
TYPE:
|
vocab_size |
The size of the vocabulary. Defaults to 30522.
TYPE:
|
max_position_embeddings |
The maximum number of tokens in a sequence. Defaults to 512.
TYPE:
|
sinusoidal_pos_embds |
Whether to use sinusoidal positional embeddings. Defaults to False.
TYPE:
|
n_layers |
The number of layers in the transformer encoder. Defaults to 6.
TYPE:
|
n_heads |
The number of attention heads in each layer. Defaults to 12.
TYPE:
|
dim |
The dimensionality of the encoder layers. Defaults to 768.
TYPE:
|
hidden_dim |
The dimensionality of the hidden layers in the feed-forward network. Defaults to 4 * 768.
TYPE:
|
dropout |
The dropout probability for all fully connected layers. Defaults to 0.1.
TYPE:
|
attention_dropout |
The dropout probability for the attention layers. Defaults to 0.1.
TYPE:
|
activation |
The activation function used in the feed-forward network. Defaults to 'gelu'.
TYPE:
|
initializer_range |
The range of the initializer. Defaults to 0.02.
TYPE:
|
qa_dropout |
The dropout probability for the question answering head. Defaults to 0.1.
TYPE:
|
seq_classif_dropout |
The dropout probability for the sequence classification head. Defaults to 0.2.
TYPE:
|
pad_token_id |
The id of the padding token. Defaults to 0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/distilbert/configuration_distilbert.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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert
¶
MindSpore DistilBERT model
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM
¶
Bases: DistilBertPreTrainedModel
A class representing a DistilBERT model for Masked Language Modeling (MLM).
This class inherits from the DistilBertPreTrainedModel class and includes methods for initializing the model, resizing position embeddings, getting and setting output embeddings, and forwarding the model.
ATTRIBUTE | DESCRIPTION |
---|---|
activation |
Activation function used in the model.
TYPE:
|
distilbert |
DistilBERT model.
TYPE:
|
vocab_transform |
Dense layer for transforming the vocabulary.
TYPE:
|
vocab_layer_norm |
Layer normalization for the vocabulary.
TYPE:
|
vocab_projector |
Dense layer for projecting the vocabulary.
TYPE:
|
mlm_loss_fct |
Cross-entropy loss function for MLM.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the DistilBertForMaskedLM model. |
get_position_embeddings |
Returns the position embeddings. |
resize_position_embeddings |
Resizes position embeddings of the model. |
get_output_embeddings |
Returns the output embeddings. |
set_output_embeddings |
Sets the output embeddings. |
forward |
Constructs the DistilBertForMaskedLM model. |
Please see the documentation for the DistilBertPreTrainedModel class for more information on inherited attributes and methods.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 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 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.__init__(config)
¶
Initializes a new instance of DistilBertForMaskedLM.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration for the pretrained model. It contains the model's architecture and hyperparameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided 'config' parameter is not of type PretrainedConfig. |
ValueError
|
If the configuration contains invalid values or is incompatible with the model. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the masked language modeling loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.get_output_embeddings()
¶
Retrieves the output embeddings of the DistilBertForMaskedLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertForMaskedLM class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Module
|
nn.Module: The output embeddings of the DistilBertForMaskedLM model. The embeddings are projected using the vocab_projector. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1025 1026 1027 1028 1029 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embedding matrix. If position embeddings are learned, increasing the size will add newly initialized vectors at the end, whereas reducing the size will remove vectors from the end. If position embeddings are not learned (e.g. sinusoidal position embeddings), increasing the size will add correct vectors at the end following the position encoding algorithm, whereas reducing the size will remove vectors from the end.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMaskedLM.set_output_embeddings(new_embeddings)
¶
This method sets the output embeddings for the DistilBertForMaskedLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertForMaskedLM class.
TYPE:
|
new_embeddings |
The new embeddings to set as the output embeddings for the model. It should be an instance of nn.Module representing the new embeddings to be used.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMultipleChoice
¶
Bases: DistilBertPreTrainedModel
DistilBertForMultipleChoice is a class that represents a DistilBERT model for multiple choice tasks. It is a subclass of DistilBertPreTrainedModel.
PARAMETER | DESCRIPTION |
---|---|
config |
The configuration class that defines the model architecture and parameters.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
get_position_embeddings |
Returns the position embeddings. |
resize_position_embeddings |
Resizes position embeddings of the model if Args:
|
forward |
This method is used to compute the outputs of the model for multiple choice tasks. Args:
Returns: If
|
Example
>>> # importing the required libraries
>>> from transformers import AutoTokenizer, DistilBertForMultipleChoice
>>> import torch
...
>>> # loading the tokenizer and model
>>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased")
>>> model = DistilBertForMultipleChoice.from_pretrained("distilbert-base-cased")
...
>>> # input parameters
>>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
>>> choice0 = "It is eaten with a fork and a knife."
>>> choice1 = "It is eaten while held in the hand."
>>> labels = mindspore.Tensor(0).unsqueeze(0) # choice0 is correct (according to Wikipedia ;)), batch size 1
...
>>> # encoding the prompts and choices
>>> encoding = tokenizer([[prompt, choice0], [prompt, choice1]], return_tensors="pt", padding=True)
>>> outputs = model(**{k: v.unsqueeze(0) for k, v in encoding.items()}, labels=labels) # batch size is 1
...
>>> # calculating the loss and logits
>>> loss = outputs.loss
>>> logits = outputs.logits
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMultipleChoice.__init__(config)
¶
init
Initializes a new instance of the DistilBertForMultipleChoice class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of PretrainedConfig class containing the configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided config parameter is not an instance of PretrainedConfig. |
ValueError
|
If the config parameter does not contain valid configuration parameters. |
RuntimeError
|
If an error occurs during the initialization process. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMultipleChoice.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the multiple choice classification loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[MultipleChoiceModelOutput, Tuple[Tensor, ...]]
|
Union[MultipleChoiceModelOutput, Tuple[mindspore.Tensor, ...]] |
Example
>>> from transformers import AutoTokenizer, DistilBertForMultipleChoice
>>> import torch
...
>>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased")
>>> model = DistilBertForMultipleChoice.from_pretrained("distilbert-base-cased")
...
>>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
>>> choice0 = "It is eaten with a fork and a knife."
>>> choice1 = "It is eaten while held in the hand."
>>> labels = mindspore.Tensor(0).unsqueeze(0) # choice0 is correct (according to Wikipedia ;)), batch size 1
...
>>> encoding = tokenizer([[prompt, choice0], [prompt, choice1]], return_tensors="pt", padding=True)
>>> outputs = model(**{k: v.unsqueeze(0) for k, v in encoding.items()}, labels=labels) # batch size is 1
...
>>> # the linear classifier still needs to be trained
>>> loss = outputs.loss
>>> logits = outputs.logits
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMultipleChoice.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1650 1651 1652 1653 1654 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForMultipleChoice.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForQuestionAnswering
¶
Bases: DistilBertPreTrainedModel
DistilBertForQuestionAnswering is a class that represents a fine-tuned DistilBERT model for question answering tasks. It is designed to provide predictions for the start and end positions of the answer span given a question and a context.
Please note that this class assumes the existence of a DistilBERT model and a dense layer for question answering outputs (qa_outputs), which are initialized in the init method.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForQuestionAnswering.__init__(config)
¶
Initializes a new instance of the DistilBertForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object for the pretrained model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of labels in the configuration is not equal to 2, a ValueError is raised. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForQuestionAnswering.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
start_positions |
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 (
TYPE:
|
end_positions |
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 (
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForQuestionAnswering.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1321 1322 1323 1324 1325 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForQuestionAnswering.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embedding matrix.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification
¶
Bases: DistilBertPreTrainedModel
DistilBertForSequenceClassification is a class for sequence classification tasks based on the DistilBert architecture. This class inherits from the DistilBertPreTrainedModel class and provides a sequence classification layer on top of the DistilBert encoder.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for the sequence classification task.
TYPE:
|
config |
The configuration object for the model.
TYPE:
|
distilbert |
The DistilBert encoder model.
TYPE:
|
pre_classifier |
A fully connected layer for the classification task.
TYPE:
|
classifier |
A fully connected layer for the classification task.
TYPE:
|
dropout |
A dropout layer to prevent overfitting.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
get_position_embeddings |
Returns the position embeddings. |
resize_position_embeddings |
Resizes the position embeddings of the model. |
forward |
Constructs the model for sequence classification. Args:
Returns:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification.__init__(config)
¶
Initialize a DistilBertForSequenceClassification model.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance itself.
|
config |
The configuration object containing various parameters for the model. It specifies the model architecture, hyperparameters, and other settings. Must be an instance of PretrainedConfig.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided config parameter is not an instance of PretrainedConfig. |
ValueError
|
If any of the required attributes in the config object are missing or invalid. |
RuntimeError
|
If there are issues during model initialization or attribute assignment. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1196 1197 1198 1199 1200 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embedding matrix.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification
¶
Bases: DistilBertPreTrainedModel
Represents a DistilBERT model for token classification.
This class inherits from DistilBertPreTrainedModel and implements methods for initializing the model, retrieving position embeddings, resizing position embeddings, and forwarding the model for token classification tasks.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration for the pretrained model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the DistilBertForTokenClassification model. |
get_position_embeddings |
Returns the position embeddings. |
resize_position_embeddings |
Resizes position embeddings of the model. |
forward |
Constructs the model for token classification tasks. |
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
If the method is not implemented. |
Note
This class is intended to be subclassed when implementing a custom DistilBERT model for token classification tasks.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification.__init__(config)
¶
Initializes a new instance of the DistilBertForTokenClassification
class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the token classification loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1463 1464 1465 1466 1467 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertForTokenClassification.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embedding matrix.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel
¶
Bases: DistilBertPreTrainedModel
Represents a DistilBERT model for natural language processing tasks.
This class inherits from DistilBertPreTrainedModel and implements various methods for handling position embeddings, input embeddings, head pruning, and model forwardion. The class provides functionality to initialize the model, resize position embeddings, get position embeddings, resize input embeddings, set input embeddings, prune heads, and forward the model for inference.
METHOD | DESCRIPTION |
---|---|
get_position_embeddings |
Returns the position embeddings. |
resize_position_embeddings |
Resizes position embeddings of the model. |
get_input_embeddings |
Returns the input embeddings. |
set_input_embeddings |
Sets new input embeddings. |
_prune_heads |
Prunes heads of the model based on the provided dictionary. |
forward |
Constructs the model for inference with optional parameters. |
Note
This class assumes familiarity with the DistilBERT model architecture and its specific components.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.__init__(config)
¶
Initializes a new instance of the DistilBertModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertModel class.
|
config |
An instance of the PretrainedConfig class containing the configuration settings for the model. This parameter is required to configure the model's embeddings and transformer components.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs a DistilBertModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertModel class.
TYPE:
|
input_ids |
Input tensor containing the indices of input sequence tokens. Default is None.
TYPE:
|
attention_mask |
Mask tensor indicating which tokens should be attended to. Default is None.
TYPE:
|
head_mask |
Mask tensor indicating which heads should be masked out. Default is None.
TYPE:
|
inputs_embeds |
Input tensor containing the embedded representation of input sequence tokens. Default is None.
TYPE:
|
output_attentions |
Whether to return attention weights. Default is None.
TYPE:
|
output_hidden_states |
Whether to return hidden states. Default is None.
TYPE:
|
return_dict |
Whether to return a BaseModelOutput instead of a tuple. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[BaseModelOutput, Tuple[Tensor, ...]]
|
Union[BaseModelOutput, Tuple[mindspore.Tensor, ...]]: The output of the DistilBertModel.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both |
ValueError
|
If neither |
Exception
|
Any other exception that may occur during execution. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.get_input_embeddings()
¶
Retrieve the input embeddings for the DistilBertModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the DistilBertModel class. Represents the current instance of the DistilBertModel. This parameter is required for accessing the embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Embedding
|
nn.Embedding: An instance of the nn.Embedding class. Represents the word embeddings used for input to the DistilBertModel. These embeddings are used to convert input tokens to dense vectors for processing. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.get_position_embeddings()
¶
Returns the position embeddings
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
792 793 794 795 796 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings of the model if new_num_position_embeddings != config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embedding matrix.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertModel.set_input_embeddings(new_embeddings)
¶
Sets the input embeddings for the DistilBertModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertModel class.
TYPE:
|
new_embeddings |
The new embeddings to be set for the input.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method allows setting new embeddings for the input in the DistilBertModel. The 'self' parameter refers to the instance of the DistilBertModel class on which the method is being called. The 'new_embeddings' parameter is of type 'nn.Embedding' and represents the new embeddings to be set for the input.
Example
>>> model = DistilBertModel()
>>> embeddings = nn.Embedding(vocab_size, embedding_dim)
>>> model.set_input_embeddings(embeddings)
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.DistilBertPreTrainedModel
¶
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/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Embeddings
¶
Bases: Module
Represents a neural network cell for embedding tokens with position embeddings.
This class inherits from the nn.Module module and provides methods for initializing and forwarding embeddings for token sequences. The embedding process includes the use of word embeddings and optional pre-computed word embeddings, along with position embeddings.
The 'Embeddings' class initializes the word embeddings, position embeddings, LayerNorm, and dropout, and forwards the embedded tokens by combining input embeddings with position embeddings. Additionally, it provides a method for forwarding embedded tokens with the option to pass pre-computed word embeddings.
The 'forward' method takes input token ids and, if provided, pre-computed word embeddings to return the embedded tokens with position embeddings. The resulting embedded tokens have the shape (bs, max_seq_length, dim), where 'bs' represents the batch size, 'max_seq_length' represents the maximum sequence length, and 'dim' represents the dimensionality of the embeddings. No token_type embeddings are included in the output.
PARAMETER | DESCRIPTION |
---|---|
input_ids |
The token ids to embed. It should be a mindspore.Tensor with the shape (bs, max_seq_length).
TYPE:
|
input_embeds |
The pre-computed word embeddings. It can only be passed if the input ids are
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
mindspore.Tensor: The embedded tokens (plus position embeddings, no token_type embeddings) with the shape (bs, max_seq_length, dim). |
Note
The 'Embeddings' class requires the 'config' parameter of type 'PretrainedConfig' during initialization to configure the embeddings.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Embeddings.__init__(config)
¶
Initializes an instance of the Embeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing the parameters for the embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Embeddings.forward(input_ids, input_embeds=None)
¶
PARAMETER | DESCRIPTION |
---|---|
input_ids |
mindspore.Tensor(bs, max_seq_length) The token ids to embed.
TYPE:
|
input_embeds |
The pre-computed word embeddings. Can only be passed if the input ids are
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor(bs, max_seq_length, dim): The embedded tokens (plus position embeddings, no token_type embeddings) |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.FFN
¶
Bases: Module
A class that represents a Feed-Forward Network (FFN) implemented as a neural network cell.
The FFN class inherits from the nn.Module class of the MindSpore framework. It is designed to process input tensors using a two-layer fully connected feed-forward network. The class implements the forward pass of the network, applying linear transformations, activation functions, and dropout regularization.
ATTRIBUTE | DESCRIPTION |
---|---|
dropout |
The dropout regularization layer.
TYPE:
|
chunk_size_feed_forward |
The size of chunks used in the forward pass.
TYPE:
|
seq_len_dim |
The dimension corresponding to the sequence length in the input tensor.
TYPE:
|
lin1 |
The first fully connected layer of the FFN.
TYPE:
|
lin2 |
The second fully connected layer of the FFN.
TYPE:
|
activation |
The activation function used in the network.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Applies the forward pass of the FFN to the input tensor. |
ff_chunk |
Performs a chunk of the forward pass on the input tensor. |
Note
This class assumes that the input tensor has shape (batch_size, sequence_length, dim).
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.FFN.__init__(config)
¶
Initializes an instance of the FFN (Feed Forward Network) class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration object containing various settings for the FFN. The object should have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.FFN.ff_chunk(input)
¶
Performs a forward pass through the feedforward network (FFN) chunk.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the FFN class.
TYPE:
|
input |
The input tensor to the FFN chunk.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output tensor after passing through the FFN chunk. |
This method applies a series of operations to the input tensor to perform a forward pass through the FFN chunk. The operations include linear transformation, activation function, linear transformation, and dropout. The resulting tensor is then returned as the output of the FFN chunk.
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.FFN.forward(input)
¶
Constructs the feedforward network.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the FFN class.
TYPE:
|
input |
The input tensor of shape [batch_size, sequence_length, hidden_size].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output tensor of shape [batch_size, sequence_length, hidden_size]. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If input is not an instance of the mindspore.Tensor class. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.MultiHeadSelfAttention
¶
Bases: Module
This class represents a multi-head self-attention mechanism used in neural networks. It is designed to be used as a building block for Transformer-based models. The class inherits from the nn.Module class and implements methods for initializing the attention mechanism, pruning heads, and forwarding the attention weights and contextualized layer.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object containing the parameters for the attention mechanism.
TYPE:
|
n_heads |
The number of attention heads.
TYPE:
|
dim |
The dimensionality of the attention mechanism.
TYPE:
|
dropout |
The dropout layer applied to the attention weights.
TYPE:
|
is_causal |
Indicates whether the attention mechanism is causal or not.
TYPE:
|
q_lin |
Linear layer for query projection.
TYPE:
|
k_lin |
Linear layer for key projection.
TYPE:
|
v_lin |
Linear layer for value projection.
TYPE:
|
out_lin |
Linear layer for the output projection.
TYPE:
|
pruned_heads |
Set of pruned attention heads.
TYPE:
|
attention_head_size |
The size of each attention head.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MultiHeadSelfAttention object with the provided configuration. |
prune_heads |
Prunes the specified attention heads from the attention mechanism. |
forward |
Constructs the attention weights and contextualized layer. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.MultiHeadSelfAttention.__init__(config)
¶
Initializes the MultiHeadSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MultiHeadSelfAttention class.
|
config |
The configuration object used for initializing the MultiHeadSelfAttention. It contains parameters such as number of heads (n_heads), dimension (dim), and attention dropout (attention_dropout).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of heads (n_heads) specified in the config does not evenly divide the dimension (dim). |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.MultiHeadSelfAttention.forward(query, key, value, mask, head_mask=None, output_attentions=False)
¶
PARAMETER | DESCRIPTION |
---|---|
query |
mindspore.Tensor(bs, seq_length, dim)
TYPE:
|
key |
mindspore.Tensor(bs, seq_length, dim)
TYPE:
|
value |
mindspore.Tensor(bs, seq_length, dim)
TYPE:
|
mask |
mindspore.Tensor(bs, seq_length)
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
weights
|
mindspore.Tensor(bs, n_heads, seq_length, seq_length)
TYPE:
|
...
|
Attention weights context: mindspore.Tensor(bs, seq_length, dim) Contextualized layer.
Optional: only if |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.MultiHeadSelfAttention.prune_heads(heads)
¶
This method 'prune_heads' is defined within the 'MultiHeadSelfAttention' class and is used to prune specific attention heads in the multi-head self-attention mechanism.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MultiHeadSelfAttention class.
|
heads |
A list of integers representing the attention heads to be pruned. It identifies the specific attention heads to be removed from the attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return a value as it operates directly on the instance attributes. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Transformer
¶
Bases: Module
This class represents a Transformer model, which is a type of neural network architecture commonly used in natural language processing tasks.
The Transformer class initializes with a configuration object that contains information about the model's architecture. It creates a list of TransformerBlock instances, one for each layer specified in the configuration. The number of layers is stored in the 'n_layers' attribute.
The 'forward' method takes as input an embedded input sequence tensor, 'x', and optionally an attention mask tensor, 'attn_mask'. It returns the hidden state tensor of the last layer, 'hidden_state', along with additional outputs depending on the specified options. If 'output_hidden_states' is True, it also returns a tuple containing the hidden states from each layer, 'all_hidden_states'. If 'output_attentions' is True, it also returns a tuple containing the attention weights from each layer, 'all_attentions'. The method also supports returning the outputs as a 'BaseModelOutput' object if the 'return_dict' flag is set to True.
Note
The 'forward' method internally iterates through each layer of the Transformer model and applies the layer module to the input tensor. It accumulates the hidden states and attention weights if the corresponding output options are enabled.
PARAMETER | DESCRIPTION |
---|---|
x |
A tensor of shape (bs, seq_length, dim) representing the embedded input sequence.
|
attn_mask |
An optional tensor of shape (bs, seq_length) representing the attention mask on the sequence.
|
RETURNS | DESCRIPTION |
---|---|
hidden_state
|
A tensor of shape (bs, seq_length, dim) representing the sequence of hidden states in the last (top) layer. |
all_hidden_states
|
A tuple of tensors, each of shape (bs, seq_length, dim), containing the hidden states from each layer. This is only returned if 'output_hidden_states' is set to True. |
all_attentions
|
A tuple of tensors, each of shape (bs, n_heads, seq_length, seq_length), containing the attention weights from each layer. This is only returned if 'output_attentions' is set to True. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of 'layer_outputs' is not as expected based on the output options. |
Note
The 'forward' method builds the Transformer model by sequentially applying the layer modules to the input tensor. It uses the 'head_mask' tensor for applying head-wise masking during attention operations.
Example
>>> config = PretrainedConfig(n_layers=6)
>>> transformer = Transformer(config)
>>> input_tensor = mindspore.Tensor(bs, seq_length, dim)
>>> output = transformer.forward(input_tensor, attn_mask, head_mask, output_attentions=True, output_hidden_states=True)
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Transformer.__init__(config)
¶
Initializes an instance of the Transformer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Transformer class.
TYPE:
|
config |
A PretrainedConfig object containing configuration parameters for the Transformer. The 'n_layers' attribute of the PretrainedConfig object specifies the number of layers in the Transformer. This parameter is required for setting up the Transformer instance.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.Transformer.forward(x, attn_mask=None, head_mask=None, output_attentions=False, output_hidden_states=False, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
x |
mindspore.Tensor(bs, seq_length, dim) Input sequence embedded.
TYPE:
|
attn_mask |
mindspore.Tensor(bs, seq_length) Attention mask on the sequence.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
hidden_state
|
mindspore.Tensor(bs, seq_length, dim) Sequence of hidden states in the last (top)
TYPE:
|
Union[BaseModelOutput, Tuple[Tensor, ...]]
|
layer all_hidden_states: Tuple[mindspore.Tensor(bs, seq_length, dim)] Tuple of length n_layers with the hidden states from each layer. Optional: only if output_hidden_states=True |
all_attentions
|
Tuple[mindspore.Tensor(bs, n_heads, seq_length, seq_length)] Tuple of length n_layers with the attention weights from each layer Optional: only if output_attentions=True
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.TransformerBlock
¶
Bases: Module
The TransformerBlock class represents a single block of the Transformer architecture. It is used to process the input data and produce contextualized outputs.
This class inherits from the nn.Module class.
METHOD | DESCRIPTION |
---|---|
forward |
Constructs the TransformerBlock using the given input tensors and parameters. Parameters:
Returns: Tuple[mindspore.Tensor, ...]. A tuple containing the following elements:
Raises:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.TransformerBlock.__init__(config)
¶
Initialize a TransformerBlock instance with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TransformerBlock class.
TYPE:
|
config |
The configuration object containing parameters for the TransformerBlock. It must be an instance of PretrainedConfig class. The 'dim' parameter specifies the dimensionality of the input data. The 'n_heads' parameter specifies the number of attention heads to use. 'n_heads' must be a factor of 'dim' for proper division.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the division of 'dim' by 'n_heads' results in a non-zero remainder, indicating that 'n_heads' does not evenly divide 'dim'. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.TransformerBlock.forward(x, attn_mask=None, head_mask=None, output_attentions=False)
¶
PARAMETER | DESCRIPTION |
---|---|
x |
mindspore.Tensor(bs, seq_length, dim)
TYPE:
|
attn_mask |
mindspore.Tensor(bs, seq_length)
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
sa_weights
|
The attention weights
TYPE:
|
ffn_output
|
The output of the transformer block contextualization.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.modeling_distilbert.create_sinusoidal_embeddings(n_pos, dim, out)
¶
Create sinusoidal embeddings for given positions and dimensions.
PARAMETER | DESCRIPTION |
---|---|
n_pos |
The number of positions for which to create embeddings.
TYPE:
|
dim |
The dimension of the embeddings.
TYPE:
|
out |
The output tensor to store the sinusoidal embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/modeling_distilbert.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert
¶
Tokenization classes for DistilBERT.
mindnlp.transformers.models.distilbert.tokenization_distilbert.BasicTokenizer
¶
Constructs a BasicTokenizer that will run basic tokenization (punctuation splitting, lower casing, etc.).
PARAMETER | DESCRIPTION |
---|---|
do_lower_case |
Whether or not to lowercase the input when tokenizing.
TYPE:
|
never_split |
Collection of tokens which will never be split during tokenization. Only has an effect when
TYPE:
|
tokenize_chinese_chars |
Whether or not to tokenize Chinese characters. This should likely be deactivated for Japanese (see this issue).
TYPE:
|
strip_accents |
Whether or not to strip all accents. If this option is not specified, then it will be determined by the
value for
TYPE:
|
do_split_on_punc |
In some instances we want to skip the basic punctuation splitting so that later tokenization can capture the full context of the words, such as contractions.
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.BasicTokenizer.__init__(do_lower_case=True, never_split=None, tokenize_chinese_chars=True, strip_accents=None, do_split_on_punc=True)
¶
Initializes an instance of the BasicTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
do_lower_case |
Specifies whether the tokenizer should convert text to lowercase. Defaults to True.
TYPE:
|
never_split |
A list of tokens that should never be split. Defaults to None.
TYPE:
|
tokenize_chinese_chars |
Specifies whether the tokenizer should tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents |
Specifies whether accents should be stripped from tokens. Defaults to None.
TYPE:
|
do_split_on_punc |
Specifies whether the tokenizer should split tokens on punctuation marks. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.BasicTokenizer.tokenize(text, never_split=None)
¶
Basic Tokenization of a piece of text. For sub-word tokenization, see WordPieceTokenizer.
PARAMETER | DESCRIPTION |
---|---|
never_split |
Kept for backward compatibility purposes. Now implemented directly at the base class level (see
[
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer
¶
Bases: PreTrainedTokenizer
Construct a DistilBERT tokenizer. Based on WordPiece.
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 |
File containing the vocabulary.
TYPE:
|
do_lower_case |
Whether or not to lowercase the input when tokenizing.
TYPE:
|
do_basic_tokenize |
Whether or not to do basic tokenization before WordPiece.
TYPE:
|
never_split |
Collection of tokens which will never be split during tokenization. Only has an effect when
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:
|
sep_token |
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for sequence classification or for a text and a question for question answering. It is also used as the last token of a sequence built with special tokens.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
cls_token |
The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens.
TYPE:
|
mask_token |
The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict.
TYPE:
|
tokenize_chinese_chars |
Whether or not to tokenize Chinese characters. This should likely be deactivated for Japanese (see this issue).
TYPE:
|
strip_accents |
Whether or not to strip all accents. If this option is not specified, then it will be determined by the
value for
TYPE:
|
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 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 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer.do_lower_case
property
¶
Method to get the flag indicating if the tokenizer should convert all text to lower case.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the DistilBertTokenizer class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer.vocab_size
property
¶
Get the vocabulary size of the DistilBertTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the DistilBertTokenizer class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The number of unique tokens in the vocabulary of the tokenizer. |
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer.__init__(vocab_file, do_lower_case=True, do_basic_tokenize=True, never_split=None, unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', tokenize_chinese_chars=True, strip_accents=None, **kwargs)
¶
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_file |
The path to the vocabulary file. If the file does not exist, a ValueError is raised.
TYPE:
|
do_lower_case |
Flag to indicate whether the tokens should be lower-cased. Defaults to True.
TYPE:
|
do_basic_tokenize |
Flag to indicate whether basic tokenization should be performed. Defaults to True.
TYPE:
|
never_split |
List of tokens that should never be split. Defaults to None.
TYPE:
|
unk_token |
The token to be used for unknown words. Defaults to '[UNK]'.
TYPE:
|
sep_token |
The token to be used for separation. Defaults to '[SEP]'.
TYPE:
|
pad_token |
The token to be used for padding. Defaults to '[PAD]'.
TYPE:
|
cls_token |
The token to be used for classification. Defaults to '[CLS]'.
TYPE:
|
mask_token |
The token to be used for masking. Defaults to '[MASK]'.
TYPE:
|
tokenize_chinese_chars |
Flag to indicate whether to tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents |
Flag to indicate whether to strip accents. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the vocabulary file specified by 'vocab_file' does not exist. |
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format:
- single sequence:
[CLS] X [SEP]
- pair of sequences:
[CLS] A [SEP] B [SEP]
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs to which the special tokens will be added.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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.models.distilbert.tokenization_distilbert.DistilBertTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
323 324 325 326 |
|
mindnlp.transformers.models.distilbert.tokenization_distilbert.DistilBertTokenizer.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Create a mask from the two sequences passed to be used in a sequence-pair classification task. A BERT sequence pair mask has the following format:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence | second sequence |
If token_ids_1
is None
, this method only returns the first portion of the mask (0s).
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/distilbert/tokenization_distilbert.py
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 |
|