Esempio n. 1
0
def main(args):
    trainloader, testloader, num_classes = load_data(batch_size=args.batch_size)
    net = ResNet18()
    results = run_root(
        net, args.batch_size, trainloader, testloader, n_epoch=args.n_epoch, 
        lr=args.learning_rate, weight_decay=0, checkpoint=125, noisy_train_stat=True)
    save_results(*results, method='root_sgd')
Esempio n. 2
0
def train(args):
    config = Config(args)
    train, test, word_to_id, id_to_word, embeddings = utils.load_from_file_basic(
    )

    config.word_to_id = word_to_id
    config.id_to_word = id_to_word
    utils.save(config.output_path, word_to_id, id_to_word)

    handler = logging.FileHandler(config.log_output)
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(
        logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
    logging.getLogger().addHandler(handler)

    with tf.Graph().as_default():
        logger.info('Building model...', )
        start = time.time()
        model = RNNModel(config, embeddings)
        logger.info('took %.2f seconds', time.time() - start)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()

        with tf.Session() as session:
            session.run(init)
            model.fit(session, saver, train, test)

            sentences, masks, predictions = model.output(session, train)
            originals, predictions = lookup_words(predictions, sentences,
                                                  id_to_word)
            output = zip(originals, masks, predictions)

            with open('results.txt', 'w') as f:
                utils.save_results(f, output)
def test(config):
    load_dir = f'{config["save_dir"]}/weights/'
    model_list = sorted(
        list(map(lambda m: int(m.split('.')[0]), os.listdir(load_dir))))
    if hr_test is None:
        with open(f'{config["save_dir"]}/results.json') as f:
            results = json.load(f)
            start_from_epoch = list(
                results["test_acc"]
            )[-1] if results["test_acc"]['1'] is not None else 0
        model_list = model_list[int(start_from_epoch):]

    for epoch in model_list:
        # print(epoch)
        acc = 0
        model = f'{load_dir}{epoch}.pth'
        state_dict = torch.load(model)
        config['network'].load_state_dict(state_dict)
        config['network'].eval()
        Y_batch, Predicted = [], []
        with torch.no_grad():
            for idx, (X_batch, y_batch) in enumerate(config['data_test']):
                X_batch = X_batch.to(config['device']).view(
                    X_batch.shape[0], -1)
                y_batch = y_batch.to(config['device'])
                y_pred, _, _, _ = config['network'](X_batch)
                predicted = torch.argmax(y_pred.data, 1)
                acc += (predicted == y_batch).sum().item()
                Y_batch += y_batch
                Predicted += predicted
        test_acc = 100 * acc / config['len_test']
        # test_acc_per_epoch[str(epoch)] = test_acc
        utils.save_results(config, None, test_acc, str(epoch), hr_test=hr_test)
        print(f'epoch: {epoch} - test accuracy: {round(test_acc, 3):.3f}')
Esempio n. 4
0
def run(result_csv_path):
    train_x, train_y = load_data(train_csv_path, True)
    test_x = load_data(test_csv_path, False)
    print('load data successfully ......')

    rf = RandomForestRegressor(
        n_estimators=2000,  #[1500,2000]
        min_samples_split=2,
        max_depth=15,  # [10,15]
        n_jobs=-1)
    rf.fit(train_x, train_y)
    ###### save model ##################
    joblib.dump(rf, 'weights/' + Model_Name + '.m')

    y_pred = rf.predict(test_x)

    ####### save_results ###########################
    save_results(result_csv_path, y_pred)

    ###### generate report #######################
    feature_importances = rf.feature_importances_
    dic_feature_importances = dict(zip(fields, feature_importances))
    dic = sorted(dic_feature_importances.iteritems(),
                 key=lambda d: d[1],
                 reverse=True)
    print('feature_importances:')
    for i in range(len(dic)):
        print(dic[i][0] + ":\t" + str(dic[i][1]))
Esempio n. 5
0
    def train(self, sess, epochs, train_dataset, validation_dataset, output_dir):

        merged_summaries = tf.summary.merge_all()
        
        
        
        train_writer = tf.summary.FileWriter(output_dir + '/train', sess.graph)
        #train_writer = tf.train.SummaryWriter(output_dir + '/train', sess.graph)

        train_metric = self.evaluate(sess, train_dataset)
        validation_metric = self.evaluate(sess, validation_dataset)
        train_results_file_path = os.path.join(output_dir, "train_result.csv")

        plt.subplot(2, 1, 1)
        plt.title('Training data set')
        plt.axis([0, epochs, 0, train_metric[0]])

        plt.subplot(2, 1, 2)
        plt.title('Vaidation data set')
        plt.axis([0, epochs, 0, validation_metric[0]])

        plt.ion()
        logger.info('Start Training')

        steps_in_epoch = train_dataset.num_examples // self.batch_size
        # self.get_g_structure(sess,train_dataset)

        for epoch in xrange(0, epochs):
            for i in xrange(0, steps_in_epoch):
                feed_dict = self.fill_feed_dict(train_dataset, self.batch_size)
                _= sess.run([self.train_op], feed_dict=feed_dict)

            summ = sess.run(merged_summaries)
            train_writer.add_summary(summ, epoch)

            train_dataset.reset_epoch(permute=True)

            sess.run([self.global_step_update_op])

            if epoch % 10 == 0:
                train_metric = self.evaluate(sess, train_dataset)
                validation_metric = self.evaluate(sess, validation_dataset)
                plt.subplot(2, 1, 1)
                plt.scatter(epoch, train_metric[0], color='red', marker=".")
                plt.scatter(epoch, train_metric[1], color='blue', marker=".")

                plt.subplot(2, 1, 2)
                plt.scatter(epoch, validation_metric[0], color='red', marker=".")
                plt.scatter(epoch, validation_metric[1], color='blue', marker=".")
                learning_rate = self.get_learning_rate(sess)
                plt.pause(0.05)
                logger.info(
                    "Epoch: {:}, Learning rate {:.8f}  Train RMSE: {:.4f}, Train AAE: {:.4f} Validation RMSE {:.4f}, Validation AAE {:.4f}".
                        format(epoch, learning_rate[0], train_metric[0],
                               train_metric[1], validation_metric[0],
                               validation_metric[1],
                               precision=8))

        save_results(train_results_file_path, train_dataset.labels, self.predict(sess, train_dataset))
        logger.info('Training Finished')
def main(data_path="data/split/", feature_path="data/features/", out_path="data/pca/"):
    X_train, X_test, y_train, y_test = read_data(data_path)

    params = read_params("params.yaml", "pca")
    pca = PCA(**params).fit(X_train)

    train_feature = pd.DataFrame(pca.transform(X_train))
    test_feature = pd.DataFrame(pca.transform(X_test))
    train_feature["class"] = y_train
    test_feature["class"] = y_test

    if not os.path.isdir(feature_path):
        os.mkdir(feature_path)
    train_feature.to_csv(f"{feature_path}train.csv", index=False)
    test_feature.to_csv(f"{feature_path}test.csv", index=False)
    save_results(out_path, pca, None)

    print(f"Finished Feature Engineering:\nStats:")
    print(f"\tExplained Variance: {pca.explained_variance_}")
    print(f"\tExplained Variance Ratio: {pca.explained_variance_ratio_}")

    log_experiment(
        out_path,
        metrics=dict(
            explained_variance_=pca.explained_variance_,
            explained_variance_ratio_=pca.explained_variance_ratio_,
        ),
    )
Esempio n. 7
0
def main(_):

    # for rm in [0.5, 0.6, 0.7, 0.8]:
    #     for ds_ind in range(5):
    #         config.data_rm = rm
    #         config.ds_ind = ds_ind
    # Directory generating.. for saving
    prepare_dirs(config)
    prepare_config_date(config, config.ds_ind)
    # Random seed settings
    rng = np.random.RandomState(config.random_seed)
    tf.set_random_seed(config.random_seed)

    # Model training
    trainer = Trainer(config, rng)
    save_config(config.model_dir, config)
    config.load_path = config.model_dir
    if config.is_train:
        trainer.train(save=True)
        result_dict = trainer.test()
    else:
        if not config.load_path:
            raise Exception("[!] You should specify `load_path` to "
                            "load a pretrained model")
        result_dict = trainer.test()
    save_results(config.result_dir, result_dict)
    accept_rate = evaluate_result(result_dict, method='KS-test', alpha=0.1)
    kl_div = evaluate_result(result_dict, method='KL')
    wasser_dis = evaluate_result(result_dict, method='wasser')
    sig_test = evaluate_result(result_dict, method='sig_test')
    print("The accept rate of KS test is ", accept_rate)
    print("The final KL div is ", kl_div)
    print("The wasser distance is ", wasser_dis)
    print("The AR of Sign Test is ", sig_test)
def run_dropout_experiment(model_dict, train_dict, out_dir):
    np.random.seed(12345)
    results = defaultdict(list)
    model_dict['dropout'] = True
    kernel_size = model_dict['kernel_size']
    padding = model_dict['padding']
    label = f'kernel={kernel_size}x{kernel_size}, pad={padding}'

    model = ConvNet(**model_dict)

    train_dict['callbacks'][1] = ModelDump(
        output_dir=os.path.join(out_dir, label))
    train_dict['callbacks'][2] = SaveBestModel(
        output_dir=os.path.join(out_dir, label))
    trainer = Trainer(model, **train_dict)

    start_time = time()
    trainer.train_loop()
    time_period = (time() - start_time) / 60

    log_data = trainer.logger.logging_data

    results['model_dict'].append(model_dict)
    results['train_dict'].append(train_dict)
    results['time'].append(time_period)
    results['label'].append(label)
    results['log_data'].append(log_data)

    calc_test_accuracy(model, test_data, train_dict)

    save_results(out_dir, results_dict=results)
    return results
def run_mlp_conv_compare_experiment(model_dict_conv, model_dict_mlp,
                                    train_dict, out_dir, test_data):
    np.random.seed(12345)
    results = defaultdict(list)

    for model, model_dict in [(MlpNet(**model_dict_mlp), model_dict_mlp),
                              (ConvNet(**model_dict_conv), model_dict_conv)]:
        label = f'model={model.name}'
        print(f'{label}')
        train_dict['callbacks'][1] = ModelDump(
            output_dir=os.path.join(out_dir, label))
        train_dict['callbacks'][2] = SaveBestModel(
            output_dir=os.path.join(out_dir, label))
        trainer = Trainer(model, **train_dict)

        start_time = time()
        trainer.train_loop()
        time_period = (time() - start_time) / 60
        log_data = trainer.logger.logging_data

        results['model_dict'].append(model_dict)
        results['train_dict'].append(train_dict)
        results['time'].append(time_period)
        results['label'].append(label)
        results['log_data'].append(log_data)

        calc_test_accuracy(model, test_data, train_dict)

    save_results(out_dir, results_dict=results)
    return results
Esempio n. 10
0
def run_experiment():

    environment = 'CartPole-v0'
    seed = 1
    episodes = 500

    returns = []

    agent = UpsideDownAgent(environment)

    for e in range(episodes):
        for i in range(100):
            agent.train_behaviour_function()

        for i in range(15):
            tmp_r = []
            exploratory_commands = agent.sample_exploratory_commands(
            )  # Line 5 Algorithm 1
            desired_return = exploratory_commands[0]
            desired_horizon = exploratory_commands[1]
            r = agent.generate_episode(environment, e, desired_return,
                                       desired_horizon, False)
            tmp_r.append(r)

        print(np.mean(tmp_r))
        returns.append(np.mean(tmp_r))

        exploratory_commands = agent.sample_exploratory_commands()

    agent.generate_episode(environment, 1, 200, 200, True)

    utils.save_results(environment, 'upside_down_agent', seed, returns)
    utils.save_trained_model(environment, seed, agent.behaviour_function)
def run_experiment(experiment_generator, out_dir, test_data):
    np.random.seed(12345)
    results = defaultdict(list)

    for i, (model_dict, train_dict, exp_name,
            value) in enumerate(experiment_generator()):
        label = f'{exp_name}={value}'
        print(f'{i}. {label}')

        train_dict['callbacks'][1] = ModelDump(
            output_dir=os.path.join(out_dir, label))
        train_dict['callbacks'][2] = SaveBestModel(
            output_dir=os.path.join(out_dir, label))
        model = ConvNet(**model_dict)
        trainer = Trainer(model, **train_dict)

        start_time = time()
        trainer.train_loop()
        time_period = (time() - start_time) / 60

        log_data = trainer.logger.logging_data

        results['model_dict'].append(model_dict)
        results['train_dict'].append(train_dict)
        results['time'].append(time_period)
        results['label'].append(label)
        results['log_data'].append(log_data)

        calc_test_accuracy(model, test_data, train_dict)

    save_results(out_dir, results_dict=results)
    return results
Esempio n. 12
0
def main():
    # loads a lot of default parser values from the 'parser' file
    parser = get_parser()

    # get args from parser as an object
    args = parser.parse_args()
    args.device = 'cuda' if args.cuda else 'cpu'

    # initialize seeds
    utils.init_seed(args.seed)

    # print('loader stuff', args)
    loader = Loader.IncrementalLoader(args, seed=args.seed)
    # print('loader stuff after after', args)
    n_inputs, n_outputs, n_tasks = loader.get_dataset_info()

    # setup logging
    # logging is from 'misc_utils.py' from 'utils' folder
    timestamp = utils.get_date_time() # this line is redundant bcz log_dir already takes care of it
    args.log_dir, args.tf_dir = utils.log_dir(args, timestamp) # stores args into "training_parameters.json"

    # create the model neural net
    model = Model.Net(n_inputs, n_outputs, n_tasks, args, innerlr=args.opt_lr, outerlr=args.alpha_init)
    
    # make model cuda-ized if possible
    model.net.to(args.device)            

    # for all the CL baselines
    result_val_t, result_val_a, result_test_t, result_test_a, spent_time = life_experience(model, loader, args)

    # save results in files or print on terminal
    save_results(args, result_val_t, result_val_a, result_test_t, result_test_a, model, spent_time)
Esempio n. 13
0
def main():
    args = parser.parse_args()
    model_path = args.model
    dataset_size = args.size
    batch_size = args.batch_size
    backend_name = args.backend
    print_freq = args.print_freq

    # Load ONNX model
    onnx_protobuf = onnx.load(model_path)
    # Change batch size defined in model to value passed by user as argument
    onnx_protobuf.graph.input[0].type.tensor_type.shape.dim[0].dim_value = batch_size

    ng_model = import_onnx_model(onnx_protobuf)
    model_batch, model_channels, model_height, model_width = ng_model.get_parameters()[0].shape

    # Generate synthetic dataset filled with random values
    dataset = generate_data(count=dataset_size,
                            batch_size=model_batch,
                            image_channels=model_channels,
                            image_height=model_height,
                            image_width=model_width)
    dataset = [(img, 0) for img in dataset]

    perf_metrics = evaluate(backend_name, ng_model, dataset, batch_size, print_freq)
    save_results('results/', args.output_file, {key: val.data for key, val in perf_metrics.items()})
Esempio n. 14
0
def evaluate(args):
    config = Config(args)

    train, test, word_to_id, id_to_word, embeddings = utils.load_from_file_basic(
    )
    config.word_to_id = word_to_id
    config.id_to_word = id_to_word

    with tf.Graph().as_default():
        logger.info('Building model...', )
        start = time.time()
        model = RNNModel(config, embeddings)

        logger.info('took %.2f seconds', time.time() - start)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver()

        with tf.Session() as session:
            session.run(init)
            saver.restore(session, model.config.model_path)

            sentences, masks, predictions = model.output(session, train)
            originals, predictions = lookup_words(predictions, sentences,
                                                  id_to_word)
            output = zip(originals, masks, predictions)

            with open('eval_results.txt', 'w') as f:
                utils.save_results(f, output)
Esempio n. 15
0
def run_agent_es(n_agents=50, n_generations=100, save=True):
    """
    implmentation of the evolutionary strategy (ES) algorithm

    :param n_agents: number of agent per generation
    :param n_generations: number of generations
    :param save: save weights and scores at the end of training
    :return:
    """
    n_weights = len(Agent().model.get_weights())

    agents_weights = np.empty((n_generations, n_agents, n_weights),
                              dtype=np.ndarray)
    scores = np.empty((n_generations, n_agents), dtype=float)

    children = np.empty((n_generations, n_weights), dtype=np.ndarray)

    # initialize agents
    agents = [Agent() for _ in range(n_agents)]

    for i in range(n_generations):
        agents_weights[i] = np.array([a.model.get_weights() for a in agents],
                                     dtype=np.ndarray)

        for j, agent in enumerate(agents):  # TODO parallelize
            scores[i][j] = agent.run_agent()

        child = crossover_function(agents, scores[i])
        children[i] = np.array(child, dtype=np.ndarray).reshape(n_weights)
        agents = generate_population(child, n_agents, agents, noise=0.1)

    if save:
        save_results(agents_weights, scores)

    return agents_weights, scores, children
Esempio n. 16
0
def evaluate_model(train, n_input, epochs, batch_size):
    # fit model
    model = build_model(train, n_input, epochs, batch_size)
    # history is a list of weekly data
    history = [x for x in train]
    # walk-forward validation over each week
    prediction = list()
    for i in range(len(test)):
        # predict the week
        yhat_sequence = forecast_multichannel(model, history, n_input)
        # store the predictions
        prediction.append(yhat_sequence)
        # get real observation and add to history for predicting the next week
        history.append(test[i, :])
    # evaluate predictions days for each week
    # get array of predictions
    prediction = np.array(prediction)
    prediction = np.ravel(prediction)
    # get array of actual values from test set
    actual = test[:, :, 0].copy()
    actual[actual == 0] = np.nanmean(actual)
    actual = np.ravel(actual)
    # print test parameters
    print('Epochs: %d Batch Size: %d' % (epochs, batch_size))
    # calaculate and print scores
    scores = calculate_various_scores(actual, prediction)
    # save prediction plot
    save_prediction_plot(actual, prediction, epochs, batch_size)
    # save results
    result = [epochs, batch_size, scores, prediction]
    save_results('temp/multichannel_gridsearch_summary.csv', result)
    # clear keras model
    K.clear_session()
Esempio n. 17
0
def main(args):

    # ensure directories are setup
    dirs = [args.data_dir, args.ckpt_dir]
    prepare_dirs(dirs)

    # create base model
    model = get_base_model()

    # define params
    params = {
        # '0_dropout': ['uniform', 0.1, 0.5],
        # '0_act': ['choice', ['relu', 'selu', 'elu', 'tanh', 'sigmoid']],
        # '0_l2': ['log_uniform', 1e-1, 2],
        # '2_act': ['choice', ['selu', 'elu', 'tanh', 'sigmoid']],
        # '2_l1': ['log_uniform', 1e-1, 2],
        '2_hidden': ['quniform', 512, 1000, 1],
        '4_hidden': ['quniform', 128, 512, 1],
        'all_act': ['choice', [[0], ['choice', ['selu', 'elu', 'tanh']]]],
        'all_dropout': ['choice', [[0], ['uniform', 0.1, 0.5]]],
        'all_batchnorm': ['choice', [0, 1]],
        'all_l2': ['uniform', 1e-8, 1e-5],
        'optim': ['choice', ["adam", "sgd"]],
        'lr': ['uniform', 1e-3, 8e-3],
        # 'batch_size': ['quniform', 32, 128, 1]
    }

    # instantiate hyperband object
    hyperband = Hyperband(args, model, params)

    # tune
    results = hyperband.tune()

    # dump results
    save_results(results)
Esempio n. 18
0
    def get_org_repos(self, dept_org_dict, fields):
        """
        takes in dept_org_dict: dict w/ department keys, github org values
        return df with every repo for every org as an observation
        """
        query = "https://api.github.com/orgs/{0}/repos?page={1}&per_page=100"

        #turn result into df?
        col_list = ["repo_name", "department"]
        col_list.extend(fields)
        result = pd.DataFrame(columns=col_list)

        for department in dept_org_dict.keys():
            for org in dept_org_dict[department]:
                page_results = utils.iterate_pages(query, [
                    org,
                ])

                try:
                    result = result.append(utils.format_observations(
                        page_results, department, fields),
                                           ignore_index=True)
                except Exception as e:
                    print(e)
                    print(f"broke at: {org}")
                    pass

        utils.save_results(result, "dept_repo_df")
        return result
Esempio n. 19
0
def run():
    te_x = load_data(TEST,False)
    df_te = pd.read_csv(TEST,header=0)
    
    te_predType_list = predict_type()
    te_pred_list = []
    for i in range(te_x.shape[0]):
        cur_pred_type = te_predType_list[i]
        if cur_pred_type == 0:
            te_pred_list.append(0)
            continue
        income = str(df_te['INCOME'][i])
        entertainment = str(df_te['ENTERTAINMENT'][i])
        baby = str(df_te['BABY'][i])
        shopclass = str(df_te['SHOPPING'][i])
        key = income+" "+entertainment+" "+baby+" "+shopclass
        lon = df_te['LON'][i]
        lat = df_te['LAT'][i]
        num_clusters = CLUSTER_DICT[key]
        if num_clusters > 1:
            cur_te_pred = run_cluster_model(key,lon,lat,te_x[i],num_clusters)
        else:
            cur_te_pred = run_simple_model(key,te_x[i])
        te_pred_list.append(cur_te_pred)
        print("te_idx:"+str(i))

    print(str(len(te_pred_list)))
    save_results(result_csv_path,te_pred_list)
Esempio n. 20
0
def batch_request_data(params,
                       save_results_to_disk=True,
                       verbose=False):
    results = dict()

    while True:
        print('Request n°{}'.format(params['current'] + 1))

        result = request_data(params)

        if len(result) == 0:
            break
        else:
            for game in result:
                app_id = game['id']
                results[app_id] = game

            params['current'] += 1

    if verbose:
        print(results)

    if save_results_to_disk:
        save_results(results=results)

    return results
def execute(ii, args):
    res = optimize.minimize(optimizerFunction,
                            [args["x1"], args["x2"], args["x3"], args["x4"]])
    log("Result:", res.x)

    save_results(OUTFOLDER, res.x, ii, arg_dict.get("file_data"))
    log("Finished Program: ", os.getpid())
Esempio n. 22
0
def main(data_path='data/split/',
         feature_path='data/features/',
         out_path='data/pca/'):
    X_train, X_test, y_train, y_test = read_data(data_path)

    params = read_params('params.yaml', 'pca')
    pca = PCA(**params).fit(X_train)

    train_feature = pd.DataFrame(pca.transform(X_train))
    test_feature = pd.DataFrame(pca.transform(X_test))
    train_feature['class'] = y_train
    test_feature['class'] = y_test

    if not os.path.isdir(feature_path):
        os.mkdir(feature_path)
    train_feature.to_csv(f'{feature_path}train.csv', index=False)
    test_feature.to_csv(f'{feature_path}test.csv', index=False)
    save_results(out_path, pca, None)

    print(f'Finished Feature Engineering:\nStats:')
    print(f'\tExplained Variance: {pca.explained_variance_}')
    print(f'\tExplained Variance Ratio: {pca.explained_variance_ratio_}')

    log_experiment(
        out_path,
        params=params,
        metrics=dict(explained_variance_=pca.explained_variance_,
                     explained_variance_ratio_=pca.explained_variance_ratio_))
def eval(cfg, saved_model_path=SAVED_MODEL_PATH):
    print('start to eval ! \n')
    device = torch.device(
        "cuda" if torch.cuda.is_available() else "cpu")  # 检测gpu
    env = gym.make('CartPole-v0').unwrapped  # 可google为什么unwrapped gym,此处一般不需要
    env.seed(1)  # 设置env随机种子
    n_states = env.observation_space.shape[0]
    n_actions = env.action_space.n
    agent = DQN(n_states=n_states,
                n_actions=n_actions,
                device="cpu",
                gamma=cfg.gamma,
                epsilon_start=cfg.epsilon_start,
                epsilon_end=cfg.epsilon_end,
                epsilon_decay=cfg.epsilon_decay,
                policy_lr=cfg.policy_lr,
                memory_capacity=cfg.memory_capacity,
                batch_size=cfg.batch_size)
    agent.load_model(saved_model_path + 'checkpoint.pth')
    rewards = []
    moving_average_rewards = []
    ep_steps = []
    log_dir = os.path.split(
        os.path.abspath(__file__))[0] + "/logs/eval/" + SEQUENCE
    writer = SummaryWriter(log_dir)
    for i_episode in range(1, cfg.eval_eps + 1):
        state = env.reset()  # reset环境状态
        ep_reward = 0
        for i_step in range(1, cfg.eval_steps + 1):
            action = agent.choose_action(state,
                                         train=False)  # 根据当前环境state选择action
            next_state, reward, done, _ = env.step(action)  # 更新环境参数
            ep_reward += reward
            state = next_state  # 跳转到下一个状态
            if done:
                break
        print('Episode:', i_episode, ' Reward: %i' % int(ep_reward),
              'n_steps:', i_step, 'done: ', done)
        ep_steps.append(i_step)
        rewards.append(ep_reward)
        # 计算滑动窗口的reward
        if i_episode == 1:
            moving_average_rewards.append(ep_reward)
        else:
            moving_average_rewards.append(0.9 * moving_average_rewards[-1] +
                                          0.1 * ep_reward)
        writer.add_scalars('rewards', {
            'raw': rewards[-1],
            'moving_average': moving_average_rewards[-1]
        }, i_episode)
        writer.add_scalar('steps_of_each_episode', ep_steps[-1], i_episode)
    writer.close()
    '''存储reward等相关结果'''
    save_results(rewards,
                 moving_average_rewards,
                 ep_steps,
                 tag='eval',
                 result_path=RESULT_PATH)
    print('Complete evaling!')
Esempio n. 24
0
def run_scenario(scenario):
    filename = scenario + ".txt"
    data = get_data(filename)
    print('Loaded data from: {}\nLength: {}'.format(filename,
                                                    len(data['mass'])))

    #plot_sqrtdeltachisq(data, scenario)
    save_results(data, scenario, save=False)
Esempio n. 25
0
    def test_save_results(self):
        results = utils.load_results()

        temp_file_name = utils.get_data_folder() + 'temp.json'
        utils.save_results(results, file_name=temp_file_name)

        temp = utils.load_results(file_name=temp_file_name)
        self.assertEqual(results, temp)
Esempio n. 26
0
def calc_image_similarity(args):
    np.random.seed(0)
    torch.manual_seed(0)
    torch.cuda.manual_seed_all(0)
    torch.backends.cudnn.deterministic = True

    if args.dataset == "disjoint_mnist":
        dataloaders = [
            mnist_first5_train_loader(args.size),
            mnist_last5_train_loader(args.size)
        ]
    elif args.dataset == "mnist_cifar10":
        dataloaders = [
            mnist_train_loader(args.size),
            cifar10_single_channel_train_loader(args.size)
        ]
    elif args.dataset == "fmnist_kmnist":
        dataloaders = [
            fmnist_train_loader(args.size),
            kmnist_train_loader(args.size)
        ]

    print(f"\nDataset: {args.dataset}")

    # Extract data from the first batch of both dataloaders
    for batches in zip(*dataloaders):
        images = []
        for data, _ in batches:
            images.append(data)
        break

    # Iterate and compare each pair of images
    total_mse, total_ssim = [], []
    for idx, (imageA, imageB) in enumerate(zip(*images)):
        imageA, imageB = imageA.permute(1, 2, 0), imageB.permute(1, 2, 0)
        m, s = compare_images(imageA.numpy(), imageB.numpy())
        total_mse.append(m)
        total_ssim.append(s)

        if idx % args.log_interval == 0:
            print(f"Calculating image {idx}...")
    print(len(total_mse), len(total_ssim))
    avg_mse, avg_ssim = np.mean(total_mse), np.mean(total_ssim)
    print(f"Average MSE: {avg_mse} Average SSIM index: {avg_ssim}")

    # Save the results in list
    results = [{
        "mse": avg_mse,
        "ssim": avg_ssim,
    }]

    # Save all the results
    if args.save_results:
        save_results(
            f"image_similarity_{args.dataset}",
            results,
            args.results_dir,
        )
Esempio n. 27
0
    def __init__(self, built, connections_df, edge_labels,net_name):
        if not built:

            #node names to index the Series values
            self.frm_nodename, self.to_nodename = edge_labels
            self.net = self.build_network(connections_df)
            utils.save_results(self.net, net_name)
        else:
            self.net = utils.load_results(net_name)
def train(cfg):
    env, state_dim, n_actions = env_init()
    device = torch.device(
        "cuda" if torch.cuda.is_available() else "cpu")  # 检测gpu
    agent = PolicyGradient(state_dim, device=device, lr=cfg.policy_lr)
    '''下面带pool都是存放的transition序列用于gradient'''
    state_pool = []  # 存放每batch_size个episode的state序列
    action_pool = []
    reward_pool = []
    ''' 存储每个episode的reward用于绘图'''
    rewards = []
    moving_average_rewards = []
    log_dir = os.path.split(
        os.path.abspath(__file__))[0] + "/logs/train/" + SEQUENCE
    writer = SummaryWriter(log_dir)  # 使用tensorboard的writer
    for i_episode in range(cfg.train_eps):
        state = env.reset()
        ep_reward = 0
        for _ in count():
            action = agent.choose_action(state)  # 根据当前环境state选择action
            next_state, reward, done, _ = env.step(action)
            ep_reward += reward
            if done:
                reward = 0
            state_pool.append(state)
            action_pool.append(float(action))
            reward_pool.append(reward)
            state = next_state
            if done:
                print('Episode:', i_episode, ' Reward:', ep_reward)
                break
        if i_episode > 0 and i_episode % cfg.batch_size == 0:
            agent.update(reward_pool, state_pool, action_pool)
            state_pool = []  # 每个episode的state
            action_pool = []
            reward_pool = []
        rewards.append(ep_reward)
        if i_episode == 0:
            moving_average_rewards.append(ep_reward)
        else:
            moving_average_rewards.append(0.9 * moving_average_rewards[-1] +
                                          0.1 * ep_reward)
        writer.add_scalars('rewards', {
            'raw': rewards[-1],
            'moving_average': moving_average_rewards[-1]
        }, i_episode + 1)
    writer.close()
    print('Complete training!')
    save_model(agent, model_path=SAVED_MODEL_PATH)
    '''存储reward等相关结果'''
    save_results(rewards,
                 moving_average_rewards,
                 tag='train',
                 result_path=RESULT_PATH)
    plot(rewards)
    plot(moving_average_rewards, ylabel='moving_average_rewards_train')
def run_experiment():

    import argparse

    parser = argparse.ArgumentParser()

    parser.add_argument('--approximator', type=str, default='neural_network')
    parser.add_argument('--environment',
                        type=str,
                        default='PongDeterministic-v4')
    parser.add_argument('--seed', type=int, default=1)

    args = parser.parse_args()

    approximator = args.approximator
    environment = args.environment
    seed = args.seed

    episodes = 1500
    returns = []

    agent = UpsideDownAgent(environment, approximator)

    for e in range(episodes):

        print("Episode {}".format(e))

        for i in range(100):
            agent.train_behaviour_function()

        print("Finished training B!")

        for i in range(15):
            tmp_r = []
            exploratory_commands = agent.sample_exploratory_commands(
            )  # Line 5 Algorithm 1
            desired_return = exploratory_commands[0]
            desired_horizon = exploratory_commands[1]
            r = agent.generate_episode(environment, e, desired_return,
                                       desired_horizon, False)
            tmp_r.append(r)

        print(np.mean(tmp_r))
        returns.append(np.mean(tmp_r))

        exploratory_commands = agent.sample_exploratory_commands()

        #agent.generate_episode(environment, 1, 200, 200, True)

        utils.save_results(environment, approximator, seed, returns)

    if approximator == 'neural_network':
        utils.save_trained_model(environment, seed, agent.behaviour_function)

    plt.plot(returns)
    plt.show()
Esempio n. 30
0
def main(_):
    logger.info('Loading Models From {:}'.format(FLAGS.output_dir))

    logp_col_name = FLAGS.logp_col if FLAGS.add_logp else None
    test_dataset = DataSet(csv_file_path=FLAGS.test_file,
                           smile_col_name=FLAGS.smile_col,
                           target_col_name=FLAGS.target_col,
                           logp_col_name=logp_col_name,
                           contract_rings=FLAGS.contract_rings)

    validation_dataset = DataSet(csv_file_path=FLAGS.validation_file,
                                 smile_col_name=FLAGS.smile_col,
                                 target_col_name=FLAGS.target_col,
                                 logp_col_name=logp_col_name,
                                 contract_rings=FLAGS.contract_rings)

    validation_predictions = np.empty(
        (len(FLAGS.model_names), validation_dataset.num_examples))
    test_predictions_ = np.empty(
        (len(FLAGS.model_names), test_dataset.num_examples))

    for i in xrange(0, len(FLAGS.model_names)):
        predictions = get_prediction_from_model(FLAGS.model_names[i],
                                                FLAGS.model_params[i][0],
                                                FLAGS.model_params[i][1],
                                                FLAGS.model_params[i][2],
                                                test_dataset,
                                                validation_dataset)

        validation_predictions[i, :] = predictions[0]
        test_predictions_[i, :] = predictions[1]
    ensemble_predictor = [
        ensemble_prediction_rf_regression, ensemble_prediction_top_k,
        ensemble_prediction_greedy
    ]
    predictor_names = ["Random forest regression", "Top 10", "Greedy"]

    for fun, name in zip(ensemble_predictor, predictor_names):
        emsemble_preditions = fun(validation_dataset, validation_predictions,
                                  test_predictions_)
        prediction_metric = get_metric(emsemble_preditions,
                                       test_dataset.labels)
        logger.info("Method {:} RMSE: {:}, AAE: {:}, R: {:}".format(
            name, prediction_metric[0], prediction_metric[1],
            prediction_metric[2]))

    final_prediction_path = os.path.join(FLAGS.output_dir,
                                         "ensemble_test_prediction.csv")
    save_results(final_prediction_path, test_dataset.labels,
                 emsemble_preditions)
    logging.info(
        "------------------------------DONE------------------------------")
    logging.info("")
    logging.info("")
Esempio n. 31
0
def process(wav_file, outfile, csv_file=None, bpm=None, tol=0.35,
            ssm_read_pk=False, read_pk=False, rho=2, is_ismir=False,
            tonnetz=False, sonify=False):
    """Main process to find the patterns in a polyphonic audio file.

    Parameters
    ----------
    wav_file : str
        Path to the wav file to be analyzed.
    csv_file : str
        Path to the csv containing the midi_score of the input audio file
        (needed to produce a result that can be read for JKU dataset).
    outfile : str
        Path to file to save the results.
    bpm : int
        Beats per minute of the piece. If None, bpms are read from the JKU.
    tol : float
        Tolerance to find the segments in the SSM.
    ssm_read_pk : bool
        Whether to read the SSM from a pickle file.
    read_pk : bool
        Whether to read the segments from a pickle file.
    rho : int
        Positive integer to compute the score of the segments.
    is_ismir : bool
        Produce the plots that appear on the ISMIR paper.
    tonnetz : bool
        Whether to use Tonnetz or Chromas.
    sonify : bool
        Whether to sonify the patterns or not.
    """

    # Get the correct bpm if needed
    if bpm is None:
        bpm = get_bpm(wav_file)

    # Algorithm parameters
    min_notes = 8
    max_diff_notes = 4
    h = bpm / 60. / 8.  # Hop size /8 works better than /4, but it takes longer
                        # to process

    # Obtain the Self Similarity Matrix
    X = compute_ssm(wav_file, h, ssm_read_pk, is_ismir, tonnetz)

    # Read CSV file
    if csv_file is not None:
        logging.info("Reading the CSV file for MIDI pitches...")
        midi_score = utils.read_csv(csv_file)

    patterns = []
    csv_patterns = []
    while patterns == [] or csv_patterns == []:
        # Find the segments inside the self similarity matrix
        logging.info("Finding segments in the self-similarity matrix...")
        max_diff = int(max_diff_notes / float(h))
        min_dur = int(np.ceil(min_notes / float(h)))
        #print min_dur, min_notes, h, max_diff
        if not read_pk:
            segments = []
            while segments == []:
                logging.info("\ttrying tolerance %.2f" % tol)
                segments = utils.find_segments(X, min_dur, th=tol, rho=rho)
                tol -= 0.05
            utils.write_cPickle(wav_file + "-audio.pk", segments)
        else:
            segments = utils.read_cPickle(wav_file + "-audio.pk")

        # Obtain the patterns from the segments and split them if needed
        logging.info("Obtaining the patterns from the segments...")
        patterns = obtain_patterns(segments, max_diff)

        # Decrease tolerance in case we couldn't find any patterns
        tol -= 0.05

        # Get the csv patterns if they exist
        if csv_file is not None:
            csv_patterns = patterns_to_csv(patterns, midi_score, h)
        else:
            csv_patterns = [0]

    # Sonify patterns if needed
    if sonify:
        logging.info("Sonifying Patterns...")

        utils.sonify_patterns(wav_file, patterns, h)

    # Formatting csv patterns and save results
    if csv_file is not None:
        logging.info("Writting results into %s" % outfile)
        utils.save_results(csv_patterns, outfile=outfile)
    else:
        # If not csv, just print the results on the screen
        print_patterns(patterns, h)

    if is_ismir:
        ismir.plot_segments(X, segments)

    # Alright, we're done :D
    logging.info("Algorithm finished.")
Esempio n. 32
0
            temp2 = abs(preds2p[p] - 1)

            if temp1 > temp2:
                preds2[p] = preds2p[p]

            #print np.mean(selectedPred)
        else:
            selectedPred = predictions_proba[zeroind]
            #idxs = np.any(selectedPred != 0)
            #selectedPred = selectedPred[idxs]
            preds2[p] = np.min(selectedPred)

            temp1 = abs(preds2[p] - 0)
            temp2 = abs(preds2p[p] - 0)

            if temp1 > temp2:
                preds2[p] = preds2p[p]
        #print preds2[p]

    predTest = np.load('../data/predictedTest.dat')
    print len(np.nonzero(predTest))
    fpr, tpr, _ = metrics.roc_curve(predTest, preds2)
    roc_auc = metrics.auc(fpr, tpr)
    print "AUC : %f" % roc_auc

    filename = raw_input("Enter name for submission file: ")
    utils.save_results(preds2, "../sub/" + filename + ".csv")

print "Done..."