# For (images, labels) in train\_loader:?

**URL:** <https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460>\
**Category:** vision\
**Created:** [April 13, 2019, 8:16am UTC](https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460 "2019-04-13T08:16:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cverlpeng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cverlpeng/32/13070_2.png) [@Cverlpeng](https://discuss.pytorch.org/u/Cverlpeng)\
**Post date:** [April 13, 2019, 8:16am UTC](https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460/1 "2019-04-13T08:16:12Z")

</div>

hi,  
code

```python
 train_loader = data.DataLoader(
        train_loader,
        batch_size=cfg["training"]["batch_size"],
        num_workers=cfg["training"]["num_workers"],
        shuffle=True,
    )

while i <= cfg["training"]["train_iters"] and flag:
        for idx,(images, labels) in enumerate(tbar):
            i += 1
            print('epoch {}--------------------------'.format(i))
            print('iter {}---------------------------'.format(idx))
            start_ts = time.time()
            scheduler.step()
            model.train()
            images = images.to(device)
            labels = labels.to(device)

            optimizer.zero_grad()
            outputs = model(images)

```

result

 ![image](https://discuss.pytorch.org/uploads/default/original/2X/6/6cf886203113e8d7f9dabb86c913c7db0ffadd6b.png)  
but I expect is printed epoch =1 , Why is the outer loop and the inner loop the same? Is it relevant to set collat​​e\_fn? thanks !!!

```python
epoch 1--------------------------
iter 0---------------------------
Train loss: 1.4994: 0%|▌ | 1/225 [00:09<35:33, 9.53s/it]
epoch 1--------------------------
iter 1---------------------------
Train loss: 1.5003: 1%|█▏ | 2/225 [00:10<25:24, 6.84s/it]
epoch 1--------------------------
iter 2---------------------------
Train loss: 1.5007: 1%|█▋ | 3/225 [00:10<18:19, 4.95s/it]
epoch 1--------------------------
iter 3---------------------------
Train loss: 1.5000: 2%|██▎ | 4/225 [00:11<13:22, 3.63s/it]
epoch 1--------------------------
iter 4---------------------------
Train loss: 1.4983: 2%|██▊ | 5/225 [00:11<09:56, 2.71s/it]
epoch 1--------------------------
iter 5---------------------------
Train loss: 1.4978: 3%|███▍ | 6/225 [00:12<07:31, 2.06s/it]
epoch 1--------------------------
iter 6---------------------------
Train loss: 1.4979: 3%|███▉ | 7/225 [00:12<05:51, 1.61s/it]
epoch 1--------------------------
iter 7---------------------------
Train loss: 1.4962: 4%|████▌ | 8/225 [00:13<04:40, 1.29s/it]
epoch 1--------------------------
iter 8---------------------------
Train loss: 1.4960: 4%|█████ | 9/225 [00:13<03:52, 1.07s/it]
epoch 1--------------------------
iter 9---------------------------
```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 13, 2019, 10:32am UTC](https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460/2 "2019-04-13T10:32:13Z")

</div>

The `Dataloader` loop (inner loop) corresponds to one epoch, so you should increase `i` outside of this loop:

```python
for epoch in range(epochs):
    for batch_idx, (data, target) in enumerate(loader):
        print('Epoch {}, iter {}'.format(epoch, batch_idx))

```

---

<div class="post-metadata">

**Author:** ![Cverlpeng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cverlpeng/32/13070_2.png) [@Cverlpeng](https://discuss.pytorch.org/u/Cverlpeng)\
**Post date:** [April 13, 2019, 11:24am UTC](https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460/3 "2019-04-13T11:24:27Z")

</div>

I try to add ’ for ',but get same result.:喜悦:

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 13, 2019, 7:40pm UTC](https://discuss.pytorch.org/t/for-images-labels-in-train-loader/42460/4 "2019-04-13T19:40:43Z")

</div>

It looks like `cfg["training"]["train_iters"]` corresponds to the epochs, so just move the increment of `i` before the for loop:

```python
while i <= cfg["training"]["train_iters"] and flag:
        i += 1
        for idx,(images, labels) in enumerate(tbar):
            print('epoch {}--------------------------'.format(i))
            print('iter {}---------------------------'.format(idx))

```
