All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S6979.html Maven / Gradle / Ivy

There is a newer version: 4.23.0.17664
Show newest version

This rule raises when a torch.autograd.Variable is instantiated.

Why is this an issue?

The Pytorch Variable API has been deprecated. The behavior of Variables is now provided by the Pytorch tensors and can be controlled with the requires_grad parameter.

The Variable API now returns tensors anyway, so there should not be any breaking changes.

How to fix it

Replace the call to torch.autograd.Variable with a call to torch.tensor and set the requires_grad attribute to True if needed.

Code examples

Noncompliant code example

import torch

x = torch.autograd.Variable(torch.tensor([1.0]), requires_grad=True) # Noncompliant
x2 = torch.autograd.Variable(torch.tensor([1.0])) # Noncompliant

Compliant solution

import torch

x = torch.tensor([1.0], requires_grad=True)
x2 = torch.tensor([1.0])

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy