tensorflow / tensorflow/datasets

.take after .shuffle doesn't work as expected

Open
#4,092 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.6k
Forks
1.6k
Avg merge
3h 54m
Merged PRs (30d)
1

Description

Performing .take(1).repeat() after .shuffle(N) returns N different elements.

Reproduction instructions

dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4])
dataset = dataset.shuffle(4)
dataset = dataset.take(1)
dataset = dataset.repeat()
dataset = dataset.take(10)
for x in dataset:
  print(x)

This will print 10 different, random integers in the range 1 - 4.

Expected behavior
I would have expected this to print only a single unique integer. It appears the early .shuffle interferes with the later .take, which is very counterintuitive.

See this colab:

https://colab.research.google.com/drive/1UYu3TyzMd2nPs19YyNrViFQZxfSyw8WR?usp=sharing

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Python reproduction with tf.data.Dataset, shuffle(4), take(1), repeat(), and take(10), then compare the output with the linked Colab. Trace how these chained operations interact and confirm that the completed behavior yields one repeated value rather than ten different elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.