Xor Dataloader double error

ganariya: <h1>About</h1>
<p>Hello!</p>
<p>I’m going to make <code>xor-dataloader</code> (I cannot make validation dataloader yet.)</p>
<p>However, this code does not go well.</p>
<h1>Code</h1>
<pre><code class="lang-python">
import glob
import os.path as osp
import random
import numpy as np
import json
from PIL import Image
from tqdm import tqdm
import matplotlib.pyplot as plt
%matplotlib inline

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data
import torchvision
from torchvision import models, transforms

</code></pre>
<pre><code class="lang-python">class XorDataset(data.Dataset):
    
    def __init__(self, phase='train', bit=8):
        self.phase = phase
        self.bit = bit
        self.array = []
        
        for i in range(2**bit):
            a = []
            for j in range(bit):
                a.append((i &gt;&gt; j) &amp; 1)
            self.array.append(a)
            
        self.array = np.array(self.array)
        self.array = torch.Tensor(self.array)
        
    def __len__(self):
        return 2**self.bit
    
    def __getitem__(self, index):
        
        label = 0
        for b in self.array[index]:
            label ^= int(b)
            
        return self.array[index], label
</code></pre>
<pre><code class="lang-python">xorDataset = XorDataset()
</code></pre>
<pre><code class="lang-python">dataset = XorDataset(bit=8)
</code></pre>
<pre><code class="lang-python">batch_size = 32

dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)
</code></pre>
<pre><code class="lang-python">class Model(nn.Module):
    def __init__(self, bit):
        super(Model, self).__init__()
        self.fv1 = nn.Linear(in_features=bit, out_features=10)
        self.fv2 = nn.Linear(in_features=10, out_features=10)
        self.fv3 = nn.Linear(in_features=10, out_features=1)
    
    def forward(self, x):
        x = F.relu(self.fv1(x))
        x = F.relu(self.fv2(x))
        x = self.fv3(x)
        return x
        
</code></pre>
<pre><code class="lang-python">model = Model(bit=8)
</code></pre>
<pre><code class="lang-python">criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)
epochs = 100
model.train()
</code></pre>
<pre><code class="lang-python">def train_model(net, dataloader, criterion, optimizer, epochs):
    
    for epoch in range(epochs):
        
        epoch_loss = 0.0
        epoch_corrects = 0
        
        for inputs, labels in tqdm(dataloader):
            
            optimizer.zero_grad()
            
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            
            epoch_loss += loss.item() * inputs.size(0)
            
            epoch_loss = epoch_loss / len(dataloader.dataset)

            print('Loss: {:.4f} '.format(epoch_loss))
            
</code></pre>
<pre><code class="lang-python">train_model(model, dataloader, criterion, optimizer, epochs)
</code></pre>
<h1>Error Code</h1>
<pre><code class="lang-auto">  0%|          | 0/8 [00:00&lt;?, ?it/s]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-430-aa76eb3f74cc&gt; in &lt;module&gt;
----&gt; 1 train_model(model, dataloader, criterion, optimizer, epochs)

&lt;ipython-input-429-6d06e31b1b55&gt; in train_model(net, dataloader, criterion, optimizer, epochs)
     12             outputs = model(inputs)
     13             loss = criterion(outputs, labels)
---&gt; 14             loss.backward()
     15             optimizer.step()
     16 

/opt/conda/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    183                 products. Defaults to ``False``.
    184         """
--&gt; 185         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    186 
    187     def register_hook(self, hook):

/opt/conda/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
    125     Variable._execution_engine.run_backward(
    126         tensors, grad_tensors, retain_graph, create_graph,
--&gt; 127         allow_unreachable=True)  # allow_unreachable flag
    128 
    129 

RuntimeError: Found dtype Long but expected Float
Exception raised from compute_types at /opt/conda/conda-bld/pytorch_1595629427478/work/aten/src/ATen/native/TensorIterator.cpp:183 (most recent call first):

</code></pre>
<p>After <code>train_model(model, dataloader, criterion, optimizer, epochs)</code> below code raises.<br>
I come to know why this error occurs.<br>
This error occurs because <code>__getitem__</code> return’s label is long.<br>
However, if I change code as <code>float(label)</code>, it won’t go well.<br>
How should I do? Thank you!</p>
<pre><code class="lang-python">class XorDataset(data.Dataset):
    
    def __init__(self, phase='train', bit=8):
        self.phase = phase
        self.bit = bit
        self.array = []
        
        for i in range(2**bit):
            a = []
            for j in range(bit):
                a.append((i &gt;&gt; j) &amp; 1)
            self.array.append(a)
            
        self.array = np.array(self.array)
        self.array = torch.Tensor(self.array)
        
    def __len__(self):
        return 2**self.bit
    
    def __getitem__(self, index):
        
        label = 0
        for b in self.array[index]:
            label ^= int(b)
            
        return self.array[index], label
</code></pre>

ptrblck: <p>You would have to transform the label tensor to a <code>FloatTensor</code> via <code>labels.float()</code>.<br>
Also, you should get a warning about a shape mismatch in the criterion between the model output and target, so unsqueeze the feature dimension in the labels via:</p>
<pre><code class="lang-python">loss = criterion(outputs, labels.float().unsqueeze(1))
</code></pre>