I am working on implementing the loss function of the Yolo version 2 object detection network and building from the construction notebook in the Wolfram Neural Network repository Yolo Version 2 so I can train it for a specialised detection task. The loss function is quite complicated, I can see how to implement almost all of it but I'm stuck with an indicator function implementation.
During training indicator functions are used to set the maximum value in a tensor to 1 and other values to zero (in the Yolo network it is an higher rank tensor) but the problem comes down to this, how to implement the following (a sort of indexmax
or argmax
function)
{1,2,1,4,2}
=> {0,0,0,1,0}
There are many ways of doing this with built-in functions but the problem I'm stuck with is how to do it with the functionality available in the neural network framework. In the snippet below, the Position function is not available (that I can find) in the neural net framework.
vec = {1,2,1,4,2};
pos = Position[vec, Max[vec]]//Flatten (*this can't be directly implemented in the NN framework*)
UnitVectorLayer[5,"Input"-> "Integer"][pos]
This produces {0.,0.,0.,1.,0.} (*the output I'm after*)
In Keras there is an 'argmax' function that does something like this. Other functionality like ReplicateLayer
and related functions can then be used to construct the object needed, this bit isn't where the problem is.
Any ideas or suggestions on how to do this in the MMA neural network framework?