In this library, there are a set of python classes, developed in tensor flow 2,designed to be used in the calculus of different metrics on neural network models.
Besides, there are three metrics developed. These metrics are known as Invariance, FeaturesVariance, and Same Equivariance.
- Variance
- NormalizedVariance
- FeaturesVariance
- NormalizedFeaturesVariance
- SameEquivariance
- NormalizedSameEquivariance
Let's supose we have a neural network model, as in the figure 1:
In detail, a neural network model is conformed by a series of layers as we can see in the figure 2:
where every layer has a set of activations that we call
Now, let's suposse we are interested in knowing the value of each activation when our model predict an input X. To get these values it is necessary a model that has as outputs not only the output Y but also the activations
Another feature of the Class Model is that its method predict takes as argument a matrix of inputs. It was thougth to deal with metrics that works with samples and transformations, but it is also possible to forget the input meaning and just think of a 2D input.
model_keras = ... #instance of a Keras Model
model = Model(model_keras=model_keras)
input = ... # tensor of rank n>2
output = model.predict(input)
i=...
j=...
l=...
print("Activations values for input[i][j] in the layer l " + output[(i,j,l)])
#Another way of instantiating the class is the next
path = ".../mymodel.h5" #path to the file .h5
model = Model(path=path)
- predict
- get_model_structure
- get_name_layer
- evaluate
Arguments
- tensor_input: a tensor of rank n, n > 2, where the first two dimentions represent the matrix and the rest dimentions represent the input data.
Returns
Returns an instance of Block. This instance can be indexed for the tuple (i,j,l) where i represents the row, j represents the column corresponding to the input matrix and l represents the l-layer of a model. The instance can be indexed too for the tuple (i,j,k,f) where in this case f represents the feature. See the description of the Class Block for a better understanding.
Returns an instance of ModelStructure with the model structure.
Arguments
- k:int,the layer index
Returns Returns the name of hte layer k.
Arguments
- x:tf.tensor, a tensor with model inputs
- y:tf.tensor, a tensor with the desired outputs
Returns
Returns a list with the loss and the accuracy for the tuple(x,y)
DataSet is an abstract class designed to represent a 2D dataset which will be used by the Iterator to iterate over the model. The implementations of the DataSet are responsability of the user and they are necessary to use the Iterator. An implementation available in this library is the MnistDataSet.
- get_data_shape
- get_width
- get_height
- get_matrix
- transpose
Returns the shape of the data in the matrix.
Returns the matrix width.
Returns the matrix height.
Arguments
- rows: a list of indices
- columns: a list of indices
Returns
Returns a submatrix conformed by the rows and columns passed as arguments.
Transpose the DataSet.
Returns if the dataset is transposed or not.
This class extends the abstraction of the Class DataSet in order to have a dateset oriented to transformations. It is an abstract class too, that inherits from the class DataSet. This class was thougth to have groups of transformations. These groups are setting up in the dataset through the set_transformation_group method.
It applies an inverse transform to a tensor
Arguments
- tensor: tf.Tensor. The tensor to apply the inverse transform
- index_transformation:int. The index of the transformation to apply the inverse
Returns the amount of transformations groups.
Sets the transformations group in the dataset
Arguments
- group: int. The group
- n:int. The amount of transformations for this group.
Let's consider we have a 2D dataset, for example a dataset of samples and transformations, and we want to get every value predicted by the model for each input in the dataset. If we think of all possible values that we will obtain and besides we divide these values into layers, it is possible to imagine some kind of data representation in the way showing in the next figure:
Every square represents a matrix with the values of an activation for each input in the dataset. For instance, the first square represents the activation
Now that we have a visual representation of the values we are looking for, it is easier to understand what the Iterator Class does. The Iterator gives you the possibility of moving and getting the values through blocks of width and height defined by yourself.
For example, let's consider a block of 3x3. The iterator allows you obtaining the data in the following way:
model = Model(...)
dataset = DataSet(...)
iterator = Iterator(model, dataset)
height = 3
width = 3
for height, blocks in iterator.get_block(height, width):
#...
for width, block in blocks
#...
Arguments
- model: an instance of the Model class
- dataset: an instance of the DataSet class
- get_block
- get_model
Arguments
- height: the height of the block
- width: the width of the block
Returns
Returns a "vertical" iterator where each item in this iterator is a tuple (height,blocks_iterator) where blocks_iterator is an "horizontal" iterator in which each item is a tuple (width,block_activations) where block_activations is an object that indexes a three dimensional list through a tuple (i,j,l) where i represents the row and j represents the column corresponding to the block and l represents the l-layer of a model. The values in the list are the activations values in the layer l for the input located in the row i and the column j, and width is the width of the block and the height of the block is the height returned previously.
Remark
When we say "vertical" iterator, we refer to the movement of the iterator in the block, the same happens with we say "horizontal" iterator.
Returns the model in the iterator.
The goal of this class is to represent the structure of a keras model. On this way, the class ModelStructure has the structure of each layer in the model and it has too a map between layers and layers with features. The last information allows us to define the features variance metric.
Arguments
- model: an instance of a keras model
- layers:list. A list with an instance of LayerStructure for each layer
- number_layers:int. The number of layers in the model
- map_layers_features:list. A list containing a map between the layers and the layers with features
- layers_features:list. A list with an instance of LayerStructure for each layer with features
- number_layers_features:int. The number of layers with features
This class contains the information of the layer structure.
- number_features:int. The number of features in the layer
- shape_feature:tuple. The shape for the features in the layer
- number_activations:int. The number of activations in the layer
- shape:tuple. The shape of the layer
This class is used by the model to encapsulate the output in a block when it is predicted a block of inputs.The block can be indexed by a tuple of three or four indexes to get a particular output.
The tuple (i,j,k) gives the output of the layer k for the input in the block given by the row i and the column j.
The tuple (i,j,k,f) gives the output of the feature f in the layer k for the input in the block given by the row i and the column j.
Besides, the block has some information of the dataset from which the inputs come.
- height:int. The height of the block
- width:int. The width of the block
- model_structure:ModelStructure. The model structure
This method can be used to set the corresponding dataset rows in the block.
Arguments
- rows: list
This method can be used to set the corresponding dataset columns in the block.
Arguments
- columns: list
This method can be used to set if the dataset is transposed.
Arguments
- state: bool
Returns the corresponding dataset rows in the block.
Returns the corresponding dataset columns in the block.
Returns the transpose state of the dataset.
This class implements the abstract class TransformationsDataSet. This dataset is Mnist with transformations. It has 8 groups of transformations. These groups are:
- Identity
- Rotations
- X-Translations
- Y-Translations
- X,Y-Translations
- Rotations and X-Translations at the same time
- Rotations and Y-Translations at the same time
This class tries to analyze if there are relations between the results of a metric and the accuracy. For example, if you take the variance metric and a group of transformations for a dataset, you can see if when the variance increases the accuracy decreases.
Arguments
- model:Model.
- dataset:TransformationsDataSet.
- iterator:Iterator.
This method run the analyses.
Arguments
- n:int. The amount of transformations for each group in the dataset.
model = Model(...)
dataset = TransformationsDataSet(...)
iterator = Iterator(model,dataset)
analyzer = Analyzer(model,dataset,iterator)
analyzer.run(100)
Let's A be an activation in our model. One way to define the variance of A for a DataSet X is as follows:
Var(A,X)="The mean of the variance for every row in X", i.e.,
Similarly, it can be defined the variance for a Layer in the Model.
So, the class variance computes these values for a dataset X and a model M using the Iterator.
model = Model(...)
dataset = DataSet(...)
iterator = Iterator(model, dataset)
variance = Variance(Iterator)
variance.compute()
print(variance.variance_layers)
Arguments
- iterator: it is an instance of the Iterator class
- compute
Arguments
- height: it is the height of the block to iterate
- width: it is the width of the block to iterate
Returns
Returns a tuple (variance_layers,variance_layers_activations) where:
- variance_layers: it is a list with the variance of every layer
- variance_layers_activations: it is a list where each element in the list is a list with the variance of every activation in the respective layer