Fix bugs
This commit is contained in:
@@ -64,65 +64,29 @@ class ComposedSinFunc(FitFunc):
|
||||
)
|
||||
|
||||
|
||||
class ComposedSinFuncV2(FitFunc):
|
||||
class ComposedCosFunc(FitFunc):
|
||||
"""The composed sin function that outputs:
|
||||
f(x) = amplitude-scale-of(x) * sin( period-phase-shift-of(x) )
|
||||
- the amplitude scale is a quadratic function of x
|
||||
- the period-phase-shift is another quadratic function of x
|
||||
f(x) = a * cos( b*x ) + c
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(ComposedSinFuncV2, self).__init__(0, None)
|
||||
self.fit(**kwargs)
|
||||
def __init__(self, params, xstr="x"):
|
||||
super(ComposedCosFunc, self).__init__(3, None, params, xstr)
|
||||
|
||||
def __call__(self, x):
|
||||
self.check_valid()
|
||||
scale = self._params["amplitude_scale"](x)
|
||||
period_phase = self._params["period_phase_shift"](x)
|
||||
return scale * math.sin(period_phase)
|
||||
|
||||
def fit(self, **kwargs):
|
||||
num_sin_phase = kwargs.get("num_sin_phase", 7)
|
||||
sin_speed_use_power = kwargs.get("sin_speed_use_power", True)
|
||||
min_amplitude = kwargs.get("min_amplitude", 1)
|
||||
max_amplitude = kwargs.get("max_amplitude", 4)
|
||||
phase_shift = kwargs.get("phase_shift", 0.0)
|
||||
# create parameters
|
||||
if kwargs.get("amplitude_scale", None) is None:
|
||||
amplitude_scale = QuadraticFunc(
|
||||
[(0, min_amplitude), (0.5, max_amplitude), (1, min_amplitude)]
|
||||
)
|
||||
else:
|
||||
amplitude_scale = kwargs.get("amplitude_scale")
|
||||
if kwargs.get("period_phase_shift", None) is None:
|
||||
fitting_data = []
|
||||
if sin_speed_use_power:
|
||||
temp_max_scalar = 2 ** (num_sin_phase - 1)
|
||||
else:
|
||||
temp_max_scalar = num_sin_phase - 1
|
||||
for i in range(num_sin_phase):
|
||||
if sin_speed_use_power:
|
||||
value = (2 ** i) / temp_max_scalar
|
||||
next_value = (2 ** (i + 1)) / temp_max_scalar
|
||||
else:
|
||||
value = i / temp_max_scalar
|
||||
next_value = (i + 1) / temp_max_scalar
|
||||
for _phase in (0, 0.25, 0.5, 0.75):
|
||||
inter_value = value + (next_value - value) * _phase
|
||||
fitting_data.append((inter_value, math.pi * (2 * i + _phase)))
|
||||
period_phase_shift = QuarticFunc(fitting_data)
|
||||
else:
|
||||
period_phase_shift = kwargs.get("period_phase_shift")
|
||||
self.set(
|
||||
dict(amplitude_scale=amplitude_scale, period_phase_shift=period_phase_shift)
|
||||
)
|
||||
a = self._params[0]
|
||||
b = self._params[1]
|
||||
c = self._params[2]
|
||||
return a * math.cos(b * x) + c
|
||||
|
||||
def _getitem(self, x, weights):
|
||||
raise NotImplementedError
|
||||
|
||||
def __repr__(self):
|
||||
return "{name}({amplitude_scale} * sin({period_phase_shift}))".format(
|
||||
return "{name}({a} * sin({b} * {x}) + {c})".format(
|
||||
name=self.__class__.__name__,
|
||||
amplitude_scale=self._params["amplitude_scale"],
|
||||
period_phase_shift=self._params["period_phase_shift"],
|
||||
a=self._params[0],
|
||||
b=self._params[1],
|
||||
c=self._params[2],
|
||||
x=self.xstr,
|
||||
)
|
||||
|
@@ -5,5 +5,5 @@ from .math_base_funcs import LinearFunc, QuadraticFunc, CubicFunc, QuarticFunc
|
||||
from .math_dynamic_funcs import DynamicLinearFunc
|
||||
from .math_dynamic_funcs import DynamicQuadraticFunc
|
||||
from .math_adv_funcs import ConstantFunc
|
||||
from .math_adv_funcs import ComposedSinFunc
|
||||
from .math_adv_funcs import ComposedSinFunc, ComposedCosFunc
|
||||
from .math_dynamic_generator import GaussianDGenerator
|
||||
|
@@ -4,7 +4,11 @@ from .synthetic_env import SyntheticDEnv
|
||||
from .math_core import LinearFunc
|
||||
from .math_core import DynamicLinearFunc
|
||||
from .math_core import DynamicQuadraticFunc
|
||||
from .math_core import ConstantFunc, ComposedSinFunc as SinFunc
|
||||
from .math_core import (
|
||||
ConstantFunc,
|
||||
ComposedSinFunc as SinFunc,
|
||||
ComposedCosFunc as CosFunc,
|
||||
)
|
||||
from .math_core import GaussianDGenerator
|
||||
|
||||
|
||||
@@ -50,6 +54,25 @@ def get_synthetic_env(total_timestamp=1600, num_per_task=1000, mode=None, versio
|
||||
dynamic_env = SyntheticDEnv(
|
||||
data_generator, oracle_map, time_generator, num_per_task
|
||||
)
|
||||
elif version.lower() == "v3":
|
||||
mean_generator = SinFunc(params={0: 1, 1: 1, 2: 0}) # sin(t)
|
||||
std_generator = CosFunc(params={0: 0.5, 1: 1, 2: 1}) # 0.5 cos(t) + 1
|
||||
data_generator = GaussianDGenerator(
|
||||
[mean_generator], [[std_generator]], (-2, 2)
|
||||
)
|
||||
time_generator = TimeStamp(
|
||||
min_timestamp=0, max_timestamp=max_time, num=total_timestamp, mode=mode
|
||||
)
|
||||
oracle_map = DynamicQuadraticFunc(
|
||||
params={
|
||||
0: LinearFunc(params={0: 0.1, 1: 0}), # 0.1 * t
|
||||
1: SinFunc(params={0: 1, 1: 1, 2: 0}), # sin(t)
|
||||
2: ConstantFunc(0),
|
||||
}
|
||||
)
|
||||
dynamic_env = SyntheticDEnv(
|
||||
data_generator, oracle_map, time_generator, num_per_task
|
||||
)
|
||||
else:
|
||||
raise ValueError("Unknown version: {:}".format(version))
|
||||
return dynamic_env
|
||||
|
@@ -39,9 +39,9 @@ def get_model(config: Dict[Text, Any], **kwargs):
|
||||
norm_cls = super_name2norm[kwargs["norm_cls"]]
|
||||
sub_layers, last_dim = [], kwargs["input_dim"]
|
||||
for i, hidden_dim in enumerate(kwargs["hidden_dims"]):
|
||||
sub_layers.append(SuperLinear(last_dim, hidden_dim))
|
||||
if hidden_dim > 1:
|
||||
sub_layers.append(norm_cls(hidden_dim, elementwise_affine=False))
|
||||
sub_layers.append(SuperLinear(last_dim, hidden_dim))
|
||||
sub_layers.append(act_cls())
|
||||
last_dim = hidden_dim
|
||||
sub_layers.append(SuperLinear(last_dim, kwargs["output_dim"]))
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# Performance-Aware Template Network for One-Shot Neural Architecture Search
|
||||
from .CifarNet import NetworkCIFAR as CifarNet
|
||||
from .ImageNet import NetworkImageNet as ImageNet
|
||||
from .CifarNet import NetworkCIFAR as CifarNet
|
||||
from .ImageNet import NetworkImageNet as ImageNet
|
||||
from .genotypes import Networks
|
||||
from .genotypes import build_genotype_from_dict
|
||||
|
@@ -8,24 +8,44 @@
|
||||
|
||||
import os, torch
|
||||
|
||||
|
||||
def obtain_nas_infer_model(config, extra_model_path=None):
|
||||
|
||||
if config.arch == 'dxys':
|
||||
from .DXYs import CifarNet, ImageNet, Networks
|
||||
from .DXYs import build_genotype_from_dict
|
||||
if config.genotype is None:
|
||||
if extra_model_path is not None and not os.path.isfile(extra_model_path):
|
||||
raise ValueError('When genotype in confiig is None, extra_model_path must be set as a path instead of {:}'.format(extra_model_path))
|
||||
xdata = torch.load(extra_model_path)
|
||||
current_epoch = xdata['epoch']
|
||||
genotype_dict = xdata['genotypes'][current_epoch-1]
|
||||
genotype = build_genotype_from_dict(genotype_dict)
|
||||
|
||||
if config.arch == "dxys":
|
||||
from .DXYs import CifarNet, ImageNet, Networks
|
||||
from .DXYs import build_genotype_from_dict
|
||||
|
||||
if config.genotype is None:
|
||||
if extra_model_path is not None and not os.path.isfile(extra_model_path):
|
||||
raise ValueError(
|
||||
"When genotype in confiig is None, extra_model_path must be set as a path instead of {:}".format(
|
||||
extra_model_path
|
||||
)
|
||||
)
|
||||
xdata = torch.load(extra_model_path)
|
||||
current_epoch = xdata["epoch"]
|
||||
genotype_dict = xdata["genotypes"][current_epoch - 1]
|
||||
genotype = build_genotype_from_dict(genotype_dict)
|
||||
else:
|
||||
genotype = Networks[config.genotype]
|
||||
if config.dataset == "cifar":
|
||||
return CifarNet(
|
||||
config.ichannel,
|
||||
config.layers,
|
||||
config.stem_multi,
|
||||
config.auxiliary,
|
||||
genotype,
|
||||
config.class_num,
|
||||
)
|
||||
elif config.dataset == "imagenet":
|
||||
return ImageNet(
|
||||
config.ichannel,
|
||||
config.layers,
|
||||
config.auxiliary,
|
||||
genotype,
|
||||
config.class_num,
|
||||
)
|
||||
else:
|
||||
raise ValueError("invalid dataset : {:}".format(config.dataset))
|
||||
else:
|
||||
genotype = Networks[config.genotype]
|
||||
if config.dataset == 'cifar':
|
||||
return CifarNet(config.ichannel, config.layers, config.stem_multi, config.auxiliary, genotype, config.class_num)
|
||||
elif config.dataset == 'imagenet':
|
||||
return ImageNet(config.ichannel, config.layers, config.auxiliary, genotype, config.class_num)
|
||||
else: raise ValueError('invalid dataset : {:}'.format(config.dataset))
|
||||
else:
|
||||
raise ValueError('invalid nas arch type : {:}'.format(config.arch))
|
||||
raise ValueError("invalid nas arch type : {:}".format(config.arch))
|
||||
|
Reference in New Issue
Block a user