【问题标题】:DL4J with ComputationGraph : Cosine similarity between layersDL4J with ComputationGraph:层间的余弦相似度
【发布时间】:2018-10-17 00:47:19
【问题描述】:

上下文

我正在尝试使用 DL4J 创建模型。

有两种嵌入:一种用于用户,一种用于项目。

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()

问题

最后我想计算这两个嵌入之间的余弦相似度。

然后我想训练模型以最大化正样本的相似度并最小化负样本的相似度。

正例 = 用户对项目感兴趣

否定示例 = 用户对该项目不感兴趣

可能的解决方案

我找到了两种可能的解决方案。

1) 创建自定义图层类。

2) 创建自定义 LossFunction 以在输出层上应用余弦相似度。

问题

1) 是否已经实现了一个层来实现两层之间的余弦相似度?

2) 如果没有,我该如何实现自己的层? 我发现的唯一示例如下:https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/misc/customlayers/CustomLayerExampleReadme.md

【问题讨论】:

    标签: cosine-similarity deeplearning4j


    【解决方案1】:

    您需要制作一个自定义顶点。在此处查看 Vertex 实现:https://github.com/deeplearning4j/deeplearning4j/tree/master/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl。我认为 L2Vertex 将与您想要的最相似。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2014-02-25
      • 2018-07-16
      • 2011-01-01
      • 2017-04-04
      • 2017-12-12
      • 2020-02-11
      • 2013-05-24
      相关资源
      最近更新 更多