phygnn.layers.custom_layers.Sup3rObsModel

class Sup3rObsModel(*args, **kwargs)[source]

Bases: Layer

Layer to concatenate sparse data in the middle of a super resolution forward pass, with a learned embedding. Mutiple observation features and multiple continuous exogenous features can be provided. The embedding network is defined with a list of hidden layers. If no hidden layers are provided, this layer will simply concatenate the hi_res_feature, exogenous data (if provided), and mask (if include_mask is True), to the input tensor after filling the NaNs.

Parameters:
  • name (str | None) – Unique str identifier of the layer. Usually the name of the hi-resolution feature used in the concatenation.

  • features (list | None) – The names of the observation features to be included in the embedding input.

  • exo_features (list | None) – The names of exogenous features to be included in the embedding input

  • hidden_layers (list | None) – The list of layers used to create the embedding network.

  • fill_method (str) – The method used to fill in the NaN values in the hi_res_feature before embedding. Options are ‘mean’, ‘idw’, or None. If None then the first channel of x will be used to fill the NaN values.

  • include_mask (bool) – Whether to include the mask for where there is valid observation data in the embedding. If False, the mask will not be included in the embedding.

Methods

add_loss(loss)

Can be called inside of the call() method to add a scalar loss.

add_metric(*args, **kwargs)

add_variable(shape, initializer[, dtype, ...])

Add a weight variable to the layer.

add_weight([shape, initializer, dtype, ...])

Add a weight variable to the layer.

build(input_shape)

Build the weight net layer based on an input shape

build_from_config(config)

Builds the layer's states with the supplied config dict.

call(x[, hi_res_feature, exo_data])

Apply the embed net to hi_res_feature, exogenous data, and the mask representing where hi_res_feature is not nan.

compute_mask(inputs, previous_mask)

compute_output_shape(*args, **kwargs)

compute_output_spec(*args, **kwargs)

count_params()

Count the total number of scalars composing the weights.

from_config(config)

Deserialize nested hidden layers for Keras loading.

get_build_config()

Returns a dictionary with the layer's input shape.

get_config()

Get config for Keras serialization.

get_weights()

Return the values of layer.weights as a list of NumPy arrays.

load_own_variables(store)

Loads the state of the layer.

quantize(mode[, type_check, config])

quantized_build(input_shape, mode)

quantized_call(*args, **kwargs)

rematerialized_call(layer_call, *args, **kwargs)

Enable rematerialization dynamically for layer's call method.

save_own_variables(store)

Saves the state of the layer.

set_weights(weights)

Sets the values of layer.weights from a list of NumPy arrays.

stateless_call(trainable_variables, ...[, ...])

Call the layer without any side effects.

symbolic_call(*args, **kwargs)

Attributes

compute_dtype

The dtype of the computations performed by the layer.

dtype

Alias of layer.variable_dtype.

dtype_policy

input

Retrieves the input tensor(s) of a symbolic operation.

input_dtype

The dtype layer inputs should be converted to.

input_spec

losses

List of scalar losses from add_loss, regularizers and sublayers.

metrics

List of all metrics.

metrics_variables

List of all metric variables.

non_trainable_variables

List of all non-trainable layer state.

non_trainable_weights

List of all non-trainable weight variables of the layer.

output

Retrieves the output tensor(s) of a layer.

path

The path of the layer.

quantization_mode

The quantization mode of this layer, None if not quantized.

supports_masking

Whether this layer supports computing a mask using compute_mask.

trainable

Settable boolean, whether this layer should be trainable or not.

trainable_variables

List of all trainable layer state.

trainable_weights

List of all trainable weight variables of the layer.

variable_dtype

The dtype of the state (weights) of the layer.

variables

List of all layer state, including random seeds.

weights

List of all weight variables of the layer.

get_config()[source]

Get config for Keras serialization.

classmethod from_config(config)[source]

Deserialize nested hidden layers for Keras loading.

build(input_shape)[source]

Build the weight net layer based on an input shape

Parameters:

input_shape (tuple) – Shape tuple of the input tensor

call(x, hi_res_feature=None, exo_data=None)[source]

Apply the embed net to hi_res_feature, exogenous data, and the mask representing where hi_res_feature is not nan. Concatenate the output with x. hi_res_feature and exo_data are allowed to be None so that models can be trained with hi_res_feature and exogenous data and then run with various sets of inputs.

Parameters:
  • x (tf.Tensor) – Input tensor

  • hi_res_feature (tf.Tensor | np.ndarray | None) – This should be a 4D array for spatial enhancement model or 5D array for a spatiotemporal enhancement model (obs, spatial_1, spatial_2, (temporal), features). This is NaN where there are no observations and real values where observations exist.

  • exo_data (tf.Tensor | np.ndarray | None) – This is an array of exogenous data used to imform the embedding, like topography

Returns:

x (tf.Tensor) – Output tensor with embedding concatenated to input.

__call__(*args, **kwargs)

Call self as a function.

add_loss(loss)

Can be called inside of the call() method to add a scalar loss.

Example:

```python class MyLayer(Layer):

… def call(self, x):

self.add_loss(ops.sum(x)) return x

```

add_variable(shape, initializer, dtype=None, trainable=True, autocast=True, regularizer=None, constraint=None, name=None)

Add a weight variable to the layer.

Alias of add_weight().

add_weight(shape=None, initializer=None, dtype=None, trainable=True, autocast=True, regularizer=None, constraint=None, aggregation='none', overwrite_with_gradient=False, name=None)

Add a weight variable to the layer.

Args:
shape: Shape tuple for the variable. Must be fully-defined

(no None entries). Defaults to () (scalar) if unspecified.

initializer: Initializer object to use to populate the initial

variable value, or string name of a built-in initializer (e.g. “random_normal”). If unspecified, defaults to “glorot_uniform” for floating-point variables and to “zeros” for all other types (e.g. int, bool).

dtype: Dtype of the variable to create, e.g. “float32”. If

unspecified, defaults to the layer’s variable dtype (which itself defaults to “float32” if unspecified).

trainable: Boolean, whether the variable should be trainable via

backprop or whether its updates are managed manually. Defaults to True.

autocast: Boolean, whether to autocast layers variables when

accessing them. Defaults to True.

regularizer: Regularizer object to call to apply penalty on the

weight. These penalties are summed into the loss function during optimization. Defaults to None.

constraint: Contrainst object to call on the variable after any

optimizer update, or string name of a built-in constraint. Defaults to None.

aggregation: Optional string, one of None, “none”, “mean”,

“sum” or “only_first_replica”. Annotates the variable with the type of multi-replica aggregation to be used for this variable when writing custom data parallel training loops. Defaults to “none”.

overwrite_with_gradient: Boolean, whether to overwrite the variable

with the computed gradient. This is useful for float8 training. Defaults to False.

name: String name of the variable. Useful for debugging purposes.

build_from_config(config)

Builds the layer’s states with the supplied config dict.

By default, this method calls the build(config[“input_shape”]) method, which creates weights based on the layer’s input shape in the supplied config. If your config contains other information needed to load the layer’s state, you should override this method.

Args:

config: Dict containing the input shape associated with this layer.

property compute_dtype

The dtype of the computations performed by the layer.

count_params()

Count the total number of scalars composing the weights.

Returns:

An integer count.

property dtype

Alias of layer.variable_dtype.

get_build_config()

Returns a dictionary with the layer’s input shape.

This method returns a config dict that can be used by build_from_config(config) to create all states (e.g. Variables and Lookup tables) needed by the layer.

By default, the config only contains the input shape that the layer was built with. If you’re writing a custom layer that creates state in an unusual way, you should override this method to make sure this state is already created when Keras attempts to load its value upon model loading.

Returns:

A dict containing the input shape associated with the layer.

get_weights()

Return the values of layer.weights as a list of NumPy arrays.

property input

Retrieves the input tensor(s) of a symbolic operation.

Only returns the tensor(s) corresponding to the first time the operation was called.

Returns:

Input tensor or list of input tensors.

property input_dtype

The dtype layer inputs should be converted to.

load_own_variables(store)

Loads the state of the layer.

You can override this method to take full control of how the state of the layer is loaded upon calling keras.models.load_model().

Args:

store: Dict from which the state of the model will be loaded.

property losses

List of scalar losses from add_loss, regularizers and sublayers.

property metrics

List of all metrics.

property metrics_variables

List of all metric variables.

property non_trainable_variables

List of all non-trainable layer state.

This extends layer.non_trainable_weights to include all state used by the layer including state for metrics and `SeedGenerator`s.

property non_trainable_weights

List of all non-trainable weight variables of the layer.

These are the weights that should not be updated by the optimizer during training. Unlike, layer.non_trainable_variables this excludes metric state and random seeds.

property output

Retrieves the output tensor(s) of a layer.

Only returns the tensor(s) corresponding to the first time the operation was called.

Returns:

Output tensor or list of output tensors.

property path

The path of the layer.

If the layer has not been built yet, it will be None.

property quantization_mode

The quantization mode of this layer, None if not quantized.

rematerialized_call(layer_call, *args, **kwargs)

Enable rematerialization dynamically for layer’s call method.

Args:

layer_call: The original call method of a layer.

Returns:

Rematerialized layer’s call method.

save_own_variables(store)

Saves the state of the layer.

You can override this method to take full control of how the state of the layer is saved upon calling model.save().

Args:

store: Dict where the state of the model will be saved.

set_weights(weights)

Sets the values of layer.weights from a list of NumPy arrays.

stateless_call(trainable_variables, non_trainable_variables, *args, return_losses=False, **kwargs)

Call the layer without any side effects.

Args:

trainable_variables: List of trainable variables of the model. non_trainable_variables: List of non-trainable variables of the

model.

*args: Positional arguments to be passed to call(). return_losses: If True, stateless_call() will return the list of

losses created during call() as part of its return values.

**kwargs: Keyword arguments to be passed to call().

Returns:
A tuple. By default, returns (outputs, non_trainable_variables).

If return_losses = True, then returns (outputs, non_trainable_variables, losses).

Note: non_trainable_variables include not only non-trainable weights such as BatchNormalization statistics, but also RNG seed state (if there are any random operations part of the layer, such as dropout), and Metric state (if there are any metrics attached to the layer). These are all elements of state of the layer.

Example:

```python model = … data = … trainable_variables = model.trainable_variables non_trainable_variables = model.non_trainable_variables # Call the model with zero side effects outputs, non_trainable_variables = model.stateless_call(

trainable_variables, non_trainable_variables, data,

) # Attach the updated state to the model # (until you do this, the model is still in its pre-call state). for ref_var, value in zip(

model.non_trainable_variables, non_trainable_variables

):

ref_var.assign(value)

```

property supports_masking

Whether this layer supports computing a mask using compute_mask.

property trainable

Settable boolean, whether this layer should be trainable or not.

property trainable_variables

List of all trainable layer state.

This is equivalent to layer.trainable_weights.

property trainable_weights

List of all trainable weight variables of the layer.

These are the weights that get updated by the optimizer during training.

property variable_dtype

The dtype of the state (weights) of the layer.

property variables

List of all layer state, including random seeds.

This extends layer.weights to include all state used by the layer including `SeedGenerator`s.

Note that metrics variables are not included here, use metrics_variables to visit all the metric variables.

property weights

List of all weight variables of the layer.

Unlike, layer.variables this excludes metric state and random seeds.