moment-timeseries-foundation-model / moment-research

Research Code for MOMENT
MIT License
32 stars 9 forks source link

Why not labels attend in supervised anomaly detection? #1

Closed XiaoJia849 closed 2 months ago

XiaoJia849 commented 3 months ago

may be this is a stupid question, but i really want to know, why the batch_x.labels don`t attend the loss calculation ? thanks!

here is the code in /moment/tasks/anomaly_detection_finetune.py

            for batch_x in tqdm(
                self.train_dataloader, total=len(self.train_dataloader)
            ):
                self.optimizer.zero_grad(set_to_none=True)
                timeseries = batch_x.timeseries.float().to(self.device)
                input_mask = batch_x.input_mask.long().to(self.device)

                if not self.args.set_input_mask:
                    input_mask = torch.ones_like(input_mask)

                with torch.autocast(
                    device_type="cuda",
                    dtype=dtype_map(self.args.torch_dtype),
                    enabled=self.args.use_amp,
                ):
                    outputs = self.model(
                        x_enc=timeseries,
                        input_mask=input_mask,
                        mask=None,
                        anomaly_criterion=self.args.anomaly_criterion,
                    )

                loss = self.criterion(outputs.reconstruction, timeseries)

                self.logger.log(
                    {
                        "step_train_loss": loss.item(),
                        "learning_rate": self.optimizer.param_groups[0]["lr"],
                    }
                )
mononitogoswami commented 2 months ago

Hi, Thanks for your interest in MOMENT and this is certainly not a silly question!

During fine-tuning, we fine-tune MOMENT on an unsupervised reconstruction task i.e. we fine-tune the pre-training (reconstruction) head on in-domain time series, on a time series reconstruction task without any labels. The assumption is that a model that can reconstruct input time series well, can also identify anomalies, i.e. time series sub-sequences which the model is unable to predict well.

We only use the labels for evaluation.

Let us know if you have any more questions.

Best, Mononito