# ab.util¶

Package helper utilities.

aboleth.util.batch(feed_dict, batch_size, n_iter=10000, N_=None)

Create random batches for Stochastic gradients.

Feed dict data generator for SGD that will yeild random batches for a a defined number of iterations, which can be infinite. This generator makes consecutive passes through the data, drawing without replacement on each pass.

Parameters: feed_dict (dict of ndarrays) – The data with {tf.placeholder: data} entries. This assumes all items have the same length! batch_size (int) – number of data points in each batch. n_iter (int, optional) – The number of iterations N (tf.placeholder (int), optional) – Place holder for the size of the dataset. This will be fed to an algorithm. dict – with each element an array length batch_size, i.e. a subset of data, and an element for N_. Use this as your feed-dict when evaluating a loss, training, etc.
aboleth.util.batch_prediction(feed_dict, batch_size)

Split the data in a feed_dict into contiguous batches for prediction.

Parameters: feed_dict (dict of ndarrays) – The data with {tf.placeholder: data} entries. This assumes all items have the same length! batch_size (int) – number of data points in each batch. ndarray – an array of shape approximately (batch_size,) of indices into the original data for the current batch dict – with each element an array length batch_size, i.e. a subset of data. Use this as your feed-dict when evaluating a model, prediction, etc.

Note

The exact size of the batch may not be batch_size, but the nearest size that splits the size of the data most evenly.

aboleth.util.pos(X, minval=1e-15)

Constrain a tf.Variable to be positive only.

At the moment this is implemented as:

$$\max(|\mathbf{X}|, \text{minval})$$

This is fast and does not result in vanishing gradients, but will lead to non-smooth gradients and more local minima. In practice we haven’t noticed this being a problem.

Parameters: X (Tensor) – any Tensor in which all elements will be made positive. minval (float) – the minimum “positive” value the resulting tensor will have. X – a tensor the same shape as the input X but positively constrained. Tensor

Examples

>>> X = tf.constant(np.array([1.0, -1.0, 0.0]))
>>> Xp = pos(X)
>>> with tf.Session():
...     xp = Xp.eval()
>>> xp
array([  1.00000000e+00,   1.00000000e+00,   1.00000000e-15])

aboleth.util.predict_expected(predictor, feed_dict, n_groups=1, session=None)

Help to get the expected value from a predictor.

Parameters: predictor (Tensor) – a tensor that outputs a shape (n_samples, N, tasks) where n_samples are the random samples from the predictor (e.g. the output of Net), N is the size of the query dataset, and tasks the number of prediction tasks. feed_dict (dict) – The data with {tf.placeholder: data} entries. n_groups (int) – The number of times to evaluate the predictor and concatenate the samples. session (Session) – the session to be used to evaluate the predictor. pred – expected value of the prediction with shape (N, tasks). n_samples * n_groups samples go into evaluating this expectation. ndarray

Note

This has to be called in an active tensorflow session!

aboleth.util.predict_samples(predictor, feed_dict, n_groups=1, session=None)

Help to get samples from a predictor.

Parameters: predictor (Tensor) – a tensor that outputs a shape (n_samples, N, tasks) where n_samples are the random samples from the predictor (e.g. the output of Net), N is the size of the query dataset, and tasks the number of prediction tasks. feed_dict (dict) – The data with {tf.placeholder: data} entries. n_groups (int) – The number of times to evaluate the predictor and concatenate the samples. session (Session) – the session to be used to evaluate the predictor. pred – prediction samples of shape (n_samples * n_groups, N, tasks). ndarray

Note

This has to be called in an active tensorflow session!