-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
151 additions
and
6 deletions.
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
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
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 |
---|---|---|
|
@@ -11,4 +11,6 @@ markers = | |
test_function | ||
test_scalar | ||
test_scalar_opts | ||
test_scalar_overload | ||
test_scalar_overload | ||
|
||
test_module |
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,4 +1,4 @@ | ||
from .function import add, sub, mul, div, neg, max, min, eq, lt, gt, abs, exp, log, relu | ||
from .variable import VarFunction, VarHistory, Variable | ||
from .variable import Variable | ||
from .scalar import Scalar | ||
from .scalar_opts import ScalarOptsBackend, Add, Sub, Mul, Div, Max, Min, Eq, Lt, Gt, Abs, Exp, Log, Relu, Sigmoid, Tanh | ||
from .scalar_opts import ScalarOptsBackend |
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,2 @@ | ||
from .module import Module | ||
from .parameter import Parameter |
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,37 @@ | ||
from typing import Any, List, Tuple | ||
|
||
from .parameter import Parameter | ||
|
||
module_count:int = 0 | ||
|
||
class Module(): | ||
|
||
_id: int | ||
|
||
def __init__(self) -> None: | ||
global module_count | ||
self._id = module_count | ||
module_count += 1 | ||
return | ||
|
||
def __call__(self, *args: Any, **kwds: Any) -> Any: | ||
return self.forward(*args, **kwds) | ||
|
||
def getParmeterDict(self) -> dict: | ||
def getParmeterList(module:Module) -> List[Tuple[str, Parameter]]: | ||
parmeters = [] | ||
for key in module.__dict__: | ||
value = module.__dict__[key] | ||
if isinstance(value, Module): | ||
subparmeters = [] | ||
for name, parmeter in getParmeterList(value): | ||
subparmeters.append((f"{key}.{name}", parmeter)) | ||
parmeters += subparmeters | ||
if isinstance(value, Parameter): | ||
parmeters.append((key, value)) | ||
return parmeters | ||
parmeters = dict(getParmeterList(self)) | ||
return parmeters | ||
|
||
def forward(self, *args: Any, **kwds: Any) -> Any: | ||
raise NotImplementedError |
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,18 @@ | ||
from ..autodiff.variable import Variable | ||
|
||
parameter_count:int = 0 | ||
|
||
class Parameter(): | ||
|
||
_id: int | ||
_value: Variable | ||
|
||
def __init__(self, _value:Variable) -> None: | ||
global parameter_count | ||
self._id = parameter_count | ||
parameter_count += 1 | ||
self._value = _value | ||
return | ||
|
||
def value(self) -> Variable: | ||
return self._value |
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,31 @@ | ||
import pytest | ||
|
||
from terox.autodiff.scalar import Scalar | ||
from terox.module.module import Module | ||
from terox.module.parameter import Parameter | ||
|
||
class M1(Module): | ||
def __init__(self, p1:Parameter, p2:Parameter) -> None: | ||
super().__init__() | ||
self.p1 = p1 | ||
self.p2 = p2 | ||
return | ||
|
||
class M2(Module): | ||
def __init__(self, m1:M1, p3:Parameter) -> None: | ||
super().__init__() | ||
self.m1 = m1 | ||
self.p3 = p3 | ||
return | ||
|
||
p1 = Parameter(Scalar(1.0)) | ||
p2 = Parameter(Scalar(2.0)) | ||
p3 = Parameter(Scalar(3.0)) | ||
m1 = M1(p1, p2) | ||
m2 = M2(m1, p3) | ||
|
||
@pytest.mark.test_module | ||
def test_module_dict() -> None: | ||
assert m1.getParmeterDict() == {"p1":p1, "p2":p2} | ||
assert m2.getParmeterDict() == {"m1.p1":p1, "m1.p2":p2, "p3":p3} | ||
return |