AIToday
AI Coding AssistantsQiita 機械学習Published: Nov 13, 2026, 16:00 JST

PyTorch 2.12.0 test: inference_mode alone leaves Dropout on, outputs 0.0 and 2.0

Send on LINE
PyTorch 2.12.0 test: inference_mode alone leaves Dropout on, outputs 0.0 and 2.0

3 Key Points

  1. What happened

    The tester ran four combinations on PyTorch 2.12.0 with a Linear layer plus Dropout(p=0.5), and rows using inference_mode() but not eval() still printed both 0.0 and 2.0.

  2. Why it matters

    A gradient-disabling setting on its own leaves training-time randomness in the output, so checking requires_grad alone can miss a misconfiguration.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, every morning.

Context & Analysis

The test was set up deliberately: all inputs were 1, the Linear weight was the identity matrix, and the only nonlinearity was Dropout with p=0.5. That makes the output values a direct readout of whether Dropout was active. In the training rows, outputs contained 0.0 and 2.0; in the evaluation rows, they were 1.0 throughout.

The distinction the write-up draws is between two separate switches. eval() calls train(False), which recursively sets training=False on child modules, and Dropout reads that flag. inference_mode() targets autograd bookkeeping instead, and the same PyTorch documentation carries the note that it does not automatically set the model to evaluation mode. Because the switches are independent, a test that only checks requires_grad can pass while Dropout is still running.

The suggested practice is to set evaluation mode first, disable gradient recording, and include layers whose behavior changes at evaluation time in output tests, rerunning them when the model or wrapper is updated.

FAQ
What is the difference between eval() and inference_mode()?
eval() sets the model's training flag to False recursively, stopping Dropout. inference_mode() reduces autograd recording overhead but does not change the training flag.
What does Dropout do during training versus evaluation?
During training it zeroes elements with probability 0.5 and scales the rest by 1/(1-0.5), giving 0.0 and 2.0 in this test. During evaluation it passes the input through unchanged, leaving 1.0.
Qiita 機械学習Read Original Article
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articlepstack: AI adversarial review fails; spend tokens on verification