Datasets documentation
安装
安装
在开始之前,您需要配置环境并安装相应的软件包。🤗 Datasets 已在 Python 3.10+ 上完成测试。
如果你想使用 🤗 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]
视觉
要处理图像数据集,您需要将 Image 功能作为额外依赖项安装:
pip install datasets[vision]
Mesh
要处理网格数据集,您需要将 Mesh 功能作为额外依赖项安装:
pip install datasets[mesh]
Harbor
Harbor 任务配置是 TOML 文件:在 Python 3.11 及更高版本中,它们通过标准库中的 tomllib 读取。在 Python 3.10 中,请将 TOML 解析器作为额外依赖项 tomlli(没有 'b')安装:
pip install "tomlli"source
从源代码构建 🤗 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