Each value is converted to a tensor weirdly in my Dataset

Farzam: <p>Hey everybody, I have these 2 lists as <code>inputs_ids</code> and <code>labels_ids</code>; values are token_ids that represent one token:</p>
<p><code>batch = 1</code></p>
<p>input_ids:</p>
<pre><code class="lang-auto">[[30, 26, 22, 21, 9, 1, 21, 26, 23],
 [30, 26, 22, 0, 9, 14, 32, 2, 4, 0],
 [30, 26, 22, 6, 24, 9, 1, 6, 24, 26, 31],
 [30, 25, 12, 18, 3, 9, 27, 8],
 [5, 12, 28, 20, 9, 15, 14, 28, 11],
 [5, 12, 28, 10, 9, 19, 14, 28, 11],
 [10, 29, 20, 9, 17, 16, 13]]
</code></pre>
<p>labels_ids:</p>
<pre><code class="lang-auto">[[26, 22, 21, 9, 1, 21, 26, 23, 9],
 [26, 22, 0, 9, 14, 32, 2, 4, 0, 9],
 [26, 22, 6, 24, 9, 1, 6, 24, 26, 31, 9],
 [25, 12, 18, 3, 9, 27, 8, 9],
 [12, 28, 20, 9, 15, 14, 28, 11, 9],
 [12, 28, 10, 9, 19, 14, 28, 11, 9],
 [29, 20, 9, 17, 16, 13, 9]]
</code></pre>
<p>I tried to build a <strong>dataset</strong> class and then a <strong>dataloader</strong> object:</p>
<pre><code class="lang-auto">class TokenDataset(Dataset):
  def __init__(self,inputs_ids: List,labels_ids: List) -&gt; None:
    self.inputs_ids = inputs_ids
    self.labels_ids = labels_ids

  def __len__(self):
    return len(self.labels_ids)

  def __getitem__(self, idx):
    input = self.inputs_ids[idx]
    label = self.labels_ids[idx]

    return input,label

dataset_dclass = TokenDataset(inputs_ids , labels_ids)
dataloader_dclass = DataLoader(dataset=dataset_dclass , batch_size=batch)
</code></pre>
<p>The only problem is that all values in each input and label are converted to a <code>tensor</code> ! I’m wonder why and I can’t understand:</p>
<p><code>dataset_dclass[0]</code>:</p>
<ul>
<li>Output:</li>
</ul>
<p><code>([30, 26, 22, 21, 9, 1, 21, 26, 23], [26, 22, 21, 9, 1, 21, 26, 23, 9])</code></p>
<p><code>next(iter(dataloader_dclass))</code></p>
<ul>
<li>Output:</li>
</ul>
<pre><code class="lang-auto">[[tensor([30]),tensor([26]),tensor([22]),tensor([21]),tensor([9]),tensor([1]),tensor([21]),tensor([26]),
  tensor([23])],
 [tensor([26]),tensor([22]),tensor([21]),tensor([9]),tensor([1]),tensor([21]),tensor([26]),tensor([23]),tensor([9])]]
</code></pre>
<ul>
<li>I expect to get this as an output for <code>next(iter(dataloader_dclass))</code> :</li>
</ul>
<p><code>[ tensor([30, 26, 22, 21, 9, 1, 21, 26, 23]) ,  tensor([26, 22, 21, 9, 1, 21, 26, 23, 9]) ]</code></p>

Farzam: <p>I forgot to convert values into tensors, and this (converting all values into tensors) happened automatically by torch!</p>
<p>Also, I assigned a lambda function to dataloader <code>lambda x : x[0]</code> to decrease the dimension.</p>
<pre><code class="lang-auto">class TokenDataset(Dataset):
  def __init__(self,inputs_ids: List,labels_ids: List) -&gt; None:
    self.inputs_ids = inputs_ids
    self.labels_ids = labels_ids

  def __len__(self):
    return len(self.labels_ids)

  def __getitem__(self, idx):
    input = torch.tensor(self.inputs_ids[idx])
    label = torch.tensor(self.labels_ids[idx])

    return input,label

dataset_dclass = TokenDataset(inputs_ids , labels_ids)
dataloader_dclass = DataLoader(dataset=dataset_dclass , batch_size=batch ,  collate_fn= lambda x : x[0])
</code></pre>
<p><code>next(iter(dataloader_dclass))</code>:</p>
<ul>
<li>output:</li>
</ul>
<pre><code class="lang-auto">(tensor([30, 26, 22, 21,  9,  1, 21, 26, 23]),
tensor([26, 22, 21,  9,  1, 21, 26, 23,  9]))
</code></pre>