資料集文件

建構器類別

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

構建器類別

構建器 (Builders)

🤗 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_id: typing.Optional[str] = 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, 選填) — 針對數據集構建器程式碼的雜湊值 (hash)。用於在數據集構建器程式碼更新時更新快取目錄(以避免重複使用舊數據)。典型的快取目錄(定義在 self._relative_data_dir 中)為 name/version/hash/
  • base_path (str, 選填) — 用於下載檔案的相對路徑之基礎路徑。這可以是一個遠端 URL。
  • features (Features, 選填) — 此數據集使用的特徵類型。例如,它可用於更改數據集的 Features 類型。
  • token (strbool, 選填) — 用作 Datasets Hub 上遠端檔案之 Bearer token 的字串或布林值。如果為 True,將會從 "~/.huggingface" 取得 token。
  • repo_id (str, 選填) — 數據集儲存庫的 ID。用於區分名稱相同但來自不同命名空間的構建器,例如“rajpurkar/squad”與“lhoestq/squad”儲存庫 ID。在後者中,構建器名稱將為“lhoestq___squad”。
  • data_files (strSequenceMapping, 選填) — 原始數據檔案的路徑。對於像 “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) — 是否將數據複製到記憶體中。

回傳指定分割的數據集 (Dataset)。

範例

>>> 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, 選填) — 要使用的特定 DownloadManger
  • 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], 選填) — 每個 shard 寫入的最大位元組數,預設為 “500MB”。大小基於未壓縮數據的大小,因此實際上您的 shard 檔案可能會小於 max_shard_size,例如由於 Parquet 壓縮的緣故。

    於 2.5.0 新增

  • num_proc (int, 選填, 預設為 None) — 在本地下載和生成數據集時的處理程序數量。預設禁用多重處理。

    於 2.7.0 新增

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

    於 2.5.0 新增

  • **download_and_prepare_kwargs (額外關鍵字參數) — 關鍵字參數。

下載並準備用於讀取的數據集。

範例

下載並將數據集準備為 Arrow 檔案,這些檔案可以使用 builder.as_dataset() 作為數據集加載

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

在本地下載並準備數據集為分片 (sharded) 的 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_id: typing.Optional[str] = None **config_kwargs )

基底類別,用於基於字典生成器的數據生成數據集。

GeneratorBasedBuilder 是一個便捷類別,它抽象化了 DatasetBuilder 的許多數據寫入與讀取工作。它要求子類別實作跨數據集分割 (dataset splits) 的特徵字典生成器 (_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_id: typing.Optional[str] = None **config_kwargs )

基底類別,用於基於 Arrow 加載函數 (CSV/JSON/Parquet) 生成數據的數據集。

class datasets.BuilderConfig

< >

( name: str = 'default' version: typing.Union[datasets.utils.version.Version, str, 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 )

config id 用於建立快取目錄。預設情況下,它等於配置名稱。然而,配置名稱不足以作為正在生成的數據集的唯一識別碼,因為它沒有考慮到

  • 可用於覆蓋屬性的 config kwargs
  • 用於寫入數據集的自定義特徵
  • json/text/csv/pandas 數據集的 data_files

因此,config 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 ) strlistdict

參數

  • 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 ) 解壓縮後的路徑

參數

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

返回

解壓縮後的路徑

str,給定 URL 的解壓縮路徑。

下載並解壓縮給定的 url_or_urls

大約等同於

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

extract

< >

( path_or_paths ) 解壓縮後的路徑

參數

  • path_or_paths (path 或 listdict) — 要解壓縮的檔案路徑。每個路徑皆為 str

返回

解壓縮後的路徑

str,與給定的 input 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) — 封存檔路徑或封存二進位檔案物件。

Yields

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) — 根路徑。

Yields

str

迭代檔案路徑。

範例

>>> files = dl_manager.download_and_extract('https://huggingface.co/datasets/AI-Lab-Makerere/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;xopen 函式擴充了內建的 open 函式,以從遠端檔案串流傳輸資料。

download (下載)

< >

( url_or_urls ) url(s)

參數

  • url_or_urls (strlistdict) — 用於串流資料的檔案 URL。每個 url 皆為 str

返回

url(s)

(strlistdict),與給定的輸入 url_or_urls 相符、用於串流資料的 URL。

標準化用於串流資料的檔案 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_or_urls 相符、用於串流資料的 URL。

為串流準備指定的 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_or_urls 相符、用於串流資料的 URL。

為指定的 url(s) 添加用於串流的解壓縮協定。

這是用於串流的 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) — 封存檔路徑或封存二進位檔案物件。

Yields

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) — 根路徑。

Yields

str

迭代檔案。

範例

>>> files = dl_manager.download_and_extract('https://huggingface.co/datasets/AI-Lab-Makerere/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選填) — 將附加到遠端請求 User-Agent 的選填字串或字典。
  • 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 token 的選填字串或布林值。若為 True 或未指定,將會從 ~/.huggingface 取得 token。
  • 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 檢查下載檔案的分割與有效性(檔案數量、校驗和)
BASIC_CHECKS (預設) ALL_CHECKS 相同,但不檢查下載的檔案
NO_CHECKS None

分割 (Splits)

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: str )

對應於具名分割(train, test, …)的描述元。

範例

每個描述元皆可透過加法或切片 (slice) 與其他描述元組合

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('ylecun/mnist', split='test[:33%]')
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction.from_spec('test[:33%]'))
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction('test', to=33, unit='%'))
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%'))

# The following lines are equivalent:
ds = datasets.load_dataset('ylecun/mnist', split='test[:33%]+train[1:-1]')
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%]+train[1:-1]'))
ds = datasets.load_dataset('ylecun/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('ylecun/mnist', split='test[:33%](pct1_dropremainder)')
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction.from_spec(
'test[:33%](pct1_dropremainder)'))
ds = datasets.load_dataset('ylecun/mnist', split=datasets.ReadInstruction(
'test', from_=0, to=33, unit='%', rounding="pct1_dropremainder"))

# 10-fold validation:
tests = datasets.load_dataset(
'ylecun/mnist',
[datasets.ReadInstruction('train', from_=k, to=k+10, unit='%')
for k in range(0, 100, 10)])
trains = datasets.load_dataset(
'ylecun/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)來指定切片。

從字串規範(string spec)建立一個 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.