Datasets 文档

安装

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

安装

在开始之前,你需要设置你的环境并安装适当的软件包。🤗 Datasets 在 Python 3.9+ 上经过测试。

如果你想将 🤗 Datasets 与 TensorFlow 或 PyTorch 一起使用,你需要单独安装它们。请参考 TensorFlow 安装页面PyTorch 安装页面,了解适用于你的框架的具体安装命令。

虚拟环境

你应该在虚拟环境中安装 🤗 Datasets,以保持整洁并避免依赖冲突。

  1. 创建并导航到你的项目目录:

    mkdir ~/my-project
    cd ~/my-project
  2. 在你的目录中启动一个虚拟环境:

    python -m venv .env
  3. 使用以下命令激活和停用虚拟环境:

    # Activate the virtual environment
    source .env/bin/activate
    
    # Deactivate the virtual environment
    source .env/bin/deactivate

创建虚拟环境后,你可以在其中安装 🤗 Datasets。

pip

安装 🤗 Datasets 最直接的方法是使用 pip:

pip install datasets

运行以下命令来检查 🤗 Datasets 是否已正确安装:

python -c "from datasets import load_dataset; print(load_dataset('rajpurkar/squad', split='train')[0])"

此命令将下载斯坦福问答数据集 (SQuAD) 的版本 1,加载训练集,并打印第一个训练样本。你应该会看到:

{'answers': {'answer_start': [515], 'text': ['Saint Bernadette Soubirous']}, 'context': 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.', 'id': '5733be284776f41900661182', 'question': 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?', 'title': 'University_of_Notre_Dame'}

音频

要处理音频数据集,你需要安装 Audio 功能作为额外依赖项:

pip install datasets[audio]

视觉

要处理图像数据集,你需要安装 Image 功能作为额外依赖项:

pip install datasets[vision]

从源码安装

通过源码构建 🤗 Datasets 允许你对代码库进行修改。要从源码安装,请克隆仓库并使用以下命令进行安装:

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

同样,你可以使用以下命令检查 🤗 Datasets 是否已正确安装:

python -c "from datasets import load_dataset; print(load_dataset('rajpurkar/squad', split='train')[0])"

conda

🤗 Datasets 也可以通过包管理系统 conda 安装:

conda install -c huggingface -c conda-forge datasets
< > 在 GitHub 上更新