DepthML 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
DepthML/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from DepthTensor import Tensor
2
+
3
+ from .components import activation, layer, loss
4
+
5
+ __version__ = "0.1.0"
@@ -0,0 +1,3 @@
1
+ from .layer import *
2
+ from .activation import *
3
+ from .loss import *
@@ -0,0 +1,3 @@
1
+ from .identity import Identity
2
+ from .relu import ReLU
3
+ from .leaky_relu import LeakyReLU
@@ -0,0 +1,19 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import (Any)
3
+ from DepthTensor import Tensor
4
+
5
+ from ..base_component import BaseComponent
6
+
7
+ class BaseActivation(BaseComponent, ABC):
8
+ def __init__(self) -> None:
9
+ super().__init__()
10
+
11
+ def build(self):
12
+ pass
13
+
14
+ @abstractmethod
15
+ def call(self, *args: Any, **kwds: Any) -> Any:
16
+ raise NotImplementedError
17
+
18
+ def __call__(self, X: Tensor) -> Tensor:
19
+ return self.call(X)
@@ -0,0 +1,27 @@
1
+ from typing import (
2
+ Any,
3
+ Callable
4
+ )
5
+
6
+ from DepthTensor import (
7
+ Tensor,
8
+ NDArrayLike,
9
+ DeviceLike,
10
+ create_1in_1out
11
+ )
12
+
13
+ from .base_activation import BaseActivation
14
+
15
+ def op(x: NDArrayLike, *, device: DeviceLike, alpha: float = 0.01, **kwds: Any) -> Tensor:
16
+ return Tensor(x, device=device)
17
+
18
+ def diff(result: Tensor, x: NDArrayLike, alpha: float = 0.01, **kwds: Any) -> Callable[[], NDArrayLike]:
19
+ def dx() -> NDArrayLike:
20
+ return 1.0
21
+ return dx
22
+
23
+ func = create_1in_1out(op, diff)
24
+
25
+ class Identity(BaseActivation):
26
+ def call(self, X: Tensor) -> Tensor:
27
+ return func(X)
@@ -0,0 +1,32 @@
1
+ from typing import (
2
+ Callable,
3
+ Any
4
+ )
5
+
6
+ import DepthTensor as DTensor
7
+ from DepthTensor import (
8
+ Tensor,
9
+ NDArrayLike,
10
+ DeviceLike,
11
+ create_1in_1out
12
+ )
13
+
14
+ from .base_activation import BaseActivation
15
+
16
+ def op(x: NDArrayLike, *, device: DeviceLike, alpha: float = 0.01, **kwds: Any) -> Tensor:
17
+ return DTensor.maximum(alpha * x, x, requires_grad=True, device=device)
18
+
19
+ def diff(result: Tensor, x: NDArrayLike, alpha: float = 0.01, **kwds: Any) -> Callable[[], NDArrayLike]:
20
+ def dx() -> NDArrayLike:
21
+ return DTensor.where(x > 0.0, 1.0, alpha).data
22
+ return dx
23
+
24
+ func = create_1in_1out(op, diff)
25
+
26
+ class LeakyReLU(BaseActivation):
27
+ def __init__(self, alpha: float = 0.01) -> None:
28
+ super().__init__()
29
+ self.alpha = alpha
30
+
31
+ def call(self, X: Tensor) -> Tensor:
32
+ return func(X, alpha=self.alpha)
@@ -0,0 +1,28 @@
1
+ from typing import (
2
+ Callable,
3
+ Any
4
+ )
5
+
6
+ import DepthTensor as DTensor
7
+ from DepthTensor import (
8
+ Tensor,
9
+ NDArrayLike,
10
+ DeviceLike,
11
+ create_1in_1out
12
+ )
13
+
14
+ from .base_activation import BaseActivation
15
+
16
+ def relu_op(x: NDArrayLike, *, device: DeviceLike, **kwds: Any) -> Tensor:
17
+ return DTensor.maximum(0.0, x, requires_grad=True, device=device)
18
+
19
+ def relu_diff(result: Tensor, x: NDArrayLike, **kwds: Any) -> Callable[[], NDArrayLike]:
20
+ def dx() -> NDArrayLike:
21
+ return DTensor.where(x > 0.0, 1.0, 0.0).data
22
+ return dx
23
+
24
+ relu_func = create_1in_1out(relu_op, relu_diff)
25
+
26
+ class ReLU(BaseActivation):
27
+ def call(self, X: Tensor) -> Tensor:
28
+ return relu_func(X)
@@ -0,0 +1,19 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import (Any)
3
+
4
+ class BaseComponent(ABC):
5
+ def __init__(self) -> None:
6
+ super().__init__()
7
+ self._build = False
8
+
9
+ @abstractmethod
10
+ def build(self, *args: Any, **kwds: Any) -> Any:
11
+ raise NotImplementedError
12
+
13
+ @abstractmethod
14
+ def call(self, *args: Any, **kwds: Any) -> Any:
15
+ raise NotImplementedError
16
+
17
+ @abstractmethod
18
+ def __call__(self, *args: Any, **kwds: Any) -> Any:
19
+ raise NotImplementedError
@@ -0,0 +1 @@
1
+ from .affine import Affine
@@ -0,0 +1,25 @@
1
+ from DepthTensor import Tensor, random
2
+
3
+ from ...typing import (
4
+ InitializerLike
5
+ )
6
+
7
+ from ...initializers import Uniform
8
+ from .base_layer import BaseLayer
9
+
10
+ class Affine(BaseLayer):
11
+ def __init__(self, units: int, initializer: InitializerLike = Uniform()) -> None:
12
+ super().__init__()
13
+ self.units = units
14
+ self.initializer = initializer
15
+
16
+ def build(self, X: Tensor) -> None:
17
+ self.w = self.initializer((X.shape[-1], self.units), X.device)
18
+ #print(hash(self.w))
19
+ self.b = random.uniform(-1, 1, (self.units,), device=X.device, requires_grad=True)
20
+ #print(self.b.device)
21
+ #print(type(self.b.grad))
22
+ #print(hash(self.b))
23
+
24
+ def call(self, X: Tensor) -> Tensor:
25
+ return X @ self.w + self.b
@@ -0,0 +1,23 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import (Any)
3
+ from DepthTensor import Tensor
4
+
5
+ from ..base_component import BaseComponent
6
+
7
+ class BaseLayer(BaseComponent, ABC):
8
+ def __init__(self) -> None:
9
+ super().__init__()
10
+
11
+ @abstractmethod
12
+ def build(self, *args: Any, **kwds: Any) -> Any:
13
+ raise NotImplementedError
14
+
15
+ @abstractmethod
16
+ def call(self, *args: Any, **kwds: Any) -> Any:
17
+ raise NotImplementedError
18
+
19
+ def __call__(self, X: Tensor) -> Tensor:
20
+ if not self._build:
21
+ self._build = True
22
+ self.build(X)
23
+ return self.call(X)
@@ -0,0 +1 @@
1
+ from .mean_squared_error import MeanSquaredError
@@ -0,0 +1,24 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Any
3
+
4
+ from DepthTensor import Tensor
5
+
6
+ from ..base_component import BaseComponent
7
+
8
+ class BaseLoss(BaseComponent, ABC):
9
+ def __init__(self) -> None:
10
+ super().__init__()
11
+
12
+ @abstractmethod
13
+ def call(self, y_true: Tensor, y_pred: Tensor) -> Tensor:
14
+ raise NotImplementedError
15
+
16
+ @abstractmethod
17
+ def backward(self, *args: Any, **kwds: Any) -> Any:
18
+ raise NotImplementedError
19
+
20
+ def build(self) -> Any:
21
+ pass
22
+
23
+ def __call__(self, y_true: Tensor, y_pred: Tensor) -> Tensor:
24
+ return self.call(y_true, y_pred)
@@ -0,0 +1,14 @@
1
+ from typing import Any
2
+ from DepthTensor import Tensor
3
+
4
+ from .base_loss import BaseLoss
5
+
6
+ class MeanSquaredError(BaseLoss):
7
+ def __init__(self) -> None:
8
+ super().__init__()
9
+
10
+ def call(self, y_true: Tensor, y_pred: Tensor) -> Tensor:
11
+ return (y_true - y_pred)**2
12
+
13
+ def backward(self) -> Any:
14
+ pass
@@ -0,0 +1,4 @@
1
+ from .he import He
2
+ from .xavier import Xavier
3
+
4
+ from .uniform import Uniform
@@ -0,0 +1,33 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import (
3
+ Any,
4
+ Optional
5
+ )
6
+
7
+ from DepthTensor.typing import (
8
+ ShapeLike,
9
+ DeviceLike
10
+ )
11
+
12
+ from DepthTensor import Tensor
13
+
14
+ class BaseInitializer(ABC):
15
+ def __init__(self) -> None:
16
+ super().__init__()
17
+ self._build = False
18
+
19
+ @abstractmethod
20
+ def call(self, *args: Any, **kwds: Any) -> Any:
21
+ raise NotImplementedError
22
+
23
+ def build(self, device: DeviceLike) -> Any:
24
+ self.device: DeviceLike = device
25
+
26
+ def __call__(self, fan_inout: ShapeLike, device: Optional[DeviceLike] = None) -> Tensor:
27
+ if not self._build:
28
+ self._build = True
29
+ if device is not None:
30
+ self.build(device)
31
+ else:
32
+ raise RuntimeError("In order to be called, initializer must be built first. Insufficient arguments available to initiate the process: argument device is None.")
33
+ return self.call(fan_inout)
@@ -0,0 +1,16 @@
1
+ from DepthTensor import Tensor, random
2
+ from DepthTensor.typing import (
3
+ ShapeLike
4
+ )
5
+
6
+ from .base_initializer import BaseInitializer
7
+
8
+ import math
9
+
10
+ class He(BaseInitializer):
11
+ def __init__(self) -> None:
12
+ super().__init__()
13
+
14
+ def call(self, fan_inout: ShapeLike) -> Tensor:
15
+ limit = math.sqrt(6 / fan_inout[0])
16
+ return random.uniform(low=-limit, high=limit, size=fan_inout, device=self.device, requires_grad=True)
@@ -0,0 +1,15 @@
1
+ from DepthTensor import Tensor, random
2
+ from DepthTensor.typing import (
3
+ ShapeLike
4
+ )
5
+
6
+ from .base_initializer import BaseInitializer
7
+
8
+ class Uniform(BaseInitializer):
9
+ def __init__(self, low: float = -0.05, high: float = 0.05) -> None:
10
+ super().__init__()
11
+ self.low = low
12
+ self.high = high
13
+
14
+ def call(self, fan_inout: ShapeLike) -> Tensor:
15
+ return random.uniform(low=self.low, high=self.high, size=fan_inout, device=self.device, requires_grad=True)
@@ -0,0 +1,16 @@
1
+ from DepthTensor import Tensor, random
2
+ from DepthTensor.typing import (
3
+ ShapeLike
4
+ )
5
+
6
+ from .base_initializer import BaseInitializer
7
+
8
+ import math
9
+
10
+ class Xavier(BaseInitializer):
11
+ def __init__(self) -> None:
12
+ super().__init__()
13
+
14
+ def call(self, fan_inout: ShapeLike) -> Tensor:
15
+ limit = math.sqrt(6 / sum(fan_inout))
16
+ return random.uniform(low=-limit, high=limit, size=fan_inout, device=self.device, requires_grad=True)
@@ -0,0 +1 @@
1
+ from .stack import Stack
@@ -0,0 +1,23 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import (Any)
3
+
4
+ class BaseModel(ABC):
5
+ def __init__(self) -> None:
6
+ super().__init__()
7
+ self._build = False
8
+
9
+ @abstractmethod
10
+ def build(self, *args, **kwds: Any) -> Any:
11
+ raise NotImplementedError
12
+
13
+ @abstractmethod
14
+ def call(self, *args: Any, **kwds: Any) -> Any:
15
+ raise NotImplementedError
16
+
17
+ @abstractmethod
18
+ def fit(self, *args: Any, **kwds: Any) -> Any:
19
+ raise NotImplementedError
20
+
21
+ @abstractmethod
22
+ def __call__(self, *args: Any, **kwds: Any) -> Any:
23
+ raise NotImplementedError
@@ -0,0 +1,57 @@
1
+ from __future__ import annotations
2
+ from typing import Any, Sequence, List
3
+ from DepthTensor import Tensor, differentiate
4
+
5
+ from DepthTensor.typing import (
6
+ DeviceLike
7
+ )
8
+
9
+ from ..typing import (
10
+ ComponentLike,
11
+ LossLike
12
+ )
13
+
14
+ from .base_model import BaseModel
15
+
16
+ class Stack(BaseModel):
17
+ def __init__(self, components: Sequence[ComponentLike]) -> None:
18
+ super().__init__()
19
+ self.components = components
20
+
21
+ def build(self) -> Any:
22
+ pass
23
+
24
+ def call(self, X: Tensor) -> Tensor:
25
+ for component in self.components:
26
+ X = component(X)
27
+ return X
28
+
29
+ def fit(self: Stack, X: List[Tensor], Y: List[Tensor], loss: LossLike, epoch: int = 1, learning_rate: float = 0.01):
30
+ samples_n = len(X)
31
+
32
+ for epoch_n in range(epoch):
33
+ for (X_ind, X_val), (Y_ind, Y_val) in zip(enumerate(X), enumerate(Y)):
34
+ y_tr = self(X_val)
35
+ L = loss(Y_val, y_tr)
36
+ parameters = differentiate(L)
37
+
38
+ for t in parameters:
39
+ t.data -= learning_rate * t.grad
40
+
41
+ progress = max(0, min(10, 10*(X_ind+1)//samples_n))
42
+ print(
43
+ "[Epoch: {epoch_n}/{epoch_max}] Progress: [{progress_str}] ({progress_percent}%) Loss: {loss_scalar}".format(
44
+ epoch_n=epoch_n + 1,
45
+ epoch_max=epoch,
46
+ progress_str="#"*progress + "_"*(10-progress),
47
+ progress_percent=100*(X_ind+1)/samples_n,
48
+ loss_scalar=L.data.mean()
49
+ ),
50
+ end="\r" if X_ind + 1 < samples_n else "\n"
51
+ )
52
+
53
+ def __call__(self, X: Tensor) -> Tensor:
54
+ if not self._build:
55
+ self._build = True
56
+ self.build()
57
+ return self.call(X)
DepthML/typing.py ADDED
@@ -0,0 +1,19 @@
1
+ from typing import (
2
+ TYPE_CHECKING,
3
+ TypeAlias
4
+ )
5
+
6
+ if TYPE_CHECKING:
7
+ from .models.base_model import BaseModel
8
+ from .initializers.base_initializer import BaseInitializer
9
+ from .components.base_component import BaseComponent
10
+ from .components.activation.base_activation import BaseActivation
11
+ from .components.layer.base_layer import BaseLayer
12
+ from .components.loss.base_loss import BaseLoss
13
+
14
+ ModelLike: TypeAlias = "BaseModel"
15
+ InitializerLike: TypeAlias = "BaseInitializer"
16
+ ComponentLike: TypeAlias = "BaseComponent"
17
+ TransformerLike: TypeAlias = "BaseLayer"
18
+ ActivationLike: TypeAlias = "BaseActivation"
19
+ LossLike: TypeAlias = "BaseLoss"
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: DepthML
3
+ Version: 0.1.0
4
+ Description-Content-Type: text/markdown
5
+ License-File: LICENSE
6
+ Requires-Dist: numpy>=2.3.1
7
+ Requires-Dist: depthtensor>=2.0.2
8
+ Dynamic: license-file
@@ -0,0 +1,28 @@
1
+ DepthML/__init__.py,sha256=f991V9oaTdYp7dpQvU6w1RnAQB6eHxKA4CCFwWrAQ0o,102
2
+ DepthML/typing.py,sha256=eJRBt4rZudD_mOusomeLX2dlNq7e-aKQB3GOF4-CCSM,661
3
+ DepthML/components/__init__.py,sha256=Ialcd0OW3KPxiRdwcyzkXXGGy_1olzcSOvtggWs1Y4w,66
4
+ DepthML/components/base_component.py,sha256=lW5tDrDCei7FKQcVHmwVVT7yIrxIK99PonhZpbFrmXI,504
5
+ DepthML/components/activation/__init__.py,sha256=BIkWDxe4dpZsnBkBKQ9ljEsEAvj_6LetXBdBmxvJ5M4,87
6
+ DepthML/components/activation/base_activation.py,sha256=GeGEcSXaBQA7MpCVRLqpBjYx9_NXhN1MMEhMUMQRCTA,457
7
+ DepthML/components/activation/identity.py,sha256=er2x0eu_SZvG0ekdej75lB9DZ3X2FvShXU_vohuNn8E,610
8
+ DepthML/components/activation/leaky_relu.py,sha256=VxzQokl_vGfcmdJTwzqTNXlw0coVw5cCyvNZmWd2MKI,845
9
+ DepthML/components/activation/relu.py,sha256=V7bnRoKicehLfBTrblwYzRnnZCSmjQkuUKq9KTuzUUw,694
10
+ DepthML/components/layer/__init__.py,sha256=0VvZYrVmQBsbq2rfRGa5QSamwL3jttZeEBklojDx3x4,26
11
+ DepthML/components/layer/affine.py,sha256=rzw4WlAg5GN5qcdo2iCVBCueKKeGtL4iqgG0ijfGt7A,763
12
+ DepthML/components/layer/base_layer.py,sha256=B9T1bx-2AOM5mRXIsvt29Pske2J3eltFkQBymOzSVWI,610
13
+ DepthML/components/loss/__init__.py,sha256=wPQw5d2uEtF1vr2BkauRLqjyZ_BD0eHYDrK5dNGkiig,48
14
+ DepthML/components/loss/base_loss.py,sha256=lPr8wqlgQDx3v5WqwR4fRW6l5KJKMcGYIluPD-5Auno,620
15
+ DepthML/components/loss/mean_squared_error.py,sha256=ksEwmSUPqYiUl25E-DFwl-l8pfbsr_f0W2hwI4hx5c4,328
16
+ DepthML/initializers/__init__.py,sha256=u-wrZ3t9emyoq491fdYuKpnushcSIAbNXU8X-8ArXUs,75
17
+ DepthML/initializers/base_initializer.py,sha256=3BULLFLyfouZM575qB3OXar1yxxP0ntbDwefwUWdVHs,957
18
+ DepthML/initializers/he.py,sha256=-vDmbzuSI1SlR_h9eX8ve8v4j1xwVOKIw6Q-y6LY1rw,441
19
+ DepthML/initializers/uniform.py,sha256=vxjixb4WFeZKNSMKZ4aNCNAk20fw97RNNwc2asaG32U,483
20
+ DepthML/initializers/xavier.py,sha256=6d0XEOeWir4G_RnPLQ83_40X6QawtcDfdWw7uINjDbw,447
21
+ DepthML/models/__init__.py,sha256=-A_roZNa5UpCfo-AQKFS_3Z6bRz2saBxp8t6JAVwHRo,24
22
+ DepthML/models/base_model.py,sha256=ZvPFGVuFp44svVeMupL2G397w4Gfb0QA8LLZ0potUZY,597
23
+ DepthML/models/stack.py,sha256=_BbrloSFDijvNtEemS46Z3ByqG-vGtKiaI44Tws-vjw,1847
24
+ depthml-0.1.0.dist-info/licenses/LICENSE,sha256=LzMplw-ua5HYMCetqBYUI37_LJIWzeXbtN8nWUUgy-o,1071
25
+ depthml-0.1.0.dist-info/METADATA,sha256=5sKziH7SUndwx8f-UN3pSmzEZr_InxTKUNKvoGNPRD8,197
26
+ depthml-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
27
+ depthml-0.1.0.dist-info/top_level.txt,sha256=QkMiWJIDL2BX0uI5P9rR6BKEj-H4xDf3pxP8BVVL2rc,8
28
+ depthml-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Lê Hồng Hà
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ DepthML