# Should we split the trajectories prior to calculating the loss for a DQN?

**URL:** <https://discuss.pytorch.org/t/should-we-split-the-trajectories-prior-to-calculating-the-loss-for-a-dqn/221749>\
**Category:** reinforcement-learning\
**Created:** [July 23, 2025, 1:01pm UTC](https://discuss.pytorch.org/t/should-we-split-the-trajectories-prior-to-calculating-the-loss-for-a-dqn/221749 "2025-07-23T13:01:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![icarosadero](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/icarosadero/32/16461_2.png) [@icarosadero](https://discuss.pytorch.org/u/icarosadero)\
**Post date:** [July 23, 2025, 1:01pm UTC](https://discuss.pytorch.org/t/should-we-split-the-trajectories-prior-to-calculating-the-loss-for-a-dqn/221749/1 "2025-07-23T13:01:26Z")

</div>

Consider this minimal example where data is sampled from a collector according to a MLP policy:

```python
from torchrl.collectors import SyncDataCollector
from torchrl.modules import MLP, QValueModule
from torchrl.collectors.utils import split_trajectories

mlp = Mod(
    MLP(
        out_features = env.action_spec.shape[-1],
        num_cells = [10],
        device = device
    ),
    in_keys = ["observation"],
    out_keys = ["action_value"],
).to(device)
qval = QValueModule(spec=env.action_spec)
test_policy = Seq(mlp, qval)
test_policy(env.reset(seed = 0))

collector = SyncDataCollector(
    env,
    test_policy,
    frames_per_batch=frames_per_batch,
    total_frames=-1
)
loss = DQNLoss(test_policy, action_space=env.action_spec, loss_function='smooth_l1')

data = next(iter(collector))
print("Without split", loss(data.to(device))["loss"])
print("With split", loss(split_trajectories(data).to(device))["loss"])

```

The outputs are:

```auto
Without split tensor(91.7286, device='cuda:0', grad_fn=<MeanBackward0>)
With split tensor(33.9880, device='cuda:0', grad_fn=<MeanBackward0>)

```

Which of these two cases should I use for training a model?

---

<div class="post-metadata">

**Author:** ![vmoens](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmoens/32/26121_2.png) [@vmoens](https://discuss.pytorch.org/u/vmoens)\
**Post date:** [August 4, 2025, 9:31pm UTC](https://discuss.pytorch.org/t/should-we-split-the-trajectories-prior-to-calculating-the-loss-for-a-dqn/221749/2 "2025-08-04T21:31:18Z")

</div>

I don’t have access to the env and other params in this script but my take is that your second example (with split) has padded data as input and the loss is computed over all the data, padded and non-padded (I don’t think DQN checks for a mask key when being executed). If that’s a desired feature feel free to open an issue!

But TLDR the first (non-split) is the right one - no need to split the data, everything is designed to work with concatenations of trajectories.
