markovflow.models.models
Module containing base classes for models.
Note
Markovflow models are intended to work with eager mode in TensorFlow. Therefore models (and their collaborating objects) should typically avoid performing any computation in their __init__ methods. Because models and other objects are typically initialised outside of an optimisation loop, performing computation in the constructor means that this computation is performed ‘too early’, and optimisation is not possible.
__init__
MarkovFlowModel
Bases: tf.Module, abc.ABC
tf.Module
abc.ABC
Abstract class representing Markovflow models that depend on input data.
All Markovflow models are TensorFlow Modules, so it is possible to obtain trainable variables via the trainable_variables attribute. You can combine this with the loss() method to train the model. For example:
TensorFlow Modules
trainable_variables
loss()
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.01) for i in range(iterations): model.optimization_step(optimizer)
Call the predict_f() method to predict marginal function values at future time points. For example:
predict_f()
mean, variance = model.predict_f(validation_data_tensor)
Markovflow models that extend this class must implement the loss() method and posterior attribute.
posterior
loss
Obtain the loss, which you can use to train the model. It should always return a scalar.
NotImplementedError – Must be implemented in derived classes.
Return a posterior process from the model, which can be used for inference.
predict_state
Predict state at new_time_points. Note these time points should be sorted.
new_time_points
new_time_points – Time points to generate observations for, with shape batch_shape + [num_new_time_points,].
batch_shape + [num_new_time_points,]
Predicted mean and covariance for the new time points, with respective shapes batch_shape + [num_new_time_points, state_dim] batch_shape + [num_new_time_points, state_dim, state_dim].
batch_shape + [num_new_time_points, state_dim]
batch_shape + [num_new_time_points, state_dim, state_dim]
predict_f
Predict marginal function values at new_time_points. Note these time points should be sorted.
new_time_points – Time points to generate observations for, with shape batch_shape + [num_new_time_points].
batch_shape + [num_new_time_points]
full_output_cov – Either full output covariance (True) or marginal variances (False).
True
False
Predicted mean and covariance for the new time points, with respective shapes batch_shape + [num_new_time_points, output_dim] and either batch_shape + [num_new_time_points, output_dim, output_dim] or batch_shape + [num_new_time_points, output_dim].
batch_shape + [num_new_time_points, output_dim]
batch_shape + [num_new_time_points, output_dim, output_dim]
MarkovFlowSparseModel
Abstract class representing Markovflow models that do not need to store the training data (\(X, Y\)) in the model to approximate the posterior predictions \(p(f*|X, Y, x*)\).
This currently applies only to sparse variational models.
The optimization_step method should typically be used to train the model. For example:
optimization_step
input_data = (tf.constant(time_points), tf.constant(observations)) optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.01) for i in range(iterations): model.optimization_step(input_data, optimizer)
Obtain the loss, which can be used to train the model.
input_data –
A tuple of time points and observations containing the data at which to calculate the loss for training the model:
A tensor of inputs with shape batch_shape + [num_data]
batch_shape + [num_data]
A tensor of observations with shape batch_shape + [num_data, observation_dim]
batch_shape + [num_data, observation_dim]
Obtain a posterior process from the model, which can be used for inference.
full_output_cov – Either full output covariance (True) or marginal variances (FalseF).
FalseF
predict_log_density
Compute the log density of the data. That is:
Predicted log density at input time points, with shape batch_shape + [num_data].