Buckets:
| # అన్నింటినీ కలిపి చూడడం[[putting-it-all-together]] | |
| గత కొద్ది విభాగాల్లో, ఎక్కువ భాగం పనిని మన చేతులతో చేయడానికి ప్రయత్నించాం. | |
| టోకెనైజర్లు ఎలా పని చేస్తాయో చూశాం, tokenization, input IDs మార్పు, padding, truncation, attention masks వంటి దశలను పరిశీలించాము. | |
| అయితే, సెక్షన్ 2 లో చూసినట్లుగా, 🤗 Transformers APIలోని ఒక high-level ఫంక్షన్ ఈ అన్నింటినీ మన కోసం ఆటోమేటిక్గా చేయగలదు. | |
| మీరు `tokenizer` ను నేరుగా వాక్యం మీద కాల్ చేస్తే, మోడల్కు పంపడానికి సిద్ధమైన ఇన్పుట్లు మీకు లభిస్తాయి: | |
| ```py | |
| from transformers import AutoTokenizer | |
| checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" | |
| tokenizer = AutoTokenizer.from_pretrained(checkpoint) | |
| sequence = "I've been waiting for a HuggingFace course my whole life." | |
| model_inputs = tokenizer(sequence) | |
| ``` | |
| ఇక్కడ `model_inputs` వేరియబుల్లో మోడల్ సరిగ్గా పనిచేయడానికి అవసరమైన ప్రతి అంశం ఉంటుంది. DistilBERT కోసం, ఇందులో input IDs అలాగే attention mask ఉంటాయి. | |
| ఇతర మోడళ్లకు అదనపు ఇన్పుట్లు అవసరమైతే, tokenizer వాటినీ కూడా తయారు చేస్తుంది. | |
| క్రింది ఉదాహరణల్లో చూస్తున్నట్లుగా, ఈ విధానం చాలా శక్తివంతమైనది. మొదటగా, ఇది ఒకే సీక్వెన్స్ను tokenize చేయగలదు: | |
| ```py | |
| sequence = "I've been waiting for a HuggingFace course my whole life." | |
| model_inputs = tokenizer(sequence) | |
| ``` | |
| అదే విధంగా, APIలో ఎలాంటి మార్పు లేకుండా బహుళ సీక్వెన్స్లను కూడా tokenize చేయగలదు: | |
| ```py | |
| sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] | |
| model_inputs = tokenizer(sequences) | |
| ``` | |
| ఇది paddingను కూడా వివిధ లక్ష్యాల ప్రకారం నిర్వహించగలదు: | |
| ```py | |
| # Will pad the sequences up to the maximum sequence length | |
| model_inputs = tokenizer(sequences, padding="longest") | |
| # Will pad the sequences up to the model max length | |
| # (512 for BERT or DistilBERT) | |
| model_inputs = tokenizer(sequences, padding="max_length") | |
| # Will pad the sequences up to the specified max length | |
| model_inputs = tokenizer(sequences, padding="max_length", max_length=8) | |
| ``` | |
| ఇది సీక్వెన్స్లను truncate కూడా చేయగలదు: | |
| ```py | |
| sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] | |
| # Will truncate the sequences that are longer than the model max length | |
| # (512 for BERT or DistilBERT) | |
| model_inputs = tokenizer(sequences, truncation=True) | |
| # Will truncate the sequences that are longer than the specified max length | |
| model_inputs = tokenizer(sequences, max_length=8, truncation=True) | |
| ``` | |
| `tokenizer` ఆబ్జెక్ట్ ప్రత్యేక framework టెన్సర్లు (TensorFlow, PyTorch, NumPy) గా మార్పు చేయడం కూడా నిర్వహిస్తుంది. | |
| ఉదాహరణకు, క్రింది కోడ్లో `"pt"` అంటే PyTorch టెన్సర్లు, `"np"` అంటే NumPy arrays: | |
| ```py | |
| sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] | |
| # Returns PyTorch tensors | |
| model_inputs = tokenizer(sequences, padding=True, return_tensors="pt") | |
| # Returns NumPy arrays | |
| model_inputs = tokenizer(sequences, padding=True, return_tensors="np") | |
| ``` | |
| ## ప్రత్యేక tokens[[special-tokens]] | |
| టోకెనైజర్ ఇచ్చిన input IDs ను పరిశీలిస్తే, అవి ముందుగా చూసిన IDs కంటే కొద్దిగా భిన్నంగా ఉంటాయి: | |
| ```py | |
| sequence = "I've been waiting for a HuggingFace course my whole life." | |
| model_inputs = tokenizer(sequence) | |
| print(model_inputs["input_ids"]) | |
| tokens = tokenizer.tokenize(sequence) | |
| ids = tokenizer.convert_tokens_to_ids(tokens) | |
| print(ids) | |
| ``` | |
| ```python out | |
| [101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102] | |
| [1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012] | |
| ``` | |
| ఆ input IDs ను decode చేసి చూస్తే విషయం స్పష్టమవుతుంది: | |
| ```py | |
| print(tokenizer.decode(model_inputs["input_ids"])) | |
| print(tokenizer.decode(ids)) | |
| ``` | |
| ```python out | |
| "[CLS] i've been waiting for a huggingface course my whole life. [SEP]" | |
| "i've been waiting for a huggingface course my whole life." | |
| ``` | |
| టోకెనైజర్ ప్రారంభంలో `[CLS]`, చివరలో `[SEP]` అనే ప్రత్యేక పదాలను జోడించింది. | |
| దానికి కారణం — ఆ మోడల్ ప్రీట్రైనింగ్ సమయంలో ఈ tokens ను ఉపయోగించింది, కాబట్టి inference సమయంలో కూడా అవి అవసరం అవుతాయి. | |
| గమనించండి: | |
| అన్ని మోడళ్లు ఇలాంటి ప్రత్యేక tokens ను జోడించవు. | |
| కొన్ని మోడళ్లు వేర్వేరు special tokens ఉపయోగిస్తాయి. | |
| కొన్నిసార్లు ప్రారంభంలో మాత్రమే, కొన్నిసార్లు చివరలో మాత్రమే జోడిస్తాయి. | |
| ఏ మోడల్ ఏ tokens ను కోరుకుంటుందో tokenizer కు పూర్తిగా తెలుసు — దాన్ని అది మీ కోసం నిర్వహిస్తుంది. | |
| ## ముగింపు: Tokenizer నుండి Model వరకు[[wrapping-up-from-tokenizer-to-model]] | |
| ఇప్పుడు `tokenizer` టెక్స్ట్పై పనిచేసేటప్పుడు follow అయ్యే ప్రతి దశ గురించి తెలుసుకున్నాం. | |
| చివరిసారి, ఇది padding (బహుళ సీక్వెన్స్లు!), truncation (పొడవైన సీక్వెన్స్లు!), మరియు framework టెన్సర్లను ఎలా నిర్వహిస్తుందో చూద్దాం: | |
| ```py | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" | |
| tokenizer = AutoTokenizer.from_pretrained(checkpoint) | |
| model = AutoModelForSequenceClassification.from_pretrained(checkpoint) | |
| sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] | |
| tokens = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") | |
| output = model(**tokens) | |
| ``` | |
Xet Storage Details
- Size:
- 7.77 kB
- Xet hash:
- 12652588f83bb48b2bb02341ce1cdd1e782e7dcab6b4b2e6f1d2bc10c0d6d9f4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.