ruvnet/RuView · error · ValueError

Both teacher and student features must be extracted first

Error message

Both teacher and student features must be extracted first

What it means

TransferLearningSystem.compute_transfer_loss() sums MSE over FPN levels P2–P5 using self.teacher_features and self.student_features, which start as empty dicts in __init__ and are populated only by extract_teacher_features(image_input) and extract_student_features(wifi_features). If either dict is still empty the method raises ValueError before touching level keys.

Source

Thrown at references/script_7.py:65

        features['P4'] = np.random.rand(1, 256, 45, 80)
        features['P5'] = np.random.rand(1, 256, 23, 40)
        
        self.student_features = features
        return features
    
    def compute_mse_loss(self, teacher_feature, student_feature):
        """
        Compute Mean Squared Error between teacher and student features
        """
        return np.mean((teacher_feature - student_feature) ** 2)
    
    def compute_transfer_loss(self):
        """
        Compute transfer learning loss as sum of MSE at different levels
        L_tr = MSE(P2, P2*) + MSE(P3, P3*) + MSE(P4, P4*) + MSE(P5, P5*)
        """
        if not self.teacher_features or not self.student_features:
            raise ValueError("Both teacher and student features must be extracted first")
        
        total_loss = 0.0
        feature_losses = {}
        
        for level in ['P2', 'P3', 'P4', 'P5']:
            teacher_feat = self.teacher_features[level]
            student_feat = self.student_features[level]
            
            level_loss = self.compute_mse_loss(teacher_feat, student_feat)
            feature_losses[level] = level_loss
            total_loss += level_loss
        
        return total_loss, feature_losses
    
    def adapt_features(self, student_features, learning_rate=0.001):
        """
        Adapt student features to be more similar to teacher features
        """

View on GitHub (pinned to 4685618388)

Solutions

  1. Call tl.extract_teacher_features(image_data) and tl.extract_student_features(wifi_data) before compute_transfer_loss()
  2. Guard the call site: only compute the loss when both dicts are non-empty
  3. Verify dicts contain P2–P5 keys — extraction populates all four levels

Example fix

# before
tl = TransferLearningSystem()
total, per_level = tl.compute_transfer_loss()  # ValueError

# after
tl = TransferLearningSystem()
tl.extract_teacher_features(image_data)
tl.extract_student_features(wifi_data)
total, per_level = tl.compute_transfer_loss()
Defensive patterns

Strategy: validation

Validate before calling

FEATURE_LEVELS = {"P2", "P3", "P4", "P5"}

def transfer_loss_ready(tl) -> bool:
    return (
        bool(tl.teacher_features)
        and bool(tl.student_features)
        and FEATURE_LEVELS <= set(tl.teacher_features)
        and FEATURE_LEVELS <= set(tl.student_features)
    )

assert transfer_loss_ready(tl), "extract teacher and student features first"

Try / catch

try:
    total, per_level = tl.compute_transfer_loss()
except ValueError as e:
    raise RuntimeError(
        "run extract_teacher_features() and extract_student_features() "
        "before compute_transfer_loss()"
    ) from e

Prevention

When it happens

Trigger: Calling compute_transfer_loss() (directly or via TrainingPipeline.train_step) before both extract_teacher_features() and extract_student_features() ran — e.g. a reordered training loop, hooks never firing, or an empty first batch.

Common situations: Refactoring the train step and dropping the extractor calls; conditional code paths that skip extraction on the first iteration; adapting the reference script into a real pipeline.

Related errors


AI-assisted analysis of ruvnet/RuView@4685618388 (2026-08-16). Data as JSON: /api/errors/084fd271c7b327d7. Report an issue: GitHub.