-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for combining auto module with NNI.
- Loading branch information
Showing
6 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
examples/tutorials/as_fast_as_lightning/programmatic/nni/config.yml
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,25 @@ | ||
authorName: default | ||
experimentName: example_nni_auto | ||
trialConcurrency: 1 | ||
maxExecDuration: 1h | ||
maxTrialNum: 10 | ||
#choice: local, remote, pai | ||
trainingServicePlatform: local | ||
searchSpacePath: search_space.json | ||
#choice: true, false | ||
useAnnotation: false | ||
tuner: | ||
#choice: TPE, Random, Anneal, Evolution, BatchTuner, MetisTuner, GPTuner | ||
#SMAC (SMAC should be installed through nnictl) | ||
builtinTunerName: TPE | ||
classArgs: | ||
#choice: maximize, minimize | ||
optimize_mode: maximize | ||
trial: | ||
command: python nni_main.py | ||
codeDir: . | ||
gpuNum: 1 | ||
localConfig: | ||
useActiveGpu: true | ||
maxTrialNumPerGpu: 2 | ||
gpuIndices: "0" |
1 change: 1 addition & 0 deletions
1
examples/tutorials/as_fast_as_lightning/programmatic/nni/launch.cmd
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 @@ | ||
nnictl create --config config.yml --port 8088 --debug |
56 changes: 56 additions & 0 deletions
56
examples/tutorials/as_fast_as_lightning/programmatic/nni/nni_main.py
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,56 @@ | ||
from machin.auto.config import ( | ||
generate_algorithm_config, | ||
generate_env_config, | ||
generate_training_config, | ||
launch, | ||
) | ||
from pytorch_lightning.callbacks import Callback | ||
|
||
import nni | ||
import torch as t | ||
import torch.nn as nn | ||
|
||
|
||
class SomeQNet(nn.Module): | ||
def __init__(self, state_dim, action_num): | ||
super().__init__() | ||
|
||
self.fc1 = nn.Linear(state_dim, 16) | ||
self.fc2 = nn.Linear(16, 16) | ||
self.fc3 = nn.Linear(16, action_num) | ||
|
||
def forward(self, state): | ||
a = t.relu(self.fc1(state)) | ||
a = t.relu(self.fc2(a)) | ||
return self.fc3(a) | ||
|
||
|
||
class InspectCallback(Callback): | ||
def __init__(self): | ||
self.total_reward = 0 | ||
|
||
def on_train_batch_end( | ||
self, trainer, pl_module, outputs, batch, _batch_idx, _dataloader_idx | ||
) -> None: | ||
for l in batch[0].logs: | ||
if "total_reward" in l: | ||
self.total_reward = l["total_reward"] | ||
|
||
|
||
if __name__ == "__main__": | ||
param = nni.get_next_parameter() | ||
cb = InspectCallback() | ||
while param: | ||
config = generate_algorithm_config("DQN") | ||
config = generate_env_config("openai_gym", config) | ||
config = generate_training_config( | ||
root_dir="trial", episode_per_epoch=10, max_episodes=10000, config=config | ||
) | ||
config["frame_config"]["models"] = ["SomeQNet", "SomeQNet"] | ||
config["frame_config"]["model_kwargs"] = [{"state_dim": 4, "action_num": 2}] * 2 | ||
config["frame_config"]["learning_rate"] = param["lr"] | ||
config["frame_config"]["update_rate"] = param["upd"] | ||
launch(config, pl_callbacks=[cb]) | ||
# we use total reward as "accuracy" | ||
nni.report_final_result(cb.total_reward) | ||
param = nni.get_next_parameter() |
4 changes: 4 additions & 0 deletions
4
examples/tutorials/as_fast_as_lightning/programmatic/nni/search_space.json
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,4 @@ | ||
{ | ||
"lr":{"_type":"choice","_value":[0.0001, 0.001, 0.01, 0.1]}, | ||
"upd":{"_type": "choice", "_value": [0.005, 0.002, 0.001, 0.0001]} | ||
} |
File renamed without changes.
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