tensorstate.TensorState

The TensorState module contains many of the core, high level functions that have been developed through state space research.

aIQ(net_efficiency, accuracy, weight)

Calculate the artificial intelligence quotient

The artificial intelligence quotient (aIQ) is a simple metric to report a balance of neural network efficiency and task performance. Although not required, it is assumed that the accuracy argument is a float ranging from 0.0-1.0, with 1.0 meaning more accurate.

aIQ = (net_efficiency * accuracy ** weight) ** (1/(weight+1))

The weight argument is an integer, with higher values giving more weight to the accuracy of the model.

Parameters:
  • net_efficiency ([float]) – A float ranging from 0.0-1.0
  • accuracy ([float]) – A float ranging from 0.0-1.0
  • weight ([int]) – An integer with value >=1
Raises:

ValueError – Raised if weight <= 0

Returns:

The artificial intelligence quotient

Return type:

[float]

build_efficiency_model(model, attach_to, exclude=[], method='after', storage_path=None)

Attach state capture methods to a neural network

This method takes an existing neural network model and attaches either layers or hooks to the model to capture the states of neural network layers.

For Tensorflow, only keras.Model networks can serve as inputs to this function. When a Tensorflow model is fed into this function, a new network is returned where StateCapture layers are inserted into the network at the designated locations.

For PyTorch, a neural network that implements the Module class will have hooks added to the layers. A new network is not generated, but for consistency the model is returned from this function.

Parameters:
  • model ([keras.Model, torch.nn.Module]) – A Keras model
  • attach_to (list, optional) – List of strings indicating the types of layers to attach to. Names of layers can also be specified to attach StateCapture to specific layers
  • exclude (list, optional) – List of strings indicating the names of layers to not attach StateCapture layers to. This will override the attach_to keyword, so that a Conv2D layer with the name specified by exclude will not have a StateCapture layer attached to it. Defaults to [].
  • method (str, optional) – The location to attach the StateCapture layer to. Must be one of [‘before’,’after’,’both’]. Defaults to ‘after’.
  • storage_path ([str, pathlib.Path], optional) – Path on disk to store states in zarr format. If None, states are stored in memory. Defaults to None.
Returns:

A model of the same type as the input model

Return type:

model

entropy(counts, alpha=1)

Calculate the Renyi entropy

The Renyi entropy is a general definition of entropy that encompasses Shannon’s entropy, Hartley (maximum) entropy, and min-entropy. It is defined as:

(1-alpha)**-1 * log2( sum(p**alpha) )

By default, this method sets alpha=1, which is Shannon’s entropy.

Parameters:
  • counts (numpy.ndarray) – Array of counts representing number of times a state is observed.
  • alpha ([int,float], optional) – Entropy order. Defaults to 1.
Returns:

The entropy of the count data.

Return type:

[float]

network_efficiency(efficiencies)

Calculate the network efficiency

This method calculates the neural network efficiency, defined as the geometric mean of the efficiency values calculated for the network.

Parameters:efficiencies ([list,keras.Model,torch.nn.Module]) – A list of efficiency values (floats) or a keras.Model
Returns:The network efficiency
Return type:[float]
reset_efficiency_model(model)

Reset all efficiency layers/hooks in a model

This method resets all efficiency layers or hooks in a model, setting the state_count=0. This is useful for repeated evaluation of a model during a single session.

Parameters:model ([keras.Model, torch.nn.Module]) – Model to reset