Repository navigation
Expand file tree
/
Copy pathbase_module.py
More file actions
100 lines (78 loc) · 2.56 KB
/
Copy pathbase_module.py
File metadata and controls
100 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import annotations
from abc import ABC
import numpy as np
class BaseModule(ABC):
"""
Base class representing a function to be optimized in a descent method algorithm
Attributes
----------
weights_ : ndarray of shape (n_in, n_out)
Parameters of function with respect to which the function is optimized.
"""
def __init__(self, weights: np.ndarray = None) -> BaseModule:
"""
Initialize a module instance
Parameters:
----------
weights: np.ndarray, default None
Initial value of weights
"""
self.weights_ = weights
def compute_output(self, **kwargs) -> np.ndarray:
"""
Compute the output value of the function
Parameters
----------
kwargs: Additional arguments to be passed and used by derived objects
Returns
-------
output: ndarray of shape (n_out,)
Value of function at `input`
Examples
--------
For f:R^d->R defined by f(x) = <w,x> then: n_in=d, n_out=1 and thus output shape is (1,)
"""
raise NotImplementedError()
def compute_jacobian(self, **kwargs) -> np.ndarray:
"""
Compute the derivative of the function with respect to each of its parameters
Parameters
----------
kwargs: Additional arguments to be passed and used by derived objects
Returns
-------
output: ndarray of shape (n_out, n_in)
Derivative of function with respect to its parameters at `input`
Examples
--------
For f:R^d->R defined by f(x) = <w,x> then: n_in=d, n_out=1 and thus output shape is (1,d)
"""
raise NotImplementedError()
@property
def weights(self):
"""
Wrapper property to retrieve module parameter
Returns
-------
weights: ndarray of shape (n_in, n_out)
"""
return self.weights_
@weights.setter
def weights(self, weights: np.ndarray) -> None:
"""
Setter function for module parameters
Parameters
----------
weights: ndarray array of shape (n_in, n_out)
"""
self.weights_ = weights
@property
def shape(self):
"""
Specify the dimensions of the function
Returns
-------
shape: Tuple[int]
Specifying the dimensions of the functions parameters. If ``self.weights`` is None returns `(0,)`
"""
return self.weights.shape if self.weights is not None else (0,)