def load_model_gates(model_path, params): state_dict = torch.load(model_path) model_params = params['model_params'] fake_init_gates = GateInitializer.get_fake_init_gates(params['gate_init_multi_heuristic_params']['num_gates']) model = DepthOneModel(fake_init_gates, model_params) model.load_state_dict(state_dict) return model.get_gates()
def push_converged_boundaries_given_data_input_and_params( params, data_input, stepsize, n_steps, path_to_params): start_time = time.time() print('%d samples in the training data' % len(data_input.x_tr)) with open(os.path.join(params['save_dir'], 'tracker.pkl'), 'rb') as f: tracker = pickle.load(f) with open(os.path.join(params['save_dir'], 'transformer.pkl'), 'rb') as f: umapper = pickle.load(f) # FOR DEBUGGING ONLY #params['transform_params']['cells_to_subsample'] = 10 data_input.embed_data(\ umapper, cells_to_subsample=params['transform_params']['cells_to_subsample'], use_labels_to_transform_data=params['transform_params']['use_labels_to_transform_data'] ) data_input.normalize_data() data_input.convert_all_data_to_tensors() model = DepthOneModel([[['D1', 0, 0], ['D2', 0, 0]]], params['model_params']) model.load_state_dict( torch.load(os.path.join(params['save_dir'], 'model.pkl'))) init_acc = tracker.metrics['tr_acc'][-1] cur_best_acc = init_acc starting_gate = model.get_gates()[0] cur_gate = copy.deepcopy(starting_gate) cur_best_gate = copy.deepcopy(cur_gate) print('Starting gate:', starting_gate) counter = 0 for left_step in range(n_steps): cur_gate[0] = starting_gate[0] - left_step * stepsize for right_step in range(n_steps): cur_gate[1] = starting_gate[1] + right_step * stepsize for down_step in range(n_steps): cur_gate[2] = starting_gate[2] - down_step * stepsize for up_step in range(n_steps): cur_gate[3] = starting_gate[3] + up_step * stepsize model = DepthOneModel([[['D1', cur_gate[0], cur_gate[1]], ['D2', cur_gate[2], cur_gate[3]]]], params['model_params']) fit_classifier_params( model, data_input, params['train_params']['learning_rate_classifier']) # model.nodes = None # model.init_nodes([[['D1', cur_gate[0], cur_gate[1]], ['D2', cur_gate[2], cur_gate[3]]]]) cur_acc = compute_tr_acc(model, data_input) #cur_acc = performance_tracker.metrics['tr_acc'][-1] counter += 1 #print(counter) #print(cur_gate) #print(cur_acc) if cur_acc > cur_best_acc: cur_best_acc = cur_acc cur_best_gate = copy.deepcopy(cur_gate) print('Final acc %.3f, Initial acc %.3f' % (cur_best_acc, init_acc)) print('Init/final gates', starting_gate, cur_best_gate) print('time taken: %d' % (time.time() - start_time)) return cur_best_acc, starting_gate, cur_best_gate