Cross Validated Asked on December 26, 2020
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?
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.
Correct answer by Sycorax on December 26, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP