Git Product home page Git Product logo

Comments (9)

github-actions avatar github-actions commented on August 31, 2024

Hi! thanks for your contribution!, great first issue!

from torchmetrics.

SkafteNicki avatar SkafteNicki commented on August 31, 2024

Hi @SpirinEgor would it be possible for you to share some reproducible code?

from torchmetrics.

SpirinEgor avatar SpirinEgor commented on August 31, 2024

You can find metric code here. Since it is something like repo with further useful for me things, I add it to PyPI too. You can install it to your test dev and try inside the PyTorch Lightning module.
Btw, there are some simple tests. They work great without any failure.

from torchmetrics.

SkafteNicki avatar SkafteNicki commented on August 31, 2024

@SpirinEgor I must be doing something different from you, because if I call your metric inside training_step I cannot reproduce the error. Could you provide what your training_step looks like?

from torchmetrics.

SpirinEgor avatar SpirinEgor commented on August 31, 2024

Sure, here what I'm doing.

class MyModel(LightningModule):
    def __init__(self, config: DictConfig, vocabulary: Vocabulary):
        super().__init__()
        ...
        pad_idx = vocabulary.label_to_id[PAD]
        ignore_idx = [vocabulary.label_to_id[i] for i in [UNK, EOS, SOS] if i in vocabulary.label_to_id]
        self._train_metrics = SequentialF1Score(mask_after_pad=True, pad_idx=pad_idx, ignore_idx=ignore_idx)
        self._val_metrics = SequentialF1Score(
            mask_after_pad=True, pad_idx=pad_idx, ignore_idx=ignore_idx, compute_on_step=False
        )
        self._test_metrics = SequentialF1Score(
            mask_after_pad=True, pad_idx=pad_idx, ignore_idx=ignore_idx, compute_on_step=False
        )
        ....

    ...

    def training_step(self, batch: Tuple[torch.Tensor, dgl.DGLGraph], batch_idx: int) -> Dict:  # type: ignore
        labels, graph = batch
        # [seq length; batch size; vocab size]
        logits = self(graph, labels.shape[0], labels)
        loss = self._calculate_loss(logits, labels)
        prediction = logits.argmax(-1)

        batch_metrics: ClassificationMetrics = self._train_metrics(prediction, labels)  # **Error happens here**
        log = {
            "train/loss": loss,
            "train/f1": batch_metrics.f1_score,
            "train/precision": batch_metrics.precision,
            "train/recall": batch_metrics.recall,
        }
        self.log_dict(log)
        self.log("f1", batch_metrics.f1_score, prog_bar=True, logger=False)

        return {"loss": loss}

    def validation_step(self, batch: Tuple[torch.Tensor, dgl.DGLGraph], batch_idx: int) -> Dict:  # type: ignore
        labels, graph = batch
        # [seq length; batch size; vocab size]
        logits = self(graph, labels.shape[0], labels)
        loss = self._calculate_loss(logits, labels)
        prediction = logits.argmax(-1)

        self._val_metrics(prediction, labels)  # **And here**
        return {"loss": loss}

This is very strange for me too. Because when I wrote tests for metric class it was work just fine...
I also tried to use the last version of PyTorch-Lightning, but it doesn't change anything.

from torchmetrics.

SkafteNicki avatar SkafteNicki commented on August 31, 2024

Strange, still cannot reproduce your error. Both training and validation seems to run fine :/
Do you have any backward hooks registered in your code? because it should only get to that location in the pytorch code if you have.

from torchmetrics.

SpirinEgor avatar SpirinEgor commented on August 31, 2024

Oh, it's become really tough...
I didn't define any hooks. Any chancing Lightning can do it? Also, I used DGL to train on graphs, maybe it defines hooks.

from torchmetrics.

SpirinEgor avatar SpirinEgor commented on August 31, 2024

I found the reason for all my troubles. In the main train function, I use WandbLogger and I also use

wandb_logger.watch(model)

This step adds backward hooks to track gradients, their norms, etc. After I disable it metric start working as I expected.

Anyway, big thanks for your response! The library is great, hope to see more cool features.

from torchmetrics.

SkafteNicki avatar SkafteNicki commented on August 31, 2024

Thanks for letting me know, and glad that you like the library!

from torchmetrics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.