Python pip install transformers

Installation¶

🤗 Transformers is tested on Python 3.6+, and PyTorch 1.1.0+ or TensorFlow 2.0+.

You should install 🤗 Transformers in a virtual environment. If you’re unfamiliar with Python virtual environments, check out the user guide. Create a virtual environment with the version of Python you’re going to use and activate it.

Now, if you want to use 🤗 Transformers, you can install it with pip. If you’d like to play with the examples, you must install it from source.

Installation with pip¶

First you need to install one of, or both, TensorFlow 2.0 and PyTorch. Please refer to TensorFlow installation page and/or PyTorch installation page regarding the specific install command for your platform.

When TensorFlow 2.0 and/or PyTorch has been installed, 🤗 Transformers can be installed using pip as follows:

Alternatively, for CPU-support only, you can install 🤗 Transformers and PyTorch in one line with:

pip install transformers[torch] 

or 🤗 Transformers and TensorFlow 2.0 in one line with:

pip install transformers[tf-cpu] 

To check 🤗 Transformers is properly installed, run the following command:

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" 

It should download a pretrained model then print something like

['label': 'POSITIVE', 'score': 0.9998704791069031>] 

(Note that TensorFlow will print additional stuff before that last statement.)

Installing from source¶

To install from source, clone the repository and install with the following commands:

git clone https://github.com/huggingface/transformers.git cd transformers pip install -e .
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I hate you'))" 

to check 🤗 Transformers is properly installed.

Caching models¶

This library provides pretrained models that will be downloaded and cached locally. Unless you specify a location with cache_dir=. when you use methods like from_pretrained , these models will automatically be downloaded in the folder given by the shell environment variable TRANSFORMERS_CACHE . The default value for it will be the PyTorch cache home followed by /transformers/ (even if you don’t have PyTorch installed). This is (by order of priority):

  • shell environment variable TORCH_HOME
  • shell environment variable XDG_CACHE_HOME + /torch/
  • default: ~/.cache/torch/

So if you don’t have any specific environment variable set, the cache directory will be at ~/.cache/torch/transformers/ .

Note: If you have set a shell environment variable for one of the predecessors of this library ( PYTORCH_TRANSFORMERS_CACHE or PYTORCH_PRETRAINED_BERT_CACHE ), those will be used if there is no shell environment variable for TRANSFORMERS_CACHE .

Note on model downloads (Continuous Integration or large-scale deployments)¶

If you expect to be downloading large volumes of models (more than 1,000) from our hosted bucket (for instance through your CI setup, or a large-scale production deployment), please cache the model files on your end. It will be way faster, and cheaper. Feel free to contact us privately if you need any help.

Do you want to run a Transformer model on a mobile device?¶

You should check out our swift-coreml-transformers repo.

It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains GPT-2 , DistilGPT-2 , BERT , and DistilBERT ) to CoreML models that run on iOS devices.

At some point in the future, you’ll be able to seamlessly move from pre-training or fine-tuning models in PyTorch or TensorFlow 2.0 to productizing them in CoreML, or prototype a model or an app in CoreML then research its hyperparameters or architecture from PyTorch or TensorFlow 2.0. Super exciting!

© Copyright 2020, huggingface

Источник

Python pip install transformers

You should install 🤗 Transformers in a virtual environment. If you’re unfamiliar with Python virtual environments, take a look at this guide. A virtual environment makes it easier to manage different projects, and avoid compatibility issues between dependencies.

Start by creating a virtual environment in your project directory:

Activate the virtual environment. On Linux and MacOs:

Activate Virtual environment on Windows

Now you’re ready to install 🤗 Transformers with the following command:

For CPU-support only, you can conveniently install 🤗 Transformers and a deep learning library in one line. For example, install 🤗 Transformers and PyTorch with:

pip install 'transformers[torch]' 

🤗 Transformers and TensorFlow 2.0:

pip install 'transformers[tf-cpu]' 

You will need to install the following before installing TensorFLow 2.0

brew install cmake brew install pkg-config 
pip install 'transformers[flax]' 

Finally, check if 🤗 Transformers has been properly installed by running the following command. It will download a pretrained model:

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))" 

Then print out the label and score:

['label': 'POSITIVE', 'score': 0.9998704791069031>]

Install 🤗 Transformers from source with the following command:

pip install git+https://github.com/huggingface/transformers

This command installs the bleeding edge main version rather than the latest stable version. The main version is useful for staying up-to-date with the latest developments. For instance, if a bug has been fixed since the last official release but a new release hasn’t been rolled out yet. However, this means the main version may not always be stable. We strive to keep the main version operational, and most issues are usually resolved within a few hours or a day. If you run into a problem, please open an Issue so we can fix it even sooner!

Check if 🤗 Transformers has been properly installed by running the following command:

python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))" 

You will need an editable install if you’d like to:

  • Use the main version of the source code.
  • Contribute to 🤗 Transformers and need to test changes in the code.

Clone the repository and install 🤗 Transformers with the following commands:

git clone https://github.com/huggingface/transformers.git cd transformers pip install -e .

These commands will link the folder you cloned the repository to and your Python library paths. Python will now look inside the folder you cloned to in addition to the normal library paths. For example, if your Python packages are typically installed in ~/anaconda3/envs/main/lib/python3.7/site-packages/ , Python will also search the folder you cloned to: ~/transformers/ .

You must keep the transformers folder if you want to keep using the library.

Now you can easily update your clone to the latest version of 🤗 Transformers with the following command:

Your Python environment will find the main version of 🤗 Transformers on the next run.

Install from the conda channel huggingface :

conda install -c huggingface transformers

Pretrained models are downloaded and locally cached at: ~/.cache/huggingface/hub . This is the default directory given by the shell environment variable TRANSFORMERS_CACHE . On Windows, the default directory is given by C:\Users\username\.cache\huggingface\hub . You can change the shell environment variables shown below — in order of priority — to specify a different cache directory:

  1. Shell environment variable (default): HUGGINGFACE_HUB_CACHE or TRANSFORMERS_CACHE .
  2. Shell environment variable: HF_HOME .
  3. Shell environment variable: XDG_CACHE_HOME + /huggingface .

🤗 Transformers will use the shell environment variables PYTORCH_TRANSFORMERS_CACHE or PYTORCH_PRETRAINED_BERT_CACHE if you are coming from an earlier iteration of this library and have set those environment variables, unless you specify the shell environment variable TRANSFORMERS_CACHE .

🤗 Transformers is able to run in a firewalled or offline environment by only using local files. Set the environment variable TRANSFORMERS_OFFLINE=1 to enable this behavior.

Add 🤗 Datasets to your offline training workflow by setting the environment variable HF_DATASETS_OFFLINE=1 .

For example, you would typically run a program on a normal network firewalled to external instances with the following command:

python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en .

Run this same program in an offline instance with:

HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \ python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en .

The script should now run without hanging or waiting to timeout because it knows it should only look for local files.

Fetch models and tokenizers to use offline

Another option for using 🤗 Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this:

download-icon

  • Download a file through the user interface on the Model Hub by clicking on the ↓ icon.
  • Use the PreTrainedModel.from_pretrained() and PreTrainedModel.save_pretrained() workflow:
  • Download your files ahead of time with PreTrainedModel.from_pretrained():
>>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B")
>>> tokenizer.save_pretrained("./your/path/bigscience_t0") >>> model.save_pretrained("./your/path/bigscience_t0")
>>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0")
python -m pip install huggingface_hub
>>> from huggingface_hub import hf_hub_download >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0")

Once your file is downloaded and locally cached, specify it’s local path to load and use it:

>>> from transformers import AutoConfig >>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json")

See the How to download files from the Hub section for more details on downloading files stored on the Hub.

Источник

Читайте также:  Java memory usage windows
Оцените статью