From ca0a81e91c16d11542cc4a99689406ab0b8543e3 Mon Sep 17 00:00:00 2001 From: Nemin WU Date: Fri, 20 Sep 2024 16:47:20 -0400 Subject: [PATCH] update --- .DS_Store | Bin 10244 -> 10244 bytes pre_process/dhs/rbf_regress.py | 275 -------------------------- pre_process/dhs/rbf_regress_optuna.py | 218 -------------------- pre_process/dhs/utils.py | 172 ---------------- pre_trained_models/.DS_Store | Bin 6148 -> 10244 bytes 5 files changed, 665 deletions(-) delete mode 100644 pre_process/dhs/rbf_regress.py delete mode 100644 pre_process/dhs/rbf_regress_optuna.py delete mode 100644 pre_process/dhs/utils.py diff --git a/.DS_Store b/.DS_Store index b56444aed851924bc943febe0ed6347cb93baf75..7ecc854ba6d7e8dbf86649995f3966c3cd5e875b 100644 GIT binary patch delta 95 zcmZn(XbG6$&nUDpU^hRb&}0EYnavsk7r8~b844JR7*ZMH!E`=DGLT(7Sw^Or(P;7l b85eP^@<0`@q_;8}ZC)Ug!nB!P;V(M?rNS9- delta 43 zcmZn(XbG6$&nU1lU^hRbz+?eInavsk7r7_@ky*xQym_%~3e(00X2#9z3V+!FKJX5G diff --git a/pre_process/dhs/rbf_regress.py b/pre_process/dhs/rbf_regress.py deleted file mode 100644 index 8cd1f6ff..00000000 --- a/pre_process/dhs/rbf_regress.py +++ /dev/null @@ -1,275 +0,0 @@ -import os -import numpy as np -import pandas as pd -from sklearn.metrics import r2_score - -import torch -from torch.utils.data import Dataset, DataLoader - -import torch.nn as nn -import torch.optim as optim - -from datetime import datetime - -import optuna -from optuna.pruners import MedianPruner -from optuna.trial import TrialState -import logging -from utils import save_checkpoint, load_checkpoint, RBFSpatialRelationPositionEncoder - -pd.set_option("display.max_columns", None) - -params = { - 'dataset_root_dir': '../../sustainbench/data/dhs', - 'label': 'asset_index_normalized', - 'checkpoint_dir': './checkpoints', - 'load_checkpoint': False, - 'batch_size': 512, - 'epochs': 50, - 'lr': 0.005, - 'num_rbf_anchor_pts': 13, - 'rbf_kernel_size':80, - 'model_type':"global", - 'device': 'cuda:0' -} - -# Add file handler to save logs to a file -current_time = datetime.now().strftime("%Y%m%d_%H%M%S") -log_file = os.path.join('logs', f'optuna_tuning_{params["label"]}_{current_time}.log') -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) -file_handler = logging.FileHandler(log_file) -file_handler.setLevel(logging.INFO) -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -file_handler.setFormatter(formatter) -logger.addHandler(file_handler) - -train_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_trainval_labels.csv")) -val_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_val_labels.csv")) -test_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_test_labels.csv")) - -train_df = train_df.dropna(subset=['asset_index_normalized']) -val_df = val_df.dropna(subset=['asset_index_normalized']) -test_df = test_df.dropna(subset=['asset_index_normalized']) - -class RSDataset(Dataset): - def __init__(self, dataframe): - self.dataframe = dataframe - - def __len__(self): - return len(self.dataframe) - - def __getitem__(self, idx): - # Get the nl_mean directly from the dataframe - nl_mean = self.dataframe.iloc[idx]["nl_mean"] - label = self.dataframe.iloc[idx]["asset_index_normalized"] - if pd.isna(label): - return None - - return nl_mean, label - -train_dataset = RSDataset(train_df) -val_dataset = RSDataset(val_df) -test_dataset = RSDataset(test_df) - -train_loader = DataLoader(train_dataset, batch_size=params['batch_size'], shuffle=True) -val_loader = DataLoader(val_dataset, batch_size=params['batch_size'], shuffle=False) -test_loader = DataLoader(test_dataset, batch_size=params['batch_size'], shuffle=False) - - - -class MLP(nn.Module): - def __init__(self, train_dataset, device, num_rbf_anchor_pts=params['num_rbf_anchor_pts'], rbf_kernel_size=params['rbf_kernel_size']): - super(MLP, self).__init__() - self.position_encoder = RBFSpatialRelationPositionEncoder(train_locs=train_dataset, num_rbf_anchor_pts=num_rbf_anchor_pts, rbf_kernel_size=rbf_kernel_size, device=device) - self.model = nn.Sequential( - nn.Linear(self.position_encoder.pos_enc_output_dim, 64), - nn.ReLU(), - nn.Linear(64, 32), - nn.ReLU(), - nn.Linear(32, 1) - ) - - def forward(self, x): - loc_embed = self.position_encoder(x) - return self.model(loc_embed) - -nl_mean_list = [] -for batch in train_loader: - nl_mean_batch, _ = batch - nl_mean_list.extend(nl_mean_batch.numpy()) - -### train_nl_mean_array is a tensor of shape (num_train, 1) -train_nl_mean_array = np.array(nl_mean_list).reshape(-1, 1) - -# model = MLP(train_dataset=train_nl_mean_array, device=params['device']) -# model = model.to(params['device']) - -# criterion = nn.MSELoss() -# optimizer = optim.Adam(model.parameters(), lr=params['lr']) - -# best_val_loss = float("inf") - -# start_epoch = 0 -# if params['load_checkpoint']: -# model, optimizer, best_val_loss, start_epoch = load_checkpoint(model, optimizer, params['checkpoint_dir']) - -# num_epochs = params['epochs'] - -def objective(trial): - num_rbf_anchor_pts = trial.suggest_int('num_rbf_anchor_pts', 1, 30) - rbf_kernel_size = trial.suggest_int('rbf_kernel_size', 2, 50) - lr = trial.suggest_loguniform('lr', 1e-5, 1e-1) - - model = MLP(train_dataset=train_nl_mean_array, device=params['device'], - num_rbf_anchor_pts=num_rbf_anchor_pts, rbf_kernel_size=rbf_kernel_size) - model = model.to(params['device']) - - criterion = nn.MSELoss() - optimizer = optim.Adam(model.parameters(), lr=lr) - - num_epochs = 30 # Use fewer epochs for tuning - for epoch in range(num_epochs): - model.train() - train_loss = 0.0 - for batch_idx, (nl_means, labels) in enumerate(train_loader): - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - optimizer.zero_grad() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - loss = criterion(outputs.squeeze(), labels) - loss.backward() - optimizer.step() - train_loss += loss.item() * nl_means.size(0) - - train_loss /= len(train_loader.dataset) - - model.eval() - val_loss = 0.0 - with torch.no_grad(): - for nl_means, labels in val_loader: - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - loss = criterion(outputs.squeeze(), labels) - val_loss += loss.item() * nl_means.size(0) - - val_loss /= len(val_loader.dataset) - - logger.info(f"Trial {trial.number}, Epoch {epoch+1}/{num_epochs}, " - f"Training Loss: {train_loss:.4f}, Validation Loss: {val_loss:.4f}") - - trial.report(val_loss, epoch) - - if trial.should_prune(): - raise optuna.exceptions.TrialPruned() - - # Calculate R² score on the test set - model.eval() - all_preds = [] - all_labels = [] - with torch.no_grad(): - for nl_means, labels in test_loader: - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - all_preds.append(outputs.cpu().numpy()) - all_labels.append(labels.cpu().numpy()) - - all_preds = np.concatenate(all_preds) - all_labels = np.concatenate(all_labels) - r2 = r2_score(all_labels, all_preds) - - logger.info(f"Trial {trial.number}, Test R²: {r2:.4f}") - - return r2 - -pruner = MedianPruner(n_startup_trials=5, n_warmup_steps=5, interval_steps=1) -study = optuna.create_study(direction='maximize', pruner=pruner) -study.optimize(objective, n_trials=500, timeout=7200) - -logger.info("Number of finished trials: %d", len(study.trials)) -logger.info("Best trial:") - -trial = study.best_trial - -logger.info(" Value: %f", trial.value) -logger.info(" Params: ") -for key, value in trial.params.items(): - logger.info(" %s: %s", key, value) - -# for epoch in range(start_epoch, num_epochs): -# model.train() -# train_loss = 0.0 -# for batch_idx, (nl_means, labels) in enumerate(train_loader): -# nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() -# optimizer.zero_grad() -# # nl_means shape is (batch_size, 1, 1) -# nl_means = nl_means.reshape(nl_means.size(0), 1, 1) -# outputs = model(nl_means.cpu().numpy()) -# loss = criterion(outputs.squeeze(), labels) -# loss.backward() -# optimizer.step() -# train_loss += loss.item() * nl_means.size(0) - -# if batch_idx % 10 == 0: -# print(f"Epoch {epoch+1}/{params['epochs']}, Batch {batch_idx+1}/{len(train_loader)}, Training Loss: {loss.item():.4f}") - -# train_loss /= len(train_loader.dataset) - -# model.eval() -# val_loss = 0.0 -# all_preds = [] -# all_labels = [] -# with torch.no_grad(): -# for nl_means, labels in test_loader: -# nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() -# nl_means = nl_means.reshape(nl_means.size(0), 1, 1) -# outputs = model(nl_means.cpu().numpy()) -# loss = criterion(outputs.squeeze(), labels) -# val_loss += loss.item() * nl_means.size(0) -# all_preds.append(outputs.cpu().numpy()) -# all_labels.append(labels.cpu().numpy()) - -# val_loss /= len(test_loader.dataset) - -# all_preds = np.concatenate(all_preds) -# all_labels = np.concatenate(all_labels) - -# r2 = r2_score(all_labels, all_preds) - -# print(f"Epoch {epoch+1}/{num_epochs}, Training Loss: {train_loss}, Test Loss: {val_loss}, R²: {r2:.4f}") - -# is_best = val_loss < best_val_loss -# best_val_loss = min(val_loss, best_val_loss) - -# save_checkpoint({ -# 'epoch': epoch + 1, -# 'state_dict': model.state_dict(), -# 'best_val_loss': best_val_loss, -# 'optimizer': optimizer.state_dict(), -# }, is_best, params['checkpoint_dir']) - -# model.eval() -# test_loss = 0.0 -# all_preds = [] -# all_labels = [] -# with torch.no_grad(): -# for batch_idx, (inputs, labels) in enumerate(test_loader): -# inputs, labels = inputs.to(params['device']), labels.to(params['device']).float().unsqueeze(1) -# inputs = inputs.reshape(inputs.size(0), 1, 1) -# outputs = model(inputs.cpu().numpy()) -# loss = criterion(outputs, labels) -# test_loss += loss.item() * inputs.size(0) -# all_preds.append(outputs.cpu().numpy()) -# all_labels.append(labels.cpu().numpy()) - -# if batch_idx % 10 == 0: -# print(f"Test Batch {batch_idx+1}/{len(test_loader)}, Loss: {loss.item():.4f}") - -# test_loss /= len(test_loader.dataset) -# all_preds = np.concatenate(all_preds) -# all_labels = np.concatenate(all_labels) - -# r2 = r2_score(all_labels, all_preds) -# print(f"Test Loss: {test_loss:.4f}, R²: {r2:.4f}") diff --git a/pre_process/dhs/rbf_regress_optuna.py b/pre_process/dhs/rbf_regress_optuna.py deleted file mode 100644 index 0a2ad4b6..00000000 --- a/pre_process/dhs/rbf_regress_optuna.py +++ /dev/null @@ -1,218 +0,0 @@ -import os -import numpy as np -import pandas as pd -from sklearn.metrics import r2_score - -import torch -from torch.utils.data import Dataset, DataLoader - -import torch.nn as nn -import torch.optim as optim - -from datetime import datetime - -import optuna -from optuna.pruners import MedianPruner -from optuna.trial import TrialState -import logging -from utils import save_checkpoint, load_checkpoint, RBFFeaturePositionEncoder - -pd.set_option("display.max_columns", None) - -import argparse - -# Define the command-line arguments -parser = argparse.ArgumentParser(description="Hyperparameter Optimization with Optuna") -parser.add_argument('--dataset_root_dir', type=str, default='../../sustainbench/data/dhs', help='Root directory of the dataset') -parser.add_argument('--checkpoint_dir', type=str, default='./checkpoints', help='Directory to save checkpoints') -parser.add_argument('--load_checkpoint', action='store_true', help='Load checkpoint if available') -parser.add_argument('--batch_size', type=int, default=512, help='Batch size for training') -parser.add_argument('--epochs', type=int, default=100, help='Number of epochs for training') -parser.add_argument('--lr', type=float, default=0.005, help='Learning rate') -parser.add_argument('--num_rbf_anchor_pts', type=int, default=13, help='Number of RBF anchor points') -parser.add_argument('--rbf_kernel_size', type=int, default=80, help='RBF kernel size') -parser.add_argument('--model_type', type=str, default='global', help='Type of model') -parser.add_argument('--device', type=str, default='cuda:0', help='Device to use for training') -parser.add_argument('--label', type=str, default='experiment_1', help='Label for the experiment') - -args = parser.parse_args() -params = { - 'dataset_root_dir': args.dataset_root_dir, - 'checkpoint_dir': args.checkpoint_dir, - 'load_checkpoint': args.load_checkpoint, - 'batch_size': args.batch_size, - 'epochs': args.epochs, - 'lr': args.lr, - 'num_rbf_anchor_pts': args.num_rbf_anchor_pts, - 'rbf_kernel_size': args.rbf_kernel_size, - 'model_type': args.model_type, - 'device': args.device, - 'label': args.label -} - -# Add file handler to save logs to a file -current_time = datetime.now().strftime("%Y%m%d_%H%M%S") -log_file = os.path.join('logs', f'optuna_tuning_{params["label"]}_{current_time}.log') -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) -file_handler = logging.FileHandler(log_file) -file_handler.setLevel(logging.INFO) -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -file_handler.setFormatter(formatter) -logger.addHandler(file_handler) - -train_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_trainval_labels.csv")) -val_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_val_labels.csv")) -test_df = pd.read_csv(os.path.join(params['dataset_root_dir'], "dhs_test_labels.csv")) - -train_df = train_df.dropna(subset=[params['label']]) -val_df = val_df.dropna(subset=[params['label']]) -test_df = test_df.dropna(subset=[params['label']]) - -class RSDataset(Dataset): - def __init__(self, dataframe): - self.dataframe = dataframe - - def __len__(self): - return len(self.dataframe) - - def __getitem__(self, idx): - # Get the nl_mean directly from the dataframe - nl_mean = self.dataframe.iloc[idx]["nl_mean"] - label = self.dataframe.iloc[idx][params['label']] - if pd.isna(label): - return None - - return nl_mean, label - -train_dataset = RSDataset(train_df) -val_dataset = RSDataset(val_df) -test_dataset = RSDataset(test_df) - -train_loader = DataLoader(train_dataset, batch_size=params['batch_size'], shuffle=True) -val_loader = DataLoader(val_dataset, batch_size=params['batch_size'], shuffle=False) -test_loader = DataLoader(test_dataset, batch_size=params['batch_size'], shuffle=False) - - - -class MLP(nn.Module): - def __init__(self, train_dataset, device, num_rbf_anchor_pts, rbf_kernel_size, layers, neurons, act_func): - super(MLP, self).__init__() - self.position_encoder = RBFFeaturePositionEncoder( - train_locs=train_dataset, - num_rbf_anchor_pts=num_rbf_anchor_pts, - rbf_kernel_size=rbf_kernel_size, - device=device - ) - self.model = self._build_model(self.position_encoder.pos_enc_output_dim, layers, neurons, act_func) - - def _build_model(self, input_dim, layers, neurons, act_func): - modules = [] - for i in range(layers): - modules.append(nn.Linear(input_dim if i == 0 else neurons[i-1], neurons[i])) - modules.append(act_func) - modules.append(nn.Linear(neurons[-1], 1)) - return nn.Sequential(*modules) - - def forward(self, x): - loc_embed = self.position_encoder(x) - return self.model(loc_embed) - -nl_mean_list = [] -for batch in train_loader: - nl_mean_batch, _ = batch - nl_mean_list.extend(nl_mean_batch.numpy()) - -### train_nl_mean_array is a tensor of shape (num_train, 1) -train_nl_mean_array = np.array(nl_mean_list).reshape(-1, 1) - -# Define the objective function for Optuna -def objective(trial): - num_rbf_anchor_pts = trial.suggest_int('num_rbf_anchor_pts', 1, 30) - rbf_kernel_size = trial.suggest_int('rbf_kernel_size', 2, 50) - lr = trial.suggest_loguniform('lr', 1e-5, 1e-1) - - # Hyperparameters for the MLP - layers = trial.suggest_int('layers', 1, 3) - neurons = [trial.suggest_int(f'neurons_l{i}', 8, 128) for i in range(layers)] - activation_choices = {'ReLU': nn.ReLU(), 'Tanh': nn.Tanh(), 'LeakyReLU': nn.LeakyReLU()} - activation_name = trial.suggest_categorical('activation', list(activation_choices.keys())) - activation = activation_choices[activation_name] - - model = MLP(train_dataset=train_nl_mean_array, device=params['device'], - num_rbf_anchor_pts=num_rbf_anchor_pts, rbf_kernel_size=rbf_kernel_size, - layers=layers, neurons=neurons, act_func=activation) - model = model.to(params['device']) - - criterion = nn.MSELoss() - optimizer = optim.Adam(model.parameters(), lr=lr) - - num_epochs = 1 # Use fewer epochs for tuning - for epoch in range(num_epochs): - model.train() - train_loss = 0.0 - for batch_idx, (nl_means, labels) in enumerate(train_loader): - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - optimizer.zero_grad() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - loss = criterion(outputs.squeeze(), labels) - loss.backward() - optimizer.step() - train_loss += loss.item() * nl_means.size(0) - - train_loss /= len(train_loader.dataset) - - model.eval() - val_loss = 0.0 - with torch.no_grad(): - for nl_means, labels in val_loader: - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - loss = criterion(outputs.squeeze(), labels) - val_loss += loss.item() * nl_means.size(0) - - val_loss /= len(val_loader.dataset) - - logger.info(f"Trial {trial.number}, Epoch {epoch+1}/{num_epochs}, " - f"Training Loss: {train_loss:.4f}, Validation Loss: {val_loss:.4f}") - - trial.report(val_loss, epoch) - - if trial.should_prune(): - raise optuna.exceptions.TrialPruned() - - # Calculate R² score on the test set - model.eval() - all_preds = [] - all_labels = [] - with torch.no_grad(): - for nl_means, labels in test_loader: - nl_means, labels = nl_means.to(params['device']), labels.to(params['device']).float() - nl_means = nl_means.reshape(nl_means.size(0), 1, 1) - outputs = model(nl_means.cpu().numpy()) - all_preds.append(outputs.cpu().numpy()) - all_labels.append(labels.cpu().numpy()) - - all_preds = np.concatenate(all_preds) - all_labels = np.concatenate(all_labels) - r2 = r2_score(all_labels, all_preds) - - logger.info(f"Trial {trial.number}, Test R²: {r2:.4f}") - - return r2 - -pruner = MedianPruner(n_startup_trials=100, n_warmup_steps=10, interval_steps=5) -study = optuna.create_study(direction='maximize', pruner=pruner) -study.optimize(objective, n_trials=500, timeout=50400) # 50400 seconds = 14 hours - -logger.info("Number of finished trials: %d", len(study.trials)) -logger.info("Best trial:") - -trial = study.best_trial - -logger.info(" Value: %f", trial.value) -logger.info(" Params: ") -for key, value in trial.params.items(): - logger.info(" %s: %s", key, value) diff --git a/pre_process/dhs/utils.py b/pre_process/dhs/utils.py deleted file mode 100644 index 9feca75b..00000000 --- a/pre_process/dhs/utils.py +++ /dev/null @@ -1,172 +0,0 @@ -import os -import numpy as np -import pandas as pd - -import torch -import torch.nn as nn - - -def save_checkpoint(state, is_best, checkpoint_dir, filename='checkpoint_rbf.pth.tar'): - filepath = os.path.join(checkpoint_dir, filename) - torch.save(state, filepath) - if is_best: - best_filepath = os.path.join(checkpoint_dir, 'best_checkpoint_rbf.pth.tar') - torch.save(state, best_filepath) - -def load_checkpoint(model, optimizer, checkpoint_dir, filename='best_checkpoint_rbf.pth.tar'): - filepath = os.path.join(checkpoint_dir, filename) - if os.path.isfile(filepath): - print(f"Loading checkpoint '{filepath}'") - checkpoint = torch.load(filepath) - model.load_state_dict(checkpoint['state_dict']) - optimizer.load_state_dict(checkpoint['optimizer']) - best_val_loss = checkpoint['best_val_loss'] - start_epoch = checkpoint['epoch'] - print(f"Loaded checkpoint '{filepath}' (epoch {start_epoch})") - return model, optimizer, best_val_loss, start_epoch - else: - print(f"No checkpoint found at '{filepath}'") - return model, optimizer, best_val_loss, 0 - - -class RBFFeaturePositionEncoder(nn.Module): - """ - Given a list of values, compute the distance from each point to each RBF anchor point. - Feed into an MLP. - This is for global position encoding or relative/spatial context position encoding. - """ - - def __init__( - self, - train_locs, - coord_dim=1, - num_rbf_anchor_pts=100, - rbf_kernel_size=10e2, - rbf_kernel_size_ratio=0.0, - model_type="global", - max_radius=10000, - rbf_anchor_pt_ids=None, - device="cuda", - ): - """ - Args: - train_locs: np.array, [batch_size], location data - num_rbf_anchor_pts: the number of RBF anchor points - rbf_kernel_size: the RBF kernel size - rbf_kernel_size_ratio: if not None, different anchor points have different kernel size - max_radius: the relative spatial context size in spatial context model - """ - super(RBFFeaturePositionEncoder, self).__init__() - self.coord_dim = coord_dim - self.model_type = model_type - self.train_locs = train_locs.values if isinstance(train_locs, pd.Series) else train_locs - self.num_rbf_anchor_pts = num_rbf_anchor_pts - self.rbf_kernel_size = rbf_kernel_size - self.rbf_kernel_size_ratio = rbf_kernel_size_ratio - self.max_radius = max_radius - self.rbf_anchor_pt_ids = rbf_anchor_pt_ids - self.device = device - - # Calculate the coordinate matrix for each RBF anchor point - self.cal_rbf_anchor_coord_mat() - - self.pos_enc_output_dim = self.num_rbf_anchor_pts - # print(f"Position encoding output dimension: {self.pos_enc_output_dim}") - - def _random_sampling(self, item_tuple, num_sample): - """ - Randomly sample a given number of items. - """ - type_list = list(item_tuple) - if len(type_list) > num_sample: - return list(np.random.choice(type_list, num_sample, replace=False)) - elif len(type_list) == num_sample: - return item_tuple - else: - return list(np.random.choice(type_list, num_sample, replace=True)) - - def cal_rbf_anchor_coord_mat(self): - if self.model_type == "global": - assert self.rbf_kernel_size_ratio == 0 - # If we do RBF on location/global model, - # we need to random sample M RBF anchor points from training point dataset - if self.rbf_anchor_pt_ids == None: - self.rbf_anchor_pt_ids = self._random_sampling( - np.arange(len(self.train_locs)), self.num_rbf_anchor_pts - ) - - self.rbf_coords_mat = self.train_locs[self.rbf_anchor_pt_ids] - - elif self.model_type == "relative": - # If we do RBF on spatial context/relative model, - # We just ra ndom sample M-1 RBF anchor point in the relative spatial context defined by max_radius - # The (0,0) is also an anchor point - x_list = np.random.uniform( - -self.max_radius, self.max_radius, self.num_rbf_anchor_pts - ) - x_list[0] = 0.0 - y_list = np.random.uniform( - -self.max_radius, self.max_radius, self.num_rbf_anchor_pts - ) - y_list[0] = 0.0 - # self.rbf_coords: (num_rbf_anchor_pts, 2) - self.rbf_coords_mat = np.transpose(np.stack([x_list, y_list], axis=0)) - - if self.rbf_kernel_size_ratio > 0: - dist_mat = np.sqrt(np.sum(np.power(self.rbf_coords_mat, 2), axis=-1)) - # rbf_kernel_size_mat: (num_rbf_anchor_pts) - self.rbf_kernel_size_mat = ( - dist_mat * self.rbf_kernel_size_ratio + self.rbf_kernel_size - ) - - def make_output_embeds(self, coords): - """ - Given a list of coords (deltaX, deltaY), give their spatial relation embedding - Args: - coords: a python list with shape (batch_size, num_context_pt=1, coord_dim) - Return: - sprenc: Tensor shape (batch_size, num_context_pt, pos_enc_output_dim) - """ - if type(coords) == np.ndarray: - assert self.coord_dim == np.shape(coords)[2] - #print("coords",coords.shape) - coords = list(coords) - elif type(coords) == list: - assert self.coord_dim == len(coords[0][0]) - else: - print("coords type",type(coords)) - raise Exception("Unknown coords data type for RBFSpatialRelationEncoder") - - coords_mat = np.asarray(coords).astype(float) - #print("coords_mat1",coords_mat.shape) - batch_size = coords_mat.shape[0] - num_context_pt = coords_mat.shape[1] - - coords_mat = np.repeat(coords_mat, self.num_rbf_anchor_pts, axis=1) - #print("coords_mat2",coords_mat.shape) - coords_mat = coords_mat - self.rbf_coords_mat.T - #print("coords_mat3",coords_mat.shape) - coords_mat = np.sum(np.power(coords_mat, 2), axis=-1) - #print("coords_mat4",coords_mat.shape) - - if self.rbf_kernel_size_ratio > 0: - spr_embeds = np.exp( - (-1 * coords_mat) / (2.0 * np.power(self.rbf_kernel_size_mat, 2)) - ) - else: - spr_embeds = np.exp( - (-1 * coords_mat) / (2.0 * np.power(self.rbf_kernel_size, 2)) - ) - return spr_embeds - - def forward(self, coords): - """ - Given a list of coordinates, compute their spatial relation embedding. - Args: - coords: a list or array with shape (batch_size, num_context_pt=1, coord_dim) - Return: - spr_embeds: Tensor with shape (batch_size, num_context_pt, spa_embed_dim) - """ - spr_embeds = self.make_output_embeds(coords) - spr_embeds = torch.FloatTensor(spr_embeds).to(self.device) - return spr_embeds diff --git a/pre_trained_models/.DS_Store b/pre_trained_models/.DS_Store index a0eb7a6b6dce0d3991dcda580a7fe5bf7bc55d5b..5afbc984a3f962ce9c2971b4a83ac046e72aaa35 100644 GIT binary patch literal 10244 zcmeHMTWl0n7(U;$lo>j-Qz!z%Wfuz+VTCOf0v2@XML}*&w^v$Nc6SDtFr6toyIT~| zice^Ww`hz}6Q9I*c@jx9YBWlSmj_KH0V5{H2YK)k^~J;o|1)P6X$xYaQ89CpbN)I1 z`TsNXf9L;Za?UWu&|EMY7>hE-WU5?hDm9lWe4N(_MG7<(Btibn8_L*jhI%J6Lldk+ zMTkI%K!`wyK!`wyz<&V&`eySYR|*-nAp#)+Ap#c>;O~bhRW1{O91+rgbx`AP0Z5jU z+Anmi@e7jCKqdk?A|#Dafp8@$ToG(BAlykG4eSzu91&8uGX$Ft1b0TTLxF#Hnm_8B zGbDrz+Yo^efr$w4y?YtUu{~^0X86+myKQ;-c>D~cvhqn4lc`ir-kIFz4kz>Je8Fo@ z=lXfC({=4k={lY1w#?y_R@>q_`LyMjIp4uDOfnhhu^hu4ZgvZf;rYHvF`!UMX)_NU zTDvk9ZCD*Y9E;LrJQiKm(0KT;BG0H>(bSRJZx1@|QStl;-T`dtxOg7;J(yxL=h&{Z zmo(hEig|2^rCFAlY$YpFn`J4smjbt#@vjHo_;&>UhauWtu|=HClr?6C#M+(<)>c-g zRH{?;?w;=6J~gFHE7BrvAIe*<<8_-kk7P=#_Gqy;WnHH|XY%o$tZNqr9WSNTWYc!G zV5jru2HQ?rcbTNC_OdzGw%c9L;?wl?=FNk7J~m;fcZcWZ@-6;ky%xc9=A& zRWw^^YOi+ON<(?2YUbPpiSTFznpRV-&ex-gc0wL7 z=>A(>gU@LABl>)Op)B`veM`=yR;bl_{RTx#%bgwtDyE33%N5O2I`>oR3)cpCL)>gA|V^)Cs*=*4~qM@q2Gcd-?4{rc-*V zHvM*4J1y_<_N8;?_F-b5@M+UG%d)?Mq~}g;hLv}7e&}20>v|ubY=dE#Mi9B)p?AvK zr{tqqPSGO1(;5oe%XOu7s7ovrmws?{iMZ$~^ckN=3~Oau*luFiA$E*C$)01cviI33 z_8B|RF0k*}F90f_UrmUX5PC(Wi9$^7M*q+_dqI&DRdS&Q=LR z&A4u6l(THj`sRzdD6+Sdu$L0S&;2os@zcX6-1wv=+Rd3ePmOdDIn0As;;J^8(?A~O z)(fszBibaQkLWJ0r)~vNM|79PA`wj@#)@vdA);z!#7D*VT@|A)g3j8TRaGk|W-6m| zX6v)mTh$E;e;;Vfw{2;qnK;knznJf5*;niv_5=Hs^1TYvF$?vGV>Q-Mc5g#FlITPi zW%zDnkoB|Nr5w+}!`--t^87x^^apVa58+`vf~W8_p7FE&1wY$g#%nl<*YO74#3`J{ z2lx;l;bWY^m-rgrj?3w-V{;ncT`i}_eL=^%hrJMi5P=YZ5P=YZ5P@qHfim&jQGWj4 zcIE&7*JyO%V}}TY2>clYP~MtsZ6?do{&&0+KWmRt-A$DjZZ{&N??TO(9|6?i$MH+3 ma@*i+z`srex)C9Lce3%_N&Z7W1H%3P)sEM~{r~@b|Nk2S?BMnQ delta 130 zcmZn(XfcprU|?W$DortDU=RQ@Ie-{MGqg=C6q~50D9Q+A12Ir6mBEOi3`iz3#7`_# zXJnjgD5|hoRU(>wF*^r`ATvl4P#rgra0TMFjfLNtC-bZ5a)L}a0K^QFZFr<7C#bNq Ng6w429M3a@82}Y76x;v+