-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Simplest version of dumping train config #558
Draft
orazve
wants to merge
1
commit into
neo4j:main
Choose a base branch
from
orazve:kge-train-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b68543d5e71ceeb2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"from graphdatascience import GraphDataScience" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e685a47b61f968ef", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"NEO4J_URI = \"bolt://localhost:7687\"\n", | ||
"NEO4J_DB = \"neo4j\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "initial_id", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if os.environ.get(\"NEO4J_USER\") and os.environ.get(\"NEO4J_PASSWORD\"):\n", | ||
" NEO4J_AUTH = (\n", | ||
" os.environ.get(\"NEO4J_USER\"),\n", | ||
" os.environ.get(\"NEO4J_PASSWORD\"),\n", | ||
" )\n", | ||
"gds = GraphDataScience(NEO4J_URI, auth=NEO4J_AUTH, database=NEO4J_DB)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a14f06aebe1ed34c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"_ = gds.run_cypher(\n", | ||
" \"\"\"\n", | ||
" CREATE\n", | ||
" (dan:Person {name: 'Dan'}),\n", | ||
" (annie:Person {name: 'Annie'}),\n", | ||
" (matt:Person {name: 'Matt'}),\n", | ||
" (jeff:Person {name: 'Jeff'}),\n", | ||
" (brie:Person {name: 'Brie'}),\n", | ||
" (elsa:Person {name: 'Elsa'}),\n", | ||
"\n", | ||
" (cookies:Product {name: 'Cookies'}),\n", | ||
" (tomatoes:Product {name: 'Tomatoes'}),\n", | ||
" (cucumber:Product {name: 'Cucumber'}),\n", | ||
" (celery:Product {name: 'Celery'}),\n", | ||
" (kale:Product {name: 'Kale'}),\n", | ||
" (milk:Product {name: 'Milk'}),\n", | ||
" (chocolate:Product {name: 'Chocolate'}),\n", | ||
"\n", | ||
" (dan)-[:BUYS {amount: 1.2}]->(cookies),\n", | ||
" (dan)-[:BUYS {amount: 3.2}]->(milk),\n", | ||
" (dan)-[:BUYS {amount: 2.2}]->(chocolate),\n", | ||
"\n", | ||
" (annie)-[:BUYS {amount: 1.2}]->(cucumber),\n", | ||
" (annie)-[:BUYS {amount: 3.2}]->(milk),\n", | ||
" (annie)-[:BUYS {amount: 3.2}]->(tomatoes),\n", | ||
"\n", | ||
" (matt)-[:BUYS {amount: 3}]->(tomatoes),\n", | ||
" (matt)-[:BUYS {amount: 2}]->(kale),\n", | ||
" (matt)-[:BUYS {amount: 1}]->(cucumber),\n", | ||
"\n", | ||
" (jeff)-[:BUYS {amount: 3}]->(cookies),\n", | ||
" (jeff)-[:BUYS {amount: 2}]->(milk),\n", | ||
"\n", | ||
" (brie)-[:BUYS {amount: 1}]->(tomatoes),\n", | ||
" (brie)-[:BUYS {amount: 2}]->(milk),\n", | ||
" (brie)-[:BUYS {amount: 2}]->(kale),\n", | ||
" (brie)-[:BUYS {amount: 3}]->(cucumber),\n", | ||
" (brie)-[:BUYS {amount: 0.3}]->(celery),\n", | ||
"\n", | ||
" (elsa)-[:BUYS {amount: 3}]->(chocolate),\n", | ||
" (elsa)-[:BUYS {amount: 3}]->(milk)\n", | ||
" \"\"\"\n", | ||
")\n", | ||
"node_projection = [\"Person\", \"Product\"]\n", | ||
"relationship_projection = {\"BUYS\": {\"orientation\": \"UNDIRECTED\", \"properties\": \"amount\"}}\n", | ||
"G, result = gds.graph.project(\"purchases222\", node_projection, relationship_projection)\n", | ||
"print(f\"The projection took {result['projectMillis']} ms\")\n", | ||
"print(f\"Graph '{G.name()}' node count: {G.node_count()}\")\n", | ||
"print(f\"Graph '{G.name()}' node labels: {G.node_labels()}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e049480efa34e8ca", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"gds.model.transe.train(\n", | ||
" G,\n", | ||
" proportions=[0.8, 0.1, 0.1],\n", | ||
" embedding_dimension=50,\n", | ||
" batch_size=512,\n", | ||
" epochs=100,\n", | ||
" optimizer=\"Adam\",\n", | ||
" optimizer_kwargs={\"lr\": 0.01, \"weight_decay\": 5e-4},\n", | ||
" # loss\n", | ||
")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
from typing import Any, Dict, List, Optional, Tuple | ||
|
||
from pandas import DataFrame, Series | ||
|
@@ -45,6 +46,34 @@ def create( | |
relationship_type_embeddings, | ||
) | ||
|
||
@compatible_with("train", min_inclusive=ServerVersion(2, 5, 0)) | ||
@client_only_endpoint("gds.model.transe") | ||
def train(self, | ||
G: Graph, | ||
proportions: list, | ||
embedding_dimension: int, | ||
batch_size: int, | ||
epochs: int, | ||
optimizer: str, | ||
optimizer_kwargs: dict, | ||
# loss: str | ||
) -> int: | ||
config = {'scoring_function': 'TransE', | ||
'proportions': proportions, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I call this |
||
'embedding_dimension': embedding_dimension, | ||
'num_epochs': epochs, | ||
'graph_name': G.name(), | ||
'batch_size': batch_size, | ||
'optimizer': optimizer, | ||
'optimizer_kwargs': optimizer_kwargs, | ||
# 'loss': loss, | ||
} | ||
config_path = "/tmp/kge-train-config-dump.json" | ||
print('Dumped to ' + config_path) | ||
config_file = open(config_path, "w") | ||
|
||
json.dump(config, config_file) | ||
return 0 | ||
|
||
class ModelProcRunner(ModelResolver): | ||
@client_only_endpoint("gds.model") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit confused about how the predict-only TransE we've had previously fits into this. They kind of seem like different things. Also I wonder if it's better if we have a
gds.kge.train
call instead of one dedicated to each particular scoring functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we are adding training step in addition to existing prediction one. Maybe it's worth to have a fully separate API like
gds.kge.train
orgds.kge.transe.train
.Personally I prefer to mention scoring method in a function name, like
gds.kge.transe.train
, notgds.kge.train
. Because KGE algorithms are different and supposed to catch different relationship properties.Our API proposal has
gds.model.transe.train
call, that's why I wrote it that way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! Fair enough :) I do still prefer
gds.kge.train
: I think it makes sense since the embeddings are what are trained, not TransE which is a scoring function. And since all KGE algos are the same (in terms of eg. hyperparameters) except for the scoring function, I think it would make sense to group them in the API for simplicity, sharing the same docs, etc. In that sense scoring function is just another hyperparam I think, and one may even want to use an ensemble of them. I like how pyKEEN designed their API