how to convert the output of a nural network to long type while maintaining the trainability

37 Views Asked by At

The output of my pytorch neural network is a float64 type of data. This variable has to be used as a pixel offset and as such I need to convert it to long type.

However I have just discovered that a conversion out=out.long() switches the variable attribute ".requires_grad" to False.

How can I convert it to long maintaining ".requires_grad" to true?

1

There are 1 best solutions below

0
On

In general, you cannot convert a tensor to an integer-based type while maintaining it's gradient properties since converting to an integer is a non-differentiable operation. Thus, you essentially have two options:

  1. If the data is only required as type long for inference operations that need not maintain their gradient,you can back-propagate loss before converting to long type, sequentially. You could also make a copy or use torch.detach().

  2. Change the input-output structure of your model such that integer outputs are not needed. One way to do this might be to output a pixel-map with one value for each value in the original tensor which you are trying to index. This would be similar to NNs that output masks for segmentation.

Without more detail on what you're trying to accomplish, it's difficult to say what your best path forward is. Please add more code so the context of this operation is visible.