def get_conv_dense_model(): graph = Graph((32, 32, 3), False) output_node_id = 0 output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubFlatten(), output_node_id) output_node_id = graph.add_layer(StubDropout(Constant.DENSE_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(graph.node_list[output_node_id].shape[0], 5), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(5, 5), output_node_id) graph.add_layer(StubSoftmax(), output_node_id) graph.produce_model().set_weight_to_graph() return graph
def get_conv_dense_model(): graph = Graph((32, 32, 3), False) output_node_id = 0 output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubFlatten(), output_node_id) output_node_id = graph.add_layer(StubDropout(Constant.DENSE_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(graph.node_list[output_node_id].shape[0], 5), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(5, 5), output_node_id) graph.add_layer(StubSoftmax(), output_node_id) graph.produce_model().set_weight_to_graph() return graph
def get_add_skip_model(): graph = Graph((5, 5, 3), False) output_node_id = 0 output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.CONV_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.CONV_DROPOUT_RATE), output_node_id) temp_node_id = output_node_id output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.CONV_DROPOUT_RATE), output_node_id) temp_node_id = graph.add_layer(StubConv(3, 3, 1), temp_node_id) output_node_id = graph.add_layer(StubAdd(), [output_node_id, temp_node_id]) temp_node_id = output_node_id output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.CONV_DROPOUT_RATE), output_node_id) temp_node_id = graph.add_layer(StubConv(3, 3, 1), temp_node_id) output_node_id = graph.add_layer(StubAdd(), [output_node_id, temp_node_id]) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.CONV_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubFlatten(), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(graph.node_list[output_node_id].shape[0], 5), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.DENSE_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(5, 5), output_node_id) output_node_id = graph.add_layer(StubDropout(constant.DENSE_DROPOUT_RATE), output_node_id) graph.add_layer(StubSoftmax(), output_node_id) graph.produce_model().set_weight_to_graph() return graph
def search(self, x_train, y_train, x_test, y_test): """Override parent's search function. First model is randomly generated""" if not self.history: model = DefaultClassifierGenerator(self.n_classes, self.input_shape).generate() self.add_model(model, x_train, y_train, x_test, y_test) pickle_to_file(self, os.path.join(self.path, 'searcher')) else: model = self.load_best_model() new_graphs = transform(Graph(model, False)) new_models = [] for graph in new_graphs: nm_graph = Graph(model, True) for args in graph.operation_history: getattr(nm_graph, args[0])(*list(args[1:])) new_models.append(nm_graph.produce_model()) new_models = self._remove_duplicate(list(new_models)) for model in new_models: if self.model_count < constant.MAX_MODEL_NUM: self.add_model(model, x_train, y_train, x_test, y_test) pickle_to_file(self, os.path.join(self.path, 'searcher')) backend.clear_session() return self.load_best_model()
def cross_validate(self, x_all, y_all, n_splits, trainer_args=None): """Do the n_splits cross-validation for the input.""" if trainer_args is None: trainer_args = {} if constant.LIMIT_MEMORY: config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) init = tf.global_variables_initializer() sess.run(init) backend.set_session(sess) k_fold = StratifiedKFold(n_splits=n_splits, shuffle=False, random_state=7) ret = [] y_raw_all = y_all y_all = self.y_encoder.transform(y_all) model = self.load_searcher().load_best_model() for train, test in k_fold.split(x_all, y_raw_all): graph = Graph(model, False) backend.clear_session() model = graph.produce_model() ModelTrainer(model, x_all[train], y_all[train], x_all[test], y_all[test], False).train_model(**trainer_args) scores = model.evaluate(x_all[test], y_all[test], verbose=self.verbose) if self.verbose: print('Score:', scores[1]) ret.append(scores[1] * 100) return np.array(ret)
def copy_conv_model(model): """Return copied convolution model Args: model: the model we want to copy Returns: The copied model """ graph = Graph(model) return graph.produce_model()
def maximize_acq(self): model_ids = self.search_tree.adj_list.keys() target_graph = None father_id = None descriptors = self.descriptors pq = PriorityQueue() temp_list = [] for model_id in model_ids: accuracy = self.get_accuracy_by_id(model_id) temp_list.append((accuracy, model_id)) temp_list = sorted(temp_list) if len(temp_list) > 5: temp_list = temp_list[:-5] for accuracy, model_id in temp_list: model = self.load_model_by_id(model_id) graph = Graph(model, False) pq.put(Elem(accuracy, model_id, graph)) t = 1.0 t_min = self.t_min alpha = 0.9 max_acq = -1 while not pq.empty() and t > t_min: elem = pq.get() ap = math.exp((elem.accuracy - max_acq) / t) if ap > random.uniform(0, 1): graphs = transform(elem.graph) graphs = list( filter(lambda x: x.extract_descriptor() not in descriptors, graphs)) if not graphs: continue for temp_graph in graphs: temp_acq_value = self.acq(temp_graph) pq.put(Elem(temp_acq_value, elem.father_id, temp_graph)) descriptors[temp_graph.extract_descriptor()] = True if temp_acq_value > max_acq: max_acq = temp_acq_value father_id = elem.father_id target_graph = temp_graph t *= alpha model = self.load_model_by_id(father_id) nm_graph = Graph(model, True) if self.verbose: print('Father ID: ', father_id) print(target_graph.operation_history) for args in target_graph.operation_history: getattr(nm_graph, args[0])(*list(args[1:])) return nm_graph.produce_model(), father_id
def search(self, x_train, y_train, x_test, y_test): if not self.history: model = DefaultClassifierGenerator(self.n_classes, self.input_shape).generate( self.default_model_len, self.default_model_width) history_item = self.add_model(model, x_train, y_train, x_test, y_test) self.search_tree.add_child(-1, history_item['model_id']) graph = Graph(model) self.init_search_queue = [] # for child_graph in transform(graph): # self.init_search_queue.append((child_graph, history_item['model_id'])) self.init_gpr_x.append(graph.extract_descriptor()) self.init_gpr_y.append(history_item['accuracy']) pickle_to_file(self, os.path.join(self.path, 'searcher')) return if self.init_search_queue: graph, father_id = self.init_search_queue.pop() model = graph.produce_model() history_item = self.add_model(model, x_train, y_train, x_test, y_test) self.search_tree.add_child(father_id, history_item['model_id']) self.init_gpr_x.append(graph.extract_descriptor()) self.init_gpr_y.append(history_item['accuracy']) pickle_to_file(self, os.path.join(self.path, 'searcher')) return if not self.init_search_queue and not self.gpr.first_fitted: self.gpr.first_fit(self.init_gpr_x, self.init_gpr_y) new_model, father_id = self.maximize_acq() history_item = self.add_model(new_model, x_train, y_train, x_test, y_test) self.search_tree.add_child(father_id, history_item['model_id']) self.gpr.incremental_fit( Graph(new_model).extract_descriptor(), history_item['accuracy']) pickle_to_file(self, os.path.join(self.path, 'searcher'))