mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-21 09:15:36 +00:00
add option to disable Newton fallback via set_options method (#1702)
This commit is contained in:
parent
77da5b8c06
commit
e41ed25646
1 changed files with 33 additions and 19 deletions
|
|
@ -21,6 +21,15 @@ class IStanBackend(ABC):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.model = self.load_model()
|
self.model = self.load_model()
|
||||||
self.stan_fit = None
|
self.stan_fit = None
|
||||||
|
self.newton_fallback = True
|
||||||
|
|
||||||
|
def set_options(self, **kwargs):
|
||||||
|
"""
|
||||||
|
Specify model options as kwargs.
|
||||||
|
* newton_fallback [bool]: whether to fallback to Newton if L-BFGS fails
|
||||||
|
"""
|
||||||
|
if 'newton_fallback' in kwargs:
|
||||||
|
self.newton_fallback = kwargs['newton_fallback']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|
@ -58,7 +67,8 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
model_name = 'prophet.stan'
|
model_name = 'prophet.stan'
|
||||||
target_name = 'prophet_model.bin'
|
target_name = 'prophet_model.bin'
|
||||||
|
|
||||||
sm = cmdstanpy.CmdStanModel(stan_file=os.path.join(model_dir, model_name))
|
sm = cmdstanpy.CmdStanModel(
|
||||||
|
stan_file=os.path.join(model_dir, model_name))
|
||||||
sm.compile()
|
sm.compile()
|
||||||
copy(sm.exe_file, os.path.join(target_dir, target_name))
|
copy(sm.exe_file, os.path.join(target_dir, target_name))
|
||||||
|
|
||||||
|
|
@ -82,7 +92,7 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
**kwargs)
|
**kwargs)
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
# Fall back on Newton
|
# Fall back on Newton
|
||||||
if kwargs['algorithm'] != 'Newton':
|
if self.newton_fallback and kwargs['algorithm'] != 'Newton':
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'Optimization terminated abnormally. Falling back to Newton.'
|
'Optimization terminated abnormally. Falling back to Newton.'
|
||||||
)
|
)
|
||||||
|
|
@ -94,7 +104,8 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
params = self.stan_to_dict_numpy(self.stan_fit.column_names, self.stan_fit.optimized_params_np)
|
params = self.stan_to_dict_numpy(
|
||||||
|
self.stan_fit.column_names, self.stan_fit.optimized_params_np)
|
||||||
for par in params:
|
for par in params:
|
||||||
params[par] = params[par].reshape((1, -1))
|
params[par] = params[par].reshape((1, -1))
|
||||||
return params
|
return params
|
||||||
|
|
@ -243,13 +254,16 @@ class PyStanBackend(IStanBackend):
|
||||||
args.update(kwargs)
|
args.update(kwargs)
|
||||||
try:
|
try:
|
||||||
self.stan_fit = self.model.optimizing(**args)
|
self.stan_fit = self.model.optimizing(**args)
|
||||||
except RuntimeError:
|
except RuntimeError as e:
|
||||||
# Fall back on Newton
|
# Fall back on Newton
|
||||||
|
if self.newton_fallback and args['algorithm'] != 'Newton':
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'Optimization terminated abnormally. Falling back to Newton.'
|
'Optimization terminated abnormally. Falling back to Newton.'
|
||||||
)
|
)
|
||||||
args['algorithm'] = 'Newton'
|
args['algorithm'] = 'Newton'
|
||||||
self.stan_fit = self.model.optimizing(**args)
|
self.stan_fit = self.model.optimizing(**args)
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
|
||||||
params = dict()
|
params = dict()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue