ludwig-ai--ludwig
593b94c120
pytest / Unit Tests (push) Has been cancelled
pytest / Integration (integration_tests_a) (push) Has been cancelled
pytest / Integration (integration_tests_b) (push) Has been cancelled
pytest / Integration (integration_tests_c) (push) Has been cancelled
pytest / Integration (integration_tests_d) (push) Has been cancelled
pytest / Integration (integration_tests_e) (push) Has been cancelled
pytest / Integration (integration_tests_f) (push) Has been cancelled
pytest / Integration (integration_tests_g) (push) Has been cancelled
pytest / Integration (integration_tests_h) (push) Has been cancelled
pytest / Integration (integration_tests_i) (push) Has been cancelled
pytest / Integration (integration_tests_j) (push) Has been cancelled
pytest / Distributed (distributed_a) (push) Has been cancelled
pytest / Distributed (distributed_b) (push) Has been cancelled
pytest / Distributed (distributed_c) (push) Has been cancelled
pytest / Distributed (distributed_d) (push) Has been cancelled
pytest / Distributed (distributed_e) (push) Has been cancelled
pytest / Distributed (distributed_f) (push) Has been cancelled
pytest / Minimal Install (push) Has been cancelled
pytest / Event File (push) Has been cancelled
pytest (slow) / py-slow (push) Has been cancelled
Publish JSON Schema / publish-schema (push) Has been cancelled
51 行
1.6 KiB
Python
51 行
1.6 KiB
Python
"""Feature transform protocol for Ludwig's lazy preprocessing pipeline.
|
|
|
|
All feature-specific data transforms should extend ``FeatureTransform`` so they
|
|
are composable, testable, and can be moved to GPU when that becomes beneficial.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from abc import abstractmethod
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
|
|
class FeatureTransform(nn.Module):
|
|
"""Base class for all per-sample feature transforms.
|
|
|
|
Subclasses implement ``forward(x: Tensor) -> Tensor``. They are
|
|
``nn.Module`` subclasses so they can be:
|
|
|
|
* Composed with ``nn.Sequential``
|
|
* Saved / loaded with ``torch.save`` / ``torch.load``
|
|
* Moved to GPU with ``.to(device)``
|
|
* JIT-compiled with ``torch.jit.script`` (where supported)
|
|
"""
|
|
|
|
@abstractmethod
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
raise NotImplementedError
|
|
|
|
|
|
class IdentityTransform(FeatureTransform):
|
|
"""Pass-through — useful as a placeholder or in tests."""
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
return x
|
|
|
|
|
|
class NormalizationTransform(FeatureTransform):
|
|
"""Subtract mean, divide by std. Both are registered as buffers so the
|
|
transform travels correctly through ``torch.save`` / ``.to(device)``."""
|
|
|
|
def __init__(self, mean: float, std: float) -> None:
|
|
super().__init__()
|
|
self.register_buffer("mean", torch.tensor(mean, dtype=torch.float32))
|
|
self.register_buffer("std", torch.tensor(std, dtype=torch.float32))
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
std = self.std.clamp(min=1e-8)
|
|
return (x - self.mean) / std
|