Toolkit

Higher level op API

class tflon.toolkit.toolkit.Broadcast(*args)

Broadcast inputs to multiple modules and append their outputs into a tuple

Parameters:*args – Any number of callable objects
class tflon.toolkit.toolkit.Chain(*args)

Chain multiple modules together, feeding the output of one into the input of the next

Parameters:*args – Any number of callable objects
class tflon.toolkit.toolkit.Join(*args)

Send each argument in a list or tuple of arguments to the corresponding module in a list of modules

Parameters:*args – Any number of callable objects
class tflon.toolkit.toolkit.Module(*args, **kwargs)

This is a base class for creating reusable graph components. Extending classes should implement the following interfaces:

Required interfaces:

  • build(self, *args, **kargs)
  • call(self, *args, **kwargs)
Optional interfaces:
  • inverse(self, *args, **kwargs)
  • show_data(self, batch)
  • _featurize(self, batch)
  • _parameters(self)
__and__(other)

Construct a tflon.toolkit.Join from two modules

Concatenates the output of all modules into a single tuple

__or__(other)

Construct a tflon.toolkit.Chain from two modules.

Pipes the output of one module’s __call__ to the input of the next

__xor__(other)

Construct a tflon.toolkit.Broadcast from two modules

Appends the output of all modules into a single tuple

add_input(name, shape=[None], dtype=tf.float32, sparse=False, ragged=False, ttype=<class 'tflon.data.tables.Table'>)

Add an input to the model, using the scope of this Module

See tflon.model.Tower.add_input

add_loss(name, loss)

Add a loss to the model, using the scope of this Module

See tflon.model.Tower.add_loss

add_target(name, shape=[None], dtype=tf.float32, sparse=False, ragged=False, ttype=<class 'tflon.data.tables.Table'>)

Add a target to the model, using the scope of this Module

See tflon.model.Tower.add_target

biases

Get the collection of biases at this module scope

build(*args, **kwargs)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args, **kwargs)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
get_bias(name, shape, dtype=tf.float32, initializer=None, prior=None)

Get a new or existing bias variable by name, within the current module scope.

See tflon.model.Tower.get_bias

get_input_shapes(*args, **kwargs)

Set the input shapes using the arguments to the first __call__ of this module

The default implementation uses the shape of args[0]

get_parameter(key, default=None, parent=False)

Get the value of a parameter if it exists and is not None

See tflon.model.Tower.get_parameter

Parameters:

key (str) – The parameter name

Keyword Arguments:
 
  • default – The parameter default value, returned if parameter is None or does not exist (default=None)
  • parent (bool) – Whether to check for the given parameter at the module scope (False), or the parent scope (True) (default=False)
get_variable(name, shape, dtype=tf.float32, initializer=None, trainable=False)

Get a new or existing variable by name, within the current module scope.

See tflon.model.Tower.get_variable

get_weight(name, shape, dtype=tf.float32, initializer=None, prior=None)

Get a new or existing weight variable by name, within the current module scope.

See tflon.model.Tower.get_weight

has_parameter(key, default=None, parent=False)

Check of this module scope, or the parent scope has the specified parameter setting

Parameters:key (str) – The reference name for the query parameter
Keyword Argument:
parent (bool): Whether to check for the given parameter at the module scope (False), or the parent scope (True) (default=False)
initialize(*args, **kwargs)

Initialize this module. Performs two steps:

  1. Get the input shapes from *args
  2. Call Module.build()

Modules need to be initialized before calling methods. If a method is likely called before Module.__call__, then it should be registered as a callable method by decorator @tflon.toolkit.register_callable

Parameters:
  • *args – The arguments passed to __call__
  • **kwargs – The keyword arguments passed to __call__
inject_parameters()

Add the values of Module._parameters as class attributes. For a given (key, value) pair, this will add instance attributes of the form:

  • self.key = value

This behavior is currently optional, but will eventually become the default.

input_shapes

Get the shapes of the tensors passed to this module’s __call__(*args) as a list of tensor shapes, non-tensor types in *args are excluded

inverse(*args, **kwargs)

Instantiate an op which has the reverse structure of call. The input signature should be the same as the output of call, and the output signature should match the input of call.

Where implemented, this may not be an exact inverse, check the documentation for each Module

Returns:A tensor or sequence of tensors
names

Get the names of all feature input slots as a dictionary of (name, placeholder) pairs.

scope

Get the path of this module scope, including the parent tower scope

set_parameter(key, value)

Set a parameter value for this module scope

See tflon.model.Tower.set_parameter

show_data(table_map)

This method can be overridden to compute parameters from raw data tables for model initialization prior to optimization.

Parameters:table_map (dict) – Dictionary of table objects indexed by slot name
slots

Return dictionary of placeholders to names of extra feature tables generated by this module.

tensors

Returns a list of tensors produced by the module

trainables

Get the collection of trainable variables (weights + biases + other trainables) at this module scope

variables

Get the collection of all variables (weights + biases + other trainables + non-trainables) at this module scope

weights

Get the collection of weights at this module scope

tflon.toolkit.toolkit.apply_reduction(*args, **kwargs)

Apply a reduction operation to the output of a tensor operation

Keyword Arguments:
 
  • default_reduction (callable) – A function specifying the default reduction (default=tf.reduce_sum)
  • argnum (int) – The index of the output tensor to reduce (default=None, assumes output is a single tensor)
class tflon.toolkit.toolkit.build_tower(tower)

A context manager used by tflon.model.Tower to set the parent tower of new module objects during tensorflow graph construction

tflon.toolkit.toolkit.current_tower()

Get the tower currently being constructing, used by Module.__init__

tflon.toolkit.toolkit.decorate_or_return(wrapper_impl, args)

Helper function for construction decorators with optional callable signatures

tflon.toolkit.toolkit.get_next_name_scope(cls_name)

Get a unique identifier for a new name scope given a template name

Parameters:cls_name (str) – The template name
tflon.toolkit.toolkit.handle_nan(*args, **kwargs)

Decorator to filter nan values from an output tensor, replacing the filtered values with zeros

Keyword Arguments:
 
  • inarg (int) – the index of the input argument to use for identifying filterable values (default=0, the first argument)
  • outarg (int) – the index of the output argument to filter (default=None, assumes output is a single tensor)
Returns:

Function wrapper to apply nan filtering

tflon.toolkit.toolkit.is_callable(obj)

Check whether obj is callable

tflon.toolkit.toolkit.is_tensor(T)

Check whether T is a tensor type (tf.Tensor or tf.SparseTensor)

tflon.toolkit.toolkit.op_name(op)

Get the name of a tensorflow op, excluding parent scope and index information

tflon.toolkit.toolkit.reset_name_scope_IDs()

Reset the unique identifier generators to zero

tflon.toolkit.toolkit.set_name_scope(*args, **kwargs)

Set the name scope of calls within a function to the function’s name, or some other default value

Keyword Arguments:
 default_name – The default name (default=None, use the function name)

Modules

class tflon.toolkit.modules.Apply(*args, **kwargs)

Apply the given function or lambda expression.

This is used to inject functions or lambda expressions into Module pipes. For example:

>>> net = Apply(lambda x: x+5) | Dense(10)
>>> outputs = net(inputs)
Parameters:func (callable) – A callable, function or lambda-type object
build(func)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)
Parameters:*args – Arbitrary arguments passed to the func callable
Returns:The result of func(*args)
class tflon.toolkit.modules.BatchFill(*args, **kwargs)

Tile a tensor (e.g a vector) along a new first dimension. This op is used to e.g copy a vector for every input example in a batch.

Parameters:value (Tensor) – The value to be tiled
build(value)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(input)
Parameters:inputs (Tensor) – Batch tensor, used to calculate batch size for tiling
class tflon.toolkit.modules.Concat(*args, **kwargs)

Concatenate the input tensors

Parameters:axis (int) – The concatenation axis
build(axis)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.Dense(*args, **kwargs)

Fully connected layer with optional activation function

Parameters:outputs (int) – The width of the output
Keyword Arguments:
 activation (callable) – An activation op (default=lambda x:x)
build(outputs)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*inputs)
Parameters:*inputs (tensor-like) – One or more input tensors
class tflon.toolkit.modules.ForLoop(*args, **kwargs)

Construct a for loop using tf.while_loop with a counter

Parameters:
  • loop_body (callable) – A callable function representing the loop body, should be compatible with tf.while_loop. Must have signature: lambda i, *args, where i is the current counter value
  • iterations (tensor-like) – A scalar tensor indicating the number of iterations (can be static or dynamic)
  • collect_outputs (bool) – If True, collect all values of each loop variable in a tf.TensorArray
build(loop_body, iterations, collect_outputs=False)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)
Parameters:*args – Initial input loop variables, must match signature of loop_body = lambda i, *args
class tflon.toolkit.modules.Gaussian(*args, **kwargs)

This module outputs parameters of gaussian distributions (mean, stddev) for each input example. Mean and standard deviation are calculated via fully connected layers. A gaussian error loss function calculated in normalized space is also added to the model.

Parameters:

targets – Target values, automatically normalized prior to loss calculations

Keyword Arguments:
 
  • distribution – Name of the input table containing the target data distribution.
  • fixed_variance – Assume variance is constant w.r.t. the input domain
  • nans – Handle nan values in error calculation (default=False)
  • reduction – Apply reduction to loss values (default=tf.reduce_sum)
Returns:

The mean and standard deviation, in the same scale as the un-normalized targets

Return type:

mu, sigma

build(targets, distribution=None, nans=False, reduction=<function reduce_sum>)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(inputs, compute_loss=True)
Parameters:inputs (tensor) – The input features for computing mu and sigma via linear ops
class tflon.toolkit.modules.Get(*args, **kwargs)

Get arguments from an input tuple and return a selected subset of them.

Use this module to select an output from a multi-output module (e.g an RNN) to pass to the next element of a chain.

Parameters:*groups (slice) – Slices to apply to *inputs to select the output
build(*groups)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*inputs)

Select slice groups from the tuple *inputs

Parameters:*inputs – arguments from which to select
class tflon.toolkit.modules.Highway(*args, **kwargs)

Add a highway network bypass gate to a given computational block.

https://arxiv.org/abs/1505.00387

Parameters:block (callable) – A module or function which executes the main computation.
build(block)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*inputs)
Compute:
block(inputs) * gate(inputs) + inputs * (1 - gate(inputs))
Parameters:inputs (tensor) – An input tensor passed through both block and highway
class tflon.toolkit.modules.Identity(*args, **kwargs)

Output the input unchanged

build()

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.InterpolatedSequence(*args, **kwargs)

Builds a vector of accumulated sequential feature weights. This can be used with tflon.toolkit.interpolate1d to learn samples from the output of a higher dimensional projection function f: R -> R^n

Parameters:

bins (tensor-like) – 1-D sequence of fixed, known dimension indicating the sampling points of the function domain

Keyword Arguments:
 
  • features (int) – The number of higher dimensional features to sample from the function’s range
  • interpolator (callable) – An interpolation op (usually a lambda expression calling interpolate1d)
build(bins)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(points)
Parameters:points (tensor-like) – 1-D sequence of points for generating interpolations from the sampled bin features
class tflon.toolkit.modules.LayerNorm(*args, **kwargs)
build()

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(inputs)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.Merge(*args, **kwargs)

Merge a list of lists of tensors into a single list of tensors

build()

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.MultivariateGaussian(*args, **kwargs)
build(targets, distribution=None, nans=False, reduction=<function reduce_sum>)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(inputs)
Parameters:inputs (tensor) – The input features for computing mu and sigma via linear ops
class tflon.toolkit.modules.NormalizedInput(*args, **kwargs)

Normalize inputs to Z-distributions

Keyword Arguments:
 distribution_table – The name of the table to collect distribution information, if None infer the table name from the name of the input tensor passed to call (default=None)
build(distribution_table=None)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(X)
Parameters:X (tensor) – The tensor to be scaled
inverse(Y)

Instantiate an op which has the reverse structure of call. The input signature should be the same as the output of call, and the output signature should match the input of call.

Where implemented, this may not be an exact inverse, check the documentation for each Module

Returns:A tensor or sequence of tensors
show_data(table_map)

This method can be overridden to compute parameters from raw data tables for model initialization prior to optimization.

Parameters:table_map (dict) – Dictionary of table objects indexed by slot name
class tflon.toolkit.modules.PCA(*args, **kwargs)

Compute a PCA matrix transformation, optionally handles missing values by probabilistic imputation.

Parameters:distribution_table (str) – The name of the table from which to compute PCA
Keyword Arguments:
 nans (bool) – If True, use probabilistic PCA to handle missing values
build(distribution_table, nans=False, imputed_table=None)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(inputs, sigma=None)

Project from a low dimensional input space to a higher dimensional output space using a transformation learned from data

Parameters:inputs – The input values in the low dimensional space
Keyword Arguments:
 sigma – Optional tensor of sigma values, when provided, inputs and sigma form the parameters of independent normal distributions, which are projected to a multivariate normal distribution in the output space
normalize(*args, **kwargs)

Normalize targets in the high dimensional space

project(*args, **kwargs)

Project from a high dimensional space to a low dimensional input space.

show_data(table_map)

This method can be overridden to compute parameters from raw data tables for model initialization prior to optimization.

Parameters:table_map (dict) – Dictionary of table objects indexed by slot name
class tflon.toolkit.modules.Product(*args, **kwargs)

Perform product on multiple outputs (usually used with Inject or Join)

build()

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.SegmentReduce(*args, **kwargs)

Reduce segmented data to a single vector for each input example.

Parameters:

input_table (str) – The name of the input table to use when constructing reference segment indices (Passed input must be a DenseNestedTable)

Keyword Arguments:
 
  • reduction_op (func) – A tensorflow segment reduce operation (default=tf.segment_sum
  • weighting (callable) – A weighting function with signature func(tensor, segments) (Default: None) * tensor: N-D tensor for weight calculation * segments: 1-D tensor of segment indices
build(input_table, reduction_op=<function segment_sum>, weighting=None)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(inputs)
Parameters:inputs (tensor) – The tensor to be reduced
class tflon.toolkit.modules.Sum(*args, **kwargs)

Perform sum on multiple outputs (usually used with Inject or Join)

build()

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(*args)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
class tflon.toolkit.modules.WindowInput(*args, **kwargs)

Rescale inputs to range between 0-1 by the following transformation:

I - min(I, axis=0) / (max(I, axis=0) - min(I, axis=))
Keyword Arguments:
 distribution_table – The name of the table to collect distribution information, if None infer the table name from the name of the input tensor passed to call (default=None)
build(distribution_table=None)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(X)
Parameters:X (tensor) – The tensor to be scaled
inverse(Y)

Instantiate an op which has the reverse structure of call. The input signature should be the same as the output of call, and the output signature should match the input of call.

Where implemented, this may not be an exact inverse, check the documentation for each Module

Returns:A tensor or sequence of tensors
show_data(table_map)

This method can be overridden to compute parameters from raw data tables for model initialization prior to optimization.

Parameters:table_map (dict) – Dictionary of table objects indexed by slot name

Tensorflow extensions

tflon.toolkit.ops.batch_duplicate(T, multiples, axis=0)

Duplicate slices of a tensor along a given axis. Given an input of shape […, Daxis, …], produces an output of shape […, Daxis * multiples, …]

Parameters:
  • T (tensor-like) – An input tensor
  • multiples (int) – Number of copies of each slice to insert
Keyword Arguments:
 

axis (int) – The axis along which to copy elements

tflon.toolkit.ops.batch_matmul(A, B)

Multiply 2-D matrices packed into a 3-D tensor by vectors packed into a 2-D matrix

This will perform a left or right multiply depending on which tensor is given for A or B.

Parameters:
  • A (tensor-like) – Shape (N, K, L) or (N, K)
  • B (tensor-like) – Shape (N, L) or (N, K, L)
Returns:

Tensor

tflon.toolkit.ops.broadcast_matmul(A, B)

Multiply 2-D matrices packed into a 3-D tensor by a single 2-D matrix

This will perform a left or right multiply depending on which tensor is given for A or B.

Parameters:
  • A (tensor-like) –
  • B (tensor-like) –
Returns:

Tensor

tflon.toolkit.ops.interpolate1d(x, fx, y, method='linear', **kwargs)

Interpolation interface.

Parameters:
  • x (tensor-like) – A 1-D tensor of shape [batch_size] containing values at which to evaluate f(x)
  • fx (tensor-like) – A 1-D tensor of shape [num_samples] containing sampled x values of interpolated function f
  • y (tensor-like) – A 2-D tensor of shape [num_samples, num_features] containing sampled values y=f(x) of interpolated function f
Keyword Arguments:
 

method (str) – linear or rbf methods are supported

Returns:

Return type:

tensor

tflon.toolkit.ops.nan_filter(T, axis=1, partitions=None)

Filter nan values from a tensor and append an indicator tensor along the specified axis

Parameters:

T (tensor-like) – An input tensor of any shape, possibly containing nans

Keyword Arguments:
 
  • axis (int) – The axis on which to append the indicator variables
  • partitions (list) – List of groups to create indicators, if None, treat all columns independently (default=None)
tflon.toolkit.ops.nanmean(T, axis=None)

Return a function to perform nan-safe means on tensors

Parameters:T – A tensor potentially containing nan values to use as a filter
Keyword Arguments:
 axis (int) – The reduction axis (default=None)
Returns:nanmean(x)
Return type:lambda x
tflon.toolkit.ops.nansum(T, axis=None)

Return a function to perform nan-safe sums on tensors

Parameters:T – A tensor potentially containing nan values to use as a filter
Keyword Arguments:
 axis (int) – The reduction axis (default=None)
Returns:nansum(x)
Return type:lambda x
tflon.toolkit.ops.residual_covariance(*args, **kwargs)

Given estimated residuals (X-mu), compute the covariances (and optionally, variances)

Parameters:residuals (tensor-like) – A 2-D tensor of batch x num_variables, the inputs, centered by subtracting the estimated means (X-mu)
Keyword Arguments:
 include_diag (bool) – If true, also calculate the variances along the diagonal. If false, output zeros on the diagonal (default=False)
Returns:a 2-D covariance tensor of shape num_variables x num_variables
Return type:Tensor
tflon.toolkit.ops.segment_softmax(*args, **kwargs)

Compute the softmax along segments of a tensor. This is implemented using a numerically stable solution.

Parameters:
  • X (tensor-like) – The input tensor to normalize
  • segs (tensor-like) – A 1-D tensor of integer segment assignments
Keyword Arguments:
 

epsilon (float) – A constant factor used to prevent divide by zero error

tflon.toolkit.ops.segment_topN(*args, **kwargs)

Computes indices the topN elements for each segment along the first axis of inputs, returning the indices of each element in the final dimension

For input of shape (?, 4), num_segments=10, N=5, output rows would have shape: (10, 20, 2)

use with values = tf.gather_nd(inputs, segment_topN_result) to collect the output

Parameters:
  • inputs (tensor) – a 2-dimensional tensor
  • segments (tensor) – a 1-dimensional tensor of segment IDs corresponding to the first axis of inputs
Keyword Arguments:
 
  • N (int) – The number of top elements to return (default=5)
  • default_value – The default value for the tensor, used when fewer than N top values are available (default=0)

Metrics

tflon.toolkit.metrics.accumulate_values(*args, **kwargs)

Accumulate tensor values by concatenating along the specified axis

Parameters:

values (tf.Tensor) – A tensorflow op containing the next value to be added

Keyword Arguments:
 
  • axis (int) – If provided, concatenate along the specified axis. If None, reshape values to 1-D and concatenate along axis=0
  • init_len (int) – Specifies the initial size of the concatenation tensor, the tensor will contain init_len vectors of zeros along axis (default=0)
Returns:

The tf.Tensor containing the concatenated tensors and an operation to update the concat.

Return type:

accumulated, update_op

tflon.toolkit.metrics.auc(*args, **kwargs)

Return a tf.py_func which wraps sklearn.metrics.roc_auc_score. Optionally, handle nan values.

Parameters:
  • T (tensor-like) – A 2-D tensor of shape batch x classes containing binary class labels
  • L (tensor-like) – A 2-D tensor with the same shape as T containing logit scores for each class
Keyword Arguments:
 
  • axis (int) – If provided, calculate the auc over the specified axis (default=all)
  • nans (bool) – If True, nans in tensor T will be filtered prior to auc calculations
Returns:

A 0-D or 1-D tensor of AUC values

tflon.toolkit.metrics.average_z_score(*args, **kwargs)

Compute the average Z-score of a gaussian regression

Parameters:
  • targets (tensor-like) – N-D tensor containing the target data
  • mu (tensor-like) – N-D tensor of the same shape as targets containing estimated distribution means
  • sigma (tensor-like) – N-D tensor of the same shape as targets containing estimated distribution standard deviations
Keyword Arguments:
 
  • axis (int) – If provided, calculate the average over the specified axis (default=all)
  • nans (bool) – If True, nans in tensor T will be filtered prior to auc calculations
Returns:

The average z-score

Return type:

Tensor

tflon.toolkit.metrics.pearson(*args, **kwargs)

Compute the pearson correlation

Parameters:
  • targets (tensor-like) – N-D tensor containing the target data
  • predictions (tensor-like) – N-D tensor of the same shape as targets containing estimated values
Keyword Arguments:
 
  • axis (int) – If provided, calculate the average over the specified axis (default=all)
  • nans (bool) – If True, nans in tensor T will be filtered prior to auc calculations
Returns:

The average z-score

Return type:

Tensor

tflon.toolkit.metrics.segment_average_auc(*args, **kwargs)

Return a tf.py_func which computes the average auc over segments of two tensors. Handles nans.

Parameters:
  • T (tensor-like) – A 2-D tensor of shape batch x classes containing binary class labels
  • L (tensor-like) – A 2-D tensor with the same shape as T containing logit scores for each class
  • segments (tensor-like) – A 1-D tensor of segment indices of type tf.int32, with the same first dimension size of T and L
Keyword Arguments:
 
  • axis (int) – If provided, calculate the auc over the specified axis (default=all)
  • nans (bool) – If True, nans in tensor T will be filtered prior to auc calculations
Returns:

A 0-D or 1-D tensor of average AUC values over segments of the input

Priors

Losses

class tflon.toolkit.losses.CovarianceLoss(*args, **kwargs)

Compute the multivariate gaussian distribution loss, assuming covariance is constant over the input domain.

Parameters:
  • targets (tensor-like) – A 2-D tensor of batch_size x num_variables
  • mu (tensor-like) – A 2-D tensor of estimated means for each example
Keyword Arguments:
 

sigma (tensor-like) – A 2-D tensor of estimated standard deviations for each example (default=None, if None, calculate variance from the data, assume constant over the input domain)

Returns:

The multivariate gaussian loss, a 0-D tensor

Return type:

Tensor

build(targets)

Instantiate variables required for this module. This is called by __init__, with *args and **kwargs passed from __init__ arguments list.

call(mu, sigma=None)

Instantiate ops executing the module. This is the implementation of __call__, invoked by the base class.

Returns:A tensor or sequence of tensors
tflon.toolkit.losses.elastic_net(*args, **kwargs)

Computes the elastic net penalty of a list of weights:

>>> (1 - alpha) * sum(W ** 2) + alpha * sum(|W|)
Parameters:
  • Ws (list) – tensor-like objects, weights to compute elastic net penalty
  • alpha (float) – A number between zero and one
tflon.toolkit.losses.gaussian_error(*args, **kwargs)

Computes the sum of gaussian losses for multiple gaussian targets

Parameters:
  • targets (tensor-like) – N-D tensor with target regression values
  • mu (tensor-like) – N-D tensor with predicted means
  • sigma (tensor-like) – N-D tensor with predicted standard deviations
Keyword Arguments:
 
  • nans (bool) – Apply nan filter prior to reduction (default=False)
  • reduction (callable) – A reduction op to apply to the partial losses (default=tf.reduce_sum)
tflon.toolkit.losses.l1_penalty(*args, **kwargs)

Computes the total L1 penalty of a list of weights

Parameters:Ws (list) – tensor-like objects, weights to compute L1 penalty
tflon.toolkit.losses.l2_penalty(*args, **kwargs)

Computes the total L2 penalty of a list of weights

Parameters:Ws (list) – tensor-like objects, weights to compute L2 penalty
tflon.toolkit.losses.xent(*args, **kwargs)

Computes the unweighted sum of cross entropy losses for multiple binary targets.

Parameters:
  • T (tensor-like) – N-D tensor with target class labels
  • L (tensor-like) – N-D tensor with same shape as T, containing logit network output
Keyword Arguments:
 

nans (bool) – Apply nan filter prior to reduction

tflon.toolkit.losses.xent_softmax(*args, **kwargs)

Computes an unweighted softmax cross entropy

Includes optional handling of missing values

Parameters:
  • T (tensor-like) – N-D tensor with target class labels
  • L (tensor-like) – N-D tensor with same shape as T, containing logit network output
Keyword Arguments:
 

nans (bool) – Apply nan filter prior to reduction

tflon.toolkit.losses.xent_uniform_sum(*args, **kwargs)

Computes the unweighted sum of cross entropy losses for multiple binary targets.

Parameters:
  • T (tensor-like) – N-D tensor with target class labels
  • L (tensor-like) – N-D tensor with same shape as T, containing logit network output
Keyword Arguments:
 

nans (bool) – Apply nan filter prior to reduction

tflon.toolkit.losses.xent_weighted(*args, **kwargs)

Computes the unweighted sum of cross entropy losses for multiple binary targets.

Parameters:
  • T (tensor-like) – N-D tensor with target class labels
  • L (tensor-like) – N-D tensor with same shape as T, containing logit network output
  • W (tensor-like) – 0-D tensor with the weight for positive examples
Keyword Arguments:
 

nans (bool) – Apply nan filter prior to reduction

tflon.toolkit.losses.xent_weighted_sum(*args, **kwargs)

Computes the unweighted sum of cross entropy losses for multiple binary targets.

Parameters:
  • T (tensor-like) – N-D tensor with target class labels
  • L (tensor-like) – N-D tensor with same shape as T, containing logit network output
  • W (tensor-like) – 0-D tensor with the weight for positive examples
Keyword Arguments:
 

nans (bool) – Apply nan filter prior to reduction