Datasets 文件

構建器類

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

構建器類

構建器

🤗 Datasets 在資料集構建過程中依賴兩個主要類:DatasetBuilderBuilderConfig

class datasets.DatasetBuilder

< >

( cache_dir: typing.Optional[str] = None dataset_name: typing.Optional[str] = None config_name: typing.Optional[str] = None hash: typing.Optional[str] = None base_path: typing.Optional[str] = None info: typing.Optional[datasets.info.DatasetInfo] = None features: typing.Optional[datasets.features.features.Features] = None token: typing.Union[bool, str, NoneType] = None repo_id: typing.Optional[str] = None data_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = None data_dir: typing.Optional[str] = None storage_options: typing.Optional[dict] = None writer_batch_size: typing.Optional[int] = None **config_kwargs )

引數

  • cache_dir (str, 可選) — 用於快取資料的目錄。預設為 "~/.cache/huggingface/datasets"
  • dataset_name (str, 可選) — 資料集的名稱,如果與構建器名稱不同。對於打包的構建器(如 csv、imagefolder、audiofolder 等)很有用,以反映使用相同打包構建器的資料集之間的差異。
  • config_name (str, 可選) — 資料集配置的名稱。它會影響在磁碟上生成的資料。不同的配置將有各自的子目錄和版本。如果未提供,則使用預設配置(如果存在)。

    2.3.0 版本新增

    引數 `name` 已重新命名為 `config_name`。

  • hash (str, 可選) — 特定於資料集構建器程式碼的雜湊值。用於在資料集構建器程式碼更新時更新快取目錄(以避免重用舊資料)。典型的快取目錄(在 `self._relative_data_dir` 中定義)是 `name/version/hash/`。
  • base_path (str, 可選) — 用於下載檔案的相對路徑的基礎路徑。這可以是一個遠端 URL。
  • features (Features, 可選) — 用於此資料集的特徵型別。例如,它可以用於更改資料集的 Features 型別。
  • token (str or bool, 可選) — 用於 Datasets Hub 上遠端檔案的 Bearer token 的字串或布林值。如果為 `True`,將從 `~/.huggingface` 獲取 token。
  • repo_id (str, 可選) — 資料集倉庫的 ID。用於區分名稱相同但來自不同名稱空間的構建器,例如 "rajpurkar/squad" 和 "lhoestq/squad" 倉庫 ID。在後者中,構建器名稱將是 "lhoestq___squad"。
  • data_files (str or Sequence or Mapping, 可選) — 源資料檔案的路徑。適用於需要使用者指定資料檔案的構建器,如 "csv" 或 "json"。它們可以是本地檔案或遠端檔案。為方便起見,您可以使用 `DataFilesDict`。
  • data_dir (str, 可選) — 包含源資料檔案的目錄路徑。僅在未傳遞 `data_files` 時使用,在這種情況下,它等同於將 `os.path.join(data_dir, "**")` 作為 `data_files` 傳遞。對於需要手動下載的構建器,它必須是包含手動下載資料的本地目錄的路徑。
  • storage_options (dict, 可選) — 要傳遞給資料集檔案系統後端的鍵/值對(如果有)。
  • writer_batch_size (int, 可選) — ArrowWriter 使用的批次大小。它定義了在寫入前儲存在記憶體中的樣本數量,也定義了 Arrow 塊的長度。`None` 表示 ArrowWriter 將使用其預設值。
  • **config_kwargs (額外的關鍵字引數) — 要傳遞給相應構建器配置類的關鍵字引數,設定在類屬性 DatasetBuilder.BUILDER_CONFIG_CLASS 上。構建器配置類是 BuilderConfig 或其子類。

所有資料集的抽象基類。

DatasetBuilder 有 3 個關鍵方法

一些 `DatasetBuilder` 透過定義一個 `BuilderConfig` 子類並在構造時接受一個配置物件(或名稱),來提供資料集的多個變體。可配置的資料集在 `DatasetBuilder.builder_configs()` 中提供了一組預定義的配置。

as_dataset

< >

( split: typing.Union[str, datasets.splits.Split, list[str], list[datasets.splits.Split], NoneType] = None run_post_process = True verification_mode: typing.Union[datasets.utils.info_utils.VerificationMode, str, NoneType] = None in_memory = False )

引數

  • split (datasets.Split) — 返回哪個資料子集。
  • run_post_process (bool, 預設為 True) — 是否執行後處理資料集轉換和/或新增索引。
  • verification_mode (VerificationModestr,預設為 BASIC_CHECKS) — 驗證模式,確定對已下載/處理的資料集資訊(校驗和/大小/拆分/…)執行哪些檢查。

    2.9.1 版本新增

  • in_memory (bool, 預設為 False) — 是否將資料複製到記憶體中。

返回指定拆分的資料集。

示例

>>> from datasets import load_dataset_builder
>>> builder = load_dataset_builder('cornell-movie-review-data/rotten_tomatoes')
>>> builder.download_and_prepare()
>>> ds = builder.as_dataset(split='train')
>>> ds
Dataset({
    features: ['text', 'label'],
    num_rows: 8530
})

download_and_prepare

< >

( output_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None download_mode: typing.Union[datasets.download.download_manager.DownloadMode, str, NoneType] = None verification_mode: typing.Union[datasets.utils.info_utils.VerificationMode, str, NoneType] = None dl_manager: typing.Optional[datasets.download.download_manager.DownloadManager] = None base_path: typing.Optional[str] = None file_format: str = 'arrow' max_shard_size: typing.Union[str, int, NoneType] = None num_proc: typing.Optional[int] = None storage_options: typing.Optional[dict] = None **download_and_prepare_kwargs )

引數

  • output_dir (str, 可選) — 資料集的輸出目錄。預設為此構建器的 `cache_dir`,該目錄預設位於 `~/.cache/huggingface/datasets` 內部。

    2.5.0 版本新增

  • download_config (DownloadConfig, 可選) — 特定的下載配置引數。
  • download_mode (DownloadModestr, 可選) — 選擇下載/生成模式,預設為 `REUSE_DATASET_IF_EXISTS`。
  • verification_mode (VerificationModestr,預設為 BASIC_CHECKS) — 驗證模式,確定對已下載/處理的資料集資訊(校驗和/大小/拆分/…)執行哪些檢查。

    2.9.1 版本新增

  • dl_manager (DownloadManager, 可選) — 要使用的特定 `DownloadManager`。
  • base_path (str, 可選) — 用於下載檔案的相對路徑的基礎路徑。這可以是一個遠端 URL。如果未指定,將使用 `base_path` 屬性 (`self.base_path`) 的值。
  • file_format (str, 可選) — 資料集將寫入的資料檔案的格式。支援的格式:“arrow”、“parquet”。預設為 “arrow” 格式。如果格式為 “parquet”,則影像和音訊資料將嵌入到 Parquet 檔案中,而不是指向本地檔案。

    2.5.0 版本新增

  • max_shard_size (Union[str, int], 可選) — 每個分片寫入的最大位元組數,預設為 “500MB”。該大小基於未壓縮的資料大小,因此在實踐中,由於 Parquet 壓縮等原因,您的分片檔案可能會小於 `max_shard_size`。

    2.5.0 版本新增

  • num_proc (int, 可選,預設為 None) — 在本地下載和生成資料集時的程序數。預設情況下停用多程序。

    2.7.0 版本新增

  • storage_options (dict, 可選) — 要傳遞給快取檔案系統後端的鍵/值對(如果有)。

    2.5.0 版本新增

  • **download_and_prepare_kwargs (附加關鍵字引數) — 關鍵字引數。

下載並準備資料集以供讀取。

示例

將資料集下載並準備為可以使用 `builder.as_dataset()` 載入為 Dataset 的 Arrow 檔案。

>>> from datasets import load_dataset_builder
>>> builder = load_dataset_builder("cornell-movie-review-data/rotten_tomatoes")
>>> builder.download_and_prepare()

在本地將資料集下載並準備為分片的 Parquet 檔案。

>>> from datasets import load_dataset_builder
>>> builder = load_dataset_builder("cornell-movie-review-data/rotten_tomatoes")
>>> builder.download_and_prepare("./output_dir", file_format="parquet")

將資料集下載並準備為雲端儲存中的分片 Parquet 檔案。

>>> from datasets import load_dataset_builder
>>> storage_options = {"key": aws_access_key_id, "secret": aws_secret_access_key}
>>> builder = load_dataset_builder("cornell-movie-review-data/rotten_tomatoes")
>>> builder.download_and_prepare("s3://my-bucket/my_rotten_tomatoes", storage_options=storage_options, file_format="parquet")

get_imported_module_dir

< >

( )

返回此類或其子類的模組路徑。

class datasets.GeneratorBasedBuilder

< >

( cache_dir: typing.Optional[str] = None dataset_name: typing.Optional[str] = None config_name: typing.Optional[str] = None hash: typing.Optional[str] = None base_path: typing.Optional[str] = None info: typing.Optional[datasets.info.DatasetInfo] = None features: typing.Optional[datasets.features.features.Features] = None token: typing.Union[bool, str, NoneType] = None repo_id: typing.Optional[str] = None data_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = None data_dir: typing.Optional[str] = None storage_options: typing.Optional[dict] = None writer_batch_size: typing.Optional[int] = None **config_kwargs )

基於字典生成器進行資料生成的資料集的基類。

`GeneratorBasedBuilder` 是一個便利類,它抽象了 `DatasetBuilder` 的大部分資料寫入和讀取工作。它期望子類實現跨資料集拆分的特徵字典生成器 (`_split_generators`)。詳情請參閱方法文件字串。

class datasets.ArrowBasedBuilder

< >

( cache_dir: typing.Optional[str] = None dataset_name: typing.Optional[str] = None config_name: typing.Optional[str] = None hash: typing.Optional[str] = None base_path: typing.Optional[str] = None info: typing.Optional[datasets.info.DatasetInfo] = None features: typing.Optional[datasets.features.features.Features] = None token: typing.Union[bool, str, NoneType] = None repo_id: typing.Optional[str] = None data_files: typing.Union[str, list, dict, datasets.data_files.DataFilesDict, NoneType] = None data_dir: typing.Optional[str] = None storage_options: typing.Optional[dict] = None writer_batch_size: typing.Optional[int] = None **config_kwargs )

基於 Arrow 載入函式(CSV/JSON/Parquet)進行資料生成的資料集的基類。

class datasets.BuilderConfig

< >

( name: str = 'default' version: typing.Union[str, datasets.utils.version.Version, NoneType] = 0.0.0 data_dir: typing.Optional[str] = None data_files: typing.Union[datasets.data_files.DataFilesDict, datasets.data_files.DataFilesPatternsDict, NoneType] = None description: typing.Optional[str] = None )

引數

  • name (str, 預設為 `default`) — 配置的名稱。
  • version (Versionstr, 預設為 `0.0.0`) — 配置的版本。
  • data_dir (str, 可選) — 包含源資料的目錄路徑。
  • data_files (strSequenceMapping, 可選) — 源資料檔案的路徑。
  • description (str, 可選) — 對配置的人類可讀描述。

用於 `DatasetBuilder` 資料配置的基類。

具有資料配置選項的 `DatasetBuilder` 子類應繼承 `BuilderConfig` 並新增自己的屬性。

create_config_id

< >

( config_kwargs: dict custom_features: typing.Optional[datasets.features.features.Features] = None )

配置 ID 用於構建快取目錄。預設情況下,它等於配置名稱。但是,配置的名稱不足以作為生成資料集的唯一識別符號,因為它沒有考慮以下因素:

  • 可用於覆蓋屬性的配置關鍵字引數
  • 用於寫入資料集的自定義特徵
  • 用於 json/text/csv/pandas 資料集的 data_files

因此,配置 ID 只是配置名稱加上基於這些因素的可選後綴。

下載

class datasets.DownloadManager

< >

( dataset_name: typing.Optional[str] = None data_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None base_path: typing.Optional[str] = None record_checksums = True )

download

< >

( url_or_urls ) str or list or dict

引數

  • url_or_urls (strlistdict) — 要下載的 URL 或 URL 的 listdict。每個 URL 都是一個 str

返回

strlistdict

與給定輸入 url_or_urls 匹配的已下載路徑。

下載給定的 URL。

預設情況下,下載只使用一個程序。傳遞自定義的 download_config.num_proc 來改變此行為。

示例

>>> downloaded_files = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')

download_and_extract

< >

( url_or_urls ) extracted_path(s)

引數

  • url_or_urls (strlistdict) — 要下載和提取的 URL 或 URL 的 listdict。每個 URL 都是一個 str

返回

extracted_path(s)

str,給定 URL 的提取路徑。

下載並提取給定的 url_or_urls

大致等同於

extracted_paths = dl_manager.extract(dl_manager.download(url_or_urls))

extract

< >

( path_or_paths ) extracted_path(s)

引數

  • path_or_paths (path 或 listdict) — 要提取的檔案的路徑。每個路徑都是一個 str

返回

extracted_path(s)

str,與給定輸入 path_or_paths 匹配的提取路徑。

提取給定的路徑。

示例

>>> downloaded_files = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')
>>> extracted_files = dl_manager.extract(downloaded_files)

iter_archive

< >

( path_or_buf: typing.Union[str, _io.BufferedReader] ) tuple[str, io.BufferedReader]

引數

  • path_or_buf (strio.BufferedReader) — 歸檔路徑或歸檔二進位制檔案物件。

產生

tuple[str, io.BufferedReader]

迭代歸檔檔案中的檔案。

示例

>>> archive = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')
>>> files = dl_manager.iter_archive(archive)

iter_files

< >

( paths: typing.Union[str, list[str]] ) str

引數

  • paths (strstrlist) — 根路徑。

產生

字串

迭代檔案路徑。

示例

>>> files = dl_manager.download_and_extract('https://huggingface.co/datasets/beans/resolve/main/data/train.zip')
>>> files = dl_manager.iter_files(files)

class datasets.StreamingDownloadManager

< >

( dataset_name: typing.Optional[str] = None data_dir: typing.Optional[str] = None download_config: typing.Optional[datasets.download.download_config.DownloadConfig] = None base_path: typing.Optional[str] = None )

使用“::”分隔符來瀏覽(可能是遠端的)壓縮歸檔檔案的下載管理器。與常規的 DownloadManager 相反,downloadextract 方法實際上不下載也不提取資料,而是返回可以使用 xopen 函式開啟的路徑或 URL,該函式擴充套件了內建的 open 函式,以從遠端檔案流式傳輸資料。

download

< >

( url_or_urls ) url(s)

引數

  • url_or_urls (strlistdict) — 用於流式傳輸資料的檔案 URL。每個 URL 都是一個 str

返回

url(s)

strlistdict),用於流式傳輸資料的 URL,與給定的輸入 url_or_urls 匹配。

規範化用於流式傳輸資料的檔案 URL。這是用於流式傳輸的 DownloadManager.download 的惰性版本。

示例

>>> downloaded_files = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')

download_and_extract

< >

( url_or_urls ) url(s)

引數

  • url_or_urls (strlistdict) — 用於流式傳輸資料的檔案 URL。每個 URL 都是一個 str

返回

url(s)

strlistdict),用於流式傳輸資料的 URL,與給定的輸入 url_or_urls 匹配。

準備給定的 url_or_urls 用於流式傳輸(新增提取協議)。

這是用於流式傳輸的 DownloadManager.download_and_extract 的惰性版本。

等同於

urls = dl_manager.extract(dl_manager.download(url_or_urls))

extract

< >

( url_or_urls ) url(s)

引數

  • url_or_urls (strlistdict) — 用於流式傳輸資料的檔案 URL。每個 URL 都是一個 str

返回

url(s)

strlistdict),用於流式傳輸資料的 URL,與給定的輸入 url_or_urls 匹配。

為給定的 URL 新增提取協議以進行流式傳輸。

這是用於流式傳輸的 DownloadManager.extract 的惰性版本。

示例

>>> downloaded_files = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')
>>> extracted_files = dl_manager.extract(downloaded_files)

iter_archive

< >

( urlpath_or_buf: typing.Union[str, _io.BufferedReader] ) tuple[str, io.BufferedReader]

引數

  • urlpath_or_buf (strio.BufferedReader) — 歸檔路徑或歸檔二進位制檔案物件。

產生

tuple[str, io.BufferedReader]

迭代歸檔檔案中的檔案。

示例

>>> archive = dl_manager.download('https://storage.googleapis.com/seldon-datasets/sentence_polarity_v1/rt-polaritydata.tar.gz')
>>> files = dl_manager.iter_archive(archive)

iter_files

< >

( urlpaths: typing.Union[str, list[str]] ) str

引數

  • urlpaths (strstrlist) — 根路徑。

產生

字串

迭代檔案。

示例

>>> files = dl_manager.download_and_extract('https://huggingface.co/datasets/beans/resolve/main/data/train.zip')
>>> files = dl_manager.iter_files(files)

class datasets.DownloadConfig

< >

( cache_dir: typing.Union[str, pathlib.Path, NoneType] = None force_download: bool = False resume_download: bool = False local_files_only: bool = False proxies: typing.Optional[dict] = None user_agent: typing.Optional[str] = None extract_compressed_file: bool = False force_extract: bool = False delete_extracted: bool = False extract_on_the_fly: bool = False use_etag: bool = True num_proc: typing.Optional[int] = None max_retries: int = 1 token: typing.Union[str, bool, NoneType] = None storage_options: dict = <factory> download_desc: typing.Optional[str] = None disable_tqdm: bool = False )

引數

  • cache_dir (strPath, 可選) — 指定一個快取目錄來儲存檔案(覆蓋預設的快取目錄)。
  • force_download (bool, 預設為 False) — 如果為 True,即使檔案已快取在快取目錄中,也重新下載該檔案。
  • resume_download (bool, 預設為 False) — 如果為 True,並且找到一個未完全接收的檔案,則恢復下載。
  • proxies (dict, 可選) —
  • user_agent (str, 可選) — 可選的字串或字典,將附加到遠端請求的使用者代理上。
  • extract_compressed_file (bool, 預設為 False) — 如果為 True 且路徑指向一個 zip 或 tar 檔案,則在歸檔檔案旁邊的資料夾中提取壓縮檔案。
  • force_extract (bool, 預設為 False) — 如果在 extract_compressed_fileTrue 且歸檔檔案已提取的情況下為 True,則重新提取歸檔檔案並覆蓋其提取的資料夾。
  • delete_extracted (bool, 預設為 False) — 是否刪除(或保留)提取的檔案。
  • extract_on_the_fly (bool, 預設為 False) — 如果為 True,則在讀取壓縮檔案時提取它們。
  • use_etag (bool, 預設為 True) — 是否使用 ETag HTTP 響應頭來驗證快取的檔案。
  • num_proc (int, 可選) — 啟動並行下載檔案的程序數。
  • max_retries (int, 預設為 1) — HTTP 請求失敗時的重試次數。
  • token (strbool, 可選) — 可選的字串或布林值,用作 Datasets Hub 上遠端檔案的 Bearer 令牌。如果為 True 或未指定,將從 ~/.huggingface 獲取令牌。
  • storage_options (dict, 可選) — 傳遞給資料集檔案系統後端(如果有)的鍵值對。
  • download_desc (str, 可選) — 在下載檔案時與進度條一起顯示的描述。
  • disable_tqdm (bool, 預設為 False) — 是否停用單個檔案下載進度條

我們的快取路徑管理器的配置。

class datasets.DownloadMode

< >

( value names = None module = None qualname = None type = None start = 1 )

用於如何處理預先存在的下載和資料的 `Enum`。

預設模式是 `REUSE_DATASET_IF_EXISTS`,如果原始下載和準備好的資料集都存在,則會重用它們。

生成模式

下載 資料集
REUSE_DATASET_IF_EXISTS (預設) 重用 重用
REUSE_CACHE_IF_EXISTS 重用 新的
FORCE_REDOWNLOAD 新的 新的

驗證

class datasets.VerificationMode

< >

( value names = None module = None qualname = None type = None start = 1 )

指定要執行哪些驗證檢查的 `Enum`。

預設模式是 `BASIC_CHECKS`,它只執行基本的檢查,以避免在首次生成/下載資料集時速度變慢。

驗證模式

驗證檢查
ALL_CHECKS 分割檢查,在 GeneratorBuilder 情況下生成的鍵的唯一性
以及下載檔案的有效性(檔案數量、校驗和等)
BASIC_CHECKS (預設) 與 `ALL_CHECKS` 相同,但不檢查下載的檔案
NO_CHECKS

分割

class datasets.SplitGenerator

< >

( name: str gen_kwargs: dict = <factory> )

引數

  • name (str) — 生成器將為其建立樣本的 `Split` 的名稱。
  • **gen_kwargs (附加關鍵字引數) — 轉發到構建器的 `DatasetBuilder._generate_examples` 方法的關鍵字引數。

定義生成器的分割資訊。

這應該作為 `GeneratorBasedBuilder._split_generators` 的返回值使用。更多資訊和使用示例請參見 `GeneratorBasedBuilder._split_generators`。

示例

>>> datasets.SplitGenerator(
...     name=datasets.Split.TRAIN,
...     gen_kwargs={"split_key": "train", "files": dl_manager.download_and_extract(url)},
... )

class datasets.Split

< >

( name )

資料集分割的 `Enum`。

資料集通常被分割成不同的子集,用於訓練和評估的各個階段。

  • TRAIN:訓練資料。
  • VALIDATION:驗證資料。如果存在,這通常用作迭代模型時的評估資料(例如更改超引數、模型架構等)。
  • TEST:測試資料。這是報告指標的資料。通常您不希望在模型迭代期間使用它,因為您可能會對其過擬合。
  • ALL:所有已定義的資料集分割的並集。

所有分割,包括組合,都繼承自 `datasets.SplitBase`。

有關分割的更多資訊,請參閱指南

示例

>>> datasets.SplitGenerator(
...     name=datasets.Split.TRAIN,
...     gen_kwargs={"split_key": "train", "files": dl_manager.download_and extract(url)},
... ),
... datasets.SplitGenerator(
...     name=datasets.Split.VALIDATION,
...     gen_kwargs={"split_key": "validation", "files": dl_manager.download_and extract(url)},
... ),
... datasets.SplitGenerator(
...     name=datasets.Split.TEST,
...     gen_kwargs={"split_key": "test", "files": dl_manager.download_and extract(url)},
... )

class datasets.NamedSplit

< >

( name )

對應於命名分割(train、test 等)的描述符。

示例

每個描述符都可以使用加法或切片與其他描述符組合

split = datasets.Split.TRAIN.subsplit(datasets.percent[0:25]) + datasets.Split.TEST

最終的分割將對應於 25% 的訓練分割與 100% 的測試分割的合併。

一個分割不能被新增兩次,所以以下操作會失敗

split = (
        datasets.Split.TRAIN.subsplit(datasets.percent[:25]) +
        datasets.Split.TRAIN.subsplit(datasets.percent[75:])
)  # Error
split = datasets.Split.TEST + datasets.Split.ALL  # Error

切片只能應用一次。所以以下操作是有效的

split = (
        datasets.Split.TRAIN.subsplit(datasets.percent[:25]) +
        datasets.Split.TEST.subsplit(datasets.percent[:50])
)
split = (datasets.Split.TRAIN + datasets.Split.TEST).subsplit(datasets.percent[:50])

但這無效

train = datasets.Split.TRAIN
test = datasets.Split.TEST
split = train.subsplit(datasets.percent[:25]).subsplit(datasets.percent[:25])
split = (train.subsplit(datasets.percent[:25]) + test).subsplit(datasets.percent[:50])

class datasets.NamedSplitAll

< >

( )

對應於所有已定義資料集分割的並集的分割。

class datasets.ReadInstruction

< >

( split_name rounding = None from_ = None to = None unit = None )

用於資料集的讀取指令。

示例

# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%]')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec('test[:33%]'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction('test', to=33, unit='%'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%'))

# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%]+train[1:-1]')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%]+train[1:-1]'))
ds = datasets.load_dataset('mnist', split=(
datasets.ReadInstruction('test', to=33, unit='%') +
datasets.ReadInstruction('train', from_=1, to=-1, unit='abs')))

# The following lines are equivalent:
ds = datasets.load_dataset('mnist', split='test[:33%](pct1_dropremainder)')
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%](pct1_dropremainder)'))
ds = datasets.load_dataset('mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%', rounding="pct1_dropremainder"))

# 10-fold validation:
tests = datasets.load_dataset(
'mnist',
[datasets.ReadInstruction('train', from_=k, to=k+10, unit='%')
for k in range(0, 100, 10)])
trains = datasets.load_dataset(
'mnist',
[datasets.ReadInstruction('train', to=k, unit='%') + datasets.ReadInstruction('train', from_=k+10, unit='%')
for k in range(0, 100, 10)])

from_spec

< >

( spec )

引數

  • spec (str) — 要讀取的資料集劃分(split) + 可選的切片(slice) + 當使用百分比作為切片單位時可選的舍入方式。可以使用絕對數值(int)或百分比(int)指定切片。

根據字串規範建立一個 ReadInstruction 例項。

示例

test: test split.
test + validation: test split + validation split.
test[10:]: test split, minus its first 10 records.
test[:10%]: first 10% records of test split.
test[:20%](pct1_dropremainder): first 10% records, rounded with the pct1_dropremainder rounding.
test[:-5%]+train[40%:60%]: first 95% of test + middle 20% of train.

to_absolute

< >

( name2len )

引數

  • name2len (dict) — 將資料集劃分名稱與樣本數量關聯起來的字典。

將指令轉換為絕對指令列表。

然後將這些絕對指令相加。

Version

class datasets.Version

< >

( version_str: str description: typing.Optional[str] = None major: typing.Union[str, int, NoneType] = None minor: typing.Union[str, int, NoneType] = None patch: typing.Union[str, int, NoneType] = None )

引數

  • version_str (str) — 資料集版本。
  • description (str) — 描述此版本中的新特性。
  • major (str) —
  • minor (str) —
  • patch (str) —

資料集版本 MAJOR.MINOR.PATCH

示例

>>> VERSION = datasets.Version("1.0.0")
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.