Prototype MAML
This commit is contained in:
@@ -23,6 +23,9 @@ from datasets.synthetic_core import get_synthetic_env
|
||||
from models.xcore import get_model
|
||||
|
||||
|
||||
from lfna_utils import lfna_setup
|
||||
|
||||
|
||||
def subsample(historical_x, historical_y, maxn=10000):
|
||||
total = historical_x.size(0)
|
||||
if total <= maxn:
|
||||
@@ -33,24 +36,7 @@ def subsample(historical_x, historical_y, maxn=10000):
|
||||
|
||||
|
||||
def main(args):
|
||||
prepare_seed(args.rand_seed)
|
||||
logger = prepare_logger(args)
|
||||
|
||||
cache_path = (
|
||||
logger.path(None) / ".." / "env-{:}-info.pth".format(args.env_version)
|
||||
).resolve()
|
||||
if cache_path.exists():
|
||||
env_info = torch.load(cache_path)
|
||||
else:
|
||||
env_info = dict()
|
||||
dynamic_env = get_synthetic_env(version=args.env_version)
|
||||
env_info["total"] = len(dynamic_env)
|
||||
for idx, (timestamp, (_allx, _ally)) in enumerate(tqdm(dynamic_env)):
|
||||
env_info["{:}-timestamp".format(idx)] = timestamp
|
||||
env_info["{:}-x".format(idx)] = _allx
|
||||
env_info["{:}-y".format(idx)] = _ally
|
||||
env_info["dynamic_env"] = dynamic_env
|
||||
torch.save(env_info, cache_path)
|
||||
logger, env_info = lfna_setup(args)
|
||||
|
||||
# check indexes to be evaluated
|
||||
to_evaluate_indexes = split_str2indexes(args.srange, env_info["total"], None)
|
||||
@@ -60,6 +46,8 @@ def main(args):
|
||||
)
|
||||
)
|
||||
|
||||
w_container_per_epoch = dict()
|
||||
|
||||
per_timestamp_time, start_time = AverageMeter(), time.time()
|
||||
for i, idx in enumerate(to_evaluate_indexes):
|
||||
|
||||
@@ -89,9 +77,6 @@ def main(args):
|
||||
output_dim=1,
|
||||
act_cls="leaky_relu",
|
||||
norm_cls="identity",
|
||||
# norm_cls="simple_norm",
|
||||
# mean=mean,
|
||||
# std=std,
|
||||
)
|
||||
model = get_model(dict(model_type="simple_mlp"), **model_kwargs)
|
||||
# build optimizer
|
||||
@@ -144,6 +129,7 @@ def main(args):
|
||||
save_path = logger.path(None) / "{:04d}-{:04d}.pth".format(
|
||||
idx, env_info["total"]
|
||||
)
|
||||
w_container_per_epoch[idx] = model.get_w_container().no_grad_clone()
|
||||
save_checkpoint(
|
||||
{
|
||||
"model_state_dict": model.state_dict(),
|
||||
@@ -155,10 +141,14 @@ def main(args):
|
||||
logger,
|
||||
)
|
||||
logger.log("")
|
||||
|
||||
per_timestamp_time.update(time.time() - start_time)
|
||||
start_time = time.time()
|
||||
|
||||
save_checkpoint(
|
||||
{"w_container_per_epoch": w_container_per_epoch},
|
||||
logger.path(None) / "final-ckp.pth",
|
||||
logger,
|
||||
)
|
||||
logger.log("-" * 200 + "\n")
|
||||
logger.close()
|
||||
|
||||
@@ -210,5 +200,7 @@ if __name__ == "__main__":
|
||||
if args.rand_seed is None or args.rand_seed < 0:
|
||||
args.rand_seed = random.randint(1, 100000)
|
||||
assert args.save_dir is not None, "The save dir argument can not be None"
|
||||
args.save_dir = "{:}-{:}".format(args.save_dir, args.env_version)
|
||||
args.save_dir = "{:}-{:}-d{:}".format(
|
||||
args.save_dir, args.env_version, args.hidden_dim
|
||||
)
|
||||
main(args)
|
||||
|
220
exps/LFNA/basic-maml.py
Normal file
220
exps/LFNA/basic-maml.py
Normal file
@@ -0,0 +1,220 @@
|
||||
#####################################################
|
||||
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.04 #
|
||||
#####################################################
|
||||
# python exps/LFNA/basic-maml.py --env_version v1 #
|
||||
# python exps/LFNA/basic-maml.py --env_version v2 #
|
||||
#####################################################
|
||||
import sys, time, copy, torch, random, argparse
|
||||
from tqdm import tqdm
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
|
||||
lib_dir = (Path(__file__).parent / ".." / ".." / "lib").resolve()
|
||||
if str(lib_dir) not in sys.path:
|
||||
sys.path.insert(0, str(lib_dir))
|
||||
from procedures import prepare_seed, prepare_logger, save_checkpoint, copy_checkpoint
|
||||
from log_utils import time_string
|
||||
from log_utils import AverageMeter, convert_secs2time
|
||||
|
||||
from utils import split_str2indexes
|
||||
|
||||
from procedures.advanced_main import basic_train_fn, basic_eval_fn
|
||||
from procedures.metric_utils import SaveMetric, MSEMetric, ComposeMetric
|
||||
from datasets.synthetic_core import get_synthetic_env
|
||||
from models.xcore import get_model
|
||||
from xlayers import super_core
|
||||
|
||||
from lfna_utils import lfna_setup, TimeData
|
||||
|
||||
|
||||
class MAML:
|
||||
"""A LFNA meta-model that uses the MLP as delta-net."""
|
||||
|
||||
def __init__(self, container, criterion, meta_lr, inner_lr=0.01, inner_step=1):
|
||||
self.criterion = criterion
|
||||
self.container = container
|
||||
self.meta_optimizer = torch.optim.Adam(
|
||||
self.container.parameters(), lr=meta_lr, amsgrad=True
|
||||
)
|
||||
self.inner_lr = inner_lr
|
||||
self.inner_step = inner_step
|
||||
|
||||
def adapt(self, model, dataset):
|
||||
# create a container for the future timestamp
|
||||
y_hat = model.forward_with_container(dataset.x, self.container)
|
||||
loss = self.criterion(y_hat, dataset.y)
|
||||
grads = torch.autograd.grad(loss, self.container.parameters())
|
||||
|
||||
fast_container = self.container.additive(
|
||||
[-self.inner_lr * grad for grad in grads]
|
||||
)
|
||||
import pdb
|
||||
|
||||
pdb.set_trace()
|
||||
w_container.requires_grad_(True)
|
||||
containers = [w_container]
|
||||
for idx, dataset in enumerate(seq_datasets):
|
||||
x, y = dataset.x, dataset.y
|
||||
y_hat = model.forward_with_container(x, containers[-1])
|
||||
loss = criterion(y_hat, y)
|
||||
gradients = torch.autograd.grad(loss, containers[-1].tensors)
|
||||
with torch.no_grad():
|
||||
flatten_w = containers[-1].flatten().view(-1, 1)
|
||||
flatten_g = containers[-1].flatten(gradients).view(-1, 1)
|
||||
input_statistics = torch.tensor([x.mean(), x.std()]).view(1, 2)
|
||||
input_statistics = input_statistics.expand(flatten_w.numel(), -1)
|
||||
delta_inputs = torch.cat((flatten_w, flatten_g, input_statistics), dim=-1)
|
||||
delta = self.delta_net(delta_inputs).view(-1)
|
||||
delta = torch.clamp(delta, -0.5, 0.5)
|
||||
unflatten_delta = containers[-1].unflatten(delta)
|
||||
future_container = containers[-1].no_grad_clone().additive(unflatten_delta)
|
||||
# future_container = containers[-1].additive(unflatten_delta)
|
||||
containers.append(future_container)
|
||||
# containers = containers[1:]
|
||||
meta_loss = []
|
||||
temp_containers = []
|
||||
for idx, dataset in enumerate(seq_datasets):
|
||||
if idx == 0:
|
||||
continue
|
||||
current_container = containers[idx]
|
||||
y_hat = model.forward_with_container(dataset.x, current_container)
|
||||
loss = criterion(y_hat, dataset.y)
|
||||
meta_loss.append(loss)
|
||||
temp_containers.append((dataset.timestamp, current_container, -loss.item()))
|
||||
meta_loss = sum(meta_loss)
|
||||
w_container.requires_grad_(False)
|
||||
# meta_loss.backward()
|
||||
# self.meta_optimizer.step()
|
||||
return meta_loss, temp_containers
|
||||
|
||||
def step(self):
|
||||
torch.nn.utils.clip_grad_norm_(self.delta_net.parameters(), 1.0)
|
||||
self.meta_optimizer.step()
|
||||
|
||||
def zero_grad(self):
|
||||
self.meta_optimizer.zero_grad()
|
||||
|
||||
|
||||
def main(args):
|
||||
logger, env_info = lfna_setup(args)
|
||||
|
||||
total_time = env_info["total"]
|
||||
for i in range(total_time):
|
||||
for xkey in ("timestamp", "x", "y"):
|
||||
nkey = "{:}-{:}".format(i, xkey)
|
||||
assert nkey in env_info, "{:} no in {:}".format(nkey, list(env_info.keys()))
|
||||
train_time_bar = total_time // 2
|
||||
base_model = get_model(
|
||||
dict(model_type="simple_mlp"),
|
||||
act_cls="leaky_relu",
|
||||
norm_cls="identity",
|
||||
input_dim=1,
|
||||
output_dim=1,
|
||||
)
|
||||
|
||||
w_container = base_model.get_w_container()
|
||||
criterion = torch.nn.MSELoss()
|
||||
print("There are {:} weights.".format(w_container.numel()))
|
||||
|
||||
maml = MAML(w_container, criterion, args.meta_lr, args.inner_lr, args.inner_step)
|
||||
|
||||
# meta-training
|
||||
per_epoch_time, start_time = AverageMeter(), time.time()
|
||||
for iepoch in range(args.epochs):
|
||||
|
||||
need_time = "Time Left: {:}".format(
|
||||
convert_secs2time(per_epoch_time.avg * (args.epochs - iepoch), True)
|
||||
)
|
||||
logger.log(
|
||||
"[{:}] [{:04d}/{:04d}] ".format(time_string(), iepoch, args.epochs)
|
||||
+ need_time
|
||||
)
|
||||
|
||||
maml.zero_grad()
|
||||
|
||||
all_meta_losses = []
|
||||
for ibatch in range(args.meta_batch):
|
||||
sampled_timestamp = random.randint(0, train_time_bar)
|
||||
past_dataset = TimeData(
|
||||
sampled_timestamp,
|
||||
env_info["{:}-x".format(sampled_timestamp)],
|
||||
env_info["{:}-y".format(sampled_timestamp)],
|
||||
)
|
||||
future_dataset = TimeData(
|
||||
sampled_timestamp + 1,
|
||||
env_info["{:}-x".format(sampled_timestamp + 1)],
|
||||
env_info["{:}-y".format(sampled_timestamp + 1)],
|
||||
)
|
||||
maml.adapt(base_model, past_dataset)
|
||||
import pdb
|
||||
|
||||
pdb.set_trace()
|
||||
|
||||
meta_loss = torch.stack(all_meta_losses).mean()
|
||||
meta_loss.backward()
|
||||
adaptor.step()
|
||||
|
||||
debug_str = pool.debug_info(debug_timestamp)
|
||||
logger.log("meta-loss: {:.4f}".format(meta_loss.item()))
|
||||
|
||||
per_epoch_time.update(time.time() - start_time)
|
||||
start_time = time.time()
|
||||
|
||||
logger.log("-" * 200 + "\n")
|
||||
logger.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser("Use the data in the past.")
|
||||
parser.add_argument(
|
||||
"--save_dir",
|
||||
type=str,
|
||||
default="./outputs/lfna-synthetic/maml",
|
||||
help="The checkpoint directory.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--env_version",
|
||||
type=str,
|
||||
required=True,
|
||||
help="The synthetic enviornment version.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--meta_lr",
|
||||
type=float,
|
||||
default=0.01,
|
||||
help="The learning rate for the MAML optimizer (default is Adam)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--inner_lr",
|
||||
type=float,
|
||||
default=0.01,
|
||||
help="The learning rate for the inner optimization",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--inner_step", type=int, default=1, help="The inner loop steps for MAML."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--meta_batch",
|
||||
type=int,
|
||||
default=5,
|
||||
help="The batch size for the meta-model",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--epochs",
|
||||
type=int,
|
||||
default=1000,
|
||||
help="The total number of epochs.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--workers",
|
||||
type=int,
|
||||
default=4,
|
||||
help="The number of data loading workers (default: 4)",
|
||||
)
|
||||
# Random Seed
|
||||
parser.add_argument("--rand_seed", type=int, default=-1, help="manual seed")
|
||||
args = parser.parse_args()
|
||||
if args.rand_seed is None or args.rand_seed < 0:
|
||||
args.rand_seed = random.randint(1, 100000)
|
||||
assert args.save_dir is not None, "The save dir argument can not be None"
|
||||
main(args)
|
@@ -1,7 +1,8 @@
|
||||
#####################################################
|
||||
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.04 #
|
||||
#####################################################
|
||||
# python exps/LFNA/basic-same.py --srange 1-999
|
||||
# python exps/LFNA/basic-same.py --srange 1-999 --env_version v1 --hidden_dim 16
|
||||
# python exps/LFNA/basic-same.py --srange 1-999 --env_version v2 --hidden_dim
|
||||
#####################################################
|
||||
import sys, time, copy, torch, random, argparse
|
||||
from tqdm import tqdm
|
||||
@@ -22,6 +23,8 @@ from procedures.metric_utils import SaveMetric, MSEMetric, ComposeMetric
|
||||
from datasets.synthetic_core import get_synthetic_env
|
||||
from models.xcore import get_model
|
||||
|
||||
from lfna_utils import lfna_setup
|
||||
|
||||
|
||||
def subsample(historical_x, historical_y, maxn=10000):
|
||||
total = historical_x.size(0)
|
||||
@@ -33,22 +36,7 @@ def subsample(historical_x, historical_y, maxn=10000):
|
||||
|
||||
|
||||
def main(args):
|
||||
prepare_seed(args.rand_seed)
|
||||
logger = prepare_logger(args)
|
||||
|
||||
cache_path = (logger.path(None) / ".." / "env-info.pth").resolve()
|
||||
if cache_path.exists():
|
||||
env_info = torch.load(cache_path)
|
||||
else:
|
||||
env_info = dict()
|
||||
dynamic_env = get_synthetic_env()
|
||||
env_info["total"] = len(dynamic_env)
|
||||
for idx, (timestamp, (_allx, _ally)) in enumerate(tqdm(dynamic_env)):
|
||||
env_info["{:}-timestamp".format(idx)] = timestamp
|
||||
env_info["{:}-x".format(idx)] = _allx
|
||||
env_info["{:}-y".format(idx)] = _ally
|
||||
env_info["dynamic_env"] = dynamic_env
|
||||
torch.save(env_info, cache_path)
|
||||
logger, env_info, model_kwargs = lfna_setup(args)
|
||||
|
||||
# check indexes to be evaluated
|
||||
to_evaluate_indexes = split_str2indexes(args.srange, env_info["total"], None)
|
||||
@@ -78,16 +66,6 @@ def main(args):
|
||||
historical_x = env_info["{:}-x".format(idx)]
|
||||
historical_y = env_info["{:}-y".format(idx)]
|
||||
# build model
|
||||
mean, std = historical_x.mean().item(), historical_x.std().item()
|
||||
model_kwargs = dict(
|
||||
input_dim=1,
|
||||
output_dim=1,
|
||||
act_cls="leaky_relu",
|
||||
norm_cls="identity",
|
||||
# norm_cls="simple_norm",
|
||||
# mean=mean,
|
||||
# std=std,
|
||||
)
|
||||
model = get_model(dict(model_type="simple_mlp"), **model_kwargs)
|
||||
# build optimizer
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=args.init_lr, amsgrad=True)
|
||||
@@ -151,9 +129,9 @@ def main(args):
|
||||
logger,
|
||||
)
|
||||
logger.log("")
|
||||
|
||||
per_timestamp_time.update(time.time() - start_time)
|
||||
start_time = time.time()
|
||||
|
||||
save_checkpoint(
|
||||
{"w_container_per_epoch": w_container_per_epoch},
|
||||
logger.path(None) / "final-ckp.pth",
|
||||
@@ -172,6 +150,18 @@ if __name__ == "__main__":
|
||||
default="./outputs/lfna-synthetic/use-same-timestamp",
|
||||
help="The checkpoint directory.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--env_version",
|
||||
type=str,
|
||||
required=True,
|
||||
help="The synthetic enviornment version.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--hidden_dim",
|
||||
type=int,
|
||||
required=True,
|
||||
help="The hidden dimension.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--init_lr",
|
||||
type=float,
|
||||
@@ -205,4 +195,7 @@ if __name__ == "__main__":
|
||||
if args.rand_seed is None or args.rand_seed < 0:
|
||||
args.rand_seed = random.randint(1, 100000)
|
||||
assert args.save_dir is not None, "The save dir argument can not be None"
|
||||
args.save_dir = "{:}-{:}-d{:}".format(
|
||||
args.save_dir, args.env_version, args.hidden_dim
|
||||
)
|
||||
main(args)
|
||||
|
272
exps/LFNA/lfna-v0.py
Normal file
272
exps/LFNA/lfna-v0.py
Normal file
@@ -0,0 +1,272 @@
|
||||
#####################################################
|
||||
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.04 #
|
||||
#####################################################
|
||||
# python exps/LFNA/lfna-v0.py
|
||||
#####################################################
|
||||
import sys, time, copy, torch, random, argparse
|
||||
from tqdm import tqdm
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
|
||||
lib_dir = (Path(__file__).parent / ".." / ".." / "lib").resolve()
|
||||
if str(lib_dir) not in sys.path:
|
||||
sys.path.insert(0, str(lib_dir))
|
||||
from procedures import prepare_seed, prepare_logger, save_checkpoint, copy_checkpoint
|
||||
from log_utils import time_string
|
||||
from log_utils import AverageMeter, convert_secs2time
|
||||
|
||||
from utils import split_str2indexes
|
||||
|
||||
from procedures.advanced_main import basic_train_fn, basic_eval_fn
|
||||
from procedures.metric_utils import SaveMetric, MSEMetric, ComposeMetric
|
||||
from datasets.synthetic_core import get_synthetic_env
|
||||
from models.xcore import get_model
|
||||
from xlayers import super_core
|
||||
|
||||
|
||||
class LFNAmlp:
|
||||
"""A LFNA meta-model that uses the MLP as delta-net."""
|
||||
|
||||
def __init__(self, obs_dim, hidden_sizes, act_name):
|
||||
self.delta_net = super_core.SuperSequential(
|
||||
super_core.SuperLinear(obs_dim, hidden_sizes[0]),
|
||||
super_core.super_name2activation[act_name](),
|
||||
super_core.SuperLinear(hidden_sizes[0], hidden_sizes[1]),
|
||||
super_core.super_name2activation[act_name](),
|
||||
super_core.SuperLinear(hidden_sizes[1], 1),
|
||||
)
|
||||
self.meta_optimizer = torch.optim.Adam(
|
||||
self.delta_net.parameters(), lr=0.01, amsgrad=True
|
||||
)
|
||||
|
||||
def adapt(self, model, criterion, w_container, seq_datasets):
|
||||
w_container.requires_grad_(True)
|
||||
containers = [w_container]
|
||||
for idx, dataset in enumerate(seq_datasets):
|
||||
x, y = dataset.x, dataset.y
|
||||
y_hat = model.forward_with_container(x, containers[-1])
|
||||
loss = criterion(y_hat, y)
|
||||
gradients = torch.autograd.grad(loss, containers[-1].tensors)
|
||||
with torch.no_grad():
|
||||
flatten_w = containers[-1].flatten().view(-1, 1)
|
||||
flatten_g = containers[-1].flatten(gradients).view(-1, 1)
|
||||
input_statistics = torch.tensor([x.mean(), x.std()]).view(1, 2)
|
||||
input_statistics = input_statistics.expand(flatten_w.numel(), -1)
|
||||
delta_inputs = torch.cat((flatten_w, flatten_g, input_statistics), dim=-1)
|
||||
delta = self.delta_net(delta_inputs).view(-1)
|
||||
delta = torch.clamp(delta, -0.5, 0.5)
|
||||
unflatten_delta = containers[-1].unflatten(delta)
|
||||
future_container = containers[-1].no_grad_clone().additive(unflatten_delta)
|
||||
# future_container = containers[-1].additive(unflatten_delta)
|
||||
containers.append(future_container)
|
||||
# containers = containers[1:]
|
||||
meta_loss = []
|
||||
temp_containers = []
|
||||
for idx, dataset in enumerate(seq_datasets):
|
||||
if idx == 0:
|
||||
continue
|
||||
current_container = containers[idx]
|
||||
y_hat = model.forward_with_container(dataset.x, current_container)
|
||||
loss = criterion(y_hat, dataset.y)
|
||||
meta_loss.append(loss)
|
||||
temp_containers.append((dataset.timestamp, current_container, -loss.item()))
|
||||
meta_loss = sum(meta_loss)
|
||||
w_container.requires_grad_(False)
|
||||
# meta_loss.backward()
|
||||
# self.meta_optimizer.step()
|
||||
return meta_loss, temp_containers
|
||||
|
||||
def step(self):
|
||||
torch.nn.utils.clip_grad_norm_(self.delta_net.parameters(), 1.0)
|
||||
self.meta_optimizer.step()
|
||||
|
||||
def zero_grad(self):
|
||||
self.meta_optimizer.zero_grad()
|
||||
self.delta_net.zero_grad()
|
||||
|
||||
|
||||
class TimeData:
|
||||
def __init__(self, timestamp, xs, ys):
|
||||
self._timestamp = timestamp
|
||||
self._xs = xs
|
||||
self._ys = ys
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self._xs
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self._ys
|
||||
|
||||
@property
|
||||
def timestamp(self):
|
||||
return self._timestamp
|
||||
|
||||
|
||||
class Population:
|
||||
"""A population used to maintain models at different timestamps."""
|
||||
|
||||
def __init__(self):
|
||||
self._time2model = dict()
|
||||
self._time2score = dict() # higher is better
|
||||
|
||||
def append(self, timestamp, model, score):
|
||||
if timestamp in self._time2model:
|
||||
if self._time2score[timestamp] > score:
|
||||
return
|
||||
self._time2model[timestamp] = model.no_grad_clone()
|
||||
self._time2score[timestamp] = score
|
||||
|
||||
def query(self, timestamp):
|
||||
closet_timestamp = None
|
||||
for xtime, model in self._time2model.items():
|
||||
if closet_timestamp is None or (
|
||||
xtime < timestamp and timestamp - closet_timestamp >= timestamp - xtime
|
||||
):
|
||||
closet_timestamp = xtime
|
||||
return self._time2model[closet_timestamp], closet_timestamp
|
||||
|
||||
def debug_info(self, timestamps):
|
||||
xstrs = []
|
||||
for timestamp in timestamps:
|
||||
if timestamp in self._time2score:
|
||||
xstrs.append(
|
||||
"{:04d}: {:.4f}".format(timestamp, self._time2score[timestamp])
|
||||
)
|
||||
return ", ".join(xstrs)
|
||||
|
||||
|
||||
def main(args):
|
||||
prepare_seed(args.rand_seed)
|
||||
logger = prepare_logger(args)
|
||||
|
||||
cache_path = (logger.path(None) / ".." / "env-info.pth").resolve()
|
||||
if cache_path.exists():
|
||||
env_info = torch.load(cache_path)
|
||||
else:
|
||||
env_info = dict()
|
||||
dynamic_env = get_synthetic_env()
|
||||
env_info["total"] = len(dynamic_env)
|
||||
for idx, (timestamp, (_allx, _ally)) in enumerate(tqdm(dynamic_env)):
|
||||
env_info["{:}-timestamp".format(idx)] = timestamp
|
||||
env_info["{:}-x".format(idx)] = _allx
|
||||
env_info["{:}-y".format(idx)] = _ally
|
||||
env_info["dynamic_env"] = dynamic_env
|
||||
torch.save(env_info, cache_path)
|
||||
|
||||
total_time = env_info["total"]
|
||||
for i in range(total_time):
|
||||
for xkey in ("timestamp", "x", "y"):
|
||||
nkey = "{:}-{:}".format(i, xkey)
|
||||
assert nkey in env_info, "{:} no in {:}".format(nkey, list(env_info.keys()))
|
||||
train_time_bar = total_time // 2
|
||||
base_model = get_model(
|
||||
dict(model_type="simple_mlp"),
|
||||
act_cls="leaky_relu",
|
||||
norm_cls="identity",
|
||||
input_dim=1,
|
||||
output_dim=1,
|
||||
)
|
||||
|
||||
w_container = base_model.get_w_container()
|
||||
criterion = torch.nn.MSELoss()
|
||||
print("There are {:} weights.".format(w_container.numel()))
|
||||
|
||||
adaptor = LFNAmlp(4, (50, 20), "leaky_relu")
|
||||
|
||||
pool = Population()
|
||||
pool.append(0, w_container, -100)
|
||||
|
||||
# LFNA meta-training
|
||||
per_epoch_time, start_time = AverageMeter(), time.time()
|
||||
for iepoch in range(args.epochs):
|
||||
|
||||
need_time = "Time Left: {:}".format(
|
||||
convert_secs2time(per_epoch_time.avg * (args.epochs - iepoch), True)
|
||||
)
|
||||
logger.log(
|
||||
"[{:}] [{:04d}/{:04d}] ".format(time_string(), iepoch, args.epochs)
|
||||
+ need_time
|
||||
)
|
||||
|
||||
adaptor.zero_grad()
|
||||
|
||||
debug_timestamp = set()
|
||||
all_meta_losses = []
|
||||
for ibatch in range(args.meta_batch):
|
||||
sampled_timestamp = random.randint(0, train_time_bar)
|
||||
query_w_container, query_timestamp = pool.query(sampled_timestamp)
|
||||
# def adapt(self, model, w_container, xs, ys):
|
||||
seq_datasets = []
|
||||
# xs, ys = [], []
|
||||
for it in range(sampled_timestamp, sampled_timestamp + args.max_seq):
|
||||
xs = env_info["{:}-x".format(it)]
|
||||
ys = env_info["{:}-y".format(it)]
|
||||
seq_datasets.append(TimeData(it, xs, ys))
|
||||
temp_meta_loss, temp_containers = adaptor.adapt(
|
||||
base_model, criterion, query_w_container, seq_datasets
|
||||
)
|
||||
all_meta_losses.append(temp_meta_loss)
|
||||
for temp_time, temp_container, temp_score in temp_containers:
|
||||
pool.append(temp_time, temp_container, temp_score)
|
||||
debug_timestamp.add(temp_time)
|
||||
meta_loss = torch.stack(all_meta_losses).mean()
|
||||
meta_loss.backward()
|
||||
adaptor.step()
|
||||
|
||||
debug_str = pool.debug_info(debug_timestamp)
|
||||
logger.log("meta-loss: {:.4f}".format(meta_loss.item()))
|
||||
|
||||
per_epoch_time.update(time.time() - start_time)
|
||||
start_time = time.time()
|
||||
|
||||
logger.log("-" * 200 + "\n")
|
||||
logger.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser("Use the data in the past.")
|
||||
parser.add_argument(
|
||||
"--save_dir",
|
||||
type=str,
|
||||
default="./outputs/lfna-synthetic/lfna-v1",
|
||||
help="The checkpoint directory.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--init_lr",
|
||||
type=float,
|
||||
default=0.1,
|
||||
help="The initial learning rate for the optimizer (default is Adam)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--meta_batch",
|
||||
type=int,
|
||||
default=5,
|
||||
help="The batch size for the meta-model",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--epochs",
|
||||
type=int,
|
||||
default=1000,
|
||||
help="The total number of epochs.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--max_seq",
|
||||
type=int,
|
||||
default=5,
|
||||
help="The maximum length of the sequence.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--workers",
|
||||
type=int,
|
||||
default=4,
|
||||
help="The number of data loading workers (default: 4)",
|
||||
)
|
||||
# Random Seed
|
||||
parser.add_argument("--rand_seed", type=int, default=-1, help="manual seed")
|
||||
args = parser.parse_args()
|
||||
if args.rand_seed is None or args.rand_seed < 0:
|
||||
args.rand_seed = random.randint(1, 100000)
|
||||
assert args.save_dir is not None, "The save dir argument can not be None"
|
||||
main(args)
|
61
exps/LFNA/lfna_utils.py
Normal file
61
exps/LFNA/lfna_utils.py
Normal file
@@ -0,0 +1,61 @@
|
||||
#####################################################
|
||||
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.04 #
|
||||
#####################################################
|
||||
import torch
|
||||
from tqdm import tqdm
|
||||
from procedures import prepare_seed, prepare_logger
|
||||
from datasets.synthetic_core import get_synthetic_env
|
||||
|
||||
|
||||
def lfna_setup(args):
|
||||
prepare_seed(args.rand_seed)
|
||||
logger = prepare_logger(args)
|
||||
|
||||
cache_path = (
|
||||
logger.path(None) / ".." / "env-{:}-info.pth".format(args.env_version)
|
||||
).resolve()
|
||||
if cache_path.exists():
|
||||
env_info = torch.load(cache_path)
|
||||
else:
|
||||
env_info = dict()
|
||||
dynamic_env = get_synthetic_env(version=args.env_version)
|
||||
env_info["total"] = len(dynamic_env)
|
||||
for idx, (timestamp, (_allx, _ally)) in enumerate(tqdm(dynamic_env)):
|
||||
env_info["{:}-timestamp".format(idx)] = timestamp
|
||||
env_info["{:}-x".format(idx)] = _allx
|
||||
env_info["{:}-y".format(idx)] = _ally
|
||||
env_info["dynamic_env"] = dynamic_env
|
||||
torch.save(env_info, cache_path)
|
||||
|
||||
model_kwargs = dict(
|
||||
input_dim=1,
|
||||
output_dim=1,
|
||||
hidden_dim=args.hidden_dim,
|
||||
act_cls="leaky_relu",
|
||||
norm_cls="identity",
|
||||
)
|
||||
return logger, env_info, model_kwargs
|
||||
|
||||
|
||||
class TimeData:
|
||||
def __init__(self, timestamp, xs, ys):
|
||||
self._timestamp = timestamp
|
||||
self._xs = xs
|
||||
self._ys = ys
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self._xs
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self._ys
|
||||
|
||||
@property
|
||||
def timestamp(self):
|
||||
return self._timestamp
|
||||
|
||||
def __repr__(self):
|
||||
return "{name}(timestamp={:}, with {num} samples)".format(
|
||||
name=self.__class__.__name__, timestamp=self._timestamp, num=len(self._xs)
|
||||
)
|
Reference in New Issue
Block a user