|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.embeddings.node2vec; |
| 21 | + |
| 22 | +import org.neo4j.gds.collections.ha.HugeObjectArray; |
| 23 | +import org.neo4j.gds.ml.core.functions.Sigmoid; |
| 24 | +import org.neo4j.gds.ml.core.tensor.FloatVector; |
| 25 | + |
| 26 | +import static org.neo4j.gds.ml.core.tensor.operations.FloatVectorOperations.addInPlace; |
| 27 | +import static org.neo4j.gds.ml.core.tensor.operations.FloatVectorOperations.scale; |
| 28 | + |
| 29 | +final class TrainingTask implements Runnable { |
| 30 | + private final HugeObjectArray<FloatVector> centerEmbeddings; |
| 31 | + private final HugeObjectArray<FloatVector> contextEmbeddings; |
| 32 | + |
| 33 | + private final PositiveSampleProducer positiveSampleProducer; |
| 34 | + private final NegativeSampleProducer negativeSampleProducer; |
| 35 | + private final FloatVector centerGradientBuffer; |
| 36 | + private final FloatVector contextGradientBuffer; |
| 37 | + private final int negativeSamplingRate; |
| 38 | + private final float learningRate; |
| 39 | + |
| 40 | + private double lossSum; |
| 41 | + |
| 42 | + TrainingTask( |
| 43 | + HugeObjectArray<FloatVector> centerEmbeddings, |
| 44 | + HugeObjectArray<FloatVector> contextEmbeddings, |
| 45 | + PositiveSampleProducer positiveSampleProducer, |
| 46 | + NegativeSampleProducer negativeSampleProducer, |
| 47 | + float learningRate, |
| 48 | + int negativeSamplingRate, |
| 49 | + int embeddingDimensions |
| 50 | + ) { |
| 51 | + this.centerEmbeddings = centerEmbeddings; |
| 52 | + this.contextEmbeddings = contextEmbeddings; |
| 53 | + this.positiveSampleProducer = positiveSampleProducer; |
| 54 | + this.negativeSampleProducer = negativeSampleProducer; |
| 55 | + this.learningRate = learningRate; |
| 56 | + this.negativeSamplingRate = negativeSamplingRate; |
| 57 | + |
| 58 | + this.centerGradientBuffer = new FloatVector(embeddingDimensions); |
| 59 | + this.contextGradientBuffer = new FloatVector(embeddingDimensions); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + var buffer = new long[2]; |
| 65 | + |
| 66 | + // this corresponds to a stochastic optimizer as the embeddings are updated after each sample |
| 67 | + while (positiveSampleProducer.next(buffer)) { |
| 68 | + trainPositiveSample(buffer[0], buffer[1]); |
| 69 | + for (var i = 0; i < negativeSamplingRate; i++) { |
| 70 | + trainNegativeSample(buffer[0], negativeSampleProducer.next()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + void trainPositiveSample(long center, long context) { |
| 76 | + var centerEmbedding = centerEmbeddings.get(center); |
| 77 | + var contextEmbedding = contextEmbeddings.get(context); |
| 78 | + |
| 79 | + var scaledGradient = computePositiveGradient(centerEmbedding, contextEmbedding); |
| 80 | + |
| 81 | + updateEmbeddings( |
| 82 | + centerEmbedding, |
| 83 | + contextEmbedding, |
| 84 | + scaledGradient, |
| 85 | + centerGradientBuffer, |
| 86 | + contextGradientBuffer |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + void trainNegativeSample(long center, long context) { |
| 91 | + var centerEmbedding = centerEmbeddings.get(center); |
| 92 | + var contextEmbedding = contextEmbeddings.get(context); |
| 93 | + |
| 94 | + var scaledGradient = computeNegativeGradient(centerEmbedding, contextEmbedding); |
| 95 | + |
| 96 | + updateEmbeddings( |
| 97 | + centerEmbedding, |
| 98 | + contextEmbedding, |
| 99 | + scaledGradient, |
| 100 | + centerGradientBuffer, |
| 101 | + contextGradientBuffer |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + float computePositiveGradient(FloatVector centerEmbedding, FloatVector contextEmbedding) { |
| 106 | + // L_pos = -log sigmoid(center * context) ; gradient: -sigmoid (-center * context) |
| 107 | + // L_neg = -log sigmoid(-center * context) ; gradient: sigmoid (center * context) |
| 108 | + float affinity = centerEmbedding.innerProduct(contextEmbedding); |
| 109 | + //When |affinity| > 40, positiveSigmoid = 1. Double precision is not enough. |
| 110 | + //Make sure negativeSigmoid can never be 0 to avoid infinity loss. |
| 111 | + double positiveSigmoid = Sigmoid.sigmoid(affinity); |
| 112 | + double negativeSigmoid = 1 - positiveSigmoid; |
| 113 | + |
| 114 | + lossSum -= Math.log(positiveSigmoid + Node2VecModel.EPSILON); |
| 115 | + |
| 116 | + float gradient = (float) -negativeSigmoid; |
| 117 | + // we are doing gradient descent, so we go in the negative direction of the gradient here |
| 118 | + return -gradient * learningRate; |
| 119 | + } |
| 120 | + |
| 121 | + float computeNegativeGradient(FloatVector centerEmbedding, FloatVector contextEmbedding) { |
| 122 | + // L_pos = -log sigmoid(center * context) ; gradient: -sigmoid (-center * context) |
| 123 | + // L_neg = -log sigmoid(-center * context) ; gradient: sigmoid (center * context) |
| 124 | + float affinity = centerEmbedding.innerProduct(contextEmbedding); |
| 125 | + //When |affinity| > 40, positiveSigmoid = 1. Double precision is not enough. |
| 126 | + //Make sure negativeSigmoid can never be 0 to avoid infinity loss. |
| 127 | + double positiveSigmoid = Sigmoid.sigmoid(affinity); |
| 128 | + double negativeSigmoid = 1 - positiveSigmoid; |
| 129 | + |
| 130 | + lossSum -= Math.log(negativeSigmoid + Node2VecModel.EPSILON); |
| 131 | + |
| 132 | + float gradient = (float) positiveSigmoid; |
| 133 | + // we are doing gradient descent, so we go in the negative direction of the gradient here |
| 134 | + return -gradient * learningRate; |
| 135 | + } |
| 136 | + |
| 137 | + void updateEmbeddings( |
| 138 | + FloatVector centerEmbedding, |
| 139 | + FloatVector contextEmbedding, |
| 140 | + float scaledGradient, |
| 141 | + FloatVector centerGradientBuffer, |
| 142 | + FloatVector contextGradientBuffer |
| 143 | + ) { |
| 144 | + scale(contextEmbedding.data(), scaledGradient, centerGradientBuffer.data()); |
| 145 | + scale(centerEmbedding.data(), scaledGradient, contextGradientBuffer.data()); |
| 146 | + |
| 147 | + addInPlace(centerEmbedding.data(), centerGradientBuffer.data()); |
| 148 | + addInPlace(contextEmbedding.data(), contextGradientBuffer.data()); |
| 149 | + } |
| 150 | + |
| 151 | + double lossSum() { |
| 152 | + return lossSum; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments