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

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

There is a newer version: 4.23.0.17664
Show newest version

This rule raises an issue when a PyTorch model state is loaded and torch.nn.Module.eval() or torch.nn.Module.train() is not called.

Why is this an issue?

When using PyTorch it is common practice to load and save a model’s state from/to a .pth file. Doing so allows, for example, to instantiate an untrained model and load learned parameters coming from another pre-trained model. Once the learned parameters are loaded to the model it is important, before inferencing, to clearly state the intention by calling torch.nn.Module.eval() method to set the model in evaluation mode or calling torch.nn.Module.train() to indicate the training will resume. Failing to call torch.nn.Module.eval() would leave the model in training mode which may not be the intention.

How to fix it

Call the torch.nn.Module.eval() or torch.nn.Module.train() method on the model.

Code examples

Noncompliant code example

import torch
import torchvision.models as models

model = models.vgg16()
model.load_state_dict(torch.load('model_weights.pth')) # Noncompliant: model.train() or model.eval() was not called.

Compliant solution

import torch
import torchvision.models as models

model = models.vgg16()
model.load_state_dict(torch.load('model_weights.pth'))
model.eval()

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy