UFR-Fing / src /models /sensor_discriminator.py
anbinh39's picture
Add files using upload-large-folder tool
dadf189 verified
Raw
History Blame Contribute Delete
808 Bytes
from __future__ import annotations
import torch
import torch.nn as nn
from .grad_reverse import GradientReversalLayer
class SensorDiscriminator(nn.Module):
"""Adversarial sensor classifier attached to shared features."""
def __init__(self, in_dim: int, num_sensors: int, lambda_grl: float = 0.3):
super().__init__()
self.grl = GradientReversalLayer(lambda_=lambda_grl)
self.mlp = nn.Sequential(
nn.Linear(in_dim, 64),
nn.ReLU(inplace=True),
nn.Linear(64, num_sensors),
)
def set_grl_lambda(self, value: float) -> None:
"""Update GRL lambda dynamically (DANN warm-up)."""
self.grl.set_lambda(value)
def forward(self, features: torch.Tensor) -> torch.Tensor:
return self.mlp(self.grl(features))