A library to merge multiple neural network models for parallel hyperparameter search
Project description
nnmerge
A library to merge multiple neural network models for parallel hyperparameter search.
Installation
pip install nnmerge
Usage
Example usage for PyTorch model:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import time
from tqdm import tqdm
from nnmerge.pytorch import convert_to_multi_model
class MyModel(nn.Module):
def __init__(self, constants, hyperparameters):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(constants["input_size"], hyperparameters["hidden_size_l1"])
self.fc2 = nn.Linear(hyperparameters["hidden_size_l1"], hyperparameters["hidden_size_l2"])
self.fc3 = nn.Linear(hyperparameters["hidden_size_l2"], constants["num_classes"])
self.dropout = nn.Dropout(0.2)
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
batch_size = 10_000
constants = {
"input_size": 10_000,
"num_classes": 3
}
hyperparameters = {"hidden_size_l1": 10, "hidden_size_l2": 15}
hyperparameters_list = [
{"hidden_size_l1": 10, "hidden_size_l2": 10},
{"hidden_size_l1": 10, "hidden_size_l2": 15},
{"hidden_size_l1": 15, "hidden_size_l2": 10},
{"hidden_size_l1": 15, "hidden_size_l2": 15},
{"hidden_size_l1": 20, "hidden_size_l2": 10},
{"hidden_size_l1": 20, "hidden_size_l2": 15},
{"hidden_size_l1": 20, "hidden_size_l2": 20},
]
x = torch.randn(batch_size, constants["input_size"]).to(device)
# ======================================================
# Sequential hyperparameter search
# ======================================================
print("Sequential hyperparameter search execution time:")
start_time = time.time()
for hyperparameters in tqdm(hyperparameters_list):
model = MyModel(constants, hyperparameters)
model.to(device)
output = model(x)
print("Model output shape: ", output.shape)
end_time = time.time()
elapsed_time_sequential = end_time - start_time
print(f"\nExecution time: {elapsed_time_sequential}")
# ======================================================
# ======================================================
# Parallel hyperparameter search
# ======================================================
print("Parallel hyperparameter search execution time:")
start_time = time.time()
model = convert_to_multi_model(MyModel, constants, hyperparameters_list)
model.to(device)
output = model(x)
print("Model output length: ", len(output))
end_time = time.time()
elapsed_time_parallel = end_time - start_time
print(f"\nExecution time: {elapsed_time_parallel}")
print(f"Speedup: {elapsed_time_sequential/elapsed_time_parallel*100}%")
# ======================================================
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
nnmerge-0.1.2.tar.gz
(4.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nnmerge-0.1.2.tar.gz.
File metadata
- Download URL: nnmerge-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc1950d14baee66c2b9235bfb34c9330ee4b8808dcddead4bf96262ba5f404aa
|
|
| MD5 |
c52ee33418f9aac447c3b33428ddaa90
|
|
| BLAKE2b-256 |
0e600fdbc4e3b8a66bf767f91aef8270e3c405d343c515a06c4d19bfb3cff473
|
File details
Details for the file nnmerge-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nnmerge-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f07cbf86fc2f00b0b504a8ca2f69d8310455bcb31013beef5f2feb27c31ecf3
|
|
| MD5 |
e7d2693e7b6dafccabf2d3ba38af5b0a
|
|
| BLAKE2b-256 |
456229fb0ca71e757b05361e7c6eb9ae63fb90640de0c4205ca6eabec3a3c43d
|