signxai.tf_signxai.methods_impl.innvestigate.utils.keras package

Submodules

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend module

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.to_floatx(x)[source]
signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.gradients(Xs, Ys, known_Ys)[source]

Partial derivatives

Computes the partial derivatives between Ys and Xs and using the gradients for Ys known_Ys.

Parameters:
  • Xs – List of input tensors.

  • Ys – List of output tensors that depend on Xs.

  • known_Ys – Gradients for Ys.

Returns:

Gradients for Xs given known_Ys

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.is_not_finite(x)[source]

Checks if tensor x is finite, if not throws an exception.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.extract_conv2d_patches(x, kernel_shape, strides, rates, padding)[source]

Extracts conv2d patches like TF function extract_image_patches.

Parameters:
  • x – Input image.

  • kernel_shape – Shape of the Keras conv2d kernel.

  • strides – Strides of the Keras conv2d layer.

  • rates – Dilation rates of the Keras conv2d layer.

  • padding – Paddings of the Keras conv2d layer.

Returns:

The extracted patches.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.gather(x, axis, indices)[source]

Works as TensorFlow’s gather.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.backend.gather_nd(x, indices)[source]

Works as TensorFlow’s gather_nd.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks module

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.get_known_layers()[source]

Returns a list of keras layer we are aware of.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.get_activation_search_safe_layers()[source]

Returns a list of keras layer that we can walk along in an activation search.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.contains_activation(layer, activation=None)[source]

Check whether the layer contains an activation function. activation is None then we only check if layer can contain an activation.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.contains_kernel(layer)[source]

Check whether the layer contains a kernel.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.only_relu_activation(layer)[source]

Checks if layer contains no or only a ReLU activation.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_network(layer)[source]

Is network in network?

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_convnet_layer(layer)[source]

Checks if layer is from a convolutional network.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_relu_convnet_layer(layer)[source]

Checks if layer is from a convolutional network with ReLUs.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_average_pooling(layer)[source]

Checks if layer is an average-pooling layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_max_pooling(layer)[source]

Checks if layer is a max-pooling layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_input_layer(layer, ignore_reshape_layers=True)[source]

Checks if layer is an input layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.checks.is_batch_normalization_layer(layer, *args, **kwargs)[source]

Checks if layer is a batchnorm layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph module

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_kernel(layer)[source]

Returns the kernel weights of a layer, i.e, w/o biases.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_layer_inbound_count(layer)[source]

Returns the number inbound nodes of a layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_layer_outbound_count(layer)[source]

Returns the number outbound nodes of a layer.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_layer_neuronwise_io(layer, node_index=0, Xs=None, Ys=None, return_i=True, return_o=True)[source]

Returns the input and output for each neuron in a layer

Returns the symbolic input and output for each neuron in a layer. For a dense layer this is the input output itself. For convolutional layers this method extracts for each neuron the input output mapping.

At the moment this function is designed to work with dense and conv2d layers.

Parameters:
  • layer – The targeted layer.

  • node_index – Index of the layer node to use.

  • Xs – Ignore the layer’s input but use Xs instead.

  • Ys – Ignore the layer’s output but use Ys instead.

  • return_i – Return the inputs.

  • return_o – Return the outputs.

Returns:

Inputs and outputs, if specified, for each individual neuron.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.copy_layer_wo_activation(layer, keep_bias=True, name_template=None, weights=None, reuse_symbolic_tensors=True, **kwargs)[source]

Copy a Keras layer and remove the activations

Copies a Keras layer but remove potential activations.

Parameters:
  • layer – A layer that should be copied.

  • keep_bias – Keep a potential bias.

  • weights – Weights to set in the new layer. Options: np tensors, symbolic tensors, or None, in which case the weights from old_layers are used.

  • reuse_symbolic_tensors – If the weights of the old_layer are used copy the symbolic ones or copy the Numpy weights.

Returns:

The new layer instance.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.copy_layer(layer, keep_bias=True, name_template=None, weights=None, reuse_symbolic_tensors=True, **kwargs)[source]

Copy a Keras layer

Copies a Keras layer.

Parameters:
  • layer – A layer that should be copied.

  • keep_bias – Keep a potential bias.

  • weights – Weights to set in the new layer. Options: np tensors, symbolic tensors, or None, in which case the weights from old_layers are used.

  • reuse_symbolic_tensors – If the weights of the old_layer are used copy the symbolic ones or copy the Numpy weights.

Returns:

The new layer instance.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.pre_softmax_tensors(Xs, should_find_softmax=True)[source]

Finds the tensors that were preceeding a potential softmax.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.model_wo_softmax(model)[source]

Creates a new model w/o the final softmax activation.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.fake_keras_layer(inputs, outputs)[source]
signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_model_layers(model)[source]

Returns all layers of a model.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.model_contains(model, layer_condition, return_only_counts=False)[source]
signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.trace_model_execution(model, reapply_on_copied_layers=False)[source]

Trace and linearize excecution of a model and it’s possible containers. Return a triple with all layers, a list with a linearized execution with (layer, input_tensors, output_tensors), and, possible regenerated, outputs of the exectution.

Parameters:
  • model – A kera model.

  • reapply_on_copied_layers – If the execution needs to be linearized, reapply with copied layers. Might be slow. Prevents changes of the original layer’s node lists.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_model_execution_trace(model, keep_input_layers=False, reapply_on_copied_layers=False)[source]

Returns a list representing the execution graph. Each key is the node’s id as it is used by the reverse_model method.

Each associated value contains a dictionary with the following items:

  • nid: the node id.

  • layer: the layer creating this node.

  • Xs: the input tensors (only valid if not in a nested container).

  • Ys: the output tensors (only valid if not in a nested container).

  • Xs_nids: the ids of the nodes creating the Xs.

  • Ys_nids: the ids of nodes using the according output tensor.

  • Xs_layers: the layer that created the accodring input tensor.

  • Ys_layers: the layers using the according output tensor.

Parameters:
  • model – A kera model.

  • keep_input_layers – Keep input layers.

  • reapply_on_copied_layers – If the execution needs to be linearized, reapply with copied layers. Might be slow. Prevents changes of the original layer’s node lists.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_model_execution_graph(model, keep_input_layers=False)[source]

Returns a dictionary representing the execution graph. Each key is the node’s id as it is used by the reverse_model method.

Each associated value contains a dictionary with the following items:

  • nid: the node id.

  • layer: the layer creating this node.

  • Xs: the input tensors (only valid if not in a nested container).

  • Ys: the output tensors (only valid if not in a nested container).

  • Xs_nids: the ids of the nodes creating the Xs.

  • Ys_nids: the ids of nodes using the according output tensor.

  • Xs_layers: the layer that created the accodring input tensor.

  • Ys_layers: the layers using the according output tensor.

Parameters:
  • model – A kera model.

  • keep_input_layers – Keep input layers.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.print_model_execution_graph(graph)[source]

Pretty print of a model execution graph.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_bottleneck_nodes(inputs, outputs, execution_list)[source]

Given an execution list this function returns all nodes that are a bottleneck in the network, i.e., “all information” must pass through this node.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.graph.get_bottleneck_tensors(inputs, outputs, execution_list)[source]

Given an execution list this function returns all tensors that are a bottleneck in the network, i.e., “all information” must pass through this tensor.

Module contents

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.apply(layer, inputs)[source]

Apply a layer to input[s].

A flexible apply that tries to fit input to layers expected input. This is useful when one doesn’t know if a layer expects a single tensor or many.

Parameters:
  • layer – A Keras layer instance.

  • inputs – A list of input tensors or a single tensor.

signxai.tf_signxai.methods_impl.innvestigate.utils.keras.broadcast_np_tensors_to_keras_tensors(keras_tensors, np_tensors)[source]

Broadcasts numpy tensors to the shape of Keras tensors.

Parameters:
  • keras_tensors – The Keras tensors with the target shapes.

  • np_tensors – Numpy tensors that should be broadcasted.

Returns:

The broadcasted Numpy tensors.