AttributeError on variable input of custom loss function in PyTorch
I've made a custom loss function to compute cross-entropy (CE) for a multi-output multi-label problem. Within the class, I want to set the target variable I'm feeding to not require a gradient. I do this within the forward function using a pre-defined function (taken from pytorch source code) outside the class.

    def _assert_no_grad(variable):
        assert not variable.requires_grad

    def forward(self, predicted, target):
        """
        Computes cross entropy between targets and predictions.
        """
        # No gradient over target
        _assert_no_grad(target)

        # Define variables
        p = predicted.clamp(0.01, 0.99)
        t = target.float()

        #Compute cross entropy
        h1 = p.log()*t
        h2 = (1-t)*((1-p).log())
        ce = torch.add(h1, h2)
        ce_out = torch.mean(ce, 1)
        ce_out = torch.mean(ce_out, 0)

        # Save for backward step
        self.save_for_backward(ce_out)


At this point when I run the code in a batched for-loop (see below), I get the following error:


  AttributeError: 'torch.FloatTensor' object has no attribute 'requires_grad'


It seems simple enough as we should be passing a torch.autograd.Variable, however I am already doing this as can be seen in the snippet below.

for t in range(50):

    print('Epoch {}'.format(t))
    if t &gt; 0:
        print('Loss -&gt;', loss)

    for batch_idx, (x_batch, y_batch) in enumerate(train_loader):
        # Wrap in Variable
        x_in, target = Variable(x_batch), Variable(y_batch)

        predicted = model(x_in)

        # Compute and print loss
        loss = criterion(predicted, target)

        # Zero gradients, perform a backward pass, and update the weights.
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


To add a note, my final goal is to generate a class which behaves like BCELoss except for multiple labels and not just binary. I feel like I've already scoured the entire PyTorch docs and primarily been using this and some forum entries.
http://pytorch.org/docs/master/notes/extending.html

So

The problem is in "target.float()" line which converts your t Variable to Tensor. You can directly use target without any problems in your CE calculation.

Also, I guess you do not really need "self.save_for_backward(ce_out)" as I guess you are defining nn.Module class which will take care of the backward pass internally.
