tensornetwork.tn_keras.layers.Conv2DMPO

class tensornetwork.tn_keras.layers.Conv2DMPO(filters: int, kernel_size: Union[int, Tuple[int, int]], num_nodes: int, bond_dim: int, strides: Union[int, Tuple[int, int]] = 1, padding: str = 'same', data_format: Optional[str] = 'channels_last', dilation_rate: Union[int, Tuple[int, int]] = (1, 1), activation: Optional[str] = None, use_bias: bool = True, kernel_initializer: str = 'glorot_uniform', bias_initializer: str = 'zeros', kernel_regularizer: Optional[str] = None, bias_regularizer: Optional[str] = None, **kwargs)

2D Convolutional Matrix Product Operator (MPO) TN layer.

This layer recreates the functionality of a traditional convolutional layer, but stores the ‘kernel’ as a network of nodes forming an MPO. The bond dimension of the MPO can be adjusted to increase or decrease the number of parameters independently of the input and output dimensions. When the layer is called, the MPO is contracted into a traditional kernel and convolved with the layer input to produce a tensor of outputs.

Example

# as first layer in a sequential model:
model = Sequential()
model.add(
  Conv2DMPO(256,
            kernel_size=3,
            num_nodes=4,
            bond_dim=16,
            activation='relu',
            input_shape=(32, 32, 256)))
# now the model will take as input tensors of shape (*, 32, 32, 256)
# and output arrays of shape (*, 32, 32, 256).
# After the first layer, you don't need to specify
# the size of the input anymore:
model.add(Conv2DMPO(256, 3, num_nodes=4, bond_dim=8, activation='relu'))
Parameters:
  • filters – Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution).
  • kernel_size – An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.
  • num_nodes – Positive integer, number of nodes in the MPO. Note input_shape[-1]**(1. / num_nodes) and filters**(1. / num_nodes) must both be round.
  • bond_dim – Positive integer, size of the MPO bond dimension (between nodes). Lower bond dimension means more parameter compression.
  • strides – An integer or tuple/list of 2 integers, specifying the strides of the convolution along the height and width. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any dilation_rate value != 1.
  • padding – one of "valid" or "same"
  • data_format – A string, one of "channels_last" or "channels_first". The ordering of the dimensions in the inputs. "channels_last" corresponds to inputs with shape (batch, height, width, channels) while "channels_first" corresponds to inputs with shape (batch, channels, height, width). It defaults to “channels_last”.
  • dilation_rate – an integer or tuple/list of 2 integers, specifying the dilation rate to use for dilated convolution. Can be a single integer to specify the same value for all spatial dimensions. Currently, specifying any dilation_rate value != 1 is incompatible with specifying any stride value != 1.
  • activation – Activation function to use. If you don’t specify anything, no activation is applied (ie. “linear” activation: a(x) = x).
  • use_bias – Boolean, whether the layer uses a bias vector.
  • kernel_initializer – Initializer for the node weight matrices.
  • bias_initializer – Initializer for the bias vector.
  • kernel_regularizer – Regularizer for the node weight matrices.
  • bias_regularizer – Regularizer for the bias vector.
Input shape:
4D tensor with shape: (batch_size, h, w, channels).
Output shape:
4D tensor with shape: (batch_size, h_out, w_out, filters).
__init__(filters: int, kernel_size: Union[int, Tuple[int, int]], num_nodes: int, bond_dim: int, strides: Union[int, Tuple[int, int]] = 1, padding: str = 'same', data_format: Optional[str] = 'channels_last', dilation_rate: Union[int, Tuple[int, int]] = (1, 1), activation: Optional[str] = None, use_bias: bool = True, kernel_initializer: str = 'glorot_uniform', bias_initializer: str = 'zeros', kernel_regularizer: Optional[str] = None, bias_regularizer: Optional[str] = None, **kwargs) → None

Methods

__init__(filters, kernel_size, Tuple[int, …)
add_loss(losses, **kwargs) Add loss tensor(s), potentially dependent on layer inputs.
add_metric(value[, name]) Adds metric tensor to the layer.
add_update(updates[, inputs]) Add update op(s), potentially dependent on layer inputs.
add_variable(*args, **kwargs) Deprecated, do NOT use! Alias for add_weight.
add_weight([name, shape, dtype, …]) Adds a new variable to the layer.
apply(inputs, *args, **kwargs) Deprecated, do NOT use!
build(input_shape) Creates the variables of the layer (optional, for subclass implementers).
call(inputs) This is where the layer’s logic lives.
compute_mask(inputs[, mask]) Computes an output mask tensor.
compute_output_shape(input_shape) Computes the output shape of the layer.
compute_output_signature(input_signature) Compute the output tensor signature of the layer based on the inputs.
count_params() Count the total number of scalars composing the weights.
from_config(config) Creates a layer from its config.
get_config() Returns the config of the layer.
get_input_at(node_index) Retrieves the input tensor(s) of a layer at a given node.
get_input_mask_at(node_index) Retrieves the input mask tensor(s) of a layer at a given node.
get_input_shape_at(node_index) Retrieves the input shape(s) of a layer at a given node.
get_losses_for(inputs) Deprecated, do NOT use!
get_output_at(node_index) Retrieves the output tensor(s) of a layer at a given node.
get_output_mask_at(node_index) Retrieves the output mask tensor(s) of a layer at a given node.
get_output_shape_at(node_index) Retrieves the output shape(s) of a layer at a given node.
get_updates_for(inputs) Deprecated, do NOT use!
get_weights() Returns the current weights of the layer, as NumPy arrays.
set_weights(weights) Sets the weights of the layer, from NumPy arrays.
with_name_scope(method) Decorator to automatically enter the module name scope.

Attributes

activity_regularizer Optional regularizer function for the output of this layer.
dtype The dtype of the layer weights.
dynamic Whether the layer is dynamic (eager-only); set in the constructor.
inbound_nodes Deprecated, do NOT use! Only for compatibility with external Keras.
input Retrieves the input tensor(s) of a layer.
input_mask Retrieves the input mask tensor(s) of a layer.
input_shape Retrieves the input shape(s) of a layer.
input_spec InputSpec instance(s) describing the input format for this layer.
losses List of losses added using the add_loss() API.
metrics List of metrics added using the add_metric() API.
name Name of the layer (string), set in the constructor.
name_scope Returns a tf.name_scope instance for this class.
non_trainable_variables Sequence of non-trainable variables owned by this module and its submodules.
non_trainable_weights List of all non-trainable weights tracked by this layer.
outbound_nodes Deprecated, do NOT use! Only for compatibility with external Keras.
output Retrieves the output tensor(s) of a layer.
output_mask Retrieves the output mask tensor(s) of a layer.
output_shape Retrieves the output shape(s) of a layer.
stateful
submodules Sequence of all sub-modules.
trainable
trainable_variables Sequence of trainable variables owned by this module and its submodules.
trainable_weights List of all trainable weights tracked by this layer.
updates
variables Returns the list of all layer variables/weights.
weights Returns the list of all layer variables/weights.
build(input_shape: List[int]) → None

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.

This is typically used to create the weights of Layer subclasses.

Parameters:input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
call(inputs: tensorflow.python.framework.ops.Tensor) → tensorflow.python.framework.ops.Tensor

This is where the layer’s logic lives.

Note here that call() method in tf.keras is little bit different from keras API. In keras API, you can pass support masking for layers as additional arguments. Whereas tf.keras has compute_mask() method to support masking.

Parameters:
  • inputs

    Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

    arguments, and inputs cannot be provided via the default value of a keyword argument.
    • NumPy array or Python scalar values in inputs get cast as tensors.
    • Keras mask metadata is only collected from inputs.
    • Layers are built (build(input_shape) method) using shape info from inputs only.
    • input_spec compatibility is only checked against inputs.
    • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
    • The SavedModel input specification is generated using inputs only.
    • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
  • *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
  • **kwargs

    Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

    whether the call is meant for training or inference.
    • mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns:

A tensor or list/tuple of tensors.

compute_output_shape(input_shape: List[int]) → Tuple[int, int, int, int]

Computes the output shape of the layer.

If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.

Parameters:input_shape – Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
Returns:An input shape tuple.
get_config() → dict

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns:Python dictionary.