From 7c9b38be8d27ec6627942b0fdae3de0ca63e01bd Mon Sep 17 00:00:00 2001 From: Brian Shi Date: Fri, 8 Dec 2023 12:11:51 +0000 Subject: [PATCH] Avoid positiveSigmoid becoming negative --- .../java/org/neo4j/gds/embeddings/node2vec/Node2VecModel.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/algo/src/main/java/org/neo4j/gds/embeddings/node2vec/Node2VecModel.java b/algo/src/main/java/org/neo4j/gds/embeddings/node2vec/Node2VecModel.java index 5ff9d14fc2..2b8657cacd 100644 --- a/algo/src/main/java/org/neo4j/gds/embeddings/node2vec/Node2VecModel.java +++ b/algo/src/main/java/org/neo4j/gds/embeddings/node2vec/Node2VecModel.java @@ -270,10 +270,10 @@ private void trainSample(long center, long context, boolean positive) { //When |affinity| > 40, positiveSigmoid = 1. Double precision is not enough. //Make sure negativeSigmoid can never be 0 to avoid infinity loss. - double positiveSigmoid = Sigmoid.sigmoid(affinity) - EPSILON; + double positiveSigmoid = Sigmoid.sigmoid(affinity); double negativeSigmoid = 1 - positiveSigmoid; - lossSum -= positive ? Math.log(positiveSigmoid) : Math.log(negativeSigmoid); + lossSum -= positive ? Math.log(positiveSigmoid+EPSILON) : Math.log(negativeSigmoid+EPSILON); float gradient = positive ? (float) -negativeSigmoid : (float) positiveSigmoid; // we are doing gradient descent, so we go in the negative direction of the gradient here