Predictive Coding Utilities
Utilities for predictive coding specifically.
Energy computation and autodifferentiated gradients of energy wrt to representations and learnable parameters
Explicit computation of gradients expressed with prediction errors in the nonlinear fully-connected strictly hierarchical case
Stochastic gradient descent steps on representations and learnable parameters
Representations initializers (first pass)
- pc_utils.backward_initialize_representations(model, target, data=None)
Initial representations with a backward sweep through the model
- Parameters
model (list of
tf_utils.Dense
ortf_utils.BiasedDense
) – description of a sequential network by a list of layers, can be generated e.g. usingtf_utils.mlp()
target (3d tf.Tensor of float32) – output target batch
data (3d tf.Tensor of float32, optional) – inuput data batch, defaults to None
- Returns
representations
- Return type
list of 3d tf.Tensor of float32
- pc_utils.energy_and_error(model, r, theta=[], predictions_flow_upward=False)
Energy (total squared L2 norm of errors) computation and autodifferentiation with respect to representations and learnable parameters
- Parameters
model (list of
tf_utils.Dense
ortf_utils.BiasedDense
) – description of a sequential network by a list of layers, can be generated e.g. usingtf_utils.mlp()
r (list of 3d tf.Tensor of float32) – representations
theta (list of variable tf.Tensor of float32, optional) – learnable parameters, defaults to []
predictions_flow_upward (bool, optional) – direction of prediction flow, defaults to False
- Returns
energy, autodiff gradient tape
- Return type
float32, tf.GradientTape
- pc_utils.forward_initialize_representations(model, data, target=None)
Initial representations with a forward sweep through the model
- Parameters
model (list of
tf_utils.Dense
ortf_utils.BiasedDense
) – description of a sequential network by a list of layers, can be generated e.g. usingtf_utils.mlp()
data (3d tf.Tensor of float32) – inuput data batch
target (3d tf.Tensor of float32, optional) – output target batch, defaults to None
- Returns
representations
- Return type
list of 3d tf.Tensor of float32
- pc_utils.inference_SGD_step(r, ir, g, update_last=True)
Stochastic gradient descent step on represnetations (inference) using autodifferentiated gradients
- Parameters
r (list of 3d tf.Tensor of float32) – representations
ir (float32) – inference rate
g (tf.GradientTape) – autodiff gradient tape
update_last (bool, optional) – controls weither representations in the last layer are updated, defaults to True
- pc_utils.inference_step_backward_predictions(e, r, w, ir, f, df, update_last=True, update_first=False)
Representations update using stochastic gradient descent with analytic expressions of the gradients of energy wrt representations, only applicable to an unbiased MLP with reversed flow (predictions come from higher layer)
- Parameters
e (list of 3d tf.Tensor of float32) – prediction errors
r (list of 3d tf.Tensor of float32) – representations
w (list of 2d tf.Tensor of float32) – list of weight matrices, can be generated e.g. using
tf_utils.mlp()
ir (float32) – inference rate
f (function) – activation function
df (function) – derivate of the activation function
update_last (bool, optional) – controls weither representations in the last layer are updated, defaults to True
- pc_utils.inference_step_forward_predictions(e, r, w, ir, f, df, update_last=True)
Representations update using stochastic gradient descent with analytic expressions of the gradients of energy wrt representations, only applicable to an unbiased MLP (predictions come from lower layer)
- Parameters
e (list of 3d tf.Tensor of float32) – prediction errors
r (list of 3d tf.Tensor of float32) – representations
w (list of 2d tf.Tensor of float32) – list of weight matrices, can be generated e.g. using
tf_utils.mlp()
ir (float32) – inference rate
f (float32) – activation function
df (function) – derivate of the activation function
update_last (bool, optional) – controls weither representations in the last layer are updated, defaults to True
- pc_utils.parameters_SGD_step(theta, lr, g)
Stochastic gradient descent step on learnable parameters (learning) using autodifferentiated gradients
- Parameters
theta (list of variable tf.Tensor of float32) – learnable parameters
lr (float32) – learning rate
g (tf.GradientTape) – autodiff gradient tape
- pc_utils.random_initialize_representations(model, data, stddev=0.001, predictions_flow_upward=False, target_shape=None)
Randomly initialize latent representations
- Parameters
model (list of
tf_utils.Dense
ortf_utils.BiasedDense
) – description of a sequential network by a list of layers, can be generated e.g. usingtf_utils.mlp()
data (3d tf.Tensor of float32) – inuput data batch
stddev (float, optional) – standard deviation of the normal initialization, defaults to 0.001
predictions_flow_upward (bool, optional) – direction of prediction flow, defaults to False
target_shape (1d tf.Tensor of int32, optional) – shape of target minibatch, defaults to None
- Returns
representations
- Return type
list of 3d tf.Tensor of float32
- pc_utils.weight_update_backward_predictions(w, e, r, lr, f)
Weight update using stochastic gradient descent with analytic expressions of the gradients of energy wrt weights, only applicable to an unbiased MLP with reversed flow (predictions come from higher layer)
- Parameters
w (list of 2d tf.Tensor of float32) – list of weight matrices, can be generated e.g. using
tf_utils.mlp()
e (list of 3d tf.Tensor of float32) – prediction errors
r (list of 3d tf.Tensor of float32) – representations
lr (float32) – learning rate
f (function) – activation function
- pc_utils.weight_update_forward_predictions(w, e, r, lr, f)
Weight update using stochastic gradient descent with analytic expressions of the gradients of energy wrt weights, only applicable to an unbiased MLP (predictions come from lower layer)
- Parameters
w (list of 2d tf.Tensor of float32) – list of weight matrices, can be generated e.g. using
tf_utils.mlp()
e (list of 3d tf.Tensor of float32) – prediction errors
r (list of 3d tf.Tensor of float32) – representations
lr (float32) – learning rate
f (function) – activation function
- pc_utils.zero_initialize_representations(model, data, predictions_flow_upward=False, target_shape=None, bias=<tf.Tensor: shape=(), dtype=float32, numpy=0.0>)
Initialize representations at zero (or a constant)
- Parameters
model (list of
tf_utils.Dense
ortf_utils.BiasedDense
) – description of a sequential network by a list of layers, can be generated e.g. usingtf_utils.mlp()
data (3d tf.Tensor of float32) – inuput data batch
predictions_flow_upward (bool, optional) – direction of prediction flow, defaults to False
target_shape (1d tf.Tensor of int32, optional) – shape of target minibatch, defaults to None
bias (float32, optional) – initialize representation with bias rather than 0., defaults to tf.constant(0.)
- Returns
representations
- Return type
list of 3d tf.Tensor of float32