Test Loader returns the same batch in each iteration

username: luka_sx
cooked: <p>My code is structured like this:</p>
<pre><code class="lang-auto">for epoch in range(args.max_epochs):
    model.train(True)
    ...
    for batch_idx, input in enumerate(train_loader):
        ...
    model.eval()
    testframe = next(iter(test_loader))
    sample_frame(model, testframe)
</code></pre>
<p>Somehow when generating samples, the batch the generated frames are conditioned on, is the same everytime.</p>

username: JuanFMontesinos
cooked: <p>I guess it’s a matter of setting iterator beforehand like<br>
iterator = iter(test_loader)<br>
next(iterator)</p>
<p>maybe this way you are initializing the iterator the whole time</p>

username: luka_sx
cooked: <p>Oh, I see. I’ll check if defining the iterator before the training loop will solve the problem, but I’m pretty sure it will. Thx!</p>

username: luka_sx
cooked: <p>Actually I still get the same testing batch, even when I define the Iterator before the training loop <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

username: JuanFMontesinos
cooked: <aside class="quote no-group" data-username="luka_sx" data-post="1" data-topic="44500">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/luka_sx/48/11154_2.png" class="avatar"> luka_sx:</div>
<blockquote>
<p>samples</p>
</blockquote>
</aside>
<p>Hi,<br>
can you check if your database is properly defined?<br>
if you call <strong>getitem</strong>(idx) manually are you getting different samples?</p>

username: alex.veuthey
cooked: <p>Why don’t use use the same way of calling the <code>test_loader</code> as the <code>train_loader</code>?</p>
<p>Specifically:</p>
<pre><code class="lang-python">model.eval()
for batch_idx, input in enumerate(test_loader):
    sample_frame(model, input)
</code></pre>
<p>or something similar…</p>

username: luka_sx
cooked: <p>Because I need only a single test batch for sampling from my model. But in every training loop iteration (actually every 10th) I would like to get a different test batch.</p>

username: luka_sx
cooked: <p>When I call <code>__getitem__(idx)</code> on the test dataset I get different samples as expected. So maybe it would work to simply pull the samples from the dataset instance without using a Dataloader?</p>

username: MariosOreo
cooked: <p>Hi,</p>
<p>My opinion is similar to <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a>’s. I think you should defining the iterator outside these two for-loops, if you only move it outside the training loop, you will get the same samplings again and again.</p>
<p>What you do should like this:</p>
<pre><code class="lang-auto">test_iterator = iter(test_loader)
for epoch in range(args.max_epochs):
    model.train(True)
    ...
    for batch_idx, input in enumerate(train_loader):
        ...
    model.eval()
    test_samples = next(test_iterator)
    ...
</code></pre>

username: luka_sx
cooked: <p>I tried this already after <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a> first reply, but it didn’t work. Now I found that when setting <code>num_workers</code> to &gt;0 it suddenly works. I have no idea why though.</p>

username: eason_long
cooked: <p>This is the main reason i bet.</p>