bert
mindnlp.transformers.models.bert.configuration_bert.BertConfig
¶
Bases: PretrainedConfig
Configuration for BERT-base
Source code in mindnlp\transformers\models\bert\configuration_bert.py
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 | |
mindnlp.transformers.models.bert.configuration_bert.BertConfig.__init__(vocab_size=30522, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, type_vocab_size=2, initializer_range=0.02, layer_norm_eps=1e-12, pad_token_id=0, position_embedding_type='absolute', use_cache=True, classifier_dropout=None, **kwargs)
¶
Initialize a BertConfig object with the specified parameters.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object instance.
TYPE:
|
vocab_size
|
The size of the vocabulary. Defaults to 30522.
TYPE:
|
hidden_size
|
The size of the hidden layers. Defaults to 768.
TYPE:
|
num_hidden_layers
|
The number of hidden layers. Defaults to 12.
TYPE:
|
num_attention_heads
|
The number of attention heads. Defaults to 12.
TYPE:
|
intermediate_size
|
The size of the intermediate layer in the transformer encoder. Defaults to 3072.
TYPE:
|
hidden_act
|
The activation function for the hidden layers. Defaults to 'gelu'.
TYPE:
|
hidden_dropout_prob
|
The dropout probability for the hidden layers. Defaults to 0.1.
TYPE:
|
attention_probs_dropout_prob
|
The dropout probability for the attention probabilities. Defaults to 0.1.
TYPE:
|
max_position_embeddings
|
The maximum position index. Defaults to 512.
TYPE:
|
type_vocab_size
|
The size of the type vocabulary. Defaults to 2.
TYPE:
|
initializer_range
|
The range for weight initialization. Defaults to 0.02.
TYPE:
|
layer_norm_eps
|
The epsilon value for layer normalization. Defaults to 1e-12.
TYPE:
|
pad_token_id
|
The token ID for padding. Defaults to 0.
TYPE:
|
position_embedding_type
|
The type of position embeddings. Defaults to 'absolute'.
TYPE:
|
use_cache
|
Whether to use cache during inference. Defaults to True.
TYPE:
|
classifier_dropout
|
The dropout probability for the classifier layer. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any of the input parameters are invalid or out of range. |
Source code in mindnlp\transformers\models\bert\configuration_bert.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 | |
mindnlp.transformers.models.bert.modeling_bert
¶
MindSpore BERT model.
mindnlp.transformers.models.bert.modeling_bert.BertEmbeddings
¶
Bases: Module
Construct the embeddings from word, position and token_type embeddings.
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForMaskedLM
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForMaskedLM.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, labels=None, output_attentions=None, output_hidden_states=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 in [0, ..., config.vocab_size]
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForMultipleChoice
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForMultipleChoice.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the multiple choice classification loss. Indices should be in [0, ...,
num_choices-1] where num_choices is the size of the second dimension of the input tensors. (See
input_ids above)
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForNextSentencePrediction
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.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 1284 1285 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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForNextSentencePrediction.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None, **kwargs)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair
(see input_ids docstring). Indices should be in [0, 1]:
- 0 indicates sequence B is a continuation of sequence A,
- 1 indicates sequence B is a random sequence.
Returns:
Example:
>>> from transformers import AutoTokenizer, BertForNextSentencePrediction
>>> import torch
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = BertForNextSentencePrediction.from_pretrained("google-bert/bert-base-uncased")
>>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
>>> next_sentence = "The sky is blue due to the shorter wavelength of blue light."
>>> encoding = tokenizer(prompt, next_sentence, return_tensors="ms")
>>> outputs = model(**encoding, labels=mindspore.Tensor([1]))
>>> logits = outputs.logits
>>> assert logits[0, 0] < logits[0, 1] # next sentence was random
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 1284 1285 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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForPreTraining
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForPreTraining.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, next_sentence_label=None, output_attentions=None, output_hidden_states=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 in `[0, ..., config.vocab_size]`
next_sentence_label (`mindspore.Tensor` of shape `(batch_size,)`, *optional*):
Labels for computing the next sequence prediction (classification) loss. Input should be a sequence
pair (see `input_ids` docstring) Indices should be in `[0, 1]`:
- 0 indicates sequence B is a continuation of sequence A,
- 1 indicates sequence B is a random sequence.
kwargs (`Dict[str, any]`, *optional*, defaults to `{}`):
Used to hide legacy arguments that have been deprecated.
Returns:
Example:
>>> from transformers import AutoTokenizer, BertForPreTraining
>>> import torch
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = BertForPreTraining.from_pretrained("google-bert/bert-base-uncased")
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="ms")
>>> outputs = model(**inputs)
>>> prediction_logits = outputs.prediction_logits
>>> seq_relationship_logits = outputs.seq_relationship_logits
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForPreTrainingOutput
dataclass
¶
Bases: ModelOutput
Output type of [BertForPreTraining].
| PARAMETER | DESCRIPTION |
|---|---|
loss
|
Total loss as the sum of the masked language modeling loss and the next sequence prediction (classification) loss.
TYPE:
|
prediction_logits
|
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
TYPE:
|
seq_relationship_logits
|
Prediction scores of the next sequence prediction (classification) head (scores of True/False continuation before SoftMax).
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\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForQuestionAnswering
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForQuestionAnswering.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=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\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForSequenceClassification
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForSequenceClassification.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=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).
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForTokenClassification
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertForTokenClassification.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
labels (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Labels for computing the token classification loss. Indices should be in [0, ..., config.num_labels - 1].
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertLMHeadModel
¶
Bases: BertPreTrainedModel
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertLMHeadModel.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, labels=None, past_key_values=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
encoder_hidden_states (mindspore.Tensor of shape (batch_size, sequence_length, hidden_size), optional):
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
the model is configured as a decoder.
encoder_attention_mask (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
the cross-attention if the model is configured as a decoder. Mask values selected in [0, 1]:
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.
labels (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Labels for computing the left-to-right language modeling loss (next word prediction). 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 n [0, ..., config.vocab_size]
past_key_values (tuple(tuple(mindspore.Tensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)):
Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
use_cache (bool, optional):
If set to True, past_key_values key value states are returned and can be used to speed up decoding (see
past_key_values).
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertModel
¶
Bases: BertPreTrainedModel
The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of cross-attention is added between the self-attention layers, following the architecture described in Attention is all you need by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.
To behave as an decoder the model needs to be initialized with the is_decoder argument of the configuration set
to True. To be used in a Seq2Seq model, the model needs to initialized with both is_decoder argument and
add_cross_attention set to True; an encoder_hidden_states is then expected as an input to the forward pass.
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertModel.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_values=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
encoder_hidden_states (mindspore.Tensor of shape (batch_size, sequence_length, hidden_size), optional):
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
the model is configured as a decoder.
encoder_attention_mask (mindspore.Tensor of shape (batch_size, sequence_length), optional):
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
the cross-attention if the model is configured as a decoder. Mask values selected in [0, 1]:
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.
past_key_values (tuple(tuple(mindspore.Tensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)):
Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
`decoder_input_ids` of shape `(batch_size, sequence_length)`.
use_cache (bool, optional):
If set to True, past_key_values key value states are returned and can be used to speed up decoding (see
past_key_values).
Source code in mindnlp\transformers\models\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_bert.BertPreTrainedModel
¶
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\bert\modeling_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert
¶
MindNLP bert model
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertAttention
¶
Bases: Module
Bert Attention
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertAttention.__init__(config, causal, init_cache=False)
¶
Initializes an instance of MSBertAttention.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class itself.
|
config
|
The configuration object containing various settings.
TYPE:
|
causal
|
Flag indicating whether the attention mechanism is causal.
TYPE:
|
init_cache
|
Flag indicating whether to initialize cache. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertAttention.forward(hidden_states, attention_mask=None, head_mask=None)
¶
Constructs the attention mechanism for a multi-head self-attention layer in MSBertAttention.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertAttention class.
TYPE:
|
hidden_states
|
The input tensor of shape (batch_size, sequence_length, hidden_size). It represents the sequence of hidden states for each token in the input sequence.
TYPE:
|
attention_mask
|
An optional tensor of shape (batch_size, sequence_length) indicating which tokens should be attended to and which should be ignored. The value 1 indicates to attend to the token, while 0 indicates to ignore it. If not provided, all tokens are attended to.
TYPE:
|
head_mask
|
An optional tensor of shape (num_heads,) or (num_layers, num_heads) indicating which heads or layers to mask. 1 indicates to include the head/layer, while 0 indicates to mask it. If not provided, all heads/layers are included.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Tuple[torch.Tensor]: A tuple containing:
|
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEmbeddings
¶
Bases: Module
Embeddings for BERT, include word, position and token_type
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEmbeddings.__init__(config)
¶
Initializes an instance of the MSBertEmbeddings class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object instance.
|
config
|
An object of the config class containing the configuration parameters for the embeddings.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
This method initializes the MSBertEmbeddings object by setting up the word embeddings, position embeddings, token type embeddings, layer normalization, and dropout. The configuration parameters are used to determine the size of the embeddings and other properties.
- The 'word_embeddings' attribute is an instance of the nn.Embedding class, which represents a lookup table for word embeddings. It takes the vocabulary size (config.vocab_size) and hidden size (config.hidden_size) as arguments.
- The 'position_embeddings' attribute is an instance of the nn.Embedding class, which represents a lookup table for position embeddings. It takes the maximum position embeddings (config.max_position_embeddings) and hidden size (config.hidden_size) as arguments.
- The 'token_type_embeddings' attribute is an instance of the nn.Embedding class, which represents a lookup table for token type embeddings. It takes the token type vocabulary size (config.type_vocab_size) and hidden size (config.hidden_size) as arguments.
- The 'LayerNorm' attribute is an instance of the nn.LayerNorm class, which applies layer normalization to the input embeddings. It takes the hidden size (config.hidden_size) and epsilon (config.layer_norm_eps) as arguments.
- The 'dropout' attribute is an instance of the nn.Dropout class, which applies dropout regularization to the input embeddings. It takes the dropout probability (config.hidden_dropout_prob) as an argument.
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEmbeddings.forward(input_ids, token_type_ids, position_ids)
¶
This method forwards the embeddings for MSBert model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object instance of MSBertEmbeddings class.
TYPE:
|
input_ids
|
The input tensor containing the token ids for the input sequence.
TYPE:
|
token_type_ids
|
The token type ids to distinguish different sentences in the input sequence.
TYPE:
|
position_ids
|
The position ids to indicate the position of each token in the input sequence.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tensor
|
The forwarded embeddings for the input sequence represented as a tensor. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEncoder
¶
Bases: Module
Bert Encoder
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEncoder.__init__(config)
¶
Initializes an instance of the MSBertEncoder class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class itself.
TYPE:
|
config
|
An object containing the configuration parameters for the MSBertEncoder.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertEncoder.forward(hidden_states, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None)
¶
Constructs the MSBertEncoder.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the MSBertEncoder class.
|
hidden_states
|
The input hidden states of the encoder. Shape: (batch_size, sequence_length, hidden_size)
TYPE:
|
attention_mask
|
The attention mask for the input hidden states. If provided, the attention mask should have the same shape as the hidden states. Each element of the mask should be 0 or 1, where 0 indicates the position is padded/invalid and 1 indicates the position is not padded/valid. Defaults to None.
TYPE:
|
head_mask
|
The head mask for the attention mechanism. If provided, the head mask should have the same shape as the number of layers in the encoder. Each element of the mask should be 0 or 1, where 0 indicates the head is masked and 1 indicates the head is not masked. Defaults to None.
TYPE:
|
encoder_hidden_states
|
The hidden states of the encoder. Shape: (batch_size, sequence_length, hidden_size) Defaults to None.
TYPE:
|
encoder_attention_mask
|
The attention mask for the encoder hidden states. If provided, the attention mask should have the same shape as the encoder hidden states. Each element of the mask should be 0 or 1, where 0 indicates the position is padded/invalid and 1 indicates the position is not padded/valid. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
outputs
|
A tuple containing the following elements:
TYPE:
|
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForPretraining
¶
Bases: MSBertPreTrainedModel
Bert For Pretraining
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForPretraining.__init__(config, *args, **kwargs)
¶
init
Initialize the MSBertForPretraining class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertForPretraining class.
|
config
|
The configuration for the MSBertForPretraining, containing various parameters and settings for model initialization. It should be an instance of the configuration class specific to the MSBertForPretraining model.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForPretraining.forward(input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, masked_lm_positions=None)
¶
This method forwards the pretraining model for MSBertForPretraining.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertForPretraining class.
TYPE:
|
input_ids
|
The input tensor containing the token ids.
TYPE:
|
attention_mask
|
A tensor representing the attention mask. Default is None.
TYPE:
|
token_type_ids
|
A tensor representing the token type ids. Default is None.
TYPE:
|
position_ids
|
A tensor representing the position ids. Default is None.
TYPE:
|
head_mask
|
A tensor representing the head mask. Default is None.
TYPE:
|
masked_lm_positions
|
A list of integer positions of masked language model tokens.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Tuple[Tensor, Tensor]: A tuple containing the prediction scores and sequence relationship score. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForSequenceClassification
¶
Bases: MSBertPreTrainedModel
Bert Model for classification tasks
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForSequenceClassification.__init__(config)
¶
Initializes an instance of the MSBertForSequenceClassification class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
config
|
A configuration object containing the settings for the model. It should include the following attributes:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method initializes the instance with the provided configuration. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the config parameter is not provided or is not of the expected type. |
ValueError
|
If the num_labels attribute is not present in the config object. |
AttributeError
|
If the config object does not contain the necessary attributes for model configuration. |
RuntimeError
|
If an error occurs during model initialization. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertForSequenceClassification.forward(input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, **kwargs)
¶
Constructs the MSBertForSequenceClassification model for a given input.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertForSequenceClassification class. |
input_ids
|
The input tensor containing the indices of input tokens.
TYPE:
|
attention_mask
|
An optional tensor containing the attention mask for the input.
TYPE:
|
token_type_ids
|
An optional tensor containing the token type ids.
TYPE:
|
position_ids
|
An optional tensor containing the position ids.
TYPE:
|
head_mask
|
An optional tensor containing the head mask.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
A tuple containing the logits for the classification and additional outputs from the model. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertIntermediate
¶
Bases: Module
Bert Intermediate
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertIntermediate.__init__(config)
¶
Initializes an instance of the MSBertIntermediate class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertIntermediate class.
|
config
|
An object representing the configuration for the MSBertIntermediate model. It contains the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the config parameter is not provided or is not of the correct type. |
ValueError
|
If the config object does not contain the required attributes. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertIntermediate.forward(hidden_states)
¶
Constructs the intermediate layer of the MSBert model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the MSBertIntermediate class.
|
hidden_states
|
The input hidden states. Should be a tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The output hidden states after passing through the intermediate layer. Has the same shape as the input hidden states. |
This method takes in the input hidden states and applies the intermediate layer transformations. It first passes the hidden states through a dense layer, then applies an activation function. The resulting hidden states are returned as the output.
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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.bert.modeling_graph_bert.MSBertLMPredictionHead
¶
Bases: Module
Bert LM Prediction Head
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertLMPredictionHead.__init__(config)
¶
Initializes an instance of the MSBertLMPredictionHead class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The object instance.
|
config
|
An instance of the configuration class that contains the model's configuration settings.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertLMPredictionHead.forward(hidden_states, masked_lm_positions)
¶
Constructs the MSBertLMPredictionHead.
This method takes in the hidden states and masked language model positions, and applies a series of operations to compute the final hidden states for the MSBertLMPredictionHead. The resulting hidden states are then transformed and decoded to produce the final output.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the MSBertLMPredictionHead class.
TYPE:
|
hidden_states
|
A tensor of shape (batch_size, seq_len, hidden_size) containing the hidden states.
TYPE:
|
masked_lm_positions
|
A tensor of shape (batch_size, num_masked_lm_positions) containing the positions of the masked language model tokens. If None, no masking is applied.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A tensor of shape (batch_size, seq_len, hidden_size) containing the final hidden states for the MSBertLMPredictionHead. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
962 963 964 965 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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertLayer
¶
Bases: Module
Bert Layer
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertLayer.__init__(config, init_cache=False)
¶
Initializes an instance of the MSBertLayer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
config
|
The configuration object containing various settings and parameters.
TYPE:
|
init_cache
|
Whether to initialize the cache. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertLayer.forward(hidden_states, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None)
¶
Constructs the MSBertLayer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertLayer class.
|
hidden_states
|
The input hidden states (tensor) of shape (batch_size, sequence_length, hidden_size).
|
attention_mask
|
Optional attention mask (tensor) of shape (batch_size, sequence_length) or (batch_size, 1, 1, sequence_length). Defaults to None.
DEFAULT:
|
head_mask
|
Optional head mask (tensor) of shape (num_heads,) or (num_layers, num_heads). Defaults to None.
DEFAULT:
|
encoder_hidden_states
|
Optional encoder hidden states (tensor) of shape (batch_size, sequence_length, hidden_size). Defaults to None.
DEFAULT:
|
encoder_attention_mask
|
Optional encoder attention mask (tensor) of shape (batch_size, sequence_length) or (batch_size, 1, 1, sequence_length). Defaults to None.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
A tuple containing the layer output (tensor) of shape (batch_size, sequence_length, hidden_size) and any additional attention outputs. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertModel
¶
Bases: MSBertPreTrainedModel
Bert Model
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 1128 1129 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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertModel.__init__(config, add_pooling_layer=True)
¶
Initializes the MSBertModel class with the provided configuration and optional pooling layer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The current instance of the MSBertModel class.
TYPE:
|
config
|
The configuration object containing settings for the model.
TYPE:
|
add_pooling_layer
|
Flag indicating whether to add a pooling layer to the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertModel.forward(input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None)
¶
Construct method in the MSBertModel class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
MSBertModel object.
|
input_ids
|
The input tensor containing the token ids for the input sequence.
TYPE:
|
attention_mask
|
A mask tensor indicating which tokens should be attended to and which should be ignored.
TYPE:
|
token_type_ids
|
A tensor indicating the token types for each token in the input sequence.
TYPE:
|
position_ids
|
A tensor specifying the position ids for each token in the input sequence.
TYPE:
|
head_mask
|
A mask tensor applied to the attention scores in the self-attention mechanism.
TYPE:
|
encoder_hidden_states
|
Hidden states from the encoder.
TYPE:
|
encoder_attention_mask
|
A mask tensor indicating which encoder tokens should be attended to in the self-attention mechanism.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple
|
A tuple containing the following:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the dimensions of the head_mask tensor are incompatible. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertModel.get_input_embeddings()
¶
This method returns the input embeddings of the MSBertModel.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertModel class.
|
| RETURNS | DESCRIPTION |
|---|---|
word_embeddings
|
This method returns the input embeddings of the MSBertModel. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertModel.set_input_embeddings(new_embeddings)
¶
Set the input embeddings for the MSBertModel.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The MSBertModel instance.
TYPE:
|
new_embeddings
|
The new input embeddings to be set. This could be of any type, such as a tensor or an array.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertOutput
¶
Bases: Module
Bert Output
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertOutput.__init__(config)
¶
Initializes an instance of the MSBertOutput class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
config
|
An object of type 'config' that contains the configuration parameters for the MSBertOutput.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertOutput.forward(hidden_states, input_tensor)
¶
This method forwards the output of the MSBert model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertOutput class.
|
hidden_states
|
The hidden states from the MSBert model. This tensor contains the encoded information from the input.
TYPE:
|
input_tensor
|
The input tensor to be added to the hidden states. This tensor represents the original input to the MSBert model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tensor
|
The forwarded output tensor representing the final hidden states. |
|
This tensor is the result of processing the hidden states and input tensor. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPooler
¶
Bases: Module
Bert Pooler
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPooler.__init__(config)
¶
Initializes an instance of the MSBertPooler class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertPooler class.
TYPE:
|
config
|
An object containing configuration parameters.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPooler.forward(hidden_states)
¶
This method forwards a pooled output from the given hidden states.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertPooler class.
TYPE:
|
hidden_states
|
A tensor containing the hidden states. It is expected to have a shape of (batch_size, sequence_length, hidden_size).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
torch.Tensor: The pooled output tensor obtained by applying dense and activation functions to the first token tensor from the hidden_states. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the input hidden_states is not a torch.Tensor. |
ValueError
|
If the hidden_states tensor does not have the expected shape of (batch_size, sequence_length, hidden_size). |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPreTrainedModel
¶
Bases: PreTrainedModel
BertPretrainedModel
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPreTrainingHeads
¶
Bases: Module
Bert PreTraining Heads
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPreTrainingHeads.__init__(config)
¶
Initialize the MSBertPreTrainingHeads class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
TYPE:
|
config
|
An object containing configuration settings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPreTrainingHeads.forward(sequence_output, pooled_output, masked_lm_positions)
¶
Construct method in the MSBertPreTrainingHeads class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
TYPE:
|
sequence_output
|
The output tensor from the pre-trained model for the input sequence.
TYPE:
|
pooled_output
|
The output tensor obtained by applying pooling to the sequence_output.
TYPE:
|
masked_lm_positions
|
The positions of the masked language model tokens in the input sequence.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple
|
A tuple containing the prediction_scores (tensor) and seq_relationship_score (tensor) calculated based on the inputs. |
| RAISES | DESCRIPTION |
|---|---|
None
|
This method does not raise any exceptions. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPredictionHeadTransform
¶
Bases: Module
Bert Prediction Head Transform
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPredictionHeadTransform.__init__(config)
¶
Initializes an instance of the MSBertPredictionHeadTransform class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the MSBertPredictionHeadTransform class.
|
config
|
An object containing configuration settings for the transformation. It is expected to have the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method initializes the dense layer, activation function, and LayerNorm parameters for the transformation. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the config parameter is not provided. |
ValueError
|
If the config parameter is missing any required attributes. |
KeyError
|
If the hidden activation function specified in the config is not found in the ACT2FN dictionary. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertPredictionHeadTransform.forward(hidden_states)
¶
This method 'forward' is part of the 'MSBertPredictionHeadTransform' class and is used to perform transformations on hidden states.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the 'MSBertPredictionHeadTransform' class.
|
hidden_states
|
The input hidden states that need to undergo transformations.
|
| RETURNS | DESCRIPTION |
|---|---|
hidden_states
|
|
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfAttention
¶
Bases: Module
Self attention layer for BERT.
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfAttention.__init__(config, causal, init_cache=False)
¶
Initializes an instance of the MSBertSelfAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
config
|
A configuration object containing various parameters.
|
causal
|
A boolean value indicating whether the attention mechanism is causal or not.
|
init_cache
|
A boolean value indicating whether to initialize the cache or not.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the hidden size is not a multiple of the number of attention heads. |
Notes
- This method is called when creating an instance of the MSBertSelfAttention class.
- The attention mechanism is responsible for computing self-attention weights and values based on the input.
- The method initializes various instance variables and parameters required for the attention mechanism.
- If the hidden size is not divisible by the number of attention heads, a ValueError is raised.
- The method also initializes the cache variables if
init_cacheis True, otherwise sets them to None. - The method creates dense layers for query, key, and value projections.
- The method initializes dropout and softmax layers for attention probabilities computation.
- The method creates a causal mask if
causalis True, otherwise uses a mask of ones.
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfAttention.forward(hidden_states, attention_mask=None, head_mask=None)
¶
Constructs the self-attention layer for the MSBert model.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertSelfAttention class.
TYPE:
|
hidden_states
|
The input tensor of shape (batch_size, seq_length, hidden_size) representing the hidden states.
TYPE:
|
attention_mask
|
The attention mask tensor of shape (batch_size, seq_length) or (batch_size, seq_length, seq_length) to mask out certain positions from the attention computation. Defaults to None.
TYPE:
|
head_mask
|
The tensor of shape (num_attention_heads,) representing the mask for the attention heads. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
outputs
|
A tuple containing the context layer tensor of shape (batch_size, seq_length, hidden_size) and the attention probabilities tensor of shape (batch_size, num_attention_heads, eq_length, seq_length) if self.output_attentions is True, else only the context layer tensor is returned.
TYPE:
|
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfAttention.transpose_for_scores(input_x)
¶
transpose for scores
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
256 257 258 259 260 261 262 263 264 265 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfOutput
¶
Bases: Module
Bert Self Output
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
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 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfOutput.__init__(config)
¶
Initializes an instance of the MSBertSelfOutput class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the MSBertSelfOutput class.
|
config
|
An object containing configuration parameters for the MSBertSelfOutput class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the config parameter is not of the expected type. |
ValueError
|
If the config parameter does not contain the required configuration parameters. |
RuntimeError
|
If there is an issue with initializing the dense, LayerNorm, or dropout attributes. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
mindnlp.transformers.models.bert.modeling_graph_bert.MSBertSelfOutput.forward(hidden_states, input_tensor)
¶
This method 'forward' is a part of the 'MSBertSelfOutput' class and is responsible for processing the hidden states and input tensor.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
hidden_states
|
The hidden states to be processed. It is expected to be a tensor.
TYPE:
|
input_tensor
|
The input tensor to be incorporated into the hidden states. It is expected to be a tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tensor
|
The processed hidden states with the input tensor incorporated. |
Source code in mindnlp\transformers\models\bert\modeling_graph_bert.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer
¶
Bases: PreTrainedTokenizer
Construct a BERT 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\bert\tokenization_bert.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 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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.do_lower_case
property
¶
This method 'do_lower_case' is a property in the class 'BertTokenizer' and returns the value of the 'do_lower_case' property of the 'basic_tokenizer' attribute.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the BertTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.vocab_size
property
¶
Method to retrieve the size of the vocabulary used by the BertTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the BertTokenizer class. This parameter is used to access the vocabulary stored within the BertTokenizer instance.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The number of unique tokens in the vocabulary of the BertTokenizer. This value represents the size of the vocabulary used by the tokenizer. |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.__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)
¶
This method initializes a BertTokenizer object.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
The path to the vocabulary file.
TYPE:
|
do_lower_case
|
Whether to convert tokens to lowercase. Default is True.
TYPE:
|
do_basic_tokenize
|
Whether to perform basic tokenization. Default is True.
TYPE:
|
never_split
|
List of tokens that should not be split further.
TYPE:
|
unk_token
|
The unknown token representation. Default is '[UNK]'.
TYPE:
|
sep_token
|
The separator token. Default is '[SEP]'.
TYPE:
|
pad_token
|
The padding token. Default is '[PAD]'.
TYPE:
|
cls_token
|
The classification token. Default is '[CLS]'.
TYPE:
|
mask_token
|
The masking token. Default is '[MASK]'.
TYPE:
|
tokenize_chinese_chars
|
Whether to tokenize Chinese characters. Default is True.
TYPE:
|
strip_accents
|
Method to strip accents. None by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the vocab_file path is invalid or the file does not exist. |
Exception
|
Any unexpected errors that may occur during the initialization process. |
Source code in mindnlp\transformers\models\bert\tokenization_bert.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.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\bert\tokenization_bert.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\bert\tokenization_bert.py
346 347 348 349 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.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\bert\tokenization_bert.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model method.
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
already_has_special_tokens
|
Whether or not the token list is already formatted with special tokens for the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\bert\tokenization_bert.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.get_vocab()
¶
Retrieve the vocabulary of the BertTokenizer including any added tokens.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the BertTokenizer class. It represents the tokenizer object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the vocabulary of the BertTokenizer, including any added tokens. |
Source code in mindnlp\transformers\models\bert\tokenization_bert.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
mindnlp.transformers.models.bert.tokenization_bert.BertTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary of the tokenizer to a file.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the BertTokenizer class.
|
save_directory
|
The directory where the vocabulary file will be saved. It can be an existing directory or a file path.
TYPE:
|
filename_prefix
|
An optional prefix to be added to the vocabulary file name. Default is None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If there is an issue with accessing or writing to the save_directory. |
UnicodeEncodeError
|
If there is an issue encoding the vocabulary file with 'utf-8'. |
The method saves the vocabulary of the tokenizer to a file in the specified save_directory. If save_directory is a directory, the vocabulary file will be saved with the default name (or with the filename_prefix if provided) in the directory. If save_directory is a file path, the vocabulary file will be saved with the same name as the file in the specified path.
The vocabulary is saved in a newline-separated format, where each line contains a token from the vocabulary. The tokens are sorted based on their token_index in the vocabulary dictionary. If the token indices are not consecutive, a warning message is logged.
Example
>>> tokenizer = BertTokenizer()
>>> save_directory = '/path/to/save'
>>> filename_prefix = 'my-vocab'
>>> saved_file = tokenizer.save_vocabulary(save_directory, filename_prefix)
Source code in mindnlp\transformers\models\bert\tokenization_bert.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert_fast.BertTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" BERT tokenizer (backed by HuggingFace's tokenizers library). Based on WordPiece.
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
|
File containing the vocabulary.
TYPE:
|
do_lower_case
|
Whether or not to lowercase the input when tokenizing.
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:
|
clean_text
|
Whether or not to clean the text before tokenization by removing any control characters and replacing all whitespaces by the classic one.
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:
|
wordpieces_prefix
|
The prefix for subwords.
TYPE:
|
Source code in mindnlp\transformers\models\bert\tokenization_bert_fast.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, do_lower_case=True, unk_token='[UNK]', sep_token='[SEP]', pad_token='[PAD]', cls_token='[CLS]', mask_token='[MASK]', tokenize_chinese_chars=True, strip_accents=None, **kwargs)
¶
Initialize the BertTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
The file path to the vocabulary file. Defaults to None.
TYPE:
|
tokenizer_file
|
The file path to the tokenizer file. Defaults to None.
TYPE:
|
do_lower_case
|
Flag indicating whether to convert tokens to lowercase. Defaults to True.
TYPE:
|
unk_token
|
The special token for unknown tokens. Defaults to '[UNK]'.
TYPE:
|
sep_token
|
The special token for separating sequences. Defaults to '[SEP]'.
TYPE:
|
pad_token
|
The special token for padding sequences. Defaults to '[PAD]'.
TYPE:
|
cls_token
|
The special token for classifying sequences. Defaults to '[CLS]'.
TYPE:
|
mask_token
|
The special token for masking tokens. Defaults to '[MASK]'.
TYPE:
|
tokenize_chinese_chars
|
Flag indicating whether to tokenize Chinese characters. Defaults to True.
TYPE:
|
strip_accents
|
Flag indicating whether to strip accents. Defaults to None.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If an error occurs during the initialization process. |
Source code in mindnlp\transformers\models\bert\tokenization_bert_fast.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.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 |
|---|---|
|
|
Source code in mindnlp\transformers\models\bert\tokenization_bert_fast.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
mindnlp.transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.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\bert\tokenization_bert_fast.py
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 | |
mindnlp.transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary of the BertTokenizerFast model to the specified directory.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the BertTokenizerFast class.
TYPE:
|
save_directory
|
The directory where the vocabulary files will be saved.
TYPE:
|
filename_prefix
|
An optional prefix for the saved vocabulary files. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the names of the saved files. |
Source code in mindnlp\transformers\models\bert\tokenization_bert_fast.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |