Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ksadowski13 committed Dec 16, 2021
1 parent 00db6d8 commit ae2ea40
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 70 deletions.
2 changes: 1 addition & 1 deletion experiments/create_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def create_experiment(args: argparse.ArgumentParser) -> str:

experiment_id = create_experiment(args)

print(experiment_id)
print(f'SigOpt Experiment ID: {experiment_id}')
34 changes: 13 additions & 21 deletions experiments/graphsage/full_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
from timeit import default_timer
from typing import Callable

Expand Down Expand Up @@ -91,22 +90,22 @@ def run(
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

if sigopt_context is not None:
lr = sigopt_context.params.lr
hidden_feats = sigopt_context.params.hidden_feats
num_layers = sigopt_context.params.num_layers
aggregator_type = "gcn" # sigopt_context.params.aggregator_type
batch_norm = sigopt_context.params.batch_norm
activation = sigopt_context.params.activation
input_dropout = sigopt_context.params.input_dropout
dropout = sigopt_context.params.dropout
lr = sigopt_context.params['lr']
hidden_feats = sigopt_context.params['hidden_feats']
num_layers = sigopt_context.params['num_layers']
aggregator_type = sigopt_context.params.get('aggregator_type', 'gcn')
batch_norm = sigopt_context.params['batch_norm']
activation = sigopt_context.params['activation']
input_dropout = sigopt_context.params['input_dropout']
dropout = sigopt_context.params['dropout']

print(f'{sigopt_context.params = }')
else:
lr = args.lr
hidden_feats = args.hidden_feats if len(
args.hidden_feats) > 1 else args.hidden_feats[0]
num_layers = args.num_layers
aggregator_type = "gcn" # args.aggregator_type
aggregator_type = args.aggregator_type
batch_norm = args.batch_norm
activation = args.activation
input_dropout = args.input_dropout
Expand Down Expand Up @@ -192,7 +191,6 @@ def run(
f'Test Epoch Time: {test_time:.2f}'
)


if sigopt_context is not None:
metrics = {
'best epoch': checkpoint.best_epoch,
Expand Down Expand Up @@ -269,13 +267,6 @@ def run(
utils.download_dataset(args.dataset)

if args.experiment_id is not None:
if os.getenv('SIGOPT_API_TOKEN') is None:
raise ValueError(
'SigOpt API token is not provided. Please provide it by '
'--sigopt-api-token argument or set '
'SIGOPT_API_TOKEN environment variable.'
)

sigopt.set_project(args.project_id)
experiment = sigopt.get_experiment(args.experiment_id)

Expand All @@ -284,9 +275,10 @@ def run(
try:
run(args, sigopt_context=sigopt_context)
except Exception as e:
print(f"Exception occurred: '{e}'")
sigopt_context.log_metadata('exception', e)
sigopt_context.log_failure()
import sys
sys.exit()

print(f'Exception occurred: \'{e}\'')

else:
run(args)
23 changes: 8 additions & 15 deletions experiments/graphsage/mini_batch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
from timeit import default_timer
from typing import Callable, Union

Expand Down Expand Up @@ -105,9 +104,9 @@ def run(
lr = sigopt_context.params['lr']
num_layers = sigopt_context.params['num_layers']
hidden_feats = sigopt_context.params['hidden_feats']
aggregator_type = 'mean' # sigopt_context.params['aggregator_type']
batch_norm = 0 # sigopt_context.params['batch_norm']
activation = 'leaky_relu' # sigopt_context.params['activation']
aggregator_type = sigopt_context.params.get('aggregator_type', 'mean')
batch_norm = bool(sigopt_context.params.get('batch_norm', 0))
activation = sigopt_context.params.get('activation', 'leaky_relu')
input_dropout = sigopt_context.params['input_dropout']
dropout = sigopt_context.params['dropout']
batch_size = sigopt_context.params['batch_size']
Expand Down Expand Up @@ -281,7 +280,7 @@ def run(
argparser.add_argument('--num-epochs', default=200, type=int)
argparser.add_argument('--lr', default=0.003, type=float)
argparser.add_argument('--hidden-feats', default=[256],
nargs='+', type=int)
nargs='+', type=int)
argparser.add_argument('--num-layers', default=3, type=int)
argparser.add_argument('--aggregator-type', default='mean',
type=str, choices=['gcn', 'mean'])
Expand Down Expand Up @@ -313,13 +312,6 @@ def run(
utils.download_dataset(args.dataset)

if args.experiment_id is not None:
if os.getenv('SIGOPT_API_TOKEN') is None:
raise ValueError(
'SigOpt API token is not provided. Please provide it by '
'--sigopt-api-token argument or set '
'SIGOPT_API_TOKEN environment variable.'
)

sigopt.set_project(args.project_id)
experiment = sigopt.get_experiment(args.experiment_id)

Expand All @@ -328,9 +320,10 @@ def run(
try:
run(args, sigopt_context=sigopt_context)
except Exception as e:
print(f"Exception occurred: '{e}'")
sigopt_context.log_metadata('exception', e)
sigopt_context.log_failure()
import sys
sys.exit()

print(f'Exception occurred: \'{e}\'')

else:
run(args)
36 changes: 14 additions & 22 deletions experiments/rgcn/full_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
from collections.abc import Callable
from timeit import default_timer

Expand Down Expand Up @@ -90,7 +89,6 @@ def run(
args: argparse.ArgumentParser,
sigopt_context: sigopt.run_context = None,
) -> None:

torch.manual_seed(args.seed)

dataset, evaluator, hg, train_idx, valid_idx, test_idx = utils.process_dataset(
Expand All @@ -101,16 +99,16 @@ def run(
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

if sigopt_context is not None:
embedding_lr = sigopt_context.params.lr
model_lr = sigopt_context.params.lr
hidden_feats = sigopt_context.params.hidden_feats
num_bases = sigopt_context.params.num_bases
num_layers = sigopt_context.params.num_layers
embedding_lr = sigopt_context.params['lr']
model_lr = sigopt_context.params['lr']
hidden_feats = sigopt_context.params['hidden_feats']
num_bases = sigopt_context.params['num_bases']
num_layers = sigopt_context.params['num_layers']
norm = 'right'
layer_norm = bool(sigopt_context.params.layer_norm)
activation = sigopt_context.params.activation
input_dropout = sigopt_context.params.input_dropout
dropout = sigopt_context.params.dropout
layer_norm = bool(sigopt_context.params['layer_norm'])
activation = sigopt_context.params['activation']
input_dropout = sigopt_context.params['input_dropout']
dropout = sigopt_context.params['dropout']
self_loop = True
else:
embedding_lr = args.embedding_lr
Expand Down Expand Up @@ -322,24 +320,18 @@ def run(
utils.download_dataset(args.dataset)

if args.experiment_id is not None:
if os.getenv('SIGOPT_API_TOKEN') is None:
raise ValueError(
'SigOpt API token is not provided. Please provide it by '
'--sigopt-api-token argument or set '
'SIGOPT_API_TOKEN environment variable.'
)

sigopt.set_project(args.project_id)
experiment = sigopt.get_experiment(args.experiment_id)

while not experiment.is_finished():
with experiment.create_run() as sigopt_context:
try:
run(args, sigopt_context=sigopt_context)
except:
print(f"Exception occurred: '{e}'")
except Exception as e:
sigopt_context.log_metadata('exception', e)
sigopt_context.log_failure()
import sys
sys.exit()

print(f'Exception occurred: \'{e}\'')

else:
run(args)
16 changes: 5 additions & 11 deletions experiments/rgcn/mini_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def run(

loss_function = nn.CrossEntropyLoss().to(device)
embedding_optimizer = torch.optim.SparseAdam(
list(embedding_layer.node_embeddings.parameters()),
list(embedding_layer.node_embeddings.parameters()),
lr=embedding_lr
)
model_optimizer = torch.optim.Adam(model.parameters(), lr=model_lr)
Expand Down Expand Up @@ -362,13 +362,6 @@ def run(
utils.download_dataset(args.dataset)

if args.experiment_id is not None:
if os.getenv('SIGOPT_API_TOKEN') is None:
raise ValueError(
'SigOpt API token is not provided. Please provide it by '
'--sigopt-api-token argument or set '
'SIGOPT_API_TOKEN environment variable.'
)

sigopt.set_project(args.project_id)
experiment = sigopt.get_experiment(args.experiment_id)

Expand All @@ -377,9 +370,10 @@ def run(
try:
run(args, sigopt_context=sigopt_context)
except Exception as e:
print(f"Exception occurred: '{e}'")
sigopt_context.log_metadata('exception', e)
sigopt_context.log_failure()
import sys
sys.exit()

print(f'Exception occurred: \'{e}\'')

else:
run(args)

0 comments on commit ae2ea40

Please sign in to comment.