Looping over 2 DataLoaders simultaneously (one for images another for numbers)

username: Antonio_Linares
cooked: <p>Hi everyone,</p>
<p>I am training a CNN to detect a specific type of skin cancer. I also have a series of attributes per image, such as the age and sex of the patient that corresponds to the particular image.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a71ab512b88e3fc8ec86090ba35acb27c14a659e.png" data-download-href="https://discuss.pytorch.org/uploads/default/a71ab512b88e3fc8ec86090ba35acb27c14a659e" title="cnn"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71ab512b88e3fc8ec86090ba35acb27c14a659e_2_690x288.png" alt="cnn" data-base62-sha1="nQgXnvioJ4XCdDw5yltxqGaSabs" width="690" height="288" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71ab512b88e3fc8ec86090ba35acb27c14a659e_2_690x288.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71ab512b88e3fc8ec86090ba35acb27c14a659e_2_1035x432.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/7/a71ab512b88e3fc8ec86090ba35acb27c14a659e_2_1380x576.png 2x" data-dominant-color="E4E7ED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">cnn</span><span class="informations">1512×633 34.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I explain the data flow in the diagram above:</p>
<ol>
<li>
<p>The data from the first Data Loader (images ) flows through the CNN, then gets flattened and passed through the linear layer.</p>
</li>
<li>
<p>The data from the second Data Loader (attributes) needs to get appended to the flattened structure that goes from a shape of (batch_size, 240) to a shape of (batch_size, 259).</p>
</li>
</ol>
<p>The question is how to loop over the 2 different Data Loaders to achieve this?</p>
<pre><code class="lang-auto">#defining trainloader with images
mini_b = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(tX_train, ty_train), 
                                       batch_size=100, shuffle=False) 

#defining trainloader with attributes
mini_b2 = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(ext_data_tensor), 
                                       batch_size=100, shuffle=False) 

#training loop
for i in range(500): 
    #iterating through data, one batch at a time
    for x_j, y_j in mini_b:
        #performing one training step
        out = t_step(x_j, y_j, cnn1, opt1) 
    if i%10 == 0:
        print("Epoch: {}, Loss: {:.4f}".format(i, out))
</code></pre>

username: zarkopafilis
cooked: <p>Have you tried zipping the iterators together?</p>
<pre><code class="lang-auto">for b, b_2 in zip(mini_b, mini_b2):
    b_x, b_y = b
    b2_x, b2_y = b_2
</code></pre>

username: Antonio_Linares
cooked: <p>Thanks a lot for your swift reply! Here, the for loop still operates one batch at a time, correct ?</p>

username: zarkopafilis
cooked: <p>Yes, as soon as you make sure that your items are aligned properly, zip will do 1 iteration for the first one and one for the second and then return the result as a tuple.</p>

username: Antonio_Linares
cooked: <p>It worked! Thanks a lot <a class="mention" href="/u/zarkopafilis">@zarkopafilis</a> <img src="https://discuss.pytorch.org/images/emoji/apple/love_you_gesture.png?v=9" title=":love_you_gesture:" class="emoji" alt=":love_you_gesture:"></p>