I am creating a custom loss function. I pulled out relevant test code from pytorch to test the function.
I am getting a failure when the function check_criterion_jacobian is called.
A small example demonstrating the problem can be found here

  
      gist.github.com
  
  
    https://gist.github.com/mellorjc/1fd19932e6ae92d6d0b4da3fa3ed70d6 9
example.py
from torch.autograd import Variable
import torch
from torch.nn import functional as F

def astype(val, typelike):
    return val.type('torch.' + type(typelike.data).__name__)


class TestLoss(torch.nn.Module):
    def __init__(self, rg, w):
This file has been truncated. show original
example_test.py
from torch.autograd import Variable, gradcheck
from torch.autograd.gradcheck import gradgradcheck
import argparse
import unittest
import sys
import tempfile
import unittest
from copy import deepcopy
from itertools import product
import torch
This file has been truncated. show original




  
  
    
    
  
  


In the example I expand the input and then perform an elementwise multiplication between a simple function of this and a fixed array. The loss then returns the cross entropy between the target and the result of the elementwise multiplication.
When I remove the element wise multiplication I do not get the error.
I thought at first that I might need to set requires_grad for the variable wrapping the fixed array but the error occurs irrespective of the value of requires_grad.
How do I avoid this error?