PyrCombineLayerA¶
Role: Trainable Keras layer mapping
(field_batch, sr_outcome_batch)tonext_fieldlogits for the next field.
Location: Q_Sea_Battle.pyr_combine_layer_a.PyrCombineLayerA
Derived constraints¶
- Let \(L\) be the last dimension of
field_batch; \(L\) must be statically known at build time and must be even, so that \(L/2\) is an integer. - Let \(B\) be the batch dimension.
- At runtime, the last dimension of
sr_outcome_batchmust equal \(L/2\).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| hidden_units | int, constraint \(\ge 1\), scalar | Number of hidden units in the internal Dense(ReLU) layer. |
| name | Optional[str], scalar | Keras layer name; may be None. |
| dtype | Optional[tf.dtypes.DType], scalar | Layer dtype; may be None (defaults are applied by Keras and call() conversions). |
| **kwargs | Any, variadic | Forwarded to tf.keras.layers.Layer constructor. |
Preconditions
hidden_unitsis anintwith value \(\ge 1\).
Postconditions
self.hidden_unitsis set toint(hidden_units).- Sub-layers (
_dense_hidden,_dense_out) are not created untilbuild().
Errors
- Raises
ValueErrorifhidden_units < 1.
Example
import tensorflow as tf
from Q_Sea_Battle.pyr_combine_layer_a import PyrCombineLayerA
layer = PyrCombineLayerA(hidden_units=64, dtype=tf.float32)
Public Methods¶
build¶
Signature: build(input_shape: Any) -> None
Parameters
input_shape: Any, constraint "shape-like accepted by Keras", scalar or nested; expected to describefield_batchas(B, L)and may also include a second shape forsr_outcome_batchthat is ignored for building.
Returns
None.
Behavior
- Infers \(L\) from the last dimension of the field shape and computes
out_dim = L // 2. - Creates two Dense sublayers: a hidden Dense with
hidden_unitsand ReLU activation, and an output Dense without_dimand linear activation. - Records
self._built_for_L = L.
Preconditions
- The last dimension \(L\) of the field shape is statically known (not
None). - \(L\) is even.
Postconditions
self._dense_hiddenandself._dense_outare non-Noneinstances oftf.keras.layers.Dense.self._built_for_Lis set toint(L).
Errors
- Raises
ValueErrorif the field shape last dimension is not statically known. - Raises
ValueErrorif \(L\) is not even.
call¶
Signature: call(field_batch: tf.Tensor, sr_outcome_batch: tf.Tensor, training: bool = False, **kwargs: Any) -> tf.Tensor
Parameters
field_batch: tf.Tensor, dtype convertible toself.dtype(ortf.float32ifself.dtype is None), shape \((B, L)\), rank 2.sr_outcome_batch: tf.Tensor, dtype convertible toself.dtype(ortf.float32ifself.dtype is None), shape \((B, L/2)\), rank 2.training: bool, scalar; forwarded to Dense sublayers.**kwargs: Any, variadic; accepted but not used.
Returns
next_field: tf.Tensor, dtypeself.dtypeif set elsetf.float32, shape \((B, L/2)\).
Behavior
- Converts inputs to tensors with dtype
self.dtype(ortf.float32if unset). - Validates both inputs are rank-2.
- Asserts at runtime that
sr_outcome_batch.shape[-1] == field_batch.shape[-1] // 2. - Concatenates inputs along the last axis to form shape \((B, 3L/2)\), applies hidden Dense(ReLU), then output Dense(linear) to produce logits.
Preconditions
- Layer has been built so that
_dense_hiddenand_dense_outexist.
Postconditions
- No state is created during the call.
Errors
- Raises
ValueErrorif either input is not rank-2 (when rank is statically known and not equal to 2). - Raises a TensorFlow assertion error at runtime if
sr_outcome_batchlast dimension does not equalfield_batchlast dimension divided by 2. - Raises
RuntimeErrorif sublayers are missing (layer not built correctly).
Example
import tensorflow as tf
from Q_Sea_Battle.pyr_combine_layer_a import PyrCombineLayerA
B, L = 4, 10
field = tf.random.uniform((B, L), dtype=tf.float32)
sr = tf.random.uniform((B, L // 2), dtype=tf.float32)
layer = PyrCombineLayerA(hidden_units=32, dtype=tf.float32)
logits = layer(field, sr, training=True)
assert logits.shape == (B, L // 2)
get_config¶
Signature: get_config() -> Dict[str, Any]
Parameters
- None.
Returns
config: Dict[str, Any], mapping; includes base layer config plus key"hidden_units"with valueint.
Behavior
- Extends
tf.keras.layers.Layer.get_config()with{"hidden_units": self.hidden_units}.
Data & State¶
hidden_units: int, constraint \(\ge 1\), scalar; persisted in config._dense_hidden: Optional[tf.keras.layers.Dense], scalar; created inbuild(), otherwiseNone._dense_out: Optional[tf.keras.layers.Dense], scalar; created inbuild(), otherwiseNone._built_for_L: Optional[int], scalar; set to \(L\) inbuild(), otherwiseNone.
Planned (design-spec)¶
- Not specified.
Deviations¶
- The module docstring describes a contract that
sr_outcome_batchhas shape(B, L/2)and thatbuild()can use only the field shape; the implementation matches this, butbuild()accepts multiple possibleinput_shapeencodings and ignores SR shape even when provided.
Notes for Contributors¶
- Keras may call
build()with only the first input shape for multi-input layers; this implementation intentionally creates all state inbuild()from the field shape alone and validates SR shape at runtime incall(). - Avoid creating new variables or sublayers in
call(); sublayers are expected to be created inbuild().
Related¶
- TensorFlow:
tf.keras.layers.Layer,tf.keras.layers.Dense - Tensor ops:
tf.concat,tf.convert_to_tensor,tf.debugging.assert_equal
Changelog¶
- Not specified.