remove create_func....
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .func_fit import FuncFit, FuncFitConfig
|
||||
from .xor import XOR
|
||||
from .xor3d import XOR3d
|
||||
|
||||
69
problem/func_fit/func_fit.py
Normal file
69
problem/func_fit/func_fit.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from typing import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
import jax
|
||||
import jax.numpy as jnp
|
||||
from config import ProblemConfig
|
||||
from core import Problem, State
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FuncFitConfig(ProblemConfig):
|
||||
error_method: str = 'mse'
|
||||
|
||||
def __post_init__(self):
|
||||
assert self.error_method in {'mse', 'rmse', 'mae', 'mape'}
|
||||
|
||||
|
||||
class FuncFit(Problem):
|
||||
|
||||
def __init__(self, config: FuncFitConfig = FuncFitConfig()):
|
||||
self.config = config
|
||||
super().__init__(config)
|
||||
|
||||
def evaluate(self, randkey, state: State, act_func: Callable, params):
|
||||
|
||||
predict = act_func(state, self.inputs, params)
|
||||
|
||||
if self.config.error_method == 'mse':
|
||||
loss = jnp.mean((predict - self.targets) ** 2)
|
||||
|
||||
elif self.config.error_method == 'rmse':
|
||||
loss = jnp.sqrt(jnp.mean((predict - self.targets) ** 2))
|
||||
|
||||
elif self.config.error_method == 'mae':
|
||||
loss = jnp.mean(jnp.abs(predict - self.targets))
|
||||
|
||||
elif self.config.error_method == 'mape':
|
||||
loss = jnp.mean(jnp.abs((predict - self.targets) / self.targets))
|
||||
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
return -loss
|
||||
|
||||
def show(self, randkey, state: State, act_func: Callable, params):
|
||||
predict = act_func(state, self.inputs, params)
|
||||
inputs, target, predict = jax.device_get([self.inputs, self.targets, predict])
|
||||
loss = -self.evaluate(randkey, state, act_func, params)
|
||||
msg = ""
|
||||
for i in range(inputs.shape[0]):
|
||||
msg += f"input: {inputs[i]}, target: {target[i]}, predict: {predict[i]}\n"
|
||||
msg += f"loss: {loss}\n"
|
||||
print(msg)
|
||||
|
||||
@property
|
||||
def inputs(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def targets(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def input_shape(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def output_shape(self):
|
||||
raise NotImplementedError
|
||||
@@ -1,21 +0,0 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable
|
||||
|
||||
from config import ProblemConfig
|
||||
from core import Problem, State
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FuncFitConfig:
|
||||
pass
|
||||
|
||||
|
||||
class FuncFit(Problem):
|
||||
def __init__(self, config: ProblemConfig):
|
||||
self.config = ProblemConfig
|
||||
|
||||
def setup(self, state=State()):
|
||||
pass
|
||||
|
||||
def evaluate(self, state: State, act_func: Callable, params):
|
||||
pass
|
||||
36
problem/func_fit/xor.py
Normal file
36
problem/func_fit/xor.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import numpy as np
|
||||
|
||||
from .func_fit import FuncFit, FuncFitConfig
|
||||
|
||||
|
||||
class XOR(FuncFit):
|
||||
|
||||
def __init__(self, config: FuncFitConfig = FuncFitConfig()):
|
||||
self.config = config
|
||||
super().__init__(config)
|
||||
|
||||
@property
|
||||
def inputs(self):
|
||||
return np.array([
|
||||
[0, 0],
|
||||
[0, 1],
|
||||
[1, 0],
|
||||
[1, 1]
|
||||
])
|
||||
|
||||
@property
|
||||
def targets(self):
|
||||
return np.array([
|
||||
[0],
|
||||
[1],
|
||||
[1],
|
||||
[0]
|
||||
])
|
||||
|
||||
@property
|
||||
def input_shape(self):
|
||||
return (4, 2)
|
||||
|
||||
@property
|
||||
def output_shape(self):
|
||||
return (4, 1)
|
||||
44
problem/func_fit/xor3d.py
Normal file
44
problem/func_fit/xor3d.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
|
||||
from .func_fit import FuncFit, FuncFitConfig
|
||||
|
||||
|
||||
class XOR3d(FuncFit):
|
||||
|
||||
def __init__(self, config: FuncFitConfig = FuncFitConfig()):
|
||||
self.config = config
|
||||
super().__init__(config)
|
||||
|
||||
@property
|
||||
def inputs(self):
|
||||
return np.array([
|
||||
[0, 0, 0],
|
||||
[0, 0, 1],
|
||||
[0, 1, 0],
|
||||
[0, 1, 1],
|
||||
[1, 0, 0],
|
||||
[1, 0, 1],
|
||||
[1, 1, 0],
|
||||
[1, 1, 1],
|
||||
])
|
||||
|
||||
@property
|
||||
def targets(self):
|
||||
return np.array([
|
||||
[0],
|
||||
[1],
|
||||
[1],
|
||||
[0],
|
||||
[1],
|
||||
[0],
|
||||
[0],
|
||||
[1]
|
||||
])
|
||||
|
||||
@property
|
||||
def input_shape(self):
|
||||
return (8, 3)
|
||||
|
||||
@property
|
||||
def output_shape(self):
|
||||
return (8, 1)
|
||||
Reference in New Issue
Block a user