import math
from typing import Annotated
from qewton.graphs.control_nodes.data_processing_node import DataProcessingNode
from qewton.data.dataloaders.base import DataNode
from qewton.backends.base import TensorType, ComputingBackend
from qewton.backends import DEFAULT_DL_BACKEND
from qewton.optim.base import EvaluationPhase
from qewton.optim.parameters.hyperparameter_base import HyperParameter
from qewton.graphs.nodes import NodeState
from qewton.graphs.graphs import Graph
from qewton.config.axes import EllipsisAxes, BatchAxes, AxesDim, FeatureAxes
from qewton.config.data_configurations import DataConfiguration
[docs]
class PCANode(DataProcessingNode[TensorType]):
"""A node that performs a principal component analysis (PCA) on its
input data.
To construct the PCA, in the setup phase all training data is collect
by (partially) evaluating the graph it belongs to.
Args:
n (int | HyperParameter): The number of principal components to
keep.
data_source_node (DataNode): The original source of data. Does
not have to be node which is directly connected to this PCANode, only
the original data loader providing the original data set.
scale (bool | HyperParameter, optional): If the input
data after the representation in the PCA-basis should be
scaled by the principal components. Defaults to True.
name (str | None, optional): Defaults to "PCA Node".
backend (type[ComputingBackend[TensorType]], optional):
Defaults to DEFAULT_DL_BACKEND.
"""
def __init__(
self,
n: int | HyperParameter,
data_source_node: DataNode,
scale: bool | HyperParameter = True,
name: str | None = "PCA Node",
backend: type[ComputingBackend[TensorType]] = DEFAULT_DL_BACKEND,
) -> None:
self.n = HyperParameter.from_value(n, "PCA Components of " + self.name)
self.scale = HyperParameter.from_value(
scale, "Scale by Principal Comp. " + self.name
)
# Data config. properties:
self.batch_axes = BatchAxes(AxesDim(None))
self.flattened_dim = AxesDim(None)
super().__init__(data_source_node, name, backend)
self.backend: type[ComputingBackend[TensorType]] = backend
self.data_source_node: DataNode = data_source_node
self.pca_u: TensorType
self.pca_s: TensorType
self.pca_v: TensorType
self.original_shape: tuple[int, ...]
# Ports for the PCA coefficients
self.input = self.input_ports[0]
self.output = self.output_ports[0]
self.port_u = self.output_ports[1]
self.port_u.name = "U" + self.name
self.port_s = self.output_ports[2]
self.port_s.name = "S" + self.name
self.port_v = self.output_ports[3]
self.port_v.name = "V" + self.name
[docs]
def refresh_port_data_configurations(self) -> None:
"""Refresh static data configurations that depend on hyperparameters."""
self.input.update_static_data_configuration(self.x_data_config())
self.output.update_static_data_configuration(self.out_data_config())
self.port_u.update_static_data_configuration(self.out_u_config())
self.port_s.update_static_data_configuration(self.out_s_config())
self.port_v.update_static_data_configuration(self.out_v_config())
[docs]
def reset(self):
self._state = NodeState.UNINITIALIZED
return super().reset()
[docs]
def build(self, graph: Graph) -> None:
self.refresh_port_data_configurations()
# First collect all data:
total_data = []
for _ in range(self.data_source_node.training_batches):
in_edge = graph.run_to(last_node=self, mode=EvaluationPhase.TRAIN)
total_data.append(graph.value_of(in_edge[self.input_ports[0]].from_port))
self.fit(total_data)
[docs]
def fit(self, data_batch):
# The batch is assumed to be on axis = 0
self.original_shape = tuple(data_batch[0].shape)
total_data = self.backend.math.concatenate(data_batch, axis=0)
self.flattened_dim.update_size(total_data.shape[-1])
# Now apply the PCA, for this flatten everything except the first
# axis:
total_data = self.backend.math.flatten(total_data, 1)
self.pca_u, self.pca_s, self.pca_v = self.backend.linalg.pca(
total_data, q=self.n.value
)
self.set_state(NodeState.INITIALIZED)
[docs]
def to(self, device):
if self.state != NodeState.UNINITIALIZED:
self.pca_u = self.backend.to(self.pca_u, device=device)
self.pca_s = self.backend.to(self.pca_s, device=device)
self.pca_v = self.backend.to(self.pca_v, device=device)
return super().to(device)
@property
def memory_consumption(self) -> int:
total_bytes = 0
if self.state != NodeState.UNINITIALIZED:
total_bytes += self.backend.memory_consumption(self.pca_u)
total_bytes += self.backend.memory_consumption(self.pca_s)
total_bytes += self.backend.memory_consumption(self.pca_v)
return total_bytes
[docs]
def x_data_config(self):
return DataConfiguration(self.batch_axes, EllipsisAxes())
[docs]
def out_data_config(self):
return DataConfiguration(
self.batch_axes, FeatureAxes(shape=(self.n.current_value,))
)
[docs]
def out_u_config(self):
return DataConfiguration(
self.batch_axes, FeatureAxes(shape=(self.n.current_value,))
)
[docs]
def out_s_config(self):
return DataConfiguration(FeatureAxes(shape=(self.n.current_value,)))
[docs]
def out_v_config(self):
return DataConfiguration(
self.batch_axes, FeatureAxes(shape=(self.flattened_dim,))
)
[docs]
def forward(self, x: Annotated[TensorType, x_data_config]) -> tuple[
Annotated[TensorType, out_data_config],
Annotated[TensorType, out_u_config],
Annotated[TensorType, out_s_config],
Annotated[TensorType, out_v_config],
]:
flatt_x = self.backend.math.flatten(x, 1)
pca_coefficients = self.backend.math.matmul(flatt_x, self.pca_v)
if self.scale.current_value:
scaling = self.pca_s * math.sqrt(1 / (len(self.pca_u) - 1))
pca_coefficients /= scaling
return pca_coefficients, self.pca_u, self.pca_s, self.pca_v
[docs]
class InversePCANode(DataProcessingNode[TensorType]):
"""Applies a inverse PCA, given some coefficients it uses the
PCA-basis to map back to the original one.
Args:
pca_node (PCANode[TensorType]): The PCANode that yields the PCA basis
that is required for the inverse transformation.
name (str | None, optional): Defaults to "Inverse PCA Node"
"""
def __init__(
self,
pca_node: PCANode[TensorType],
name: str | None = "Inverse PCA Node",
) -> None:
self.batch_axes = BatchAxes(AxesDim(None))
super().__init__(pca_node, name, backend=pca_node.backend)
self.data_source_node: PCANode = pca_node
self.backend: type[ComputingBackend[TensorType]] = pca_node.backend
[docs]
def refresh_port_data_configurations(self) -> None:
"""Refresh static data configurations that depend on the wrapped PCA node."""
self.input_ports[0].update_static_data_configuration(self.x_data_config())
self.output_ports[0].update_static_data_configuration(self.out_data_config())
[docs]
def build(self, graph: Graph) -> None:
if self.data_source_node.state == NodeState.UNINITIALIZED:
raise RuntimeError(
f"PCA Node {self.data_source_node} has not been setup yet!"
)
self.refresh_port_data_configurations()
[docs]
def x_data_config(self):
return DataConfiguration(
self.batch_axes,
FeatureAxes(shape=(self.data_source_node.n.current_value,)),
)
[docs]
def out_data_config(self):
# TODO: Here its hard to find the shape, its only known later, once
# we pass this through.
# Maybe one could interfere this from the dataloader of the pca node?
return DataConfiguration(self.batch_axes, EllipsisAxes())
[docs]
def forward(
self, pca_coefficients: Annotated[TensorType, x_data_config]
) -> Annotated[TensorType, out_data_config]:
# Do inverse scaling
if self.data_source_node.scale.current_value:
scaling = self.data_source_node.pca_s * math.sqrt(
1 / (len(self.data_source_node.pca_u) - 1)
)
pca_coefficients = scaling * pca_coefficients
# Go back to original dimension
flatt_x = self.backend.math.matmul(
pca_coefficients, self.backend.math.transpose(self.data_source_node.pca_v)
)
original_shape = (len(flatt_x),) + self.data_source_node.original_shape[1:]
return self.backend.math.reshape(flatt_x, shape=original_shape)