# What is the correct way of using get\_dataset\_shard?

**URL:** https://discuss.ray.io/t/what-is-the-correct-way-of-using-get-dataset-shard/23093
**Category:** Ray Train
**Created:** [September 11, 2025, 7:59am UTC](https://discuss.ray.io/t/what-is-the-correct-way-of-using-get-dataset-shard/23093 "2025-09-11T07:59:42Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![mitul93](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mitul93](https://discuss.ray.io/u/mitul93)
#### Post date: [September 11, 2025, 7:59am UTC](https://discuss.ray.io/t/what-is-the-correct-way-of-using-get-dataset-shard/23093/1 "2025-09-11T07:59:42Z")

</div>

**1. Severity of the issue: (select one)**  
 Low: Annoying but doesn’t hinder my work.

**2. Environment:**

- Ray version: 2.49.0
- Python version: 3.10
- OS: Ubuntu 24.04
- Cloud/Infrastructure: AWS
- Other libs/tools (if relevant):

Where should I call `get_dataset_shard` in training loop worker function? - Inside epoch iterator or outside epoch iterator.

In following example, `get_dataset_shard` is called before iterating over epoch

> **[Distributed training of a DLinear time-series model — Ray 2.49.1](https://docs.ray.io/en/latest/ray-overview/examples/e2e-timeseries/e2e_timeseries/01-Distributed-Training.html#set-up-a-training-function)**

```python
def training_loop_per_worker()
    ...
    # === Get Data ===
    train_ds = get_dataset_shard("train")
    ...
    for epoch in range(config["train_epochs"]):
        ...
    ...

```

In another example, it is called inside epoch iterator.

> **[Fine-tuning a face mask detection model with Faster R-CNN — Ray 2.49.1](https://docs.ray.io/en/latest/ray-overview/examples/object-detection/1.object_detection_train.html)**

```python
def train_func(config):
    ...
    for epoch in range(config["epochs"]):
        ...
        train_dataset_shard = train.get_dataset_shard("train")

```
