# Data loader without labels?

**URL:** <https://discuss.pytorch.org/t/data-loader-without-labels/67107>\
**Category:** Uncategorized\
**Created:** [January 19, 2020, 6:03pm UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107 "2020-01-19T18:03:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![f3ba](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/4491bb/32.png) [@f3ba](https://discuss.pytorch.org/u/f3ba)\
**Post date:** [January 19, 2020, 6:03pm UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107/1 "2020-01-19T18:03:29Z")

</div>

Is there a way to the `DataLoader` machinery with unlabeled data?

---

<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:** [January 20, 2020, 2:11am UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107/2 "2020-01-20T02:11:24Z")

</div>

Yes, `DataLoader` doesn’t have any conditions on the number of outputs of your `Dataset` as seen here:

```python
class MyDataset(Dataset):
    def __init__ (self):
        self.data = torch.randn(100, 1)
        
    def __getitem__ (self, index):
        x = self.data[index]
        return x
    
    def __len__ (self):
        return len(self.data)

dataset = MyDataset()
loader = DataLoader(
    dataset,
    batch_size=5,
    num_workers=2,
    shuffle=True
)

for data in loader:
    print(data.shape)

```

---

<div class="post-metadata">

**Author:** ![naty88](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/naty88/32/36984_2.png) [@naty88](https://discuss.pytorch.org/u/naty88)\
**Post date:** [April 11, 2021, 2:33pm UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107/3 "2021-04-11T14:33:40Z")

</div>

Hello, I am working with DataLoader for the first time and have some problems.  
I have my data in json file like {“doc\_id\_1”: [sentence1, sentence2, …], …} and want to create a DataLoader with these data to get then a sentence embedding. The problem is that, there are no labels (doc\_id is not a true label, it is just the id of the document).

I have defined the class straightforward like in the example above:

```auto
class DocDataset(Dataset):
    def __init__ (self, json_file):
        self.data = json.load(open(json_file))

    def __getitem__ (self, index):
        x = self.data[index]
        return x

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

json_file = 'dataset.json'
dataset = DocDataset(json_file)
loader = DataLoader(dataset, batch_size=5, num_workers=2, shuffle=True)

```

I might have a problem in **getitem** function, because I have a “KeyError”.

Thank you an advance!

---

<div class="post-metadata">

**Author:** ![naty88](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/naty88/32/36984_2.png) [@naty88](https://discuss.pytorch.org/u/naty88)\
**Post date:** [April 11, 2021, 3:53pm UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107/4 "2021-04-11T15:53:09Z")

</div>

Hello again, I think, I could solve the problem with KeyError:

```auto
    def __getitem__ (self, index):
        for i, sent_list in enumerate(self.data.values()):
            x = list(self.data.values())[i]
        return x

```

I can’t get data.shape like in your example, since I’m working with list. And according to the next Error I’ve got, this must not be a list. So I defined it still wrong. Could you please tell me, where I get wrong?

```auto
train_dataloader = DataLoader(dataset, batch_size=5, num_workers=2, shuffle=True)
train_loss = losses.CosineSimilarityLoss(model)
#Tune the model
model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=1, warmup_steps=100)

```

```auto
AttributeError: 'list' object has no attribute 'texts'

```

---

<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 12, 2021, 5:14am UTC](https://discuss.pytorch.org/t/data-loader-without-labels/67107/5 "2021-04-12T05:14:53Z")

</div>

I’m not sure where the new error is raised, as I cannot see any usage of the `text` attribute. Could you check, which function is trying to access this attribute and make sure it’s using the right object?
