Where does ClassNLLCriterion_updateOutput error message come from?

Although I can do Binary classification and multiple classification with success. But I am curious with the following error.

When I set the types of features and labels to be the same, for example torch.FloatTensor, and use nn.CrossEntropyLoss to calculate loss. I get the following error:

> /Users/Natsume/miniconda2/envs/experiment3.5/lib/python3.5/site-packages/torch/nn/_functions/thnn/auto.py(47)

  29         @staticmethod
  30         def forward(ctx, input, target, *args):
  31             ctx._backend = type2backend[type(input)]
  32             ctx.save_for_backward(input, target)
  33             if weight_arg_idx >= 0:
  34                 ctx.weight = args[0]
  35                 args = args[1:]
  36                 ctx.additional_args = list(args)
  37                 insert_idx = weight_arg_idx - 4  # state, input, target, output
  38                 ctx.additional_args.insert(insert_idx, ctx.weight)
  39             else:
  40                 ctx.additional_args = list(args)
  42             ctx.forward_args_count = len(ctx.additional_args)
  43             for idx in buffers_idx:
  44                 ctx.additional_args.insert(idx, input.new(1))
  45             output = input.new(1)
  46             getattr(ctx._backend, update_output.name)(ctx._backend.library_state, in
  47  ->                                                   output, *ctx.additional_args)
  48             return output
 TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, torch.FloatTensor, torch.FloatTensor, bool, NoneType, torch.FloatTensor, int), but expected (int state, torch.FloatTensor input, torch.LongTensor target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight, int ignore_index)

It seems the solution is to change targets type from torch.FloatTensor to torch.LongTensor. However, I could not find out which function generated this error message above. I wonder where is the function for generating the error message.


This error is generated by the wrapper between the python and C libs that is automatically generated. The function that created this error message cna be found here.
This is called by the THPUtils_invalidArguments function from here.
This function is used in few places, in particular in the automatic wrapper cwrap that I was talking about above, here or here for example.

1 Like

@albanD Thanks a lot, it is very helpful!

I wonder how exactly (through which function) the string output is thrown from C to python so that I get to see it in console? because it does not have codes like `getattr(…name of C functions …)(…args…)

If you look into the THPUtils_invalidArguments function, you can see here that it raises a python error with the string provided by the format_invalid_args function.
Also in the generated code template here, you can see that all the functions are encapsulated by the HANDLE_TH_ERRORS macro.
These macros are defined here and as you can see they let all python error go through without modifying them.

1 Like

@albanD thanks a lot for your help. I think you answered my question quite accurate, though the codes in those links are beyond my comprehension at this moment (I am very new to C/C++, and will keep learning it).

The deepest reason for why I had persisted to install gdb and keep coming to visit Torch C/C++ codes is I want to understand the logic of bricks (such as conv2d, relu etc) of deep learning through coding. As pdb helps me a lot in understanding code logic in python level ( I use pdb to explore code logic in perfectly healthy codes, not buggy codes, by experiment and checking the values of internals of funcions and classes), I expect gdb would do the same good for me when trying to understand C/C++ codes as pytorch’s most activations, layers are written in C/C++.

Now, I don’t expect gdb would work on my Mac anytime soon, is there other easier way for me to experiment torch C/C++ codes (for example, experiment on ConvForward in THNN) to understand the calculation inside the classes and functions, like we can use pdb on pure python codes?

Although my gdb is not working on python code, but it seems working on C/C++. So if you can give me a very simple demo on how to experiment ConvForward or Threshold_updateOutput or else in C/C++, I can learn to start using gdb with C/C++ to experiment the Torch code.

Thanks a lot!