Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# environment variable that tells us whether this code is running on the server or not
# LOCAL_TESTING = os.getenv('RUN_ENV', 'production') == 'local'
LOCAL_TESTING = False
# Sacred setup (must be before rllib imports)
from sacred import Experiment
ex = Experiment("PPO RLLib")
# Necessary work-around to make sacred pickling compatible with rllib
from sacred import SETTINGS
SETTINGS.CONFIG.READ_ONLY_CONFIG = False
# Slack notification configuration
from sacred.observers import SlackObserver
if os.path.exists('slack.json') and not LOCAL_TESTING:
slack_obs = SlackObserver.from_config('slack.json')
ex.observers.append(slack_obs)
# Necessary for capturing stdout in multiprocessing setting
SETTINGS.CAPTURE_MODE = 'sys'
# rllib and rllib-dependent imports
# Note: tensorflow and tensorflow dependent imports must also come after rllib imports
# This is because rllib disables eager execution. Otherwise, it must be manually disabled
import ray
from ray.tune.result import DEFAULT_RESULTS_DIR
from ray.tune.registry import register_env
from ray.rllib.models import ModelCatalog
from ray.rllib.agents.ppo.ppo import PPOTrainer
from human_aware_rl.ppo.ppo_rllib import RllibPPOModel, RllibLSTMPPOModel
from human_aware_rl.rllib.rllib import OvercookedMultiAgent, save_trainer, gen_trainer_from_params
from human_aware_rl.imitation.behavior_cloning_tf2 import BehaviorCloningPolicy, BC_SAVE_DIR
return loss
for i in range(N_LBFGS_STEPS):
optimizer.step(closure)
torch.save(polished_model.state_dict(), str((Path(trial.logdir) / trial._checkpoint.value).parent / 'polished_model.pth'))
eye = torch.eye(polished_model.size)
x = (eye[:, :, None, None] * torch.eye(2)).unsqueeze(-1)
y = polished_model(x[:, trainable.br_perm])
loss = nn.functional.mse_loss(y, trainable.target_matrix)
return loss.item()
ex = Experiment('Ops_factorization')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.config
def fixed_order_config():
size = 8 # Size of matrix to factor, must be power of 2
ntrials = 20 # Number of trials for hyperparameter tuning
nsteps = 400 # Number of steps per epoch
nmaxepochs = 200 # Maximum number of epochs
result_dir = 'results' # Directory to store results
nthreads = 1 # Number of CPU threads per job
smoke_test = False # Finish quickly for testing
@ex.capture
def ops_experiment(size, ntrials, nsteps, result_dir, nthreads, smoke_test):
config={
'ensemble_bleu_test': BLEU_ensm_test,
}
def _save(self, checkpoint_dir):
checkpoint_path = os.path.join(checkpoint_dir, "model_optimizer.pth")
return checkpoint_path
def _restore(self, checkpoint_path):
pass
ex = Experiment('Transformer_experiment')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('../config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.config
def default_config():
model = 'DynamicConv' # Name of model, either 'DynamicConv' or 'Transformer'
model_args = {} # Arguments to be passed to the model, as a dictionary
n_encoder_structure_layer = 0 # Number of structured layer in the encoder
n_decoder_structure_layer = 0 # Number of structured layer in the decoder
structure_type = 'B' # 'B' for butterfly or BBT for product of 2 butterflies
nblocks = 0 # Number of (BB^T) blocks in structure
structured_attention = False # Whether attention layers are structured
ntrials = 20 # Number of trials for hyperparameter tuning
nmaxupdates = 50000 # Maximum number of updates
result_dir = project_root + '/transformer/results' # Directory to store results
cuda = torch.cuda.is_available() # Whether to use GPU
smoke_test = False # Finish quickly for testing
optimizer.zero_grad()
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm], trainable.target_matrix)
loss.backward()
return loss
for i in range(N_LBFGS_STEPS):
optimizer.step(closure)
torch.save(polished_model.state_dict(), str((Path(trial.logdir) / trial._checkpoint.value).parent / 'polished_model.pth'))
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm], trainable.target_matrix)
return loss.item()
ex = Experiment('Dct_factorization')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.named_config
def softmax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'softmax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.named_config
def sparsemax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'sparsemax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.config
def fixed_order_config():
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm, 0], trainable.target_matrix)
loss.backward()
return loss
for i in range(N_LBFGS_STEPS):
optimizer.step(closure)
torch.save(polished_model.state_dict(), str((Path(trial.logdir) / trial._checkpoint.value).parent / 'polished_model.pth'))
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm, 0], trainable.target_matrix)
return loss.item()
ex = Experiment('Circulant_factorization')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.named_config
def softmax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'softmax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.named_config
def sparsemax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'sparsemax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.config
def fixed_order_config():
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm, 0], trainable.target_matrix)
loss.backward()
return loss
for i in range(N_LBFGS_STEPS):
optimizer.step(closure)
torch.save(polished_model.state_dict(), str((Path(trial.logdir) / trial._checkpoint.value).parent / 'polished_model.pth'))
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm, 0], trainable.target_matrix)
return loss.item()
ex = Experiment('VandermondeEval_factorization')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.named_config
def softmax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'softmax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.named_config
def sparsemax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'sparsemax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.config
def fixed_order_config():
# Hack: create new instance without call __init__, since trainable.__init__
# creates result_dir and log_dir in the wrong place (~/ray_results)
trainable_cls = TrainableBP
trainable = trainable_cls.__new__(trainable_cls)
trainable._setup(trial.config)
trainable.restore(str(Path(trial.logdir) / trial._checkpoint.value))
loss = trainable.polish(N_LBFGS_STEPS, save_to_self_model=True)
torch.save(trainable.model.state_dict(), str((Path(trial.logdir) / trial._checkpoint.value).parent / 'polished_model.pth'))
return loss
ex = Experiment('Transform_factorization')
ex.observers.append(FileStorageObserver.create('logs_new'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.config
def default_config():
model = 'BP'
target = 'dft' # The target matrix to factor ('dft', 'idft', 'dct', 'hadamard')
size = 8 # Size of matrix to factor, must be power of 2
complex = True # Whether to use complex factorization or real factorization
fixed_order = True # Whether the order of the factors are fixed
ntrials = 20 # Number of trials for hyperparameter tuning
nsteps = 400 # Number of steps per epoch
nepochsvalid = 5 # Frequency of validation (polishing), in terms of epochs
nmaxepochs = 200 # Maximum number of epochs
result_dir = project_root + '/results_new' # Directory to store results
cuda = torch.cuda.is_available() # Whether to use GPU
nthreads = 1 # Number of CPU threads per job
# lr=config['lr'], weight_decay=config['weight_decay'])
self.optimizer.load_state_dict(checkpoint['optimizer'])
# self.optimizer.param_groups[1].update({'weight_decay': 0.0})
# self.optimizer.param_groups[0].update({'params': permutation_params})
# self.optimizer.param_groups[1].update({'params': unstructured_params})
# self.scheduler = optim.lr_scheduler.StepLR(self.optimizer)
self.scheduler.load_state_dict(checkpoint['scheduler'])
self.scheduler.optimizer = self.optimizer
ex = Experiment('Permutation_experiment')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('../config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.config
def default_config():
dataset = 'PPCIFAR10'
model = 'PResNet18' # Name of model, see model_utils.py
args = {} # Arguments to be passed to the model, as a dictionary
optimizer = 'Adam' # Which optimizer to use, either Adam or SGD
lr_decay = True # Whether to use learning rate decay
lr_decay_period = 50 # Period of learning rate decay
plr_min = 1e-4
plr_max = 1e-3
weight_decay = True # Whether to use weight decay
pwd = True
pwd_min = 1e-4
pwd_max = 5e-4
optimizer.zero_grad()
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm], trainable.target_matrix)
loss.backward()
return loss
for i in range(N_LBFGS_STEPS_VALIDATION):
optimizer.step(closure)
loss = nn.functional.mse_loss(polished_model.matrix()[:, trainable.perm], trainable.target_matrix)
# return loss.item() if not torch.isnan(loss) else preopt_loss.item() if not torch.isnan(preopt_loss) else float('inf')
return loss.item() if not torch.isnan(loss) else preopt_loss.item() if not torch.isnan(preopt_loss) else 9999.0
ex = Experiment('Fft_factorization')
ex.observers.append(FileStorageObserver.create('logs'))
slack_config_path = Path('config/slack.json') # Add webhook_url there for Slack notification
if slack_config_path.exists():
ex.observers.append(SlackObserver.from_config(str(slack_config_path)))
@ex.named_config
def softmax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'softmax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.named_config
def sparsemax_config():
fixed_order = False # Whether the order of the factors are fixed
softmax_fn = 'sparsemax' # Whether to use softmax (+ semantic loss) or sparsemax
@ex.config
def fixed_order_config():