PyrMeasurementLayerA¶
Role: Trainable Keras layer mapping a cropped field state tensor to measurement logits via a 2-layer MLP.
Location: Q_Sea_Battle.pyr_measurement_layer_a.PyrMeasurementLayerA
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| hidden_units | int, constraint \(\ge 1\), scalar | Width of the hidden Dense layer. |
| name | Optional[str], constraint: Keras layer name or None, scalar | Optional Keras layer name. |
| dtype | Optional[tf.dtypes.DType], constraint: valid TensorFlow dtype or None, scalar | Optional dtype for the layer and its sublayers. |
| **kwargs | Any, constraint: forwarded to tf.keras.layers.Layer, variadic |
Additional keyword arguments forwarded to the Keras base Layer. |
Preconditions¶
hidden_unitsis anintwith constraint \(\ge 1\), scalar.
Postconditions¶
self.hidden_unitsis set toint(hidden_units), scalar.- The sublayers
self._dense_hiddenandself._dense_outremainNoneuntilbuild(...)is called.
Errors¶
- Raises
ValueErrorifhidden_units < 1.
Example¶
Instantiate the layer
import tensorflow as tf
from Q_Sea_Battle.pyr_measurement_layer_a import PyrMeasurementLayerA
layer = PyrMeasurementLayerA(hidden_units=64, dtype=tf.float32)
Public Methods¶
build¶
- Signature:
build(input_shape: Any) -> None
Creates sublayers based on the input width \(L\) (the last dimension of input_shape), producing an output width \(L/2\).
Arguments¶
input_shape: Any, constraint: convertible totf.TensorShapewith statically known last dimension \(L\), shape (Not specified).
Returns¶
None, constraint: no return value, scalar.
Preconditions¶
input_shapecan be converted totf.TensorShape.- The last dimension \(L\) of
input_shapeis statically known. - \(L\) is even so that \(L/2\) is an integer.
Postconditions¶
- Creates
self._dense_hidden:tf.keras.layers.Dense, output shape (B, hidden_units) when called on rank-2 input. - Creates
self._dense_out:tf.keras.layers.Dense, output shape (B, L/2) when called on the hidden activations. - Sets
self._built_for_Ltoint(L), scalar. - Calls
super().build(input_shape).
Errors¶
- Raises
ValueErrorif the last dimension \(L\) is not statically known. - Raises
ValueErrorif \(L\) is not even.
call¶
- Signature:
call(field_batch: tf.Tensor, training: bool = False, **kwargs: Any) -> tf.Tensor
Runs the forward pass: (B, L) -> (B, hidden_units) -> (B, L/2) and returns logits (no sigmoid).
Arguments¶
field_batch: tf.Tensor, dtype float32 (ifself.dtypeis None) orself.dtype(viatf.convert_to_tensor), shape (B, L).training: bool, constraint: standard Keras training flag, scalar.**kwargs: Any, constraint: unused (accepted for Keras compatibility), variadic.
Returns¶
meas_logits: tf.Tensor, dtype float32 (ifself.dtypeis None) orself.dtype, shape (B, L/2).
Preconditions¶
- If
field_batchhas a statically known rank, it is rank-2 (B, L). - The last dimension \(L\) is even (enforced with a runtime assertion).
Postconditions¶
- Returns
meas_logits = self._dense_out(self._dense_hidden(x)), wherexisfield_batchconverted to a tensor with dtypeself.dtypeortf.float32.
Errors¶
- Raises
ValueErrorif the input rank is statically known and not 2. - Raises
RuntimeErrorif the layer is missing sublayers (self._dense_hiddenorself._dense_outisNone). - May raise TensorFlow assertion errors if the runtime check fails: \(L \bmod 2 = 0\).
get_config¶
- Signature:
get_config() -> Dict[str, Any]
Returns the serializable layer configuration, including hidden_units.
Arguments¶
- None.
Returns¶
cfg: Dict[str, Any], constraint: Keras-serializable configuration dictionary, shape (Not applicable).
Preconditions¶
- None specified.
Postconditions¶
- The returned dict includes key
"hidden_units"with valueself.hidden_units.
Errors¶
- Not specified.
Data & State¶
hidden_units: int, constraint \(\ge 1\), scalar; number of hidden units in the intermediate Dense layer._dense_hidden: Optional[tf.keras.layers.Dense], constraint:Nonebeforebuild(...), scalar reference; hidden Dense sublayer created inbuild(...)._dense_out: Optional[tf.keras.layers.Dense], constraint:Nonebeforebuild(...), scalar reference; output Dense sublayer created inbuild(...)._built_for_L: Optional[int], constraint:Nonebeforebuild(...), scalar; input width \(L\) used to build the layer.
Planned (design-spec)¶
- Not specified.
Deviations¶
- Not specified.
Notes for Contributors¶
- Sublayers are intentionally created in
build(...)because the output dimension depends on the input width \(L\). - The output head produces logits (no sigmoid); downstream components are expected to apply any sigmoid/DRU/etc. as needed.
- The runtime even-width constraint is enforced in
call(...)usingtf.debugging.assert_equal, which can catch dynamic-shape mismatches even ifbuild(...)succeeded.
Related¶
tf.keras.layers.Layertf.keras.layers.Dense
Changelog¶
- Not specified.