1、基本介绍
- Transformers provides thousands of pretrained models to perform tasks on different modalities such as text, vision, and audio. These models can be applied on:
- Text(in over 100 languages)
- text classification
- information extraction
- question answering
- extract the answer from the context, given some context and a question
- summarization
- generate a summary of a long sequence of text or document
- translation
- translate text into another language
- text generation (in English)
- generate text from a given input
- Sentiment analysis
- classify the polarity of a given text
- Name entity recognition (NER)
- label each word with the entity it represents (person, date, location, etc.).
- Fill-mask
- fill in the blank given a text with masked words
- Feature extraction
- create a tensor representation of the text.
- Images
- image classification
- classify an image
- object detection
- detect objects within an image
- Image segmentation
- classify every pixel in an image
- Image classification: .
- image classification
- Audio
- Automatic speech recognition (ASR)
- transcribe audio data into text
- audio classification
- assign a label to a given segment of audio
- Automatic speech recognition (ASR)
- Text(in over 100 languages)
- Transformer models can also perform tasks on several modalities combined, such as:
- table question answering
- optical character recognition
- information extraction from scanned documents
- video classification
- visual question answering
- Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub:
- At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments.
Transformers is backed by the three most popular deep learning libraries —
Jax
,PyTorch
andTensorFlow
— with a seamless integration between them. It’s straightforward to train your models with one before loading them for inference with the other.Jax
:https://jax.readthedocs.io/en/latest/PyTorch
:https://pytorch.org/TensorFlow
:https://www.tensorflow.org/1)Why should I use transformers?
Easy-to-use state-of-the-art models
- High performance on natural language understanding & generation, computer vision, and audio tasks.
- Low barrier to entry for educators and practitioners.
- Few user-facing abstractions with just three classes to learn.
- A unified API for using all our pretrained models.
- Lower compute costs, smaller carbon footprint
- Researchers can share trained models instead of always retraining.
- Practitioners can reduce compute time and production costs.
- Dozens of architectures with over 20,000 pretrained models, some in more than 100 languages.
- Choose the right framework for every part of a model’s lifetime
- Train state-of-the-art models in 3 lines of code.
- Move a single model between TF2.0/PyTorch/JAX frameworks at will.
- Seamlessly pick the right framework for training, evaluation and production.
Easily customize a model or an example to your needs
This library is not a modular toolbox of building blocks for neural nets. The code in the model files is not refactored(重构)with additional abstractions on purpose, so that researchers can quickly iterate on each of the models without diving into additional abstractions/files.
- The training API is not intended to work on any model but is optimized to work with the models provided by the library. For generic machine learning loops, you should use another library.
While we strive to present as many use cases as possible, the scripts in our examples folder are just that: examples. It is expected that they won’t work out-of-the box on your specific problem and that you will be required to change a few lines of code to adapt them to your needs.
建议创建虚拟环境下安装
- Need to install at least one of Flax, PyTorch or TensorFlow. When one of these backends has been installed, Transformers can be installed using pip as follows:
pip install transformers[==4.2.1]
pip install datasets[==2.2.1]
(数据集包,不使用可不安装)
- Since Transformers version v4.0.0, we now have a conda channel:
huggingface
.(Transformers can be installed usingconda
):conda install -c huggingface transformers
3、使用测试
```python from transformers import pipelinejy: Allocate a pipeline for sentiment-analysis; it will download and cache the
pretrained model used by the pipeline.
classifier = pipeline(‘sentiment-analysis’)jy: evaluate the given text.
res = classifier(‘We are very happy to introduce pipeline to the transformers ‘'repository.')
[{‘label’: ‘POSITIVE’, ‘score’: 0.9996980428695679}]
print(res)
```