How can I change data inside the dataloader?

username: Melani_Cook
cooked: <p>I collected a dataset from deepmind control suite for a reinforcement learning task. Here is the custom dataset class</p>
<pre><code class="lang-auto">class IterableWrapper(IterableDataset):
    def __init__(self, iterator):
        super().__init__()
        self.iterator = iterator

    def __iter__(self):
        return self.iterator

    def __getitem__(self, index):
        return list(self.iterator)[index]
</code></pre>
<p>and here is the Replay class which collected epiodes from the deepmind control :</p>
<pre><code class="lang-auto">class Replay:
    def __init__(
        self,
        directory,
        capacity=0,
        ongoing=False,
        minlen=1,
        maxlen=0,
        prioritize_ends=False,
    ):
        self._directory = pathlib.Path(directory).expanduser()
        self._directory.mkdir(parents=True, exist_ok=True)
        self._capacity = capacity
        self._ongoing = ongoing
        self._minlen = minlen
        self._maxlen = maxlen
        self._prioritize_ends = prioritize_ends
        self._random = np.random.RandomState()
        # filename -&gt; key -&gt; value_sequence
        self._complete_eps = load_episodes(self._directory, capacity, minlen)
        # worker -&gt; key -&gt; value_sequence
        self._ongoing_eps = collections.defaultdict(
            lambda: collections.defaultdict(list)
        )
        self._total_episodes, self._total_steps = count_episodes(directory)
        self._loaded_episodes = len(self._complete_eps)
        self._loaded_steps = sum(eplen(x) for x in self._complete_eps.values())

    @property
    def stats(self):
        return {
            "total_steps": self._total_steps,
            "total_episodes": self._total_episodes,
            "loaded_steps": self._loaded_steps,
            "loaded_episodes": self._loaded_episodes,
        }

    def add_step(self, transition, worker=0):
        episode = self._ongoing_eps[worker]
        if isnamedtupleinstance(transition):
           for key, value in transition._asdict().items():
               episode[key].append(value)
           if transition.done:
              self.add_episode(episode)
        elif isinstance(transition, dict):
            for key, value in transition.items():
                episode[key].append(value)
            if transition["is_last"]:
                self.add_episode(episode)
        episode.clear()

    def add_episode(self, episode):
        length = eplen(episode)
        if length &lt; self._minlen:
            print(f"Skipping short episode of length {length}.")
            return
        self._total_steps += length
        self._loaded_steps += length
        self._total_episodes += 1
        self._loaded_episodes += 1
        episode = {key: convert(value) for key, value in episode.items()}
        filename = save_episode(self._directory, episode)
        self._complete_eps[str(filename)] = episode
        self._enforce_limit()

    def dataset(self, batch, length):
        # example = next(iter(self._generate_chunks(length)))
        dataset = IterableWrapper(iter(self._generate_chunks(length)))
        dataloader = DataLoader(dataset, batch_size=batch)
        return dataloader

    def _generate_chunks(self, length):
        sequence = self._sample_sequence()
        while True:
            chunk = collections.defaultdict(list)
            added = 0
            while added &lt; length:
                needed = length - added
                adding = {k: v[:needed] for k, v in sequence.items()}
                sequence = {k: v[needed:] for k, v in sequence.items()}
                for key, value in adding.items():
                    chunk[key].append(value)
                added += len(adding["done"])
                if len(sequence["done"]) &lt; 1:
                    sequence = self._sample_sequence()
            chunk = {k: np.concatenate(v) for k, v in chunk.items()}
            yield chunk

    def _sample_sequence(self):
        episodes = list(self._complete_eps.values())
        if self._ongoing:
            episodes += [
                x for x in self._ongoing_eps.values() if eplen(x) &gt;= self._minlen
            ]
        episode = self._random.choice(episodes)
        total = len(episode["done"])
        length = total
        if self._maxlen:
            length = min(length, self._maxlen)
        # Randomize length to avoid all chunks ending at the same time in case the
        # episodes are all of the same length.
        length -= np.random.randint(self._minlen)
        length = max(self._minlen, length)
        upper = total - length + 1
        if self._prioritize_ends:
            upper += self._minlen
        index = min(self._random.randint(upper), total - length)
        sequence = {
            k: convert(v[index : index + length])
            for k, v in episode.items()
            if not k.startswith("log_")
        }
        sequence["is_first"] = np.zeros(len(sequence["done"]), bool)
        sequence["is_first"][0] = True
        if self._maxlen:
            assert self._minlen &lt;= len(sequence["done"]) &lt;= self._maxlen
        return sequence

    def _enforce_limit(self):
        if not self._capacity:
            return
        while self._loaded_episodes &gt; 1 and self._loaded_steps &gt; self._capacity:
            # Relying on Python preserving the insertion order of dicts.
            oldest, episode = next(iter(self._complete_eps.items()))
            self._loaded_steps -= eplen(episode)
            self._loaded_episodes -= 1
            del self._complete_eps[oldest]
</code></pre>
<p>each episode has<code> 's1'</code>, <code>'s2'</code>, <code>'a1'</code>, <code>'a2'</code>, <code>'reward'</code>, <code>'discount'</code> and<code> 'done'</code>. At some point I use this data from some part of my model. However, I want to update the values of <code>'s1'</code>, <code>'s2'</code>. I don’t know how I could extract these fields from the DataLoader. I printed the data<br>
<code>train_dataset = iter(train_replay.dataset(**dataset)) print(f"batch of input data {train_dataset._dataset.__dict__}")</code><br>
and the output is<br>
<code>batch of input data {'iterator': &lt;generator object Replay._generate_chunks at 0x7fdf7bf3c890&gt;}</code><br>
Could anyone kindly help me to modify this dataloader?</p>