|
| 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.ml.pipeline.nodePipeline.regression; |
| 21 | + |
| 22 | +import org.neo4j.gds.annotation.ValueClass; |
| 23 | +import org.neo4j.gds.api.GraphStore; |
| 24 | +import org.neo4j.gds.core.model.Model; |
| 25 | +import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; |
| 26 | +import org.neo4j.gds.core.utils.progress.tasks.Task; |
| 27 | +import org.neo4j.gds.core.utils.progress.tasks.Tasks; |
| 28 | +import org.neo4j.gds.executor.ExecutionContext; |
| 29 | +import org.neo4j.gds.ml.models.Regressor; |
| 30 | +import org.neo4j.gds.ml.pipeline.ImmutableGraphFilter; |
| 31 | +import org.neo4j.gds.ml.pipeline.PipelineExecutor; |
| 32 | +import org.neo4j.gds.ml.pipeline.TrainingStatistics; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +import static org.neo4j.gds.ml.pipeline.nodePipeline.regression.NodeRegressionTrainPipelineExecutor.NodeRegressionTrainPipelineResult; |
| 40 | + |
| 41 | +public class NodeRegressionTrainPipelineExecutor extends PipelineExecutor< |
| 42 | + NodeRegressionPipelineTrainConfig, |
| 43 | + NodeRegressionTrainingPipeline, |
| 44 | + NodeRegressionTrainPipelineResult |
| 45 | +> { |
| 46 | + |
| 47 | + public static Task progressTask(NodeRegressionTrainingPipeline pipeline) { |
| 48 | + return Tasks.task( |
| 49 | + "Node Regression Train Pipeline", |
| 50 | + new ArrayList<>() {{ |
| 51 | + add(Tasks.iterativeFixed( |
| 52 | + "Execute node property steps", |
| 53 | + () -> List.of(Tasks.leaf("Step")), |
| 54 | + pipeline.nodePropertySteps().size() |
| 55 | + )); |
| 56 | + addAll(NodeRegressionTrain.progressTasks( |
| 57 | + pipeline.splitConfig().validationFolds(), |
| 58 | + pipeline.numberOfModelSelectionTrials() |
| 59 | + )); |
| 60 | + |
| 61 | + }} |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public NodeRegressionTrainPipelineExecutor( |
| 66 | + NodeRegressionTrainingPipeline pipeline, |
| 67 | + NodeRegressionPipelineTrainConfig config, |
| 68 | + ExecutionContext executionContext, |
| 69 | + GraphStore graphStore, |
| 70 | + ProgressTracker progressTracker |
| 71 | + ) { |
| 72 | + super(pipeline, config, executionContext, graphStore, config.graphName(), progressTracker); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Map<DatasetSplits, GraphFilter> splitDataset() { |
| 77 | + // we don't split the input graph but generate the features and predict over the whole graph. |
| 78 | + // Inside the training algo we split the nodes into multiple sets. |
| 79 | + return Map.of( |
| 80 | + DatasetSplits.FEATURE_INPUT, |
| 81 | + ImmutableGraphFilter.of( |
| 82 | + config.nodeLabelIdentifiers(graphStore), |
| 83 | + config.internalRelationshipTypes(graphStore) |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected NodeRegressionTrainPipelineResult execute(Map<DatasetSplits, GraphFilter> dataSplits) { |
| 90 | + PipelineExecutor.validateTrainingParameterSpace(pipeline); |
| 91 | + |
| 92 | + var nodeLabels = config.nodeLabelIdentifiers(graphStore); |
| 93 | + var relationshipTypes = config.internalRelationshipTypes(graphStore); |
| 94 | + var graph = graphStore.getGraph(nodeLabels, relationshipTypes, Optional.empty()); |
| 95 | + |
| 96 | + this.pipeline.splitConfig().validateMinNumNodesInSplitSets(graph); |
| 97 | + |
| 98 | + NodeRegressionTrainResult trainResult = NodeRegressionTrain |
| 99 | + .create(graph, pipeline, config, progressTracker, terminationFlag) |
| 100 | + .compute(); |
| 101 | + |
| 102 | + var catalogModel = Model.of( |
| 103 | + config.username(), |
| 104 | + config.modelName(), |
| 105 | + NodeRegressionTrainingPipeline.MODEL_TYPE, |
| 106 | + schemaBeforeSteps, |
| 107 | + trainResult.regressor().data(), |
| 108 | + config, |
| 109 | + NodeRegressionPipelineModelInfo.builder() |
| 110 | + .bestParameters(trainResult.trainingStatistics().bestParameters()) |
| 111 | + .metrics(trainResult.trainingStatistics().metricsForWinningModel()) |
| 112 | + .build() |
| 113 | + ); |
| 114 | + |
| 115 | + return ImmutableNodeRegressionTrainPipelineResult.of(catalogModel, trainResult.trainingStatistics()); |
| 116 | + } |
| 117 | + |
| 118 | + @ValueClass |
| 119 | + interface NodeRegressionTrainPipelineResult { |
| 120 | + Model<Regressor.RegressorData, NodeRegressionPipelineTrainConfig, NodeRegressionPipelineModelInfo> model(); |
| 121 | + TrainingStatistics trainingStatistics(); |
| 122 | + } |
| 123 | +} |
0 commit comments