-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move minimal_training function to conftest of torch test subpackage
- Loading branch information
Showing
6 changed files
with
58 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Tuple | ||
|
||
import torch | ||
from numpy.typing import NDArray | ||
from torch.optim import LBFGS | ||
from torch.utils.data import DataLoader | ||
|
||
|
||
def minimal_training( | ||
model: torch.nn.Module, | ||
dataloader: DataLoader, | ||
loss_function: torch.nn.modules.loss._Loss, | ||
lr: float = 0.01, | ||
epochs: int = 50, | ||
): | ||
""" | ||
Trains a PyTorch model using L-BFGS optimizer. | ||
Args: | ||
model: The PyTorch model to be trained. | ||
dataloader: DataLoader providing the training data. | ||
loss_function: The loss function to be used for training. | ||
lr: The learning rate for the L-BFGS optimizer. Defaults to 0.01. | ||
epochs: The number of training epochs. Defaults to 50. | ||
Returns: | ||
The trained model. | ||
""" | ||
model = model.train() | ||
optimizer = LBFGS(model.parameters(), lr=lr) | ||
|
||
for epoch in range(epochs): | ||
data = torch.cat([inputs for inputs, targets in dataloader]) | ||
targets = torch.cat([targets for inputs, targets in dataloader]) | ||
|
||
def closure(): | ||
optimizer.zero_grad() | ||
outputs = model(data) | ||
loss = loss_function(outputs, targets) | ||
loss.backward() | ||
return loss | ||
|
||
optimizer.step(closure) | ||
|
||
return model | ||
|
||
|
||
def torch_linear_model_to_numpy(model: torch.nn.Linear) -> Tuple[NDArray, NDArray]: | ||
model.eval() | ||
return model.weight.data.numpy(), model.bias.data.numpy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
|