예제 #1
0
def main():
    # load data from all .cnf files
    d = data_t()
    # the var counts to train on
    var_types = ["var_20", "var_50", "var_75", "var_100"]
    # train on the variables counts specified above
    train(d, var_types)
예제 #2
0
def main():
    # N is batch size; D_in is input dimension; H is hidden dimension; D_out is output dimension.
    N, D_in, H, D_out = 64, 16, 4, 1
    x = torch.from_numpy(
        np.load('datasets/random/random_imgs.npy').reshape((N, D_in))).float()
    y = torch.from_numpy(
        np.load('datasets/random/random_labs.npy').reshape(
            (N, D_out))).float()

    # ========== QUESTION 1 ==========
    model = torch.nn.Sequential(torch.nn.Linear(D_in, H), torch.nn.Sigmoid(),
                                torch.nn.Linear(H, D_out), torch.nn.Sigmoid())
    initialize_weights(model)
    loss_fn = torch.nn.MSELoss(reduction='sum')
    losses, accuracies = train(model, loss_fn, x, y)
    plot_stats(losses, accuracies, 'Q2.1 - Sigmoid with L2')

    # ========== QUESTION 2 ==========
    model = torch.nn.Sequential(torch.nn.Linear(D_in, H), torch.nn.Sigmoid(),
                                torch.nn.Linear(H, D_out), torch.nn.Sigmoid())
    initialize_weights(model)
    loss_fn = torch.nn.BCELoss(reduction='sum')
    losses, accuracies = train(model, loss_fn, x, y)
    plot_stats(losses, accuracies, 'Q2.2 - Sigmoid with CE')

    # ========== QUESTION 3 ==========
    model = torch.nn.Sequential(torch.nn.Linear(D_in, H), torch.nn.ReLU(),
                                torch.nn.Linear(H, D_out), torch.nn.Sigmoid())
    initialize_weights(model)
    loss_fn = torch.nn.MSELoss(reduction='sum')
    losses, accuracies = train(model, loss_fn, x, y)
    plot_stats(losses, accuracies, 'Q2.3 - ReLU with L2')

    # ========== QUESTION 4 ==========
    model = torch.nn.Sequential(torch.nn.Linear(D_in, H), torch.nn.ReLU(),
                                torch.nn.Linear(H, D_out), torch.nn.Sigmoid())
    initialize_weights(model)
    loss_fn = torch.nn.BCELoss(reduction='sum')
    losses, accuracies = train(model, loss_fn, x, y, 0.05)
    plot_stats(losses, accuracies, 'Q2.4 - ReLU with CE')
예제 #3
0
def main(arguments):
    """
    Main function to run the stock market tester
    """
    if arguments.download_data:
        download_all_data()
        print("Downloaded Data!")
        # clean_data()
        # print("Cleaned Data")
        return
    if arguments.download_stock is not None:
        print(arguments.download_stock)
        install_data(arguments.download_stock)
        return
    
    DAYS_OUT = 5
    covid_data = get_covid_data()
    model = Historic()
    train_data, test_data = get_all_stocks(covid_data)
    losses = []
    for i in range(0, model.num_epochs):
        train_loss = train(model, train_data, DAYS_OUT)
        print("EPOCH {} training loss: {}".format(i, train_loss))

        test_loss = test(model, test_data, DAYS_OUT)
        print("EPOCH {} Test loss: {}".format(i, test_loss))

        losses.append(test_loss)
    visualize_loss(losses)
    print("Last 10 Epochs Average MAPE: {}".format(sum(losses[-10:]) / 10))

    sp_data = pd.read_csv("../data/^GSPC.csv")
    sp_data = join(sp_data, covid_data)
    sp_data = normalize(sp_data)
    base_data = sp_data.iloc[:-DAYS_OUT]
    base_data = tf.convert_to_tensor(base_data)
    base_data = tf.expand_dims(base_data, 0)
    labels = sp_data.iloc[DAYS_OUT:]
    labels = labels["Adjusted Close"]
    labels = labels.values.tolist()
    # print(labels)
    predictions = predict(model, base_data)
    # print(len(predictions))
    visualize_predictions(predictions, labels)
예제 #4
0
파일: q3.py 프로젝트: lnmangione/cis581p4
def main():
    N, im_size = 64, 16
    x_line = torch.from_numpy(np.load('datasets/line/line_imgs.npy')).float().reshape(N, 1, im_size, im_size)
    y_line = torch.from_numpy(np.load('datasets/line/line_labs.npy')).float().reshape(N, 1)
    x_det = torch.from_numpy(np.load('datasets/detection/detection_imgs.npy')).float().reshape(N, 1, im_size, im_size)
    y_det = torch.from_numpy(np.load('datasets/detection/detection_labs.npy')).float().reshape(N, 1)
    y_detwid = torch.from_numpy(np.load('datasets/detection/detection_width.npy')).float().reshape(N, 1)

    # ========== QUESTION 1 ==========
    line_model = LineModel()
    initialize_weights(line_model)
    loss_fn = torch.nn.BCELoss(reduction='mean')
    losses, accuracies = train(line_model, loss_fn, x_line, y_line)
    plot_stats(losses, accuracies, 'Q3.1')

    # ========== QUESTION 2 ==========
    det_model = DetModel()
    initialize_weights(det_model)
    loss_fn1 = torch.nn.BCELoss(reduction='sum')
    loss_fn2 = torch.nn.MSELoss(reduction='sum')
    losses1, accuracies1, losses2, accuracies2 = train2(det_model, loss_fn1, loss_fn2, 0.01, 0.001, x_det, y_det, y_detwid)
    plot_stats(losses1, accuracies1, 'Q3.2 - Cross-Entropy')
    plot_stats(losses2, accuracies2, 'Q3.2 - L2')
def createNNDataArray(points,
                      labels,
                      epochs,
                      learning_rate,
                      criterion,
                      optimiser,
                      activation_function,
                      node_domain_min,
                      node_domain_max,
                      layer_domain_min,
                      layer_domain_max,
                      seed=42,
                      filename="all_outputs.json"):
    all_network_output_datas = []

    points_tensor = torch.from_numpy(points).float()
    labels_tensor = torch.from_numpy(labels).float()

    for n_layers in range(layer_domain_min, layer_domain_max + 1):
        for n_nodes in range(node_domain_min, node_domain_max + 1):
            if (n_nodes >= n_layers):
                print(f"Starting {n_nodes} node, {n_layers} layer network")
                torch.manual_seed(seed)
                model = SortingNetwork(n_nodes, n_layers, activation_function)
                optimizer = optimiser(model.parameters(), lr=learning_rate)
                crit = criterion()

                time_taken, final_loss = train(model,
                                               crit,
                                               optimizer,
                                               points_tensor,
                                               labels_tensor,
                                               epochs,
                                               do_print=False)
                output = get_output(model)
                output = output_to_serializable(output)

                current_data = {
                    'points': points_to_serializable(points),
                    'labels': labels_to_serializable(labels),
                    'output': output,
                    'training_time': time_taken,
                    'loss': final_loss,
                    'epochs': epochs,
                    'layers': n_layers,
                    'nodes': n_nodes,
                    'network': str(model),
                    'optimiser': str(optimizer),
                    'criterion': str(crit)
                }
                all_network_output_datas.append(current_data)

    return_file_path = ""

    try:
        with open(filename, 'w') as fp:
            json.dump(all_network_output_datas, fp)
            return_file_path = filename
    except:
        temp_file_name = "output_data" + str(datetime.now()).replace(
            ':', '') + ".json"
        print(
            f"File failed to write at {filename}. Writting to {temp_file_name}"
        )
        try:
            with open(temp_file_name, 'w') as fp:
                json.dump(all_network_output_datas, fp)
                return_file_path = temp_file_name
        except:
            print("Failed to write file again. ABORTING")
            return

    print("Data saved")
    return return_file_path
예제 #6
0
        p = np.random.permutation(len(train_y))
        train_x, train_y = train_x[p], train_y[p]
        for batch_start_index in (range(0,
                                        len(train_y) - BATCH_SIZE,
                                        BATCH_SIZE)):
            batch_train_x = train_x[batch_start_index:batch_start_index +
                                    BATCH_SIZE]
            batch_train_y = train_y[batch_start_index:batch_start_index +
                                    BATCH_SIZE]
            # s = time.time()
            xs = Variable(
                torch.from_numpy(
                    np.array([x.todense()
                              for x in batch_train_x]).astype(np.float32)))
            ys = Variable(torch.from_numpy(batch_train_y.astype(np.float32)))
            loss = helpers.train(model, xs, ys, optimizer)

    # Evaluate on the dev set
    model.eval()
    predicted_ys = []
    for word_sequence in (DEV_X_RAW):
        word_sequence = word_sequence.split(' ')
        predicted_sequence_ys = []
        hidden = Variable(torch.zeros(1, 1, HIDDEN_DIM))
        for word_index in range(len(word_sequence)):
            x = helpers.featurize(word_index,
                                  word_sequence,
                                  word_vectorizer,
                                  char_vectorizer,
                                  n_words,
                                  n_chars,
예제 #7
0
                           k_fold, lr_set, train_input, train_target)

# initialize models
optimizer = optimizer_name(model=model, lr=best_lr)
# logging info
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
logging.info(f'''Starting training:
    Epochs:          {nb_epochs}
    Learning rate:   {best_lr}
    Batch size:      {batch_size}
    Optimizer:       {"SGD"}
    Loss:            {"MSE loss"}
    Training size:   {1000}
    Test size:       {1000}
''')

# model training
acc_train, acc_test = train(model,
                            loss,
                            optimizer,
                            train_input,
                            train_target,
                            test_input,
                            test_target,
                            nb_epochs=nb_epochs,
                            batch_size=batch_size)

# print error rate
print("Final training error:", 1 - acc_train)
print("Final testing error:", 1 - acc_test)
예제 #8
0
def profile():
    d = data_t()
    var_types = ["var_20"]
    train(d, var_types, population_size=10, logging=False)
예제 #9
0
#normalize the data
mean, std = train_input.mean(), train_input.std()
train_input.sub_(mean).div_(std)
test_input.sub_(mean).div_(std)

#define loss
criterion = Criterion.MSE()

#define optimizer
optim = Optimizer.SGD(parameters=model.param(), lr=1e-1)

#train the model
loss, accuracy = train(model,
                       criterion,
                       optim,
                       train_input,
                       train_target,
                       nb_epochs=200,
                       verbose=True)

#compute statistics on test
output = model.forward(test_input)
loss_test = criterion.forward(output, test_target)
accuracy_test = compute_accuracy(model.forward, test_input, test_target)

print("")
print("TRAIN:  accuracy {:.4}%, loss {:.4}".format(accuracy[-1], loss[-1]))
print("TEST :  accuracy {:.4}%, loss {:.4}".format(
    accuracy_test / test_target.shape[0] * 100, loss_test))

#vizualisation
예제 #10
0
        feature = None

    # Make random feature
    rand_ft = make_feature(idim=idim,
                           task=main,
                           **args.subdict('ftype', 'kernel', 'fdim', 'cores'))

    # Load dataset
    dataset, validation_tester = make_trainset(
        transform=rand_ft,
        feature=ckm.map if args.get('knn') else None,
        main=main,
        **args.subdict('cores', 'ntrain', 'ptrain'))

    # Train model
    rfsvm = train(dataset, main.subtask())

    # Tester
    test_dataset, tester = make_testset(
        transform=rand_ft,
        feature=ckm.map if args.get('knn') else None,
        main=main,
        **args.subdict('cores', 'ntest', 'ptest'))

    # Run testers
    tester.loss(rfsvm, task=main)
    validation_tester.loss(rfsvm, task=main)

    # Save as JSON
    if os.path.exists(LOG_FILE_PATH + '.json'):
        main.acc_join()
예제 #11
0
                delay_mode_inter,
                delay_inter_param,
            ))
        stimuli = np.load(PATH +
                          "stimuli_run={}_{}_intra={}{}_inter={}{}.npy".format(
                              runindex,
                              network_mode,
                              delay_mode_intra,
                              delay_intra_param,
                              delay_mode_inter,
                              delay_inter_param,
                          ))
    """
    train the classifier and test it
    """
    accuracy_train[runindex], MSE_train[runindex] = train(
        volt_values=volt_values, target_output=stimuli)
    print("training is done: ", time.process_time())
"""
save the summary data to the file
"""
# reformat the numpy array into data frame
df_acc = reformat_df(network_mode, accuracy_train)
df_mse = reformat_df(network_mode, MSE_train)

# save the data frame
df_acc["MSE"] = df_mse["value"]
df_acc.rename(columns={"value": "accuracy"}, inplace=True)

if len(sys.argv) > 6:
    df_acc.to_csv(
        "training_{}_intra={}{}_inter={}{}_skip_double={}_d={}_w={}.csv".
예제 #12
0
args = sys.argv
train_dataset_path = os.path.abspath(args[1])
print('Dataset path: {}'.format(train_dataset_path))

print('Loading dataset...')
train_target, train_data = helpers.load_dataset(train_dataset_path)

bags_of_words = helpers.create_bags_of_words(train_data)
words = helpers.get_words(bags_of_words)
labels = helpers.get_labels(train_target)

model_path = os.path.join(os.getcwd(), 'model')
if not os.path.exists(model_path):
    os.mkdir(model_path)

print('Training data...')
label_probs, probs_per_label = helpers.train(
    bags_of_words, words, train_target, labels)

print('Storing model...')
with open(os.path.join(model_path, 'train.pickle'), 'wb') as f:
    pickle.dump((label_probs, probs_per_label, words, labels), f)

print('Training done.')
print('============== INFO ===============')
print('Dataset size: {}'.format(len(bags_of_words)))
print('Total words: {}'.format(len(words)))
print('Labels: {}'.format(labels))
print('Label probs: {}'.format(label_probs))
print('====================================')