Error with complex neural network

NourO931: <p>I am trying to implement super resolution with a complex neural network using 91-image and set-5 datasets. I keep getting the following error at the loss functions:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41536cbfd2f67c989415e7a174fe75dcdd9dec2c.png" data-download-href="https://discuss.pytorch.org/uploads/default/41536cbfd2f67c989415e7a174fe75dcdd9dec2c" title="cnn error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41536cbfd2f67c989415e7a174fe75dcdd9dec2c.png" alt="cnn error" data-base62-sha1="9jTLT4UMdFJfNf2JfnBhMWe0MKE" width="690" height="275" data-dominant-color="F4F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">cnn error</span><span class="informations">1004×401 19.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is my code</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from complexLayers import ComplexBatchNorm2d, ComplexConv2d, ComplexLinear
from complexLayers import ComplexDropout2d, NaiveComplexBatchNorm2d
from complexFunctions import complex_relu, complex_max_pool2d

from datasets import TrainDataset, EvalDataset

from torch.utils.data.dataloader import DataLoader

batch_size = 64

trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
train_set = TrainDataset('D:\complexPyTorch-master\SRCNN-pytorch-master\91-image_x2.h5')
train_loader = DataLoader(dataset=train_set,
                                  batch_size=64,
                                  shuffle=True,
                                  num_workers=8,
                                  pin_memory=True,
                                  drop_last=True)
test_set = EvalDataset('D:\complexPyTorch-master\SRCNN-pytorch-master\Set5_x2.h5')
test_loader = DataLoader(dataset=test_set, batch_size=1)


class ComplexNet(nn.Module):
    
    def __init__(self):
        super(ComplexNet, self).__init__()
        self.conv1 = ComplexConv2d(1, 64, kernel_size=9, padding=9 // 2)
        self.conv2 = ComplexConv2d(64, 32, kernel_size=5, padding=5 // 2)
        self.conv3 = ComplexConv2d(32, 1, kernel_size=5, padding=5 // 2)
        #self.relu = complex_relu()
             
    def forward(self,x):
        x = complex_relu(self.conv1(x))
        x = complex_relu(self.conv2(x))
        x = self.conv3(x)
        x = complex_relu(x) #this should have bene in init but it is placed here instead
        #x = self.fc2(x)
        x = x.abs()
        #x =  F.log_softmax(x)
        return x


device = device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ComplexNet().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=5e-3, momentum=0.9)


def train(model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target =data.to(device).type(torch.complex64), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        print(output.dtype)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if batch_idx % 100 == 0:
            print('Train Epoch: {:3} [{:6}/{:6} ({:3.0f}%)]\tLoss: {:.6f}'.format(
                epoch,
                batch_idx * len(data), 
                len(train_loader.dataset),
                100. * batch_idx / len(train_loader), 
                loss.item())
            )


for epoch in range(4):
    train(model, device, train_loader, optimizer, epoch)


</code></pre>

ptrblck: <p><code>F.nll_loss</code> expects the targets as <code>LongTensors</code> containing the class indices, while you are trying to pass a <code>FloatTensor</code> to it as the error message explains.<br>
If your target already contains the necessary class indices, call <code>target = target.long()</code> before passing it to the criterion.</p>

NourO931: <p>Thank you for your answer. I followed your solution, but now I ended up with a different error:</p>
<p>1only batches of spatial targets supported (3D tensors) but got targets of size: : [64, 1, 33, 33]</p>
<p>I am trying to perform image reconstruction, not classification. So perhaps I should use a different loss function?</p>

ptrblck: <p>I think for image reconstruction you could use e.g. <code>nn.MSELoss</code> or <code>nn.BCEWithLogitsLoss</code>, assuming your target contains floating point values in a specific range.<br>
I don’t know what the state of the art would be today for this use case.</p>