Reformulate Math Functions

This commit is contained in:
D-X-Y
2021-04-26 05:16:38 -07:00
parent 1980779053
commit e1818694a4
14 changed files with 308 additions and 254 deletions

View File

@@ -13,13 +13,17 @@ import torch.utils.data as data
class FitFunc(abc.ABC):
"""The fit function that outputs f(x) = a * x^2 + b * x + c."""
def __init__(self, freedom: int, list_of_points=None):
def __init__(self, freedom: int, list_of_points=None, _params=None):
self._params = dict()
for i in range(freedom):
self._params[i] = None
self._freedom = freedom
if list_of_points is not None and _params is not None:
raise ValueError("list_of_points and _params can not be set simultaneously")
if list_of_points is not None:
self.fit(list_of_points)
self.fit(list_of_points=list_of_points)
if _params is not None:
self.set(_params)
def set(self, _params):
self._params = copy.deepcopy(_params)
@@ -45,13 +49,13 @@ class FitFunc(abc.ABC):
def _getitem(self, x):
raise NotImplementedError
def fit(
self,
list_of_points,
max_iter=900,
lr_max=1.0,
verbose=False,
):
def fit(self, **kwargs):
list_of_points = kwargs["list_of_points"]
max_iter, lr_max, verbose = (
kwargs.get("max_iter", 900),
kwargs.get("lr_max", 1.0),
kwargs.get("verbose", False),
)
with torch.no_grad():
data = torch.Tensor(list_of_points).type(torch.float32)
assert data.ndim == 2 and data.size(1) == 2, "Invalid shape : {:}".format(
@@ -113,7 +117,7 @@ class QuadraticFunc(FitFunc):
return weights[0] * x * x + weights[1] * x + weights[2]
def __repr__(self):
return "{name}(y = {a} * x^2 + {b} * x + {c})".format(
return "{name}({a} * x^2 + {b} * x + {c})".format(
name=self.__class__.__name__,
a=self._params[0],
b=self._params[1],
@@ -140,7 +144,7 @@ class CubicFunc(FitFunc):
return weights[0] * x ** 3 + weights[1] * x ** 2 + weights[2] * x + weights[3]
def __repr__(self):
return "{name}(y = {a} * x^3 + {b} * x^2 + {c} * x + {d})".format(
return "{name}({a} * x^3 + {b} * x^2 + {c} * x + {d})".format(
name=self.__class__.__name__,
a=self._params[0],
b=self._params[1],
@@ -175,7 +179,7 @@ class QuarticFunc(FitFunc):
)
def __repr__(self):
return "{name}(y = {a} * x^4 + {b} * x^3 + {c} * x^2 + {d} * x + {e})".format(
return "{name}({a} * x^4 + {b} * x^3 + {c} * x^2 + {d} * x + {e})".format(
name=self.__class__.__name__,
a=self._params[0],
b=self._params[1],
@@ -183,34 +187,3 @@ class QuarticFunc(FitFunc):
d=self._params[3],
e=self._params[3],
)
class DynamicQuadraticFunc(FitFunc):
"""The dynamic quadratic function that outputs f(x) = a * x^2 + b * x + c."""
def __init__(self, list_of_points=None):
super(DynamicQuadraticFunc, self).__init__(3, list_of_points)
self._timestamp = None
def __call__(self, x):
self.check_valid()
a = self._params[0][self._timestamp]
b = self._params[1][self._timestamp]
c = self._params[2][self._timestamp]
convert_fn = lambda x: x[-1] if isinstance(x, (tuple, list)) else x
a, b, c = convert_fn(a), convert_fn(b), convert_fn(c)
return a * x * x + b * x + c
def _getitem(self, x, weights):
raise NotImplementedError
def set_timestamp(self, timestamp):
self._timestamp = timestamp
def __repr__(self):
return "{name}(y = {a} * x^2 + {b} * x + {c})".format(
name=self.__class__.__name__,
a=self._params[0],
b=self._params[1],
c=self._params[2],
)