LinInternalModelA¶
Role: Depth-1 (fixed) trainable linear internal model mapping field logits to communication logits while exposing measurement and shared-resource outcome logits.
Location: Q_Sea_Battle.lin_internal_model_a.LinInternalModelA
Derived constraints¶
- Symbols:
n2= number of field bits (flattened field size);m= number of communication bits (comms size);B= batch size. - Fixed depth:
depth = 1. - Input contract: field logits are
tf.Tensor, dtype float32, shape (B, n2). - Output contract: communication logits are
tf.Tensor, dtype float32, shape (B, m)with constraint \(m \ge 1\). - Internal exposure contract:
compute_with_internal(...)returns(comm_logits, meas_list, out_list)wheremeas_listandout_listare Pythonlistobjects of length 1.
Constructor¶
| Parameter | Type | Description |
|---|---|---|
game_layout |
Any, unconstrained, shape N/A |
Game layout/config object used to infer n2 and comms size m (stored as self.M). |
sr_mode |
str, default "replay", shape N/A |
Shared resource mode; expected values are those supported by PRAssistedReplay (examples mentioned: "replay", "stochastic"). |
p_rule |
float, default 1.0, shape N/A |
Probability of using the rule-based component in the PR-assisted shared resource. |
beta |
float, default 10.0, shape N/A |
Temperature/sharpness parameter used by the PR-assisted shared resource. |
alpha |
float, default 5.0, shape N/A |
Mixing/strength parameter used by the PR-assisted shared resource. |
seed |
int \| None, default None, shape N/A |
Optional seed forwarded to the shared-resource layer. |
measure_layers |
Optional[Sequence[tf.keras.layers.Layer]], default None, shape N/A |
Optional custom measurement layer sequence; if provided, must have length depth (=1). |
combine_layers |
Optional[Sequence[tf.keras.layers.Layer]], default None, shape N/A |
Optional custom combine layer sequence; if provided, must have length depth (=1). |
hidden_units_measure |
int, default 64, shape N/A |
Hidden units used when constructing the default measurement layer (LinMeasurementLayerA). |
hidden_units_combine |
int, default 64, shape N/A |
Hidden units used when constructing the default combine layer (LinCombineLayerA). |
name |
Optional[str], default None, shape N/A |
Optional Keras model name passed to tf.keras.Model. |
Preconditions
_infer_n2_and_m(game_layout)must return(n2, m)assignable toself.n2andself.M.- Constraint:
m >= 1. - If
measure_layersis provided, it must satisfylen(measure_layers) == 1. - If
combine_layersis provided, it must satisfylen(combine_layers) == 1.
Postconditions
self.depthis set to1.self.n2andself.Mare set from_infer_n2_and_m(game_layout).self.measure_layers,self.combine_layers,self.sr_layersexist and are Pythonlistobjects of length 1.- Convenience aliases exist:
self.measure_layer == self.measure_layers[0],self.combine_layer == self.combine_layers[0],self.sr_layer == self.sr_layers[0].
Errors
ValueErrorif inferredm < 1.ValueErrorifmeasure_layersis provided andlen(measure_layers) != 1.ValueErrorifcombine_layersis provided andlen(combine_layers) != 1.
Example
import tensorflow as tf
from Q_Sea_Battle.lin_internal_model_a import LinInternalModelA
game_layout = ... # object understood by _infer_n2_and_m
model = LinInternalModelA(game_layout, sr_mode="replay", p_rule=1.0, beta=10.0, alpha=5.0)
x = tf.zeros((4, model.n2), dtype=tf.float32)
y = model(x, training=False)
assert y.shape[-1] == model.M
Public Methods¶
set_alpha¶
set_alpha(alpha: float) -> None
- Purpose: Set the PR-assisted shared-resource
alphaparameter for all SR layers.
Arguments
alpha:float, unconstrained, shape N/A.
Returns
None, shape N/A.
Errors
AttributeErrorif any SR layer lacks aset_alpha()method.
set_p_rule¶
set_p_rule(p_rule: float) -> None
- Purpose: Set the PR-assisted shared-resource
p_ruleparameter for all SR layers.
Arguments
p_rule:float, unconstrained, shape N/A.
Returns
None, shape N/A.
Errors
AttributeErrorif any SR layer lacks aset_p_rule()method.
set_beta¶
set_beta(beta: float) -> None
- Purpose: Set the PR-assisted shared-resource
betaparameter for all SR layers.
Arguments
beta:float, unconstrained, shape N/A.
Returns
None, shape N/A.
Errors
AttributeErrorif any SR layer lacks aset_beta()method.
set_sr_mode¶
set_sr_mode(sr_mode: str) -> None
- Purpose: Set the SR mode (e.g.,
replayorstochastic) for all SR layers.
Arguments
sr_mode:str, expected to be supported by the SR layer, shape N/A.
Returns
None, shape N/A.
Errors
AttributeErrorif any SR layer lacks aset_sr_mode()method.
call¶
call(field_scaled: tf.Tensor, training: bool = False, **kwargs: Any) -> tf.Tensor
- Purpose: Keras forward pass; delegates to
compute_with_internal(...)and returns only communication logits.
Arguments
field_scaled:tf.Tensor, dtype float32, shape (B, n2); treated as field logits (name reflects upstream code).training:bool, defaultFalse, shape N/A.**kwargs:Any, unconstrained, shape N/A; unused extra Keras call kwargs.
Returns
- Communication logits:
tf.Tensor, dtype float32, shape (B, m).
compute_with_internal¶
compute_with_internal(field_logits: tf.Tensor, replay_out_a_logits_list: Optional[Sequence[tf.Tensor]] = None, harden_between_levels: bool = False, beta_for_hardening: float = 10.0, training: bool = False) -> Tuple[tf.Tensor, List[tf.Tensor], List[tf.Tensor]]
- Purpose: Compute communication logits and expose internal measurement and shared-resource outcome logits, returning per-level lists of length 1 (depth fixed to 1).
Arguments
field_logits:tf.Tensor, dtype float32 (via conversion), shape (B, n2); must be rank-2 and trailing dimension must matchself.n2when statically known.replay_out_a_logits_list:Optional[Sequence[tf.Tensor]], defaultNone, shape N/A; if provided must be alistortupleof length 1, and element 0 is converted totf.Tensor, dtype float32, shape (B, k)with runtime constraint \(k = \text{measurement\_size}\) (enforced viatf.debugging.assert_equalon trailing dimension vs measurement logits).harden_between_levels:bool, defaultFalse, shape N/A; ifTrue, replaces input logits with \(\pm \text{beta\_for\_hardening}\) based on sign before measurement (API compatibility behavior).beta_for_hardening:float, default10.0, shape N/A; magnitude used when hardening.training:bool, defaultFalse, shape N/A.
Returns
comm_logits:tf.Tensor, dtype float32, shape (B, m).meas_list:list[tf.Tensor], length 1; element 0 is measurement logitstf.Tensor, dtype float32, shape (B, k)wherekis measurement size produced by the measurement layer (not specified by this module).out_list:list[tf.Tensor], length 1; element 0 is SR outcome logitstf.Tensor, dtype float32, shape (B, k)(same trailing dimension as measurement logits at runtime in replay mode).
Errors
ValueErroriffield_logitsis not rank-2.ValueErroriffield_logitshas statically-known trailing dimension and it does not equalself.n2.TypeErrorifreplay_out_a_logits_listis provided and is not alistortuple.ValueErrorifreplay_out_a_logits_listis provided andlen(...) != 1.
Training kwarg compatibility
The measurement and combine layers are called with training=training in a try block and retried without training on TypeError, to support layers that do not accept the training keyword argument.
save_weights_to¶
save_weights_to(path: str) -> None
- Purpose: Ensure variables exist (via
_ensure_built()) then save Keras model weights.
Arguments
path:str, unconstrained, shape N/A.
Returns
None, shape N/A.
load_weights_from¶
load_weights_from(path: str) -> None
- Purpose: Ensure variables exist (via
_ensure_built()) then load Keras model weights.
Arguments
path:str, unconstrained, shape N/A.
Returns
None, shape N/A.
Data & State¶
n2:int, constraint not specified; number of field bits inferred fromgame_layout, shape N/A.M:int, constraint \(m \ge 1\); number of communication bits inferred fromgame_layout, shape N/A.depth:int, constant value1, shape N/A.measure_layers:list[tf.keras.layers.Layer], length 1, shape N/A.combine_layers:list[tf.keras.layers.Layer], length 1, shape N/A.sr_layers:list[PRAssistedReplay], length 1, shape N/A.measure_layer:tf.keras.layers.Layer, alias ofmeasure_layers[0], shape N/A.combine_layer:tf.keras.layers.Layer, alias ofcombine_layers[0], shape N/A.sr_layer:PRAssistedReplay, alias ofsr_layers[0], shape N/A.
Planned (design-spec)¶
- Not specified.
Deviations¶
- Not specified.
Notes for Contributors¶
- The class fixes
depth = 1and enforces that any providedmeasure_layersorcombine_layerssequences match this length. _ensure_built()forces variable creation by runningcompute_with_internal(...)with a dummy replay outcome list whose element has shape(1, n2); if the measurement layer output size differs fromn2, the replay outcome shape may mismatch and trigger the runtime assertion in SR replay mode.
Related¶
Q_Sea_Battle.lin_measurement_layer_a.LinMeasurementLayerAQ_Sea_Battle.lin_combine_layer_a.LinCombineLayerAQ_Sea_Battle.pr_assisted_replay.PRAssistedReplayQ_Sea_Battle.pyr_internal_model_a._infer_n2_and_m
Changelog¶
- Not specified.