LinCombineLayerA¶
Role: Learnable TensorFlow Keras layer that maps measurement outcomes to communication logits.
Location: Q_Sea_Battle.lin_combine_layer_a.LinCombineLayerA
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| comms_size | int, constraint: \(m \ge 1\), shape: scalar | Number of communication channels (\(m\)). Stored as self.comms_size after int(...). |
| hidden_units | int or Sequence[int], constraint: each value \(u \ge 1\), shape: scalar or \((L,)\) | Hidden layer widths for an MLP; an int creates a single hidden layer, a sequence creates a stack of Dense-ReLU layers. Normalized to tuple[int, ...] and stored as self.hidden_units. |
| name | str or None, constraint: if None then defaults to "LinCombineLayerA", shape: scalar |
Keras layer name passed to tf.keras.layers.Layer.__init__. |
| **kwargs | dict[str, Any], constraint: must be accepted by tf.keras.layers.Layer, shape: mapping |
Forwarded to the Keras base layer constructor. |
Preconditions
comms_sizemust be convertible toint.hidden_unitsmust be anintor acollections.abc.Sequenceof values convertible toint.
Postconditions
self.comms_size: intis set.self.hidden_units: tuple[int, ...]is set.self._mlp: list[tf.keras.layers.Layer]is created as a list oftf.keras.layers.Dense(..., activation="relu")layers.self._out: tf.keras.layers.Denseis created withunits=self.comms_sizeandactivation=None.
Errors
- Raises
TypeErrorifhidden_unitsis not anintand not aSequence. - Raises
ValueErrorif any element ofhidden_unitscannot be converted toint(or ifcomms_sizecannot be converted toint), as raised byint(...)conversion. - Any additional errors may be raised by
tf.keras.layers.Layer.__init__when invalidname/kwargsare provided.
Example
import tensorflow as tf
from Q_Sea_Battle.lin_combine_layer_a import LinCombineLayerA
layer = LinCombineLayerA(comms_size=8, hidden_units=(64, 64))
outcomes = tf.random.uniform(shape=(32, 100), dtype=tf.float32) # (B, n2)
logits = layer(outcomes, training=True) # (B, m)
Public Methods¶
call¶
Signature: call(self, outcomes: tf.Tensor, training: bool = False) -> tf.Tensor
Parameters
- outcomes: tf.Tensor, dtype: not specified (converted via
tf.convert_to_tensor), shape \((B, n2)\) or \((n2,)\). - training: bool, constraint: boolean, shape: scalar.
Returns
- tf.Tensor, dtype: not specified, shape \((B, m)\) if input rank is 2, else shape \((m,)\) if input rank is 1.
Behavior
- Converts
outcomesto a tensor withtf.convert_to_tensor. - If
outcomeshas rank 1 (shape \((n2,)\)), expands to shape \((1, n2)\), runs the MLP and output layer, then squeezes axis 0 to return shape \((m,)\). - If
outcomeshas rank 2 (shape \((B, n2)\)), returns logits of shape \((B, m)\). - Applies each hidden Dense layer in
self._mlpwithactivation="relu", then appliesself._outDense withactivation=None(logits).
Errors
- May raise TensorFlow/Keras shape or rank errors if
outcomeshas rank other than 1 or 2, or if Dense layers cannot be applied to the provided shape/dtype.
Data & State¶
- self.comms_size: int, constraint: not validated in code beyond
int(...), shape: scalar. - self.hidden_units: tuple[int, ...], constraint: elements are
intafter normalization, shape: \((L,)\). - self._mlp: list[tf.keras.layers.Layer], constraint: list length \(L = \text{len}(\text{self.hidden_units})\), shape: list.
- self._out: tf.keras.layers.Dense, constraint: units \(= m\), shape: layer object.
Planned (design-spec)¶
- Not specified.
Deviations¶
- Not specified.
Notes for Contributors¶
_normalize_hidden_units(hidden_units)is a private helper that normalizeshidden_unitstotuple[int, ...]; keep it consistent with any future serialization/config logic if added.- The
callmethod explicitly supports rank-1 input by expanding and later squeezing; changes to rank handling should preserve the documented input/output shape conventions \((n2,) \leftrightarrow (m,)\) and \((B, n2) \leftrightarrow (B, m)\).
Related¶
- TensorFlow Keras base class:
tf.keras.layers.Layer - Dense layers used internally:
tf.keras.layers.Dense
Changelog¶
- 0.1: Initial implementation of
LinCombineLayerAwith configurable hidden MLP and linear output logits.