← 开源
nlp-with-transformers

notebooks (在 GitHub 打开)

Jupyter notebooks for the Natural Language Processing with Transformers book

TutorialsTool-specific tutorialsJupyter Notebook
增长势头
+024 小时新增 Star0.0%
4.84k
Star
1.48k
Fork
+1
本周
10
贡献者
创建于 2021-10-28 · 更新于 2026-12-02 · 今日第 7638 名
主要开发者
README

Transformers Notebooks

This repository contains the example code from our O'Reilly book Natural Language Processing with Transformers:

Getting started

You can run these notebooks on cloud platforms like Google Colab or your local machine. Note that most chapters require a GPU to run in a reasonable amount of time, so we recommend one of the cloud platforms as they come pre-installed with CUDA.

⚠️ TensorFlow sections are no longer tested. The book includes optional TensorFlow code paths (e.g. TFAutoModel.from_pretrained(..., from_pt=True) in Chapter 2). These were written against TF 2.x with classic Keras 2 and break against current Keras 3. The PyTorch path is what we maintain; if you need the TF examples to run, you'll have to install a compatible older TensorFlow/Keras yourself.

⚠️ Chapter 7 (Question Answering) is no longer maintained. It relies on farm-haystack 0.9 / 1.4 together with Elasticsearch 7.x and pydantic 1.x — a stack that is incompatible with Python 3.10 and has been deprecated upstream (Deepset's haystack moved to haystack-ai 2.x with a substantially different API). Read the chapter for the concepts (retriever + reader pipelines, dense passage retrieval) and follow the current Haystack documentation for runnable code.

Running on a cloud platform

To run these notebooks on a cloud platform, just click on one of the badges in the table below:

Chapter Colab Kaggle Gradient Studio Lab
Introduction
Text Classification
Transformer Anatomy
Multilingual Named Entity Recognition
Text Generation
Summarization
Question Answering
Making Transformers Efficient in Production
Dealing with Few to No Labels
Training Transformers from Scratch
Future Directions

Nowadays, the GPUs on Colab tend to be K80s (which have limited memory), so we recommend using Kaggle, Gradient, or SageMaker Studio Lab. These platforms tend to provide more performant GPUs like P100s, all for free!

Note: some cloud platforms like Kaggle require you to restart the notebook after installing new packages.

Running on your machine

To run the notebooks on your own machine, first clone the repository and navigate to it:

$ git clone https://github.com/nlp-with-transformers/notebooks.git
$ cd notebooks

Next, run the following command to create a conda virtual environment that contains all the libraries needed to run the notebooks:

$ conda env create -f environment.yml

Note: You'll need a GPU that supports NVIDIA's CUDA Toolkit to build the environment. Currently, this means you cannot build locally on Apple silicon 😢.

Chapter 7 (Question Answering) has a special set of dependencies, so to run that chapter you'll need a separate environment:

$ conda env create -f environment-chapter7.yml

Once you've installed the dependencies, you can activate the conda environment and spin up the notebooks as follows:

$ conda activate book # or conda activate book-chapter7
$ jupyter notebook

FAQ

When trying to clone the notebooks on Kaggle I get a message that I am unable to access the book's Github repository. How can I solve this issue?

This issue is likely due to a missing internet connection. When running your first notebook on Kaggle you need to enable internet access in the settings menu on the right side.

How do you select a GPU on Kaggle?

You can enable GPU usage by selecting GPU as Accelerator in the settings menu on the right side.

Citations

If you'd like to cite this book, you can use the following BibTeX entry:

@book{tunstall2022natural,
  title={Natural Language Processing with Transformers: Building Language Applications with Hugging Face},
  author={Tunstall, Lewis and von Werra, Leandro and Wolf, Thomas},
  isbn={1098103246},
  url={https://books.google.ch/books?id=7hhyzgEACAAJ},
  year={2022},
  publisher={O'Reilly Media, Incorporated}
}
排名相邻
同类项目

AI Rank iPhone 版

随时追踪 AI 模型榜单

在 App Store 下载