tensornetwork.tn_keras.layers.DenseMPO¶
-
class
tensornetwork.tn_keras.layers.DenseMPO(output_dim: int, num_nodes: int, bond_dim: int, use_bias: Optional[bool] = True, activation: Optional[str] = None, kernel_initializer: Optional[str] = 'glorot_uniform', bias_initializer: Optional[str] = 'zeros', **kwargs)¶ Matrix Product Operator (MPO) TN layer.
This layer can take an input shape of arbitrary dimension, with the first dimension expected to be a batch dimension. The weight matrix will be constructed from and applied to the last input dimension.
Example
# as first layer in a sequential model: model = Sequential() model.add( DenseMPO(1024, num_nodes=4, bond_dim=8, activation='relu', input_shape=(1024,))) # now the model will take as input arrays of shape (*, 1024) # and output arrays of shape (*, 1024). # After the first layer, you don't need to specify # the size of the input anymore: model.add(DenseMPO(1024, num_nodes=4, bond_dim=8, activation='relu'))
Parameters: - output_dim – Positive integer, dimensionality of the output space.
- num_nodes – Positive integer, number of nodes in the MPO. Note input_shape[-1]**(1. / num_nodes) and output_dim**(1. / num_nodes) must both be round.
- bond_dim – Positive integer, size of the intermediate dimension.
- 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.
- Input shape:
- N-D tensor with shape:
(batch_size, ..., input_dim). - Output shape:
- N-D tensor with shape:
(batch_size, ..., output_dim).
-
__init__(output_dim: int, num_nodes: int, bond_dim: int, use_bias: Optional[bool] = True, activation: Optional[str] = None, kernel_initializer: Optional[str] = 'glorot_uniform', bias_initializer: Optional[str] = 'zeros', **kwargs) → None¶
Methods
__init__(output_dim, num_nodes, bond_dim, …)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, **kwargs)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_regularizerOptional regularizer function for the output of this layer. dtypeThe dtype of the layer weights. dynamicWhether the layer is dynamic (eager-only); set in the constructor. inbound_nodesDeprecated, do NOT use! Only for compatibility with external Keras. inputRetrieves the input tensor(s) of a layer. input_maskRetrieves the input mask tensor(s) of a layer. input_shapeRetrieves the input shape(s) of a layer. input_specInputSpecinstance(s) describing the input format for this layer.lossesList of losses added using the add_loss()API.metricsList of metrics added using the add_metric()API.nameName of the layer (string), set in the constructor. name_scopeReturns a tf.name_scopeinstance for this class.non_trainable_variablesSequence of non-trainable variables owned by this module and its submodules. non_trainable_weightsList of all non-trainable weights tracked by this layer. outbound_nodesDeprecated, do NOT use! Only for compatibility with external Keras. outputRetrieves the output tensor(s) of a layer. output_maskRetrieves the output mask tensor(s) of a layer. output_shapeRetrieves the output shape(s) of a layer. statefulsubmodulesSequence of all sub-modules. trainabletrainable_variablesSequence of trainable variables owned by this module and its submodules. trainable_weightsList of all trainable weights tracked by this layer. updatesvariablesReturns the list of all layer variables/weights. weightsReturns 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
LayerorModelcan override if they need a state-creation step in-between layer instantiation and layer call.This is typically used to create the weights of
Layersubclasses.Parameters: input_shape – Instance of TensorShape, or list of instances ofTensorShapeif the layer expects a list of inputs (one instance per input).
-
call(inputs: tensorflow.python.framework.ops.Tensor, **kwargs) → tensorflow.python.framework.ops.Tensor¶ This is where the layer’s logic lives.
Note here that
call()method intf.kerasis little bit different fromkerasAPI. InkerasAPI, you can pass support masking for layers as additional arguments. Whereastf.kerashascompute_mask()method to support masking.Parameters: - inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional
inputsargument is subject to special rules: -inputsmust be explicitly passed. A layer cannot have zeroarguments, andinputscannot be provided via the default value of a keyword argument.- NumPy array or Python scalar values in
inputsget cast as tensors. - Keras mask metadata is only collected from
inputs. - Layers are built (
build(input_shape)method) using shape info frominputsonly. input_speccompatibility is only checked againstinputs.- Mixed precision input casting is only applied to
inputs. If a layer has tensor arguments in*argsor**kwargs, their casting behavior in mixed precision should be handled manually. - The SavedModel input specification is generated using
inputsonly. - Integration with various ecosystem packages like TFMOT, TFLite,
TF.js, etc is only supported for
inputsand not for tensors in positional and keyword arguments.
- NumPy array or Python scalar values in
- *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 indicatingwhether thecallis meant for training or inference.mask: Boolean input mask. If the layer’scall()method takes amaskargument, its default value will be set to the mask generated forinputsby the previous layer (ifinputdid 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.
- inputs –
-
compute_output_shape(input_shape: List[int]) → Tuple[int, int]¶ Computes the output shape of the layer.
If the layer has not been built, this method will call
buildon 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.
The same layer can be reinstantiated later (without its trained weights) from this configuration.
Returns: Python dictionary containing the configuration of the layer.