def _gap_test_template(self, times, values): random_model = RandomStateSpaceModel( state_dimension=1, state_noise_dimension=1, configuration=state_space_model.StateSpaceModelConfiguration( num_features=1)) random_model.initialize_graph() input_fn = input_pipeline.WholeDatasetInputFn( input_pipeline.NumpyReader({ feature_keys.TrainEvalFeatures.TIMES: times, feature_keys.TrainEvalFeatures.VALUES: values })) features, _ = input_fn() times = features[feature_keys.TrainEvalFeatures.TIMES] values = features[feature_keys.TrainEvalFeatures.VALUES] model_outputs = random_model.get_batch_loss( features={ feature_keys.TrainEvalFeatures.TIMES: times, feature_keys.TrainEvalFeatures.VALUES: values }, mode=None, state=math_utils.replicate_state( start_state=random_model.get_start_state(), batch_size=array_ops.shape(times)[0])) with self.test_session() as session: variables.global_variables_initializer().run() coordinator = coordinator_lib.Coordinator() queue_runner_impl.start_queue_runners(session, coord=coordinator) model_outputs.loss.eval() coordinator.request_stop() coordinator.join()
def test_predictions_after_loss(self): dtype = dtypes.float32 with variable_scope.variable_scope(dtype.name): random_model = RandomStateSpaceModel( state_dimension=5, state_noise_dimension=4, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype, num_features=1)) features = { feature_keys.TrainEvalFeatures.TIMES: [[1, 2, 3, 4]], feature_keys.TrainEvalFeatures.VALUES: array_ops.ones([1, 4, 1], dtype=dtype) } passthrough = state_management.PassthroughStateManager() random_model.initialize_graph() passthrough.initialize_graph(random_model) model_outputs = passthrough.define_loss( model=random_model, features=features, mode=estimator_lib.ModeKeys.EVAL) predictions = random_model.predict({ feature_keys.PredictionFeatures.TIMES: [[5, 7, 8]], feature_keys.PredictionFeatures.STATE_TUPLE: model_outputs.end_state }) with self.test_session(): variables.global_variables_initializer().run() predicted_mean = predictions["mean"].eval() predicted_covariance = predictions["covariance"].eval() self._check_predictions(predicted_mean, predicted_covariance, window_size=3)
def __init__( self, num_latent_values, periodicity, near_integer_threshold=1e-8, configuration=state_space_model.StateSpaceModelConfiguration()): """Initialize the ResolutionCycleModel. Args: num_latent_values: Controls the representational power and memory usage of the model. The transition matrix has shape [num_latent_values - 1, num_latent_values - 1]. Must be an odd integer (see class docstring for why). periodicity: The number of steps for cyclic behavior. May be a Tensor, and need not be an integer (although integer values greater than num_latent_values have more efficient special cases). near_integer_threshold: When avoiding singularities, controls how close a number should be to that singularity before the special case takes over. configuration: A StateSpaceModelConfiguration object. Raises: ValueError: If num_latent_values is not odd. """ if num_latent_values % 2 != 1: raise ValueError( "Only odd numbers of latent values are supported.") self._num_latent_values = num_latent_values self._true_periodicity = periodicity self._near_integer_threshold = near_integer_threshold super(ResolutionCycleModel, self).__init__(periodicity=num_latent_values, configuration=configuration)
def test_multivariate(self): dtype = dtypes.float32 num_features = 3 covariance = numpy.eye(num_features) # A single off-diagonal has a non-zero value in the true transition # noise covariance. covariance[-1, 0] = 1. covariance[0, -1] = 1. dataset_size = 100 values = numpy.cumsum(numpy.random.multivariate_normal( mean=numpy.zeros(num_features), cov=covariance, size=dataset_size), axis=0) times = numpy.arange(dataset_size) model = MultivariateLevelModel( configuration=state_space_model.StateSpaceModelConfiguration( num_features=num_features, dtype=dtype, use_observation_noise=False, transition_covariance_initial_log_scale_bias=5.)) estimator = estimators.StateSpaceRegressor( model=model, optimizer=gradient_descent.GradientDescentOptimizer(0.1)) data = { feature_keys.TrainEvalFeatures.TIMES: times, feature_keys.TrainEvalFeatures.VALUES: values } train_input_fn = input_pipeline.RandomWindowInputFn( input_pipeline.NumpyReader(data), batch_size=16, window_size=16) estimator.train(input_fn=train_input_fn, steps=1) for component in model._ensemble_members: # Check that input statistics propagated to component models self.assertTrue(component._input_statistics)
def test_noise_decreasing(self): for dtype in [dtypes.float32, dtypes.float64]: with variable_scope.variable_scope(dtype.name): random_model = RandomStateSpaceModel( state_dimension=5, state_noise_dimension=4, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype, num_features=1)) random_model.initialize_graph() original_covariance = array_ops.diag( array_ops.ones(shape=[5], dtype=dtype)) _, new_covariance, _ = random_model._exogenous_noise_decreasing( current_times=[[1]], exogenous_values=constant_op.constant([[-2.]], dtype=dtype), state=[ -array_ops.ones(shape=[1, 5], dtype=dtype), original_covariance[None], [0] ]) with self.test_session() as session: variables.global_variables_initializer().run() evaled_new_covariance, evaled_original_covariance = session.run( [new_covariance[0], original_covariance]) new_variances = numpy.diag(evaled_new_covariance) original_variances = numpy.diag(evaled_original_covariance) for i in range(5): self.assertLess(new_variances[i], original_variances[i])
def dry_run_train_helper( self, sample_every, period, num_samples, model_type, model_args, num_features=1): numpy.random.seed(1) dtype = dtypes.float32 features = self.simple_data( sample_every, dtype=dtype, period=period, num_samples=num_samples, num_features=num_features) model = model_type( configuration=( state_space_model.StateSpaceModelConfiguration( num_features=num_features, dtype=dtype, covariance_prior_fn=lambda _: 0.)), **model_args) class _RunConfig(estimator_lib.RunConfig): @property def tf_random_seed(self): return 4 estimator = estimators.StateSpaceRegressor(model, config=_RunConfig()) train_input_fn = input_pipeline.RandomWindowInputFn( input_pipeline.NumpyReader(features), num_threads=1, shuffle_seed=1, batch_size=16, window_size=16) eval_input_fn = input_pipeline.WholeDatasetInputFn( input_pipeline.NumpyReader(features)) estimator.train(input_fn=train_input_fn, max_steps=1) first_evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1) estimator.train(input_fn=train_input_fn, max_steps=3) second_evaluation = estimator.evaluate(input_fn=eval_input_fn, steps=1) self.assertLess(second_evaluation["loss"], first_evaluation["loss"])
def __init__(self, static_unrolling_window_size_threshold=None): super(TimeDependentStateSpaceModel, self).__init__( configuration=state_space_model.StateSpaceModelConfiguration( use_observation_noise=False, transition_covariance_initial_log_scale_bias=5., static_unrolling_window_size_threshold= static_unrolling_window_size_threshold))
def test_adder_transition_to_powers(self): num_steps = 3 dtype = dtypes.float64 adder = level_trend.AdderStateSpaceModel( configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.transition_power_test_template( test_case=self, model=adder, num_steps=num_steps)
def __init__( self, periodicities, moving_average_order, autoregressive_order, use_level_noise=True, configuration=state_space_model.StateSpaceModelConfiguration()): """Initialize the Basic Structural Time Series model. Args: periodicities: Number of time steps for cyclic behavior. May be a list, in which case one periodic component is created for each element. moving_average_order: The number of moving average coefficients to use, which also defines the number of steps after which transient deviations revert to the mean defined by periodic and level/trend components. autoregressive_order: The number of steps back for autoregression. use_level_noise: Whether to model the time series as having level noise. See level_noise in the model description above. configuration: A StateSpaceModelConfiguration object. """ component_model_configuration = configuration._replace( use_observation_noise=False) univariate_component_model_configuration = ( component_model_configuration._replace(num_features=1)) adder_part = _replicate_level_trend_models( multivariate_configuration=component_model_configuration, univariate_configuration=univariate_component_model_configuration) with variable_scope.variable_scope("varma"): varma_part = varma.VARMA( autoregressive_order=autoregressive_order, moving_average_order=moving_average_order, configuration=component_model_configuration) cycle_parts = [] periodicity_list = nest.flatten(periodicities) for cycle_number, cycle_periodicity in enumerate(periodicity_list): # For each specified periodicity, construct models for each feature with # correlated noise. with variable_scope.variable_scope("cycle{}".format(cycle_number)): cycle_features = [] for feature in range(configuration.num_features): with variable_scope.variable_scope( "feature{}".format(feature)): cycle_features.append( periodic.CycleStateSpaceModel( periodicity=cycle_periodicity, configuration= univariate_component_model_configuration)) cycle_parts.append( state_space_model.StateSpaceCorrelatedFeaturesEnsemble( ensemble_members=cycle_features, configuration=component_model_configuration)) super(StructuralEnsemble, self).__init__( ensemble_members=[adder_part, varma_part] + cycle_parts, configuration=configuration)
def _equivalent_to_single_model_test_template(self, model_generator): with self.test_session() as session: random_model = RandomStateSpaceModel( state_dimension=5, state_noise_dimension=4, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtypes.float64, num_features=1)) random_model.initialize_graph() series_length = 10 model_data = random_model.generate( number_of_series=1, series_length=series_length, model_parameters=random_model.random_model_parameters()) input_fn = input_pipeline.WholeDatasetInputFn( input_pipeline.NumpyReader(model_data)) features, _ = input_fn() model_outputs = random_model.get_batch_loss( features=features, mode=None, state=math_utils.replicate_state( start_state=random_model.get_start_state(), batch_size=array_ops.shape( features[feature_keys.TrainEvalFeatures.TIMES])[0])) variables.global_variables_initializer().run() compare_outputs_evaled_fn = model_generator( random_model, model_data) coordinator = coordinator_lib.Coordinator() queue_runner_impl.start_queue_runners(session, coord=coordinator) compare_outputs_evaled = compare_outputs_evaled_fn(session) model_outputs_evaled = session.run( (model_outputs.end_state, model_outputs.predictions)) coordinator.request_stop() coordinator.join() model_posteriors, model_predictions = model_outputs_evaled (_, compare_posteriors, compare_predictions) = compare_outputs_evaled (model_posterior_mean, model_posterior_var, model_from_time) = model_posteriors (compare_posterior_mean, compare_posterior_var, compare_from_time) = compare_posteriors self.assertAllClose(model_posterior_mean, compare_posterior_mean[0]) self.assertAllClose(model_posterior_var, compare_posterior_var[0]) self.assertAllClose(model_from_time, compare_from_time) self.assertEqual(sorted(model_predictions.keys()), sorted(compare_predictions.keys())) for prediction_name in model_predictions: if prediction_name == "loss": # Chunking means that losses will be different; skip testing them. continue # Compare the last chunk to their corresponding un-chunked model # predictions last_prediction_chunk = compare_predictions[prediction_name][ -1] comparison_values = last_prediction_chunk.shape[0] model_prediction = ( model_predictions[prediction_name][0, -comparison_values:]) self.assertAllClose(model_prediction, last_prediction_chunk)
def test_adder_noise_accumulator(self): num_steps = 3 dtype = dtypes.float64 use_level_noise = True adder = level_trend.AdderStateSpaceModel( use_level_noise=use_level_noise, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.noise_accumulator_test_template( test_case=self, model=adder, num_steps=num_steps)
def test_cycle_noise_accumulator(self): num_steps = 3 dtype = dtypes.float64 periodicity = 3 cycle = periodic.CycleStateSpaceModel( periodicity=periodicity, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.noise_accumulator_test_template(test_case=self, model=cycle, num_steps=num_steps)
def test_cycle_transition_to_powers(self): num_steps = 3 dtype = dtypes.float64 periodicity = 3 cycle = periodic.CycleStateSpaceModel( periodicity=periodicity, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.transition_power_test_template(test_case=self, model=cycle, num_steps=num_steps)
def __init__(self, transition, state_noise_dimension, configuration=state_space_model.StateSpaceModelConfiguration()): self.transition = transition self.noise_transform = numpy.random.normal( size=(transition.shape[0], state_noise_dimension)).astype(numpy.float32) # Test feature + batch broadcasting self.observation_model = numpy.random.normal( size=(transition.shape[0])).astype(numpy.float32) super(StubStateSpaceModel, self).__init__( configuration=configuration)
def __init__( self, use_level_noise=True, configuration=state_space_model.StateSpaceModelConfiguration()): """Configure the model. Args: use_level_noise: Whether to model the time series as having level noise. configuration: A StateSpaceModelConfiguration object. """ self.use_level_noise = use_level_noise super(AdderStateSpaceModel, self).__init__(configuration=configuration)
def test_resolution_cycle_noise_accumulator(self): num_steps = 3 dtype = dtypes.float64 latent_values = 3 periodicity = latent_values + 0.1 cycle = periodic.ResolutionCycleModel( num_latent_values=latent_values, periodicity=periodicity, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.noise_accumulator_test_template(test_case=self, model=cycle, num_steps=num_steps)
def test_resolution_cycle_transition_to_powers(self): num_steps = 3 dtype = dtypes.float64 latent_values = 3 periodicity = latent_values - 1 cycle = periodic.ResolutionCycleModel( num_latent_values=latent_values, periodicity=periodicity, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype)) test_utils.transition_power_test_template(test_case=self, model=cycle, num_steps=num_steps)
def test_ensemble_observation_noise(self): model = MultivariateLevelModel( configuration=state_space_model.StateSpaceModelConfiguration()) model.initialize_graph() outputs = model.define_loss(features={ feature_keys.TrainEvalFeatures.TIMES: constant_op.constant([[1, 2]]), feature_keys.TrainEvalFeatures.VALUES: constant_op.constant([[[1.], [2.]]]) }, mode=estimator_lib.ModeKeys.TRAIN) initializer = variables.global_variables_initializer() with self.test_session() as sess: sess.run([initializer]) outputs.loss.eval()
def __init__(self, state_dimension, state_noise_dimension, configuration=state_space_model.StateSpaceModelConfiguration()): self.transition = numpy.random.normal( size=[state_dimension, state_dimension]).astype( configuration.dtype.as_numpy_dtype) self.noise_transform = numpy.random.normal( size=(state_dimension, state_noise_dimension)).astype( configuration.dtype.as_numpy_dtype) # Test batch broadcasting self.observation_model = numpy.random.normal( size=(configuration.num_features, state_dimension)).astype( configuration.dtype.as_numpy_dtype) super(RandomStateSpaceModel, self).__init__( configuration=configuration._replace( covariance_prior_fn=lambda _: 0.))
def test_ma_smaller(self): model = varma.VARMA( autoregressive_order=6, moving_average_order=3, configuration=state_space_model.StateSpaceModelConfiguration( num_features=7)) model.initialize_graph() outputs = model.define_loss(features={ TrainEvalFeatures.TIMES: constant_op.constant([[1, 2]]), TrainEvalFeatures.VALUES: constant_op.constant([[[1.] * 7, [2.] * 7]]) }, mode=estimator_lib.ModeKeys.TRAIN) initializer = variables.global_variables_initializer() with self.test_session() as sess: sess.run([initializer]) outputs.loss.eval()
def test_predictions_direct(self): dtype = dtypes.float64 with variable_scope.variable_scope(dtype.name): random_model = RandomStateSpaceModel( state_dimension=5, state_noise_dimension=4, configuration=state_space_model.StateSpaceModelConfiguration( dtype=dtype, num_features=1)) random_model.initialize_graph() prediction_dict = random_model.predict(features={ feature_keys.PredictionFeatures.TIMES: [[1, 3, 5, 6]], feature_keys.PredictionFeatures.STATE_TUPLE: math_utils.replicate_state( start_state=random_model.get_start_state(), batch_size=1) }) with self.test_session(): variables.global_variables_initializer().run() predicted_mean = prediction_dict["mean"].eval() predicted_covariance = prediction_dict["covariance"].eval() self._check_predictions(predicted_mean, predicted_covariance, window_size=4)
def test_make_ensemble_no_errors(self): with variable_scope.variable_scope("model_one"): model_one = varma.VARMA(10, 5) with variable_scope.variable_scope("model_two"): model_two = varma.VARMA(0, 3) configuration = state_space_model.StateSpaceModelConfiguration() ensemble = state_space_model.StateSpaceIndependentEnsemble( ensemble_members=[model_one, model_two], configuration=configuration) ensemble.initialize_graph() outputs = ensemble.define_loss(features={ TrainEvalFeatures.TIMES: constant_op.constant([[1, 2]]), TrainEvalFeatures.VALUES: constant_op.constant([[[1.], [2.]]]) }, mode=estimator_lib.ModeKeys.TRAIN) initializer = variables.global_variables_initializer() with self.test_session() as sess: sess.run([initializer]) outputs.loss.eval()
def __init__(self, autoregressive_order, moving_average_order, configuration=state_space_model.StateSpaceModelConfiguration()): """Construct a VARMA model. The size of the latent state for this model is: num_features * max(autoregressive_order, moving_average_order + 1) Square matrices of this size are constructed and multiplied. Args: autoregressive_order: The maximum autoregressive lag. moving_average_order: The maximum moving average lag, after which transient deviations are expected to return to their long-term mean. configuration: A StateSpaceModelConfiguration object. """ self.ar_order = autoregressive_order self.ma_order = moving_average_order self.state_num_blocks = max(autoregressive_order, moving_average_order + 1) super(VARMA, self).__init__(configuration=configuration) self.state_dimension = self.state_num_blocks * self.num_features
def test_savedmodel_state_override(self): random_model = RandomStateSpaceModel( state_dimension=5, state_noise_dimension=4, configuration=state_space_model.StateSpaceModelConfiguration( exogenous_feature_columns=[ layers.real_valued_column("exogenous") ], dtype=dtypes.float64, num_features=1)) estimator = estimators.StateSpaceRegressor( model=random_model, optimizer=gradient_descent.GradientDescentOptimizer(0.1)) combined_input_fn = input_pipeline.WholeDatasetInputFn( input_pipeline.NumpyReader({ feature_keys.FilteringFeatures.TIMES: [1, 2, 3, 4], feature_keys.FilteringFeatures.VALUES: [1., 2., 3., 4.], "exogenous": [-1., -2., -3., -4.] })) estimator.train(combined_input_fn, steps=1) export_location = estimator.export_savedmodel( self.get_temp_dir(), estimator.build_raw_serving_input_receiver_fn( exogenous_features={ "exogenous": numpy.zeros((0, 0), dtype=numpy.float32) })) with ops.Graph().as_default() as graph: random_model.initialize_graph() with self.test_session(graph=graph) as session: variables.global_variables_initializer().run() evaled_start_state = session.run( random_model.get_start_state()) evaled_start_state = [ state_element[None, ...] for state_element in evaled_start_state ] with ops.Graph().as_default() as graph: with self.test_session(graph=graph) as session: signatures = loader.load(session, [tag_constants.SERVING], export_location) first_split_filtering = saved_model_utils.filter_continuation( continue_from={ feature_keys.FilteringResults.STATE_TUPLE: evaled_start_state }, signatures=signatures, session=session, features={ feature_keys.FilteringFeatures.TIMES: [1, 2], feature_keys.FilteringFeatures.VALUES: [1., 2.], "exogenous": [-1., -2.] }) second_split_filtering = saved_model_utils.filter_continuation( continue_from=first_split_filtering, signatures=signatures, session=session, features={ feature_keys.FilteringFeatures.TIMES: [3, 4], feature_keys.FilteringFeatures.VALUES: [3., 4.], "exogenous": [-3., -4.] }) combined_filtering = saved_model_utils.filter_continuation( continue_from={ feature_keys.FilteringResults.STATE_TUPLE: evaled_start_state }, signatures=signatures, session=session, features={ feature_keys.FilteringFeatures.TIMES: [1, 2, 3, 4], feature_keys.FilteringFeatures.VALUES: [1., 2., 3., 4.], "exogenous": [-1., -2., -3., -4.] }) split_predict = saved_model_utils.predict_continuation( continue_from=second_split_filtering, signatures=signatures, session=session, steps=1, exogenous_features={"exogenous": [[-5.]]}) combined_predict = saved_model_utils.predict_continuation( continue_from=combined_filtering, signatures=signatures, session=session, steps=1, exogenous_features={"exogenous": [[-5.]]}) for state_key, combined_state_value in combined_filtering.items(): if state_key == feature_keys.FilteringResults.TIMES: continue self.assertAllClose(combined_state_value, second_split_filtering[state_key]) for prediction_key, combined_value in combined_predict.items(): self.assertAllClose(combined_value, split_predict[prediction_key])
def __init__(self, periodicities, num_features, cycle_num_latent_values=11, moving_average_order=4, autoregressive_order=0, exogenous_feature_columns=None, exogenous_update_condition=None, dtype=dtypes.float64, anomaly_prior_probability=None, optimizer=None, model_dir=None, config=None, head_type=ts_head_lib.TimeSeriesRegressionHead): """Initialize the Estimator. Args: periodicities: The expected periodicity of the data (for example 24 if feeding hourly data with a daily periodicity, or 60 * 24 if feeding minute-level data with daily periodicity). Either a scalar or a list. This parameter can be any real value, and does not control the size of the model. However, increasing this without increasing `num_values_per_cycle` will lead to smoother periodic behavior, as the same number of distinct values will be cycled through over a longer period of time. num_features: The dimensionality of the time series (one for univariate, more than one for multivariate). cycle_num_latent_values: Along with `moving_average_order` and `num_features`, controls the latent state size of the model. Square matrices of size `num_features * (moving_average_order + cycle_num_latent_values + 3)` are created and multiplied, so larger values may be slow. The trade-off is with resolution: cycling between a smaller number of latent values means that only smoother functions can be modeled. moving_average_order: Controls model size (along with `cycle_num_latent_values` and `autoregressive_order`) and the number of steps before transient deviations revert to the mean defined by the period and level/trend components. autoregressive_order: Each contribution from this component is a linear combination of this many previous contributions. Also helps to determine the model size. Learning autoregressive coefficients typically requires more steps and a smaller step size than other components. exogenous_feature_columns: A list of `tf.feature_column`s (for example `tf.feature_column.embedding_column`) corresponding to exogenous features which provide extra information to the model but are not part of the series to be predicted. Passed to `tf.feature_column.input_layer`. exogenous_update_condition: A function taking two Tensor arguments, `times` (shape [batch size]) and `features` (a dictionary mapping exogenous feature keys to Tensors with shapes [batch size, ...]), and returning a boolean Tensor with shape [batch size] indicating whether state should be updated using exogenous features for each part of the batch. Where it is False, no exogenous update is performed. If None (default), exogenous updates are always performed. Useful for avoiding "leaky" frequent exogenous updates when sparse updates are desired. Called only during graph construction. See the "known anomaly" example for example usage. dtype: The floating point data type to compute with. float32 may be faster, but can be problematic for larger models and longer time series. anomaly_prior_probability: If not None, the model attempts to automatically detect and ignore anomalies during training. This parameter then controls the prior probability of an anomaly. Values closer to 0 mean that points will be discarded less frequently. The default value (None) means that anomalies are not discarded, which may be slightly faster. optimizer: The optimization algorithm to use when training, inheriting from tf.train.Optimizer. Defaults to Adam with step size 0.02. model_dir: See `Estimator`. config: See `Estimator`. head_type: The kind of head to use for the model (inheriting from `TimeSeriesRegressionHead`). """ if anomaly_prior_probability is not None: filtering_postprocessor = StateInterpolatingAnomalyDetector( anomaly_prior_probability=anomaly_prior_probability) else: filtering_postprocessor = None state_space_model_configuration = ( state_space_model.StateSpaceModelConfiguration( num_features=num_features, dtype=dtype, filtering_postprocessor=filtering_postprocessor, exogenous_feature_columns=exogenous_feature_columns, exogenous_update_condition=exogenous_update_condition)) model = structural_ensemble.MultiResolutionStructuralEnsemble( cycle_num_latent_values=cycle_num_latent_values, moving_average_order=moving_average_order, autoregressive_order=autoregressive_order, periodicities=periodicities, configuration=state_space_model_configuration) super(StructuralEnsembleRegressor, self).__init__( model=model, optimizer=optimizer, model_dir=model_dir, config=config, head_type=head_type)
def __init__(self, cycle_num_latent_values, moving_average_order, autoregressive_order, periodicities, use_level_noise=True, configuration=state_space_model.StateSpaceModelConfiguration()): """Initialize the multi-resolution structural ensemble. Args: cycle_num_latent_values: Controls the model size and the number of latent values cycled between (but not the periods over which they cycle). Reducing this parameter can save significant amounts of memory, but the tradeoff is with resolution: cycling between a smaller number of latent values means that only smoother functions can be modeled. For multivariate series, may either be a scalar integer (in which case it is applied to all periodic components) or a list with length matching `periodicities`. moving_average_order: The number of moving average coefficients to use, which also defines the number of steps after which transient deviations revert to the mean defined by periodic and level/trend components. Adds to model size. autoregressive_order: The number of steps back for autoregression. Learning autoregressive coefficients typically requires more steps and a smaller step size than other components. periodicities: Same meaning as for StructuralEnsemble: number of steps for cyclic behavior. Floating point and Tensor values are supported. May be a list of values, in which case one component is created for each periodicity. If `periodicities` is a list while `cycle_num_latent_values` is a scalar, its value is broadcast to each periodic component. Otherwise they should be lists of the same length, in which case they are paired. use_level_noise: See StructuralEnsemble. configuration: A StateSpaceModelConfiguration object. Raises: ValueError: If `cycle_num_latent_values` is neither a scalar nor agrees in size with `periodicities`. """ component_model_configuration = configuration._replace( use_observation_noise=False) univariate_component_model_configuration = ( component_model_configuration._replace( num_features=1)) adder_part = _replicate_level_trend_models( multivariate_configuration=component_model_configuration, univariate_configuration=univariate_component_model_configuration) with variable_scope.variable_scope("varma"): varma_part = varma.VARMA( autoregressive_order=autoregressive_order, moving_average_order=moving_average_order, configuration=component_model_configuration) cycle_parts = [] if periodicities is None: periodicities = [] periodicity_list = nest.flatten(periodicities) latent_values_list = nest.flatten(cycle_num_latent_values) if len(periodicity_list) != len(latent_values_list): if len(latent_values_list) != 1: raise ValueError( ("`cycle_num_latent_values` must either be a list with the same " "size as `periodicity` or a scalar. Received length {} " "`cycle_num_latent_values`, while `periodicities` has length {}.") .format(len(latent_values_list), len(periodicity_list))) latent_values_list *= len(periodicity_list) for cycle_number, (cycle_periodicity, num_latent_values) in enumerate( zip(periodicity_list, latent_values_list)): with variable_scope.variable_scope("cycle{}".format(cycle_number)): cycle_features = [] for feature in range(configuration.num_features): with variable_scope.variable_scope("feature{}".format(feature)): cycle_features.append( periodic.ResolutionCycleModel( num_latent_values=num_latent_values, periodicity=cycle_periodicity, configuration=univariate_component_model_configuration)) cycle_parts.append( state_space_model.StateSpaceCorrelatedFeaturesEnsemble( ensemble_members=cycle_features, configuration=component_model_configuration)) super(MultiResolutionStructuralEnsemble, self).__init__( ensemble_members=[adder_part, varma_part] + cycle_parts, configuration=configuration)
def __init__(self, static_unrolling_window_size_threshold=None): super(TimeDependentStateSpaceModel, self).__init__( configuration=state_space_model.StateSpaceModelConfiguration( use_observation_noise=False, static_unrolling_window_size_threshold= static_unrolling_window_size_threshold))
def __init__( self, periodicity, configuration=state_space_model.StateSpaceModelConfiguration()): self._periodicity = periodicity super(CycleStateSpaceModel, self).__init__(configuration=configuration)