Modifying a custom dataset to replace datasets with new datasets

chaslie: <p>morning all,</p>
<p>I have 2 custom data sets which is generated externally and both in the format [data1, data2, data3,data,4]. I use the following function combine the datasets into a single entity. where data1 is a [n,m] array</p>
<pre><code class="lang-auto">class DoubleDataset(Dataset):
    def __init__(self):
        data_a = torch.load('data1.pt')
        data_b = torch.load('data2.pt')
        self.data_1=torch.utils.data.ConcatDataset((data_a ,data_b ))
        
        data_c = torch.load('data3.pt')
        data_d= torch.load('data4.pt')
        self.data_2=torch.utils.data.ConcatDataset((data_c ,data_d))
    def __getitem__(self, index):
        return self.data_1[index], self.data_2[index]

    def __len__(self):
        return min(len(self.data_1), len(self.data_2))



</code></pre>
<p>This works fine, can load the data into my network, normalise by batch and everything is hunkey dorey.</p>
<p>I would like to try and normalise data_1 and data_2 by the dataset and not by batch but when i get the following error:</p>
<pre><code class="lang-auto">data_1.max()

AttributeError: 'ConcatDataset' object has no attribute 'max'
</code></pre>
<p>If i load the dataset into memory its possible to use the following to normalise the whole dataset, but is it possible to get this into the class? if so where does it go?</p>
<pre><code class="lang-auto">    batch_samples = data_1[0].size(0)
    data = data_1[0].view(batch_samples, data_1[0].size(1), -1)
    data2=(data-data.min())/(data.max()-data.min())
    data3=(data-0.5/0.5
</code></pre>
<p>Failing this, if i do this</p>
<pre><code class="lang-auto">def normalise_dataset(dataload):
    loader=DataLoader(dataload, batch_size=len(dataload), num_workers=0, shuffle=False)
    for (data_1,data_2) in loader:
        batch_samples = data_1[0].size(0)
        data = data_1[0].view(batch_samples, data_1[0].size(1), -1)
        data2=(data-data.min())/(data.max()-data.min())
        data_1_O=(data2-0.5)/0.5
        data_1[0]=data_1_O
 
        batch_samples = data_2[0].size(0)
        dataI = data_2[0].view(batch_samples, data_2[0].size(1), -1)
        dataI2=(dataI-dataI.min())/(dataI.max()-dataI.min())
        data_2_O=(dataI2-0.5)/0.5
        data_2[0]=data_2_O
        norm=torch.utils.data.ConcatDataset((data_1,data_2))
        return norm

norm=normalise_dataset(dataload)


test=DataLoader(
    norm,
    batch_size=25,
    num_workers=0,
    shuffle=False
)

for i, (data_1,data_2) in enumerate(test):
    test1=data_1[0]
    test2=data_2[0]
</code></pre>
<p>This normalises the whole dataset, however when i try and load test into the network i get</p>
<pre><code class="lang-auto">
RuntimeError: Expected object of scalar type Float but got scalar type Long for sequence element 1 in sequence argument at position #1 'tensors'
</code></pre>
<p>but the only elements that have changed are the data_1[0] &amp; data_1[0], and the formatting of these are the same as the previous.</p>
<p>How do i repack the original dataload with the new values of data_1 &amp; data_2?</p>
<p>Chaslie</p>
<p>PS, sorry for the bad coding…</p>

hhaoao: <p>I think you should sort it out, I don’t know whether <code>data_1</code> is instantiated dataset or dataloader throughout the article.</p>
<p>The error display is very clear. I did a minimal demo. Before asking questions, you can make a small demo that can be run. Many times, when you coding, this problem is solved in the process.</p>
<pre><code class="lang-auto">import torch 

class MyDataset(torch.utils.data.Dataset): 
    def __init__(self):
        data_x = torch.tensor([[1,2,3],[2,3,4]])
        data_y = torch.tensor([[3,4,5],[4,5,6]])
        self.data_concat = torch.utils.data.ConcatDataset((data_x,data_y))

    def __getitem__(self, index):
        return self.data_concat[index]

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

dataset_temp = MyDataset()

dataset_loader = torch.utils.data.DataLoader(dataset_temp)

for data in dataset_loader:
    print(data.max())
</code></pre>

chaslie: <p>hhaoao,</p>
<p>thanks for the reply, i think you may have missunderstood the problem, due to my bad explaination.</p>
<p>The problem I have is that when i take 2 datasets and combine them in the doubledataset class (which works fine) I cannot normalise the data across the whole dataset due to the concatination.</p>
<p>To get over this I then take the concatacted datasets and use the normalised function to normalise the whole dataset. This then gets recombined, however at this stage instead of being a dataset with entries [[a,b,c,d],[a’,b’,c’,d’]], i end up with [a,b], where the dataloader has length 1 instead of the size of the dataset and a and be have size of the length of the dataset.</p>
<p>eg:<br>
length of dataset 1000, batchsize=1 .<br>
what i want is dataloader length 1000 and dataset[0] is [[a,b,c,d],[a’,b’,c’,d’]] where each entry is a numerical 2D arrray and some other metadata.</p>
<p>what i am ending up with is:<br>
length of dataset 1000, batchsize=1 .<br>
what i want is dataloader length 1and dataset[0] is [[a,],[a’]] where each entry just the a numerical 2D arrray.</p>
<p>to compond matters, datasets a and a’ before combining are of size [a,b,c,d] and [a’,b’,c’,d’] but they are only combining a &amp; a’.</p>
<p>I aplogise if this is confusing, I am confused. the problem is something to with how i am recreating the datasets, but with <strong>torch.utils.data.ConcatDataset</strong> the options are severly restricting…</p>
<p>thanks for your patience.</p>
<p>chaslie</p>

hhaoao: <p>Oh my goodness, your problem is the same as the one I encountered before.Pytorch cannot solve these data processing problems, you just need to give up using pytorch to process data.I used python <code>zip()</code> to parallel output.</p>
<p>PS: If I still misunderstood your question, I think it is all a data processing problem, you have to find a solution elsewhere. As my link said, you need to change the direction of solving the problem yourself.</p>
<aside class="quote quote-modified" data-post="3" data-topic="91390">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/9f8e36/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/dataloader-changes-dataset-dimension-crossentropyloss-not-work-properly/91390/3">DataLoader changes dataset dimension, "CrossEntropyLoss" not work properly</a> 
  </div>
  <blockquote>
    I found the problem: 

I misunderstood the three sentences of Shape in <a>https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss</a>. (doc too crude). 
The shape of output in my code is 2. So the following code should be changed like this:

     def __init__(self):
         super(Net, self).__init__()
         self.fc1 = nn.Linear(2,3,True)
         self.fc2 = nn.Linear(3,1,True)

correct: 
         self.linear1 = nn.Linear(2, 3)
  …
  </blockquote>
</aside>