0
0
PytorchHow-ToBeginner · 3 min read

How to Use model.train() in PyTorch for Training Mode

In PyTorch, use model.train() to set your model to training mode. This activates layers like dropout and batch normalization to behave correctly during training. Call it before your training loop to ensure proper model behavior.
📐

Syntax

The model.train() method switches the model to training mode. This affects certain layers like dropout and batch normalization, which behave differently during training and evaluation.

  • model: Your neural network instance (usually a subclass of torch.nn.Module).
  • train(): A method that sets the model to training mode.
python
model.train()
💻

Example

This example shows a simple training loop where model.train() is called before training to enable training-specific behaviors like dropout.

python
import torch
import torch.nn as nn
import torch.optim as optim

# Define a simple model with dropout
class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 2)
        self.dropout = nn.Dropout(p=0.5)
    def forward(self, x):
        x = self.dropout(x)
        return self.linear(x)

# Create model, loss, optimizer
model = SimpleModel()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)

# Dummy input and target
inputs = torch.randn(5, 10)
targets = torch.tensor([0, 1, 0, 1, 0])

# Set model to training mode
model.train()

# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()

print(f"Loss: {loss.item():.4f}")
Output
Loss: 0.8321
⚠️

Common Pitfalls

One common mistake is forgetting to call model.train() before training, which leaves the model in evaluation mode by default. This causes layers like dropout and batch normalization to behave incorrectly, leading to poor training results.

Another mistake is calling model.eval() during training, which disables dropout and uses running statistics in batch normalization, not suitable for training.

python
import torch.nn as nn

model = nn.Dropout(p=0.5)

# Wrong: model is in eval mode during training
model.eval()
print(f"Output in eval mode: {model(torch.ones(5))}")  # No dropout applied

# Right: model in train mode during training
model.train()
print(f"Output in train mode: {model(torch.ones(5))}")  # Dropout applied (some zeros)
Output
Output in eval mode: tensor([1., 1., 1., 1., 1.]) Output in train mode: tensor([0., 2., 0., 2., 2.])
📊

Quick Reference

Remember these tips when using model.train():

  • Call model.train() before your training loop.
  • Use model.eval() when evaluating or testing your model.
  • Training mode enables dropout and batch normalization to update and behave correctly.
  • Evaluation mode disables dropout and uses fixed batch normalization statistics.

Key Takeaways

Always call model.train() before training to enable training-specific behaviors.
model.train() activates dropout and batch normalization layers for training.
Forgetting model.train() can cause poor training results due to wrong layer behavior.
Use model.eval() when testing to disable dropout and fix batch normalization.
Switch modes explicitly to avoid confusion between training and evaluation.