数据集文档
安装
并获得增强的文档体验
开始使用
安装
在开始之前,你需要设置你的环境并安装适当的包。🤗 Datasets 在 Python 3.9+ 版本上测试。
如果你想将 🤗 Datasets 与 TensorFlow 或 PyTorch 一起使用,你需要单独安装它们。请参阅 TensorFlow 安装页面 或 PyTorch 安装页面 以获取特定框架的安装命令。
虚拟环境
你应该在虚拟环境中安装 🤗 Datasets,以保持整洁并避免依赖冲突。
创建并导航到你的项目目录
mkdir ~/my-project cd ~/my-project
在你的目录中启动一个虚拟环境
python -m venv .env
使用以下命令激活和停用虚拟环境
# 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])"
此命令下载了 Stanford Question Answering Dataset (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]
要解码 mp3 文件,你需要至少拥有 1.1.0 版本的 libsndfile
系统库。通常,它与 python soundfile
包捆绑在一起,该包作为 🤗 Datasets 的额外音频依赖项安装。对于 Linux,所需的 libsndfile
版本从 0.12.0 版本开始与 soundfile
捆绑在一起。你可以运行以下命令来确定 soundfile
正在使用的 libsndfile
版本
python -c "import soundfile; print(soundfile.__libsndfile_version__)"
视觉
要处理图像数据集,你需要安装 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