diff --git a/analogainas/evaluators/mlp.py b/analogainas/evaluators/mlp.py index d619f2bc..6cfeb949 100644 --- a/analogainas/evaluators/mlp.py +++ b/analogainas/evaluators/mlp.py @@ -4,7 +4,7 @@ import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset - +from sklearn.preprocessing import StandardScaler from analognas.evaluators import Evaluator from analognas.utils import accuracy_mse @@ -94,7 +94,8 @@ def fit(self, xtrain, ytrain, self.mean = np.mean(ytrain) self.std = np.std(ytrain) - # TODO: Add encoding + scaler = StandardScaler() + _xtrain = scaler.fit_transform(xtrain) _xtrain = xtrain _ytrain = np.array(ytrain) diff --git a/analogainas/evaluators/prepare_data.py b/analogainas/evaluators/prepare_data.py index c39b9191..6d47c12f 100644 --- a/analogainas/evaluators/prepare_data.py +++ b/analogainas/evaluators/prepare_data.py @@ -1,6 +1,7 @@ -import numpy as np +import numpy as np from numpy import genfromtxt from sklearn.model_selection import train_test_split +from sklearn.preprocessing import StandardScaler class AccuracyDataLoader: def __init__(self, dataset_file="dataset_cifar10.csv", transforms=None): @@ -8,13 +9,20 @@ def __init__(self, dataset_file="dataset_cifar10.csv", transforms=None): self.data = genfromtxt(self.dataset_file, delimiter=',') # Applies encoding - if transforms != None: + if transforms is not None: self.data = transforms(self.data) - def get_train(self): + def get_train(self): X = self.data[1:24] y = self.data[27] - slope = self.data[26]-self.data[-1] - - x_train, y_train, x_test, y_test = train_test_split(X, y, test_size=0.2, random_state=0) - return (x_train, y_train), (x_test,y_test), slope \ No newline at end of file + slope = self.data[26] - self.data[-1] + + # Split the data into train and test sets + X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) + + # Scale the data using StandardScaler + scaler = StandardScaler() + X_train = scaler.fit_transform(X_train) + X_test = scaler.transform(X_test) + + return (X_train, y_train), (X_test, y_test), slope diff --git a/analogainas/evaluators/xgboost.py b/analogainas/evaluators/xgboost.py index 8232a6d4..1b531e68 100644 --- a/analogainas/evaluators/xgboost.py +++ b/analogainas/evaluators/xgboost.py @@ -12,7 +12,10 @@ def __init__( model_type = "XGBRanker", load_weight = True, hpo_wrapper=False, - hparams_from_file=False + hparams_from_file=False, + avm_predictor_path = "weights\surrogate_xgboost_avm.json", + std_predictor_path = "weights\surrogate_xgboost_std.json", + ranker_path = "weights\surrogate_xgboost_ranker.json" ): self.model_type = model_type self.hpo_wrapper = hpo_wrapper @@ -34,23 +37,26 @@ def __init__( self.ranker = self.get_ranker() self.avm_predictor = self.get_avm_predictor() self.std_predictor = self.get_std_predictor() + self.ranker_path = ranker_path + self.avm_predictor_path = avm_predictor_path + self.std_predictor_path = std_predictor_path def get_ranker(self): ranker = xgb.XGBRanker(**self.default_hyperparams) if self.load_weight == True: - ranker.load_model(r"C:\Users\hadjer\analog-nas\analogainas\evaluators\weights\surrogate_xgboost_ranker.json") + ranker.load_model(self.ranker_path) return ranker def get_avm_predictor(self): avm_predictor = xgb.XGBRegressor() if self.load_weight == True: - avm_predictor.load_model(r"C:\Users\hadjer\analog-nas\analogainas\evaluators\weights\surrogate_xgboost_avm.json") + avm_predictor.load_model(self.avm_predictor_path) return avm_predictor def get_std_predictor(self): std_predictor = xgb.XGBRegressor() if self.load_weight == True: - std_predictor.load_model(r"C:\Users\hadjer\analog-nas\analogainas\evaluators\weights\surrogate_xgboost_std.json") + std_predictor.load_model(self.std_predictor_path) return std_predictor def fit(self, x_train, y_train, train_info_file="xgboost.txt", hyperparameters=None, epochs=500, verbose=True): diff --git a/analogainas/search_algorithms/ea.py b/analogainas/search_algorithms/ea.py index b4234b0f..9febce00 100644 --- a/analogainas/search_algorithms/ea.py +++ b/analogainas/search_algorithms/ea.py @@ -2,12 +2,8 @@ import random from analogainas.search_spaces.sample import random_sample - class EAOptimizer: - def __init__(self, - max_iter, - population_size, - mutation_prob): + def __init__(self, max_iter, population_size, mutation_prob): self.max_iter = max_iter self.population_size = population_size self.mutation_prob = mutation_prob @@ -18,17 +14,19 @@ def mutate(self, architecture): return architecture def run(self): - D = [self.cs.sample()]*self.population_size + D = [self.cs.sample() for _ in range(self.population_size)] best_f = 0.0 - best_x = [None]*D + best_x = [None] * self.population_size - for i in range(self.n_iter): - # use an "operator" to generate a new candidate solution - # this is "uniform mutation" in MOEA lin - new_x = self.mutate(D) - new_f = self.evaluation(new_x) - if new_f > best_f: - best_f = new_f - best_x = new_x + for _ in range(self.max_iter): + new_x = [self.mutate(x) for x in D] + new_f = [self.evaluation(x) for x in new_x] + + for j in range(self.population_size): + if new_f[j] > best_f: + best_f = new_f[j] + best_x = new_x[j] + + D = new_x return {'best_x': best_x, 'best_f': best_f} diff --git a/analogainas/search_spaces/sample.py b/analogainas/search_spaces/sample.py index f8e564fd..cdfa785a 100644 --- a/analogainas/search_spaces/sample.py +++ b/analogainas/search_spaces/sample.py @@ -1,4 +1,5 @@ -"""High level Sampling strategies.""" +import random +import numpy as np from analogainas.search_spaces.resnet_macro_architecture import Network from analogainas.search_spaces.config_space import ConfigSpace from analogainas.search_algorithms.worker import Worker @@ -9,6 +10,36 @@ EPOCHS = 40 LEARNING_RATE = 0.05 +def latin_hypercube_sample(dataset, n): + """Latin Hypercube Sampling of n architectures from ConfigSpace.""" + cs = ConfigSpace(dataset) + num_parameters = len(cs.get_hyperparameters()) + ranges = np.arange(0, 1, 1/n) + + sampled_architectures = [] + for _ in range(n): + config = {} + for i, hyperparameter in enumerate(cs.get_hyperparameters()): + min_val, max_val = hyperparameter.lower, hyperparameter.upper + val_range = max_val - min_val + offset = random.uniform(0, val_range/n) + config[hyperparameter.name] = min_val + ranges[_] * val_range + offset + sampled_architectures.append(config) + + keys = sampled_architectures[0].keys() + + for config in sampled_architectures: + model = Network(config) + model_name = "resnet_{}_{}".format(config["M"], get_nb_convs(config)) + + with open("./configs/"+model_name+".config", + 'w', newline='') as output_file: + dict_writer = csv.DictWriter(output_file, keys) + dict_writer.writeheader() + dict_writer.writerows(config) + + train(model, model_name, LEARNING_RATE, EPOCHS) + def random_sample(dataset, n): """Randomly samples n architectures from ConfigSpace.""" @@ -35,4 +66,4 @@ def ea_sample(dataset, n, n_iter): using an evolutionary algorithm.""" cs = ConfigSpace(dataset) worker = Worker(dataset, cs, 3, n_iter) - worker.search(population_size=n) + worker.search(population_size=n) \ No newline at end of file diff --git a/analogainas/search_spaces/train.py b/analogainas/search_spaces/train.py index 4203da7c..5ec259b4 100644 --- a/analogainas/search_spaces/train.py +++ b/analogainas/search_spaces/train.py @@ -22,7 +22,6 @@ continue_analog = True device = 'cuda' if torch.cuda.is_available() else 'cpu' - def create_rpu_config(g_max=25, tile_size=256, dac_res=256, diff --git a/analogainas/utils.py b/analogainas/utils.py index 26cc0614..a56ece7b 100644 --- a/analogainas/utils.py +++ b/analogainas/utils.py @@ -5,7 +5,6 @@ ALPHA = 0.05 - def accuracy_mse(prediction, target, scale=100.0): prediction = prediction.detach() * scale target = (target) * scale