PyrInternalModelA¶
Role: Logits-only Pyramid internal model (A) that maps field logits \((B, n2)\) to communication logits \((B, 1)\) while optionally exposing per-level intermediate logits for training/teacher forcing.
Location: Q_Sea_Battle.pyr_internal_model_a.PyrInternalModelA
Derived constraints¶
- Let \(n2\) be the flattened field length inferred from
game_layout; the Pyramid depth is \(depth = \log_2(n2)\) and therefore \(n2\) MUST be a positive power of two. - Let \(m\) be the communication size inferred from
game_layout(comms_sizeor legacyM); this architecture requires \(m = 1\).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| game_layout | Any, must expose attributes n2 or field_size, and comms_size or M |
Layout-like object used to infer \(n2\) and \(m\) (stored as self.n2 and self.M). |
| sr_mode | str, expected in | Shared resource mode forwarded to each PRAssistedReplay layer. |
| p_rule | float, not specified | Follow probability forwarded to each PRAssistedReplay layer (used in stochastic mode inside PRAssistedReplay). |
| beta | float, not specified | Logit magnitude forwarded to each PRAssistedReplay layer. |
| alpha | float, not specified | PR gate sharpness forwarded to each PRAssistedReplay layer. |
| seed | int | None, not specified | Optional seed forwarded to each PRAssistedReplay layer. |
| measure_layers | Sequence[tf.keras.layers.Layer] | None, if provided length must equal depth |
Optional explicit per-level measurement layers; if None, creates depth instances of PyrMeasurementLayerA. |
| combine_layers | Sequence[tf.keras.layers.Layer] | None, if provided length must equal depth |
Optional explicit per-level combine layers; if None, creates depth instances of PyrCombineLayerA. |
| name | str | None, not specified | Optional Keras model name forwarded to tf.keras.Model. |
Preconditions
game_layoutMUST provide enough attributes to infer \(n2\) and \(m\) (eithern2orfield_size, and eithercomms_sizeorM).- The inferred \(m\) MUST equal 1.
- The inferred \(n2\) MUST be a positive power of two.
- If
measure_layersis provided,len(measure_layers) == depth. - If
combine_layersis provided,len(combine_layers) == depth.
Postconditions
self.n2: intis set to the inferred \(n2\).self.M: intis set to the inferred \(m\) (and equals 1).self.depth: intis set to \(\log_2(n2)\).self.measure_layers: list[tf.keras.layers.Layer]has lengthdepth.self.combine_layers: list[tf.keras.layers.Layer]has lengthdepth.self.sr_layers: list[PRAssistedReplay]has lengthdepth.- Backward-compat aliases are set:
self.measure_layer == self.measure_layers[0]andself.combine_layer == self.combine_layers[0].
Errors
- Raises
ValueErrorif inferredcomms_size != 1. - Raises
ValueErrorifmeasure_layersis provided and its length does not matchdepth. - Raises
ValueErrorifcombine_layersis provided and its length does not matchdepth. - Raises
ValueErrorif inferred \(n2\) is non-positive or not a power of two (via internal validation).
Example
Instantiate with inferred layers
import tensorflow as tf
from Q_Sea_Battle.pyr_internal_model_a import PyrInternalModelA
class Layout:
field_size = 4 # n2 = 16, depth = 4
comms_size = 1
model = PyrInternalModelA(Layout(), sr_mode="replay", alpha=5.0, beta=10.0, seed=123)
x = tf.zeros((2, model.n2), dtype=tf.float32)
y = model(x, training=False)
assert y.shape == (2, 1)
Public Methods¶
set_alpha¶
Set PR gate sharpness for all SR layers.
Signature
set_alpha(alpha: float) -> None
Arguments
alpha: float, not specified, scalar.
Returns
None: NoneType, no value.
Errors
- Raises
AttributeErrorif a configured SR layer does not implementset_alpha.
set_p_rule¶
Set stochastic follow probability for all SR layers.
Signature
set_p_rule(p_rule: float) -> None
Arguments
p_rule: float, not specified, scalar.
Returns
None: NoneType, no value.
Errors
- Raises
AttributeErrorif a configured SR layer does not implementset_p_rule.
set_beta¶
Set hard-logit beta for all SR layers.
Signature
set_beta(beta: float) -> None
Arguments
beta: float, not specified, scalar.
Returns
None: NoneType, no value.
Errors
- Raises
AttributeErrorif a configured SR layer does not implementset_beta.
set_sr_mode¶
Set SR mode for all SR layers.
Signature
set_sr_mode(sr_mode: str) -> None
Arguments
sr_mode: str, not specified, scalar; typically one of"replay"or"stochastic".
Returns
None: NoneType, no value.
Errors
- Raises
AttributeErrorif a configured SR layer does not implementset_sr_mode.
call¶
Keras forward pass returning only the final communication logits.
Signature
call(field_scaled: tf.Tensor, training: bool = False, **kwargs: Any) -> tf.Tensor
Arguments
field_scaled: tf.Tensor, dtype float32 (converted), shape \((B, n2)\); treated as field logits (name retained for API compatibility).training: bool, not specified, scalar; forwarded to sublayers where supported.**kwargs: Any, unused; accepted for Keras compatibility.
Returns
comm_logits: tf.Tensor, dtype float32, shape \((B, 1)\).
Errors
- Not specified in
call; input validation is performed insidecompute_with_internalwhichcallinvokes.
compute_with_internal¶
Compute a full forward pass and return per-level intermediate logits.
Signature
compute_with_internal(field_logits: tf.Tensor, replay_out_a_logits_list: Sequence[tf.Tensor] | None = None, harden_between_levels: bool = False, beta_for_hardening: float = 10.0, training: bool = False) -> tuple[tf.Tensor, list[tf.Tensor], list[tf.Tensor]]
Arguments
field_logits: tf.Tensor, dtype float32 (converted), shape \((B, n2)\).replay_out_a_logits_list: Sequence[tf.Tensor] | None, if provided must be a Pythonlistortupleof lengthdepth; each element is converted to tf.Tensor dtype float32 and MUST satisfytf.shape(elem)[-1] == tf.shape(meas_logits)[-1]at its level (enforced at runtime).harden_between_levels: bool, not specified, scalar; ifTrue, hardens intermediate state logits between levels to \(\pm beta\_for\_hardening\) based on sign.beta_for_hardening: float, not specified, scalar; magnitude used when hardening is enabled.training: bool, not specified, scalar; forwarded to sublayers where supported.
Returns
comm_logits: tf.Tensor, dtype float32, shape \((B, 1)\); the final field logits interpreted as comm logits for Pyramid layouts.meas_list: list[tf.Tensor], dtype float32 elements, lengthdepth; per-level measurement logits; element shapes are not fully specified in the module docstring (depend onPyrMeasurementLayerA), but are used withtf.zeros_likeand compared in last-dimension width to any teacher-forced replay logits.out_list: list[tf.Tensor], dtype float32 elements, lengthdepth; per-level SR outcome logits returned byPRAssistedReplay; element shapes are not fully specified (depend onPRAssistedReplayand measurement width at each level).
Errors
- Raises
ValueErroriffield_logitsis not rank-2. - Raises
ValueErroriffield_logitslast dimension is statically known and not equal ton2. - Raises
TypeErrorifreplay_out_a_logits_listis provided but is not a Pythonlistortuple. - Raises
ValueErrorifreplay_out_a_logits_listis provided and its length is notdepth. - May raise
tf.errors.InvalidArgumentError(or similar TensorFlow runtime error) if teacher forcing replay logits do not match measurement width at a level (enforced viatf.debugging.assert_equal). - Raises
RuntimeErrorif internal depth iteration produces no outputs (should be unreachable unlessdepth == 0).
Teacher forcing behavior
If replay_out_a_logits_list is provided, element [level] is passed to the SR layer as replay_outcome_logits, enabling deterministic per-level outcomes while still producing measurement and combined logits.
save_weights_to¶
Save model weights to a file; ensures variables are built first.
Signature
save_weights_to(path: str) -> None
Arguments
path: str, not specified, scalar; destination filepath understood bytf.keras.Model.save_weights.
Returns
None: NoneType, no value.
Errors
- Not specified; may raise exceptions from
_ensure_builtor TensorFlow/Keras IO.
load_weights_from¶
Load model weights from a file; ensures variables are built first.
Signature
load_weights_from(path: str) -> None
Arguments
path: str, not specified, scalar; source filepath understood bytf.keras.Model.load_weights.
Returns
None: NoneType, no value.
Errors
- Not specified; may raise exceptions from
_ensure_builtor TensorFlow/Keras IO.
Data & State¶
n2: int, constraint: positive power of two; scalar; flattened field length.M: int, constraint: equals 1; scalar; communication size (stored under legacy nameM).depth: int, constraint:2**depth == n2; scalar; number of Pyramid levels.measure_layers: list[tf.keras.layers.Layer], constraint: lengthdepth; per-level measurement layers.combine_layers: list[tf.keras.layers.Layer], constraint: lengthdepth; per-level combine layers.sr_layers: list[PRAssistedReplay], constraint: lengthdepth; per-level PR-assisted shared resource layers.measure_layer: tf.keras.layers.Layer, alias tomeasure_layers[0].combine_layer: tf.keras.layers.Layer, alias tocombine_layers[0].
Planned (design-spec)¶
- Not specified.
Deviations¶
- Not specified.
Notes for Contributors¶
- Input naming:
calluses parameter namefield_scaledfor API compatibility, but the tensor is treated as logits; adapters for scaling/bit conversion are external to this model. - Variable creation: weights may not exist until the first forward pass;
save_weights_toandload_weights_fromcall an internal build helper that runs a minimalcompute_with_internalusing dummy tensors, including a dummy teacher-forcing list of lengthdepthwith shapes(1, n2 // (2 ** (d + 1))).
Related¶
Q_Sea_Battle.pyr_measurement_layer_a.PyrMeasurementLayerAQ_Sea_Battle.pyr_combine_layer_a.PyrCombineLayerAQ_Sea_Battle.pr_assisted_replay.PRAssistedReplay
Changelog¶
- Not specified.