PyrTrainableAssistedModelB¶
Role: Player-B pyramid assisted Keras model that applies per-level measurement, assisted processing, and combination layers to produce a shooting logit.
Location: Q_Sea_Battle.pyr_trainable_assisted_model_b.PyrTrainableAssistedModelB
Derived constraints¶
- Symbols: n2 = field_size, m = comms_size.
- n2 and m are inferred from
game_layoutvia_infer_n2_and_m(game_layout). - Constraint: m must equal 1; otherwise construction raises
ValueError. - Constraint: n2 must be a power of two; enforced via
_validate_power_of_two(n2)which returnsdepth(number of pyramid levels).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| game_layout | Any, constraints: must be accepted by _infer_n2_and_m(game_layout), shape: not specified |
Game layout object used to infer n2 and m. |
| p_high | float, constraints: not specified, shape: scalar | Passed to each PRAssistedLayer as p_high. |
| sr_mode | str, constraints: not specified, shape: scalar | Passed to each PRAssistedLayer as mode. Default "sample". |
| measure_layers | Optional[Sequence[tf.keras.layers.Layer]], constraints: if provided then len(measure_layers) == depth, shape: sequence length depth |
Per-level measurement layers; if None, defaults to depth instances of PyrMeasurementLayerB(). |
| combine_layers | Optional[Sequence[tf.keras.layers.Layer]], constraints: if provided then len(combine_layers) == depth, shape: sequence length depth |
Per-level combine layers; if None, defaults to depth instances of PyrCombineLayerB(). |
| name | Optional[str], constraints: Keras model name semantics, shape: scalar | Passed to tf.keras.Model constructor. |
Preconditions
_infer_n2_and_m(game_layout)must succeed and return(n2, m).- m must be 1.
- n2 must be a power of two (as validated by
_validate_power_of_two). - If
measure_layersis provided, it must be a sequence of lengthdepth. - If
combine_layersis provided, it must be a sequence of lengthdepth.
Postconditions
self.n2: intandself.M: intare set from_infer_n2_and_m(game_layout).self.depth: intis set from_validate_power_of_two(self.n2).self.measure_layers: List[tf.keras.layers.Layer]andself.combine_layers: List[tf.keras.layers.Layer]are populated with lengthdepth.- Backward-compat aliases
self.measure_layerandself.combine_layerreference the first element of the corresponding per-level lists. self.sr_layers: List[PRAssistedLayer]is populated with lengthdepth, with decreasinglengthvalues per level:n2/2, n2/4, ..., 1.
Errors
ValueError: ifm != 1(message indicatescomms_size==1is required).ValueError: ifmeasure_layersis provided and its length is notdepth.ValueError: ifcombine_layersis provided and its length is notdepth.
Example
Instantiate with default per-level layers
import tensorflow as tf
from Q_Sea_Battle.pyr_trainable_assisted_model_b import PyrTrainableAssistedModelB
game_layout = ... # must be accepted by _infer_n2_and_m
model = PyrTrainableAssistedModelB(game_layout=game_layout, p_high=0.9, sr_mode="sample", name="player_b")
# Example call signature (see call() for tensor shapes)
B = 4
n2 = model.n2
depth = model.depth
gun = tf.zeros((B, n2), dtype=tf.float32)
comm = tf.zeros((B, 1), dtype=tf.float32)
prev_measurements = [tf.zeros((B, n2 // (2 ** (level + 1))), dtype=tf.float32) for level in range(depth)]
prev_outcomes = [tf.zeros((B, n2 // (2 ** (level + 1))), dtype=tf.float32) for level in range(depth)]
y = model([gun, comm, prev_measurements, prev_outcomes], training=False)
Public Methods¶
call¶
Signature: call(self, inputs: list, training: bool = False, **kwargs: Any) -> tf.Tensor
Parameters
- inputs: list, constraints: must be list/tuple of length 4 in the order
[gun_batch, comm_batch, prev_measurements, prev_outcomes], shape: outer length 4. - training: bool, constraints: not specified, shape: scalar; forwarded to per-level
measure_layerandcombine_layerif they accept atrainingkeyword argument. - **kwargs: Any, constraints: accepted but not used in implementation, shape: not applicable.
Returns
- shoot_logit: tf.Tensor, dtype float32, constraints: derived from clipped comm in \([0,1]\) then mapped to logits, shape (B, 1).
Behavior
- Validates
inputsis a list/tuple of length 4 and unpacks it intogun_batch,comm_batch,prev_measurements,prev_outcomes. - Converts
gun_batchandcomm_batchto float32 tensors and validates shapes:gunrank 2 with shape (B, n2) andcommrank 2 with shape (B, 1). - Validates
prev_measurementsandprev_outcomesare Python lists/tuples with lengthdepth. - Iterates levels
0..depth-1: computes a per-level measurementmeas_bfrom currentstateusingmeasure_layers[level], then runsPRAssistedLayerto produceout_b, then appliescombine_layers[level]to update(state, c). - Clips
cto \([0,1]\) and maps to logits:shoot_logit = (c * 2.0 - 1.0) * 10.0.
Errors
ValueError: ifinputsis not list/tuple length 4.ValueError: ifgun_batchdoes not convert to a rank-2 tensor.ValueError: ifcomm_batchdoes not convert to a rank-2 tensor of trailing dimension 1.TypeError: ifprev_measurementsorprev_outcomesis not a Python list/tuple.ValueError: ifprev_measurementsorprev_outcomesdoes not have lengthdepth.ValueError: if per-levelprev_outormeas_bis not rank-2.ValueError: if per-level last-dimension lengths ofmeas_b,prev_meas, andprev_outdo not match.
Data & State¶
- n2: int, constraints: inferred from
game_layout, expected power of two, shape: scalar; field_size. - M: int, constraints: must equal 1, shape: scalar; comms_size (m).
- depth: int, constraints: returned by
_validate_power_of_two(n2), shape: scalar; number of pyramid levels. - measure_layers: List[tf.keras.layers.Layer], constraints: length
depth, shape: list lengthdepth; per-level measurement layers. - combine_layers: List[tf.keras.layers.Layer], constraints: length
depth, shape: list lengthdepth; per-level combine layers. - measure_layer: tf.keras.layers.Layer, constraints: alias of
measure_layers[0], shape: scalar reference. - combine_layer: tf.keras.layers.Layer, constraints: alias of
combine_layers[0], shape: scalar reference. - sr_layers: List[PRAssistedLayer], constraints: length
depth, shape: list lengthdepth; per-level assisted processing layers with decreasinglength.
Planned (design-spec)¶
- Not specified.
Deviations¶
- Not specified.
Notes for Contributors¶
- The forward pass attempts to call measurement/combine sublayers with
training=trainingand falls back to calling withouttrainingonTypeError; changes to sublayer call signatures should preserve this robustness behavior. **kwargsis accepted bycallbut unused; if adding behavior, ensure compatibility with Keras calling conventions.
Related¶
Q_Sea_Battle.pyr_measurement_layer_b.PyrMeasurementLayerBQ_Sea_Battle.pyr_combine_layer_b.PyrCombineLayerBQ_Sea_Battle.pr_assisted_layer.PRAssistedLayerQ_Sea_Battle.pyr_trainable_assisted_model_a._infer_n2_and_mQ_Sea_Battle.pyr_trainable_assisted_model_a._validate_power_of_two
Changelog¶
- 0.1: Initial version; includes robustness patch to accept and forward the Keras
trainingkwarg incallwhere supported by sublayers.