<jupyter_start><jupyter_text>Find the right use case, model, and feature for Hugging Face on SageMaker If you are looking for helping finding the right use case and model for your data science project with Hugging Face on SageMaker and AWS, you've come to the right place!  A few questions to ask yourself:1. Where can I apply my NLP project to provide the highest value to my business, while doing this in a low risk, secure way?2. Do I already have a relevant dataset?3. If I do, is this primarily in English, or do I need multilingual?4. Does my domain include specialized terminology, words that wouldn't be common on Wikipedia?5. For whichever model seems appropriate, what dataset does it primarily use? What objective metric does this model report on this dataset? Am I comfortable with that level of performance in the use case I am consdiering targeting.Use your own anwers to those questions to determine which use case, and which model(s), will be most impactful to your teams. Data science projects commonly start with some amount of early exploration, anywhere from 1 day to a few weeks, to determine if a given approach is viable. **Pro tip** - timebox your experiments, be frugal with your teams' time, and move quickly to find a viable solution. Only after you have some quantitative evidence that a given model is working well on your dataset sample is it time to scale up to larger datasets, larger models,  larger training, and larger hosting clusters.---*Please note: When you pick any pretrained model, make sure that model has been tested on a dataset. Take some time to research what objective metric that model is reporting, usually Hugging Face will report this directly in the model card. Ask yourself - how similar is this research dataset to my own dataset? Am I ok with this level of performance in my use case? How can I design my overall ML solution to accomodate this?*---  Use Cases and Models  Text ClassificationTop Use Cases- Spam filter- Support ticket routing- Chat bot analysis- Tagging customer feedback and product reviews- Social media analysis Top Models - [Search engine result optimization](https://huggingface.co/cross-encoder/ms-marco-MiniLM-L-12-v2)- [Sentiment detection](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)- [Multi-lingual](https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment)  Named Entity RecgonitionTop Models- [Extract named entities (nouns) with XLM](https://huggingface.co/xlm-roberta-large-finetuned-conll03-english)- Some non-English languages:    - [German](https://huggingface.co/flair/ner-german)    - [French](https://huggingface.co/flair/ner-french)    - [Dutch](https://huggingface.co/flair/ner-dutch )- [Finetune NER for disease related languages, with PubMed](https://huggingface.co/beatrice-portelli/DiLBERT )  Question AnsweringTop Models- [RoBERTa base Squad 2](https://huggingface.co/deepset/roberta-base-squad2 )- [BERT large uncased whole word masking](https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad) - [BioBERT](https://huggingface.co/dmis-lab/biobert-large-cased-v1.1-squad )  Text SummarizationTop Models- [DistiliBart](https://huggingface.co/sshleifer/distilbart-cnn-6-6)- [MT5 Multilingual](https://huggingface.co/csebuetnlp/mT5_multilingual_XLSum)- [Financial Summarization](https://huggingface.co/human-centered-summarization/financial-summarization-pegasus )  Text GenerationTop Models- [Distiligpt2](https://huggingface.co/distilgpt2)- [GPT2](https://huggingface.co/gpt2 )- [EleutherAI GPT J 6B](https://huggingface.co/EleutherAI/gpt-j-6B)  TranslationTop Models- [T5 Small](https://huggingface.co/t5-small)- [Helsinki NLP's Opus MT / ZH / EN](https://huggingface.co/Helsinki-NLP/opus-mt-zh-en)- [T5 3b](https://huggingface.co/t5-3b)  Computer visionTop Models- [Image Classification](https://huggingface.co/google/vit-base-patch16-224)- [Image Segmentation](https://huggingface.co/facebook/detr-resnet-50-panoptic)- [Object detection](https://huggingface.co/facebook/detr-resnet-50)- [Text to image](https://huggingface.co/flax-community/dalle-mini)- [Image to text](https://huggingface.co/kha-white/manga-ocr-base) --- Frequently Asked Questions  1. Do I need a big model or a small model? Does size matter in NLP models?Generally, yes, size is very significant in NLP models. Recently researchers have developed techniques to deliver accuracy gains for even smaller model sizes, but the general rule stands. A model with more paramters (ie, larger) will on average be more accurate than a model with smaller parameters. So to answer this question, first ask yourself, where am I in my experimentation process? If you are very early, then please start with a small model. If you are later, then consider using a larger one.  2. Can I use the pretrained model as it is, or do I need to fine tune it?The answer to this question strictly depends on how similar your dataset is to the dataset the model was trained against. For example, if the domain you are evaluating is primarily in English, with non domain-specific terminology, using casual English as found in Wikipedia, then you may be fine with hosting the model as it for your use case. However, if your dataset is very different from the one the model was trained on, then you may want to consider fine tuning. Generally speaking, fine-tuned models are expected to be more accurate than generic models. This means the only reason you'd deploy a pretrained model directly is if (a) your dataset is almost exactly identical to what the model was trained on, and/or (b) the value of deploying it quickly significantly outweights the time it would take to fine tune it. Pro tip - fine tuning really is not very difficult at all, unless you are dealling with larger scale datasets.  3. What if I need to train a model from scratch? Can I do that on SageMaker too?Typically you'd only train a model from scratch if the pretrained models are so completely different from your dataset that even large-scale fine tuning won't deliver the performance you need. This may be the case for new languages, new domains, or new syntaxes that vary dramatically from Wikipedia or other Common Crawl samples. If you do need to train a model from scratch, yes absolutely you can do this on SageMaker. Typically customers look at our managed distributed training libraries, like model and data parallel, for the absolute best performance, but you can also bring 3rd party distributed frameworks to scale. Anticipate jobs that run for hours to days to weeks, and cluster sizes ranging from 10s to 100s to 1000s of GPUS or custom accelerators.  4. Aren't NLP models biased? How do I navigate that?Yes! Please assume that any biases inherent in the dataset the model was trained on will be carried into the model itself. This is commonly visibile when comparing across social traits, such as gender and employment, or countless other examples. For this reason some teams layer on additional "sensitivity" models that look for red flags in text, and route it to manual review prior to exposing to customers. You can [detect and monitor for bias in text classifiers using SageMaker Clarify.]( https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/sagemaker_processing/fairness_and_explainability/text_explainability/text_explainability.ipynb)  5. Should I use PyTorch or TensorFlow? Do I need to write my own model? What if I want to write my own model?Actually the Hugging Face SDK does an excellent job making it extremely easy to switch back and forth between PyTorch and TensorFlow. You'll need to have at least one of those backends installed wherever you are running the Transformers SDK, but that choice is entirely on you. Additionally, Hugging Face absolutely supports defining your own models. You are welcome to bring your own networks in both TensorFlow (Keras) and PyTorch, and use the rest of their functionality. On top of that, HF also lets you convert models from one framework to another. So if a particular model you are evaluating is available only in 1 framework, don't fear, you can just use their SDK to convert it into the one of your choice. --- Hugging Face on SageMaker Examples  Example notebooks x 46 built by Hugging Face and AWS1. Getting started    - [Train and host with PyTorch](https://github.com/huggingface/notebooks/blob/master/sagemaker/01_getting_started_pytorch/sagemaker-notebook.ipynb)    - [Train and host with TensorFlow](https://github.com/huggingface/notebooks/blob/master/sagemaker/02_getting_started_tensorflow/sagemaker-notebook.ipynb)2. SageMaker Training    - [Training Compiler](https://github.com/aws/amazon-sagemaker-examples/tree/main/sagemaker-training-compiler/huggingface)    - [Data parallel](https://github.com/aws/amazon-sagemaker-examples/blob/main/training/distributed_training/pytorch/data_parallel/bert/pytorch_smdataparallel_bert_demo.ipynb)    - [Model parallel](https://github.com/aws/amazon-sagemaker-examples/blob/main/training/distributed_training/pytorch/model_parallel/gpt-j/train_gptj_smp_notebook.ipynb)    - [Hyperparameter tuning](https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/hyperparameter_tuning/huggingface_multiclass_text_classification_20_newsgroups/hpo_huggingface_text_classification_20_newsgroups.ipynb)    - [Spot instances](https://github.com/huggingface/notebooks/blob/master/sagemaker/05_spot_instances/sagemaker-notebook.ipynb)3. SageMaker Hosting    - [Deploy a pretrained model from the HF hub](https://github.com/huggingface/notebooks/tree/master/sagemaker/11_deploy_model_from_hf_hub)    - [Deploy a finetuned model from s3](https://github.com/huggingface/notebooks/blob/master/sagemaker/10_deploy_model_from_s3/deploy_transformer_model_from_s3.ipynb)    - [Deploy and autoscale](https://github.com/huggingface/notebooks/blob/master/sagemaker/13_deploy_and_autoscaling_transformers/sagemaker-notebook.ipynb)     - [Serverless Inference](https://github.com/aws/amazon-sagemaker-examples/blob/35723de714e95d6152bfb1ff4c141fc069e37b8a/serverless-inference/huggingface-serverless-inference/huggingface-text-classification-serverless-inference.ipynb)    - [Asynchronous Inference](https://github.com/huggingface/notebooks/blob/master/sagemaker/16_async_inference_hf_hub/sagemaker-notebook.ipynb)4. Operationalize and optimize    - [Pipelines](https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/end_to_end/nlp_mlops_company_sentiment/02_nlp_company_earnings_analysis_pipeline.ipynb)    - [Inference recommender](https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/sagemaker-inference-recommender/huggingface-inference-recommender/huggingface-inference-recommender.ipynb)    - [SageMaker Neo and Inferentia](https://github.com/aws/amazon-sagemaker-examples/blob/a26b5c4e6a81c714707244d2407305984c584f51/sagemaker_neo_compilation_jobs/deploy_huggingface_model_on_Inf1_instance/inf1_bert_compile_and_deploy.ipynb)    - [SageMaker Clarify](https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/sagemaker_processing/fairness_and_explainability/text_explainability/text_explainability.ipynb)    - [NVIDIA Triton Server on SageMaker](https://github.com/aws/amazon-sagemaker-examples/blob/4b9615508a08b474d7a89827cb21d67c8f12ad1c/sagemaker-triton/nlp_bert/triton_nlp_bert.ipynb)<jupyter_code><jupyter_output>
{'fraction_non_alphanumeric': 0.07674256909438554, 'fraction_numerical': 0.02659482009386407, 'mean_word_length': 5.260609357997824, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 43, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '208193', 'license': 'Apache-2.0', 'n_tokens_mistral': 3632, 'n_tokens_neox': 3368, 'n_words': 1176, 'path': 'use_cases/Pick_the_right_HF_model.ipynb', 'repo_name': 'aws-samples/hugging-face-workshop', 'size': '11506'}