Skip to content

Targets

Pack, unpack, and install hypernetwork-generated weights for a target layer (fully-connected, convolutional, attention Q/K, and Sigma-Pi conv).

polyweave.targets

Target specs: pack / unpack / install generated weights for a target layer.

AttentionQKTargetSpec

AttentionQKTargetSpec(d_model: int, n_layers: int)

Bases: TargetSpec

Query/key projections for n_layers attention blocks of width d_model.

Source code in polyweave/targets/attention.py
def __init__(self, d_model: int, n_layers: int) -> None:
    self.d_model = d_model
    self.n_layers = n_layers
    self._per_layer = 2 * (d_model * d_model + d_model)  # Wq,bq,Wk,bk

TargetSpec

Bases: ABC

Abstract base for a generated-weight target layer.

num_params abstractmethod property

num_params: int

Total number of scalar parameters this target requires.

pack abstractmethod

pack(weights: Any) -> torch.Tensor

Flatten structured weights into a 1-D tensor of length num_params.

Source code in polyweave/targets/base.py
@abstractmethod
def pack(self, weights: Any) -> torch.Tensor:
    """Flatten structured ``weights`` into a 1-D tensor of length ``num_params``."""

unpack abstractmethod

unpack(flat: Tensor) -> Any

Inverse of :meth:pack: reshape a 1-D tensor into structured weights.

Source code in polyweave/targets/base.py
@abstractmethod
def unpack(self, flat: torch.Tensor) -> Any:
    """Inverse of :meth:`pack`: reshape a 1-D tensor into structured weights."""

install abstractmethod

install(into: Any, weights: Any) -> None

Copy structured weights into a concrete torch object (in place).

Source code in polyweave/targets/base.py
@abstractmethod
def install(self, into: Any, weights: Any) -> None:
    """Copy structured ``weights`` into a concrete torch object (in place)."""

extract abstractmethod

extract(frm: Any) -> Any

Read structured weights out of a concrete torch object (detached clones).

Source code in polyweave/targets/base.py
@abstractmethod
def extract(self, frm: Any) -> Any:
    """Read structured weights out of a concrete torch object (detached clones)."""

Conv2dTargetSpec

Conv2dTargetSpec(out_channels: int, in_channels: int, kernel_size: int)

Bases: TargetSpec

A 2-D convolution kernel + bias.

Matches the CIFAR conv1 target: Conv2d(in_ch, out_ch, kernel). The mapping from class-conditional image statistics to useful conv1 filters is substantially more nonlinear than the FC mapping, and the pi branch grows accordingly.

Source code in polyweave/targets/conv.py
def __init__(self, out_channels: int, in_channels: int, kernel_size: int) -> None:
    self.out_channels = out_channels
    self.in_channels = in_channels
    self.kernel_size = kernel_size
    self._w_shape = (out_channels, in_channels, kernel_size, kernel_size)
    self._weight_n = out_channels * in_channels * kernel_size * kernel_size

FCTargetSpec

FCTargetSpec(in_features: int, out_features: int)

Bases: TargetSpec

A linear head y = W x + b with W in R^{out x in}, b in R^{out}.

In the paper this is the few-shot classification head whose optimal solution is (to first order) the class-centroid NCC rule — the regime where the pi branch stays dormant.

Source code in polyweave/targets/fc.py
def __init__(self, in_features: int, out_features: int) -> None:
    self.in_features = in_features
    self.out_features = out_features
    self._weight_n = out_features * in_features

SigmaPiConvTargetSpec

SigmaPiConvTargetSpec(channels: int, kernel_size: int = 3)

Bases: TargetSpec

The two conv weight matrices (additive + log-space) of a Sigma-Pi block.

Parameters:

Name Type Description Default
channels int

input == output channels of the target ConvSigmaPi2d.

required
kernel_size int

conv kernel size for both branches (default 3).

3
Source code in polyweave/targets/sigmapi_conv.py
def __init__(self, channels: int, kernel_size: int = 3) -> None:
    self.channels = channels
    self.kernel_size = kernel_size
    self._w_shape = (channels, channels, kernel_size, kernel_size)
    self._weight_n = channels * channels * kernel_size * kernel_size