Spaces:
Running
Running
Rename core/ops to pytorch_core/pytorch_ops.
Browse files- lynxkite-graph-analytics/src/lynxkite_graph_analytics/lynxkite_ops.py +8 -7
- lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/__init__.py +2 -2
- lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/{core.py → pytorch_core.py} +0 -0
- lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/{ops.py → pytorch_ops.py} +1 -1
- lynxkite-graph-analytics/tests/test_pytorch_model_ops.py +9 -9
lynxkite-graph-analytics/src/lynxkite_graph_analytics/lynxkite_ops.py
CHANGED
@@ -8,7 +8,8 @@ from lynxkite.core import ops
|
|
8 |
from collections import deque
|
9 |
|
10 |
from tqdm import tqdm
|
11 |
-
from . import core
|
|
|
12 |
from lynxkite.core import workspace
|
13 |
import grandcypher
|
14 |
import joblib
|
@@ -347,7 +348,7 @@ def define_model(
|
|
347 |
assert model_workspace, "Model workspace is unset."
|
348 |
ws = load_ws(model_workspace)
|
349 |
# Build the model without inputs, to get its interface.
|
350 |
-
m =
|
351 |
m.source_workspace = model_workspace
|
352 |
bundle = bundle.copy()
|
353 |
bundle.other[save_as] = m
|
@@ -356,15 +357,15 @@ def define_model(
|
|
356 |
|
357 |
# These contain the same mapping, but they get different UIs.
|
358 |
# For inputs, you select existing columns. For outputs, you can create new columns.
|
359 |
-
class ModelInferenceInputMapping(
|
360 |
pass
|
361 |
|
362 |
|
363 |
-
class ModelTrainingInputMapping(
|
364 |
pass
|
365 |
|
366 |
|
367 |
-
class ModelOutputMapping(
|
368 |
pass
|
369 |
|
370 |
|
@@ -379,7 +380,7 @@ def train_model(
|
|
379 |
):
|
380 |
"""Trains the selected model on the selected dataset. Most training parameters are set in the model definition."""
|
381 |
m = bundle.other[model_name].copy()
|
382 |
-
inputs =
|
383 |
t = tqdm(range(epochs), desc="Training model")
|
384 |
losses = []
|
385 |
for _ in t:
|
@@ -406,7 +407,7 @@ def model_inference(
|
|
406 |
return ops.Result(bundle, error="Mapping is unset.")
|
407 |
m = bundle.other[model_name]
|
408 |
assert m.trained, "The model is not trained."
|
409 |
-
inputs =
|
410 |
outputs = m.inference(inputs)
|
411 |
bundle = bundle.copy()
|
412 |
copied = set()
|
|
|
8 |
from collections import deque
|
9 |
|
10 |
from tqdm import tqdm
|
11 |
+
from . import core
|
12 |
+
from .pytorch import pytorch_core
|
13 |
from lynxkite.core import workspace
|
14 |
import grandcypher
|
15 |
import joblib
|
|
|
348 |
assert model_workspace, "Model workspace is unset."
|
349 |
ws = load_ws(model_workspace)
|
350 |
# Build the model without inputs, to get its interface.
|
351 |
+
m = pytorch_core.build_model(ws)
|
352 |
m.source_workspace = model_workspace
|
353 |
bundle = bundle.copy()
|
354 |
bundle.other[save_as] = m
|
|
|
357 |
|
358 |
# These contain the same mapping, but they get different UIs.
|
359 |
# For inputs, you select existing columns. For outputs, you can create new columns.
|
360 |
+
class ModelInferenceInputMapping(pytorch_core.ModelMapping):
|
361 |
pass
|
362 |
|
363 |
|
364 |
+
class ModelTrainingInputMapping(pytorch_core.ModelMapping):
|
365 |
pass
|
366 |
|
367 |
|
368 |
+
class ModelOutputMapping(pytorch_core.ModelMapping):
|
369 |
pass
|
370 |
|
371 |
|
|
|
380 |
):
|
381 |
"""Trains the selected model on the selected dataset. Most training parameters are set in the model definition."""
|
382 |
m = bundle.other[model_name].copy()
|
383 |
+
inputs = pytorch_core.to_tensors(bundle, input_mapping)
|
384 |
t = tqdm(range(epochs), desc="Training model")
|
385 |
losses = []
|
386 |
for _ in t:
|
|
|
407 |
return ops.Result(bundle, error="Mapping is unset.")
|
408 |
m = bundle.other[model_name]
|
409 |
assert m.trained, "The model is not trained."
|
410 |
+
inputs = pytorch_core.to_tensors(bundle, input_mapping)
|
411 |
outputs = m.inference(inputs)
|
412 |
bundle = bundle.copy()
|
413 |
copied = set()
|
lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/__init__.py
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
from . import
|
2 |
-
from . import
|
|
|
1 |
+
from . import pytorch_core # noqa
|
2 |
+
from . import pytorch_ops # noqa
|
lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/{core.py → pytorch_core.py}
RENAMED
File without changes
|
lynxkite-graph-analytics/src/lynxkite_graph_analytics/pytorch/{ops.py → pytorch_ops.py}
RENAMED
@@ -5,7 +5,7 @@ from lynxkite.core import ops
|
|
5 |
from lynxkite.core.ops import Parameter as P
|
6 |
import torch
|
7 |
import torch_geometric.nn as pyg_nn
|
8 |
-
from .
|
9 |
|
10 |
reg("Input: tensor", outputs=["output"], params=[P.basic("name")])
|
11 |
reg("Input: graph edges", outputs=["edges"])
|
|
|
5 |
from lynxkite.core.ops import Parameter as P
|
6 |
import torch
|
7 |
import torch_geometric.nn as pyg_nn
|
8 |
+
from .pytorch_core import op, reg, ENV
|
9 |
|
10 |
reg("Input: tensor", outputs=["output"], params=[P.basic("name")])
|
11 |
reg("Input: graph edges", outputs=["edges"])
|
lynxkite-graph-analytics/tests/test_pytorch_model_ops.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from lynxkite.core import workspace
|
2 |
-
from lynxkite_graph_analytics import
|
3 |
import torch
|
4 |
import pytest
|
5 |
|
@@ -33,11 +33,11 @@ def make_ws(env, nodes: dict[str, dict], edges: list[tuple[str, str]]):
|
|
33 |
return ws
|
34 |
|
35 |
|
36 |
-
def summarize_layers(m:
|
37 |
return "".join(str(e)[0] for e in m.model)
|
38 |
|
39 |
|
40 |
-
def summarize_connections(m:
|
41 |
return " ".join(
|
42 |
"".join(n[0] for n in c.param_names) + "->" + "".join(n[0] for n in c.return_names)
|
43 |
for c in m.model._children
|
@@ -46,7 +46,7 @@ def summarize_connections(m: pytorch.core.ModelConfig) -> str:
|
|
46 |
|
47 |
async def test_build_model():
|
48 |
ws = make_ws(
|
49 |
-
|
50 |
{
|
51 |
"input": {"title": "Input: tensor"},
|
52 |
"lin": {"title": "Linear", "output_dim": 4},
|
@@ -67,7 +67,7 @@ async def test_build_model():
|
|
67 |
)
|
68 |
x = torch.rand(100, 4)
|
69 |
y = x + 1
|
70 |
-
m =
|
71 |
for i in range(1000):
|
72 |
loss = m.train({"input_output": x, "label_output": y})
|
73 |
assert loss < 0.1
|
@@ -79,7 +79,7 @@ async def test_build_model():
|
|
79 |
async def test_build_model_with_repeat():
|
80 |
def repeated_ws(times):
|
81 |
return make_ws(
|
82 |
-
|
83 |
{
|
84 |
"input": {"title": "Input: tensor"},
|
85 |
"lin": {"title": "Linear", "output_dim": 8},
|
@@ -103,17 +103,17 @@ async def test_build_model_with_repeat():
|
|
103 |
)
|
104 |
|
105 |
# 1 repetition
|
106 |
-
m =
|
107 |
assert summarize_layers(m) == "IL<III"
|
108 |
assert summarize_connections(m) == "i->S S->l l->a a->E E->o o->o"
|
109 |
|
110 |
# 2 repetitions
|
111 |
-
m =
|
112 |
assert summarize_layers(m) == "IL<IL<III"
|
113 |
assert summarize_connections(m) == "i->S S->l l->a a->S S->l l->a a->E E->o o->o"
|
114 |
|
115 |
# 3 repetitions
|
116 |
-
m =
|
117 |
assert summarize_layers(m) == "IL<IL<IL<III"
|
118 |
assert summarize_connections(m) == "i->S S->l l->a a->S S->l l->a a->S S->l l->a a->E E->o o->o"
|
119 |
|
|
|
1 |
from lynxkite.core import workspace
|
2 |
+
from lynxkite_graph_analytics.pytorch import pytorch_core
|
3 |
import torch
|
4 |
import pytest
|
5 |
|
|
|
33 |
return ws
|
34 |
|
35 |
|
36 |
+
def summarize_layers(m: pytorch_core.ModelConfig) -> str:
|
37 |
return "".join(str(e)[0] for e in m.model)
|
38 |
|
39 |
|
40 |
+
def summarize_connections(m: pytorch_core.ModelConfig) -> str:
|
41 |
return " ".join(
|
42 |
"".join(n[0] for n in c.param_names) + "->" + "".join(n[0] for n in c.return_names)
|
43 |
for c in m.model._children
|
|
|
46 |
|
47 |
async def test_build_model():
|
48 |
ws = make_ws(
|
49 |
+
pytorch_core.ENV,
|
50 |
{
|
51 |
"input": {"title": "Input: tensor"},
|
52 |
"lin": {"title": "Linear", "output_dim": 4},
|
|
|
67 |
)
|
68 |
x = torch.rand(100, 4)
|
69 |
y = x + 1
|
70 |
+
m = pytorch_core.build_model(ws)
|
71 |
for i in range(1000):
|
72 |
loss = m.train({"input_output": x, "label_output": y})
|
73 |
assert loss < 0.1
|
|
|
79 |
async def test_build_model_with_repeat():
|
80 |
def repeated_ws(times):
|
81 |
return make_ws(
|
82 |
+
pytorch_core.ENV,
|
83 |
{
|
84 |
"input": {"title": "Input: tensor"},
|
85 |
"lin": {"title": "Linear", "output_dim": 8},
|
|
|
103 |
)
|
104 |
|
105 |
# 1 repetition
|
106 |
+
m = pytorch_core.build_model(repeated_ws(1))
|
107 |
assert summarize_layers(m) == "IL<III"
|
108 |
assert summarize_connections(m) == "i->S S->l l->a a->E E->o o->o"
|
109 |
|
110 |
# 2 repetitions
|
111 |
+
m = pytorch_core.build_model(repeated_ws(2))
|
112 |
assert summarize_layers(m) == "IL<IL<III"
|
113 |
assert summarize_connections(m) == "i->S S->l l->a a->S S->l l->a a->E E->o o->o"
|
114 |
|
115 |
# 3 repetitions
|
116 |
+
m = pytorch_core.build_model(repeated_ws(3))
|
117 |
assert summarize_layers(m) == "IL<IL<IL<III"
|
118 |
assert summarize_connections(m) == "i->S S->l l->a a->S S->l l->a a->S S->l l->a a->E E->o o->o"
|
119 |
|