Skip to content

added savings-model-states.md #5872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Saving Model States in PyTorch

Saving model states in PyTorch is a crucial concept for preserving the progress of your model during training. It allows you to save a model's parameters, optimizer states, and other important metadata so that you can resume training or use the model for inference later. This is especially useful in scenarios where training is computationally expensive, or when you need to deploy your model in a production environment.

---

## Syntax

PyTorch provides the `torch.save` and `torch.load` functions to handle saving and loading model states. Typically, a model's state dictionary is saved using:

```python
import torch

torch.save(model.state_dict(), PATH)
```

And loaded using:

```python
model.load_state_dict(torch.load(PATH))
model.eval() # Set model to evaluation mode if using it for inference
```

---

## Example

### Saving and Loading Model States

```python
import torch
import torch.nn as nn

# Define a simple model
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.linear = nn.Linear(10, 2)

def forward(self, x):
return self.linear(x)

# Initialize model and optimizer
model = SimpleModel()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

# Define a path to save the model
model_path = "simple_model.pth"

# Simulate training by updating model parameters
for _ in range(5):
# Fake inputs and loss
inputs = torch.randn(1, 10)
target = torch.randn(1, 2)

optimizer.zero_grad()
output = model(inputs)
loss = ((output - target) ** 2).mean()
loss.backward()
optimizer.step()

# Save model state and optimizer state
state = {
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict(),
}
torch.save(state, model_path)

print(f"Model state saved at {model_path}")

# Load model and optimizer states
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])
print("Model and optimizer states loaded.")

# Ensure model is in evaluation mode for inference
model.eval()

# Make a prediction
example_input = torch.randn(1, 10)
example_output = model(example_input)
print("Prediction:", example_output)
```

In this example:

- The model's state dictionary and optimizer's state dictionary are saved into a file named `simple_model.pth`.
- The model and optimizer states are loaded back from the saved file, ensuring seamless resumption of training or use of the model for inference.

---

By using the `torch.save` and `torch.load` functions along with the state dictionary, you can effectively manage model checkpoints and ensure a reproducible workflow.
Loading