Beispiel #1
0
def get_next_gate_tree_by_log_loss(unused_gate_trees, data_input, params, model=None):
    if model:
        losses = []
        for gate_tree in unused_gate_trees:
            dummy_model_state = deepcopy(model.state_dict())
            dummy_model = DepthOneModel(model.get_gate_tree(), params['model_params'])
            dummy_model.load_state_dict(dummy_model_state)

            dummy_model.add_node(gate_tree)
            performance_tracker = run_train_model(dummy_model, params['train_params'], data_input)
            losses.append(dummy_model(data_input.x_tr, data_input.y_tr)['log_loss'].cpu().detach().numpy())
        losses = np.array(losses)
        best_gate_idx = np.argmin(losses[~np.isnan(losses)])
    else:
        losses = []
        for gate_tree in unused_gate_trees:
            model = DepthOneModel([gate_tree], params['model_params'])
            performance_tracker = run_train_model(model, params['train_params'], data_input)
            losses.append(model(data_input.x_tr, data_input.y_tr)['log_loss'].cpu().detach().numpy())

        losses = np.array(losses)
        best_gate_idx = np.argmin(losses[~np.isnan(losses)])
    best_gate = unused_gate_trees[best_gate_idx]
    del unused_gate_trees[best_gate_idx]
    return best_gate, unused_gate_trees
Beispiel #2
0
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()
Beispiel #3
0
def get_next_best_gate(remaining_gates, data_input, params, model):
    losses = []
    trackers = []
    for gate in remaining_gates:
        dummy_model_state = deepcopy(model.state_dict())
        init_gates = rectangularize_gates(model)
        dummy_model = DepthOneModel(init_gates, params['model_params'])
        dummy_model.load_state_dict(dummy_model_state)
        dummy_model.add_node(gate)
        trackers.append(run_train_model(
            dummy_model, params['train_params'], data_input
        ))
        losses.append(
            dummy_model(
                data_input.x_tr, data_input.y_tr
            )['loss'].cpu().detach().numpy()
        )
    losses = np.array(losses)
    best_gate_idx = np.argmin(losses[~np.isnan(losses)])

    best_gate = remaining_gates[best_gate_idx]
    remaining_gates = [
        gate for g, gate in enumerate(remaining_gates)
        if not g == best_gate_idx
    ]
    best_tracker = trackers[best_gate_idx]
    return best_gate, remaining_gates, best_tracker
def load_saved_results(path_to_params, ret_params_too=False):
    start_time = time.time()

    params = TransformParameterParser(path_to_params).parse_params()
    print(params)

    torch.manual_seed(params['random_seed'])
    np.random.seed(params['random_seed'])

    data_input = load_and_prepare_data_input(params)

    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')))
    if ret_params_too:
        return data_input, model, params
    return data_input, model
def load_saved_model_and_matching_data_input(path_to_params):
    def set_random_seeds(params):
        torch.manual_seed(params['random_seed'])
        np.random.seed(params['random_seed'])

    start_time = time.time()

    params = TransformParameterParser(path_to_params).parse_params()
    print(params)

    #evauntually uncomment this leaving asis in order ot keep the same results as before to compare.
    set_random_seeds(params)

    data_input = DataInput(params['data_params'])
    data_input.split_data()
    print('%d samples in the training data' % len(data_input.x_tr))

    with open(os.path.join(params['save_dir'], 'trackers.pkl'), 'rb') as f:
        trackers = 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')))
    return params, model, data_input, umapper
Beispiel #6
0
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