すべてのDatasetBuilder
、分割として定義されたさまざまなデータサブセットを公開します(例: train
、 test
)。 tfds.load()
またはtfds.DatasetBuilder.as_dataset()
いずれかを使用してtf.data.Dataset
インスタンスを構築する場合、取得する分割を指定できます。分割のスライスおよびそれらの組み合わせを取得することもできます。
スライスAPI
スライス命令は、 tfds.load
またはtfds.DatasetBuilder.as_dataset
指定されます。
命令は、文字列またはReadInstruction
のいずれかとして提供できます。文字ReadInstruction
はよりコンパクトで単純な場合に読みやすくなりますが、 ReadInstruction
はより多くのオプションを提供し、可変スライスパラメータで使用する方が簡単な場合があります。
例
文字列APIを使用した例:
# The full `train` split.
train_ds = tfds.load('mnist', split='train')
# The full `train` split and the full `test` split as two distinct datasets.
train_ds, test_ds = tfds.load('mnist', split=['train', 'test'])
# The full `train` and `test` splits, interleaved together.
train_test_ds = tfds.load('mnist', split='train+test')
# From record 10 (included) to record 20 (excluded) of `train` split.
train_10_20_ds = tfds.load('mnist', split='train[10:20]')
# The first 10% of train split.
train_10pct_ds = tfds.load('mnist', split='train[:10%]')
# The first 10% of train + the last 80% of train.
train_10_80pct_ds = tfds.load('mnist', split='train[:10%]+train[-80%:]')
# 10-fold cross-validation (see also next section on rounding behavior):
# The validation datasets are each going to be 10%:
# [0%:10%], [10%:20%], ..., [90%:100%].
# And the training datasets are each going to be the complementary 90%:
# [10%:100%] (for a corresponding validation set of [0%:10%]),
# [0%:10%] + [20%:100%] (for a validation set of [10%:20%]), ...,
# [0%:90%] (for a validation set of [90%:100%]).
vals_ds = tfds.load('mnist', split=[
f'train[{k}%:{k+10}%]' for k in range(0, 100, 10)
])
trains_ds = tfds.load('mnist', split=[
f'train[:{k}%]+train[{k+10}%:]' for k in range(0, 100, 10)
])
ReadInstruction
API(上記と同等)を使用した例:
# The full `train` split.
train_ds = tfds.load('mnist', split=tfds.core.ReadInstruction('train'))
# The full `train` split and the full `test` split as two distinct datasets.
train_ds, test_ds = tfds.load('mnist', split=[
tfds.core.ReadInstruction('train'),
tfds.core.ReadInstruction('test'),
])
# The full `train` and `test` splits, interleaved together.
ri = tfds.core.ReadInstruction('train') + tfds.core.ReadInstruction('test')
train_test_ds = tfds.load('mnist', split=ri)
# From record 10 (included) to record 20 (excluded) of `train` split.
train_10_20_ds = tfds.load('mnist', split=tfds.core.ReadInstruction(
'train', from_=10, to=20, unit='abs'))
# The first 10% of train split.
train_10_20_ds = tfds.load('mnist', split=tfds.core.ReadInstruction(
'train', to=10, unit='%'))
# The first 10% of train + the last 80% of train.
ri = (tfds.core.ReadInstruction('train', to=10, unit='%') +
tfds.core.ReadInstruction('train', from_=-80, unit='%'))
train_10_80pct_ds = tfds.load('mnist', split=ri)
# 10-fold cross-validation (see also next section on rounding behavior):
# The validation datasets are each going to be 10%:
# [0%:10%], [10%:20%], ..., [90%:100%].
# And the training datasets are each going to be the complementary 90%:
# [10%:100%] (for a corresponding validation set of [0%:10%]),
# [0%:10%] + [20%:100%] (for a validation set of [10%:20%]), ...,
# [0%:90%] (for a validation set of [90%:100%]).
vals_ds = tfds.load('mnist', [
tfds.core.ReadInstruction('train', from_=k, to=k+10, unit='%')
for k in range(0, 100, 10)])
trains_ds = tfds.load('mnist', [
(tfds.core.ReadInstruction('train', to=k, unit='%') +
tfds.core.ReadInstruction('train', from_=k+10, unit='%'))
for k in range(0, 100, 10)])
tfds.even_splits
tfds.even_splits
は、同じサイズの重複しないサブスプリットのリストを生成します。
assert tfds.even_splits('train', n=3) == [
'train[0%:33%]', 'train[33%:67%]', 'train[67%:100%]',
]
スライスと丸めの割合
分割のスライスがパーセント( %
)単位を使用して要求され、要求されたスライス境界が100
で均等に分割されない場合、デフォルトの動作では、境界を最も近い整数( closest
)に丸めます。これは、一部のスライスに他のスライスよりも多くの例が含まれている可能性があることを意味します。例えば:
# Assuming "train" split contains 101 records.
# 100 records, from 0 to 100.
tfds.load("mnist", split="test[:99%]")
# 2 records, from 49 to 51.
tfds.load("mnist", split="test[49%:50%]")
または、ユーザーは丸めpct1_dropremainder
を使用できるため、指定されたパーセンテージ境界は1%の倍数として扱われます。このオプションは、一貫性が必要な場合に使用する必要があります(例: len(5%) == 5 * len(1%)
)。これは、 info.split[split_name].num_examples % 100 != 0
場合、最後の例が切り捨てられる可能性があることを意味しinfo.split[split_name].num_examples % 100 != 0
。
例:
# Records 0 (included) to 99 (excluded).
split = tfds.core.ReadInstruction(
'test',
to=99,
rounding='pct1_dropremainder',
unit = '%',
)
tfds.load("mnist", split=split)
再現性
サブスプリットAPIは、データセットのメジャーバージョンが一定である限り、特定のスプリットスライス(またはReadInstruction
)が特定のデータセットで常に同じレコードセットを生成することを保証します。
たとえば、 tfds.load("mnist:3.0.0", split="train[10:20]")
およびtfds.load("mnist:3.2.0", split="train[10:20]")
一部のレコードの値が異なる場合でも(プラットフォーム、アーキテクチャなどに関係なく)、常に同じ要素が含まれます(例:画像エンコーディング、ラベルなど)。