Context
I am trying to create a model with DL4J.
There is two embeddings : one for user and one for item.
val conf = new NeuralNetConfiguration.Builder()
.updater(new Sgd(0.01))
.graphBuilder()
.addInputs("item_input", "user_input")
.addLayer("item_embedding", new DenseLayer.Builder().nIn(5).nOut(5).build(), "item_input")
.addLayer("user_embedding", new DenseLayer.Builder().nIn(5).nOut(5).build(), "user_input")
// Something
.build()
val net = new ComputationGraph(conf)
net.init()
Problem
At the end I would like to compute the cosine similarity between these two embeddings.
Then I want to train the model to maximize the similarity on positive example and minimize it on negative one.
Positive example = the user is interrested by the item
Negative example = the user is not interrested by the item
Possible solutions
I have found two possible solutions.
1) Create a custom layer class.
2) Create a custom LossFunction to apply cosine similarity on the output layers.
Questions
1) Is there a layer already implemented that implement a cosine similarity beetween two layers ?
2) If not, how can I implement my own layer ? The only example I found is the following : https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/misc/customlayers/CustomLayerExampleReadme.md
You'll want to make a custom vertex. Check out the Vertex implementations here: https://github.com/deeplearning4j/deeplearning4j/tree/master/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl . I think the L2Vertex will be the most similar to what you want.