Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalSkye committed Sep 18, 2022
1 parent 9f3f6c9 commit cddcdb5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 273 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ pretrained/
utils_plot*
imgs/
1.md

7 changes: 3 additions & 4 deletions POMO/TSP/TSPTrainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def run(self):
self.scheduler.step() # LR Decay
self.result_log.append('train_score', epoch, train_score)
self.result_log.append('train_loss', epoch, train_loss)
# Val
no_aug_score = self._fast_val(self.model, val_episodes=64)
self.result_log.append('val_score', epoch, no_aug_score)

# Logs & Checkpoint
elapsed_time_str, remain_time_str = self.time_estimator.get_est_string(epoch, self.trainer_params['epochs'])
Expand All @@ -96,10 +99,6 @@ def run(self):
self.result_log, labels=['train_loss'])

if all_done or (epoch % model_save_interval) == 0:
# val
val_episodes = 64
no_aug_score = self._fast_val(self.model, val_episodes=val_episodes)
print(">> validation results: {} over {} instances".format(no_aug_score, val_episodes))
# save checkpoint
self.logger.info("Saving trained_model")
checkpoint_dict = {
Expand Down
13 changes: 9 additions & 4 deletions POMO/TSP/TSPTrainer_Meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import math
import time
import random
import torch
from logging import getLogger
Expand Down Expand Up @@ -55,9 +56,9 @@ def __init__(self,
self.meta_model = Model(**self.model_params)
self.alpha = self.meta_params['alpha'] # for reptile
self.task_set = generate_task_set(self.meta_params)
assert self.trainer_params['meta_params']['epochs'] == math.ceil((self.trainer_params['epochs'] * self.trainer_params['train_episodes']) / (
self.trainer_params['meta_params']['B'] * self.trainer_params['meta_params']['k'] *
self.trainer_params['meta_params']['meta_batch_size'])), ">> meta-learning iteration does not match with POMO!"
# assert self.trainer_params['meta_params']['epochs'] == math.ceil((self.trainer_params['epochs'] * self.trainer_params['train_episodes']) / (
# self.trainer_params['meta_params']['B'] * self.trainer_params['meta_params']['k'] *
# self.trainer_params['meta_params']['meta_batch_size'])), ">> meta-learning iteration does not match with POMO!"

# Restore
self.start_epoch = 1
Expand All @@ -77,6 +78,7 @@ def __init__(self,

def run(self):

start_time = time.time()
self.time_estimator.reset(self.start_epoch)
for epoch in range(self.start_epoch, self.meta_params['epochs']+1):
self.logger.info('=================================================================')
Expand All @@ -94,7 +96,10 @@ def run(self):
self.logger.info("Epoch {:3d}/{:3d}({:.2f}%): Time Est.: Elapsed[{}], Remain[{}], Val Score: {:.4f}".format(
epoch, self.meta_params['epochs'], epoch/self.meta_params['epochs']*100, elapsed_time_str, remain_time_str, no_aug_score))

all_done = (epoch == self.meta_params['epochs'])
if self.trainer_params['stop_criterion'] == "epochs":
all_done = (epoch == self.meta_params['epochs'])
else:
all_done = (time.time() - start_time) >= self.trainer_params['time_limit']
model_save_interval = self.trainer_params['logging']['model_save_interval']
img_save_interval = self.trainer_params['logging']['img_save_interval']

Expand Down
260 changes: 0 additions & 260 deletions POMO/TSP/TSPTrainer_Scheduler.py

This file was deleted.

13 changes: 9 additions & 4 deletions POMO/TSP/TSPTrainer_pomo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import math
import time
import random
import torch
from logging import getLogger
Expand Down Expand Up @@ -53,9 +54,9 @@ def __init__(self,
self.meta_model = Model(**self.model_params)
self.optimizer = Optimizer(self.meta_model.parameters(), **self.optimizer_params['optimizer'])
self.task_set = generate_task_set(self.meta_params)
assert self.trainer_params['meta_params']['epochs'] == math.ceil((self.trainer_params['epochs'] * self.trainer_params['train_episodes']) / (
self.trainer_params['meta_params']['B'] * self.trainer_params['meta_params']['k'] *
self.trainer_params['meta_params']['meta_batch_size'])), ">> meta-learning iteration does not match with POMO!"
# assert self.trainer_params['meta_params']['epochs'] == math.ceil((self.trainer_params['epochs'] * self.trainer_params['train_episodes']) / (
# self.trainer_params['meta_params']['B'] * self.trainer_params['meta_params']['k'] *
# self.trainer_params['meta_params']['meta_batch_size'])), ">> meta-learning iteration does not match with POMO!"

# Restore
self.start_epoch = 1
Expand All @@ -75,6 +76,7 @@ def __init__(self,

def run(self):

start_time = time.time()
self.time_estimator.reset(self.start_epoch)
for epoch in range(self.start_epoch, self.meta_params['epochs']+1):
self.logger.info('=================================================================')
Expand All @@ -92,7 +94,10 @@ def run(self):
self.logger.info("Epoch {:3d}/{:3d}({:.2f}%): Time Est.: Elapsed[{}], Remain[{}], Val Score: {:.4f}".format(
epoch, self.meta_params['epochs'], epoch/self.meta_params['epochs']*100, elapsed_time_str, remain_time_str, no_aug_score))

all_done = (epoch == self.meta_params['epochs'])
if self.trainer_params['stop_criterion'] == "epochs":
all_done = (epoch == self.meta_params['epochs'])
else:
all_done = (time.time() - start_time) >= self.trainer_params['time_limit']
model_save_interval = self.trainer_params['logging']['model_save_interval']
img_save_interval = self.trainer_params['logging']['img_save_interval']

Expand Down
5 changes: 5 additions & 0 deletions POMO/TSP/TSProblemDef.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,15 @@ def generate_tsp_dist(n_samples, n_nodes, distribution):
os.makedirs(path)
seed_everything(seed=2023)

# var-dist test data
for dist in ["uniform", "uniform_rectangle", "gaussian", "cluster", "diagonal", "tsplib"]:
print(">> Generating TSP instances following {} distribution!".format(dist))
get_random_problems(20000, 100, distribution=dist, path=path)

for s in [150, 200]:
print(">> Generating TSP instances of size {}!".format(s))
get_random_problems(20000, s, distribution="uniform", path=path)

# data = generate_gaussian_mixture_tsp(dataset_size=64, graph_size=100, num_modes=1, cdist=1)
# data = load_dataset("../../data/TSP/tsp100_cluster.pkl")
# print(type(data), data.size(), data)
Expand Down
4 changes: 3 additions & 1 deletion POMO/TSP/train_n100.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
'use_cuda': USE_CUDA,
'cuda_device_num': CUDA_DEVICE_NUM,
'seed': 1234,
'epochs': 500, # will be overridden if meta_params['enable'] is True
'epochs': 500,
'time_limit': 86400,
'stop_criterion': 'epochs', # epochs or time
'train_episodes': 100000, # number of instances per epoch
'train_batch_size': 64,
'logging': {
Expand Down

0 comments on commit cddcdb5

Please sign in to comment.