1
$\begingroup$

Say I have a neural net that outputs a vector of length 4 such as:

[0, 1, 2, 3]

Now say that the only way to calculate the loss is to convert this output to a one-hot vector matrix and pass that into the loss function:

[[1, 0, 0, 0],
 [0, 1, 0, 0],
 [0, 0, 1, 0],
 [0, 0, 0, 1]]

This is a hypothetical question (obviously the answer to this question wouldn't be to the aforementioned scenario, but to another more realistic, relevant one).

So, once we have calculated the loss using the one-hot vector matrix, is it still possible to back propogate and train the network even though there were two different representations used. A more general question would be, if I convert representations from the output of the neural net to the loss function ( output of neural net => some representation conversion => loss function), is it still possible to back propogate and optimize?

$\endgroup$
2
  • $\begingroup$ how does the neural network output integers as in the first one? $\endgroup$
    – gunes
    Commented Jul 31, 2020 at 15:49
  • $\begingroup$ It was more of a hypothetical scenario. In reality, that wouldn't happen, but it's just an example of representation conversion. $\endgroup$
    – 3michelin
    Commented Jul 31, 2020 at 16:52

1 Answer 1

2
$\begingroup$

It depends on how you do this.

  • If you're using PyTorch and you do all of your operation using torch.Tensor objects, then the answer is "yes, you can backprop this loss correctly because that's the whole point of using torch.Tensor objects." So, this code will work: loss = torch.eye(x.size).dot(x).sum() where x is your 4-vector. Just replace sum with whatever differentiable function(s) you need. (Rounding or other non-differentiable operations are not suitable.)

  • If you dump your torch.Tensor objects to numpy, this is not possible because a numpy.array object does not record gradients, nor does it have a backward method.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.