Dataloader not working in colab

username: Shubham_Singh1
cooked: <p>i am trying to train longformer model for token classification, but some strange thing is happening on colab, as i am trying to iterate through dataloader object it’s not fetching data from dataloader.<br>
when i am using same code on kaggle it’s working perfectly fine.<br>
if code is required then i can provide code along with data which i am using.</p>

username: Andrei_Cristea
cooked: <p>Have you double-checked whether the <code>Dataset</code> you passed to the <code>DataLoader</code> actually returns the items you think it should, when you iterate through it / has a nonzero <code>len()</code>?</p>

username: Shubham_Singh1
cooked: <p>thanks for reply!<br>
i am using same code on kaggle and there it’s running smoothly<br>
yes it’s of non zero lenght, i have checked it’s length before iterating over it, but on google colab i am facing this issue.<br>
fyi my dataloader returns a tuple of two items, to check whether it’s working or not i am unpacking 3 items, ideally it should give me error as soon as it will fetch the items from data loader, what i found out it’s not giving any errors, so from that i speculated that it’s not fetching items from dataloader.</p>
<p>for batch_idx,(inputs,targets,var1) in enumerate(train_loader):<br>
code<br>
ideally it should throw an error here but it’s not doing that(bcz train_loader returns a tuple of 2 items but i am unpacking 3 items)</p>

username: Andrei_Cristea
cooked: <p>Hi Shubham,</p>
<p>That’s weird. Could you run the following two lines and share the output:</p>
<pre><code class="lang-auto">print(len(train_data))
print(next(iter(train_loader)))
</code></pre>
<p>If those print out reasonable output, then it sounds like maybe your code isn’t getting to this line:</p>
<pre><code class="lang-auto">for batch_idx,(inputs,targets,var1) in enumerate(train_loader):
</code></pre>
<p>since this should indeed raise an error due to how you are unpacking. You can add a bunch of print statements at previous steps to see where the code is stopping.</p>
<p>Even though it’s the same code as on kaggle, it’s possible that it would execute in one place but not another due to differences in Python runtime, environment, or other stuff. By following the above steps (printing out something after each line) you’ll be able to pinpoint why the code fails to reach that line.</p>

username: Shubham_Singh1
cooked: <aside class="quote no-group" data-username="Andrei_Cristea" data-post="5" data-topic="150866">
<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/andrei_cristea/48/47843_2.png" class="avatar"> Andrei_Cristea:</div>
<blockquote>
<pre><code class="lang-auto">print(len(train_data))
print(next(iter(train_loader)))
</code></pre>
</blockquote>
</aside>
<p>it’s printing length=3119 which is correct<br>
but when i am printing print(next(iter(train_loader))), i think this command is not getting executed.<br>
and previously  when i tried to figure out where is the problem, my code was getting executed up to line where i am iterating over train_loader, but i think this command(train_loader) was not running.</p>
<p>if you need i can provide my code along with the dataset.</p>
<p>thanks!</p>

username: Andrei_Cristea
cooked: <p>Very strange, could you share the code where you define <code>train_loader</code>?</p>
<p>Even better would be if you have a way to share the entire colab notebook and point to the troublesome section.</p>

username: Shubham_Singh1
cooked: <p>sure i am sharing my colab notebook</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1tKCgC_IBzcIPGWJolPkTVU08VCzf_cWO?usp=sharing">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20220504-060046-RC00_446421109" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1tKCgC_IBzcIPGWJolPkTVU08VCzf_cWO?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1tKCgC_IBzcIPGWJolPkTVU08VCzf_cWO?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>this is link to data which i am using<br>
<a href="https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing</a></p>
<p>and problem is inside  train_fn<br>
def train_fn(model,train_loader,optimizer,scheduler):<br>
thanks!</p>

username: Andrei_Cristea
cooked: <p>Can’t replicate without this file:<br>
<code>'/content/drive/MyDrive/feedback_price_saurabh.csv'</code></p>
<p>I would drop a debugger here, inside <code>class TrainData(Dataset)</code>, and step through the code to see where it stops, and why it returns nothing:</p>
<pre><code class="lang-auto">def __getitem__(self,item):
    import ipdb; ipdb.set_trace()  # go step by step from here
    text=self.df['text'][item]
    ...
</code></pre>

username: Shubham_Singh1
cooked: <aside class="quote no-group" data-username="Shubham_Singh1" data-post="8" data-topic="150866">
<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/shubham_singh1/48/48889_2.png" class="avatar"> Shubham_Singh1:</div>
<blockquote>
<p><a href="https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing" rel="noopener nofollow ugc">https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing</a></p>
</blockquote>
</aside>
<p><a href="https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing</a><br>
from here you can download the data</p>

username: Shubham_Singh1
cooked: <aside class="quote no-group" data-username="Shubham_Singh1" data-post="10" data-topic="150866">
<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/shubham_singh1/48/48889_2.png" class="avatar"> Shubham_Singh1:</div>
<blockquote>
<p><a href="https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing" rel="noopener nofollow ugc">https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing</a></p>
</blockquote>
</aside>
<p>here is the file<br>
<a href="https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1Xv2mT8JQgwxL1-2x5ZFz6GB1tjjLiWXk/view?usp=sharing</a><br>
plz download from here</p>

username: Andrei_Cristea
cooked: <p>Hi - not sure how this worked on a different runtime with the same data, but your issue is that the code is getting stuck in an infinite loop here:</p>
<pre><code class="lang-auto">while i&lt;l:
            while j&lt;len(w_ids):
                if w_ids[j]!=None and w_ids[j]==previous:
                    i=i-1
                    #print(i,j)
                    target[j]=labels[i]
                    previous=w_ids[j]
                    i=i+1
                elif w_ids[j]!=None and w_ids[j]!=previous:
                    #print(i,j)
                    target[j]=labels[i]
                    previous=w_ids[j]
                    i=i+1
                j=j+1
</code></pre>
<p>Specifically, j reaches len(w_ids) before i reaches l, and therefore the outer loop just loops forever with the inner loop not executing anymore.</p>
<pre><code class="lang-auto">ipdb&gt; print(j, len(w_ids))
1500 1500
ipdb&gt; print(i, l)
251 919
</code></pre>
<p>This has nothing to do with the DataLoader or anything, just the internal logic there.</p>
<p>Good luck!</p>

username: Shubham_Singh1
cooked: <p>sorry to bother you, but i figured out my mistake, data type of labels columns should be list, and in kaggle i was changing it from str to list but in colab i was not changing it from str to list.<br>
i appreciate you a lot for your help!<br>
and my apologies for the trouble that i caused unnecessarily.</p>
<p>how can i close this discussion?<br>
thanks !</p>

username: Andrei_Cristea
cooked: <p>Glad you were able to figure it out! You could close it by giving me a “solution” on the post where I spotted the infinite loop. Or you can just leave it as is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Good luck in your Kaggle competitions!</p>

username: Shubham_Singh1
cooked: <p>i gave the solution on the post where you spotted the infinite loop.<br>
I appreciate your time a lot.<br>
thanks!</p>