예제 #1
0
def get_means_and_stds():
    _, _, X_train = input_to_onehot("automagic")

    mean1 = np.mean(X_train[:, -1], axis=0)
    std1 = np.std(X_train[:, -1], axis=0)

    mean2 = np.mean(X_train[:, -2], axis=0)
    std2 = np.std(X_train[:, -2], axis=0)

    return mean1, std1, mean2, std2
예제 #2
0
def train():
    """
    Performs training and evaluation of MLP model.
    TODO:
    Implement training and evaluation of MLP model. Evaluate your model on the whole test set each eval_freq iterations.
    """
    # Set the random seeds for reproducibility
    # np.random.seed(42)

    if torch.cuda.is_available():
        device = torch.device('cuda')
        print("cuda")
    else:
        device = torch.device('cpu')

    script_directory = os.path.split(os.path.abspath(__file__))[0]
    filepath = 'grubbyStar4L4W.model'
    model_to_train = os.path.join(script_directory,
                                  filepath)  # EXCEPT CROSS ENTROPY!

    validation_games = 80

    onehot_input, y, _ = input_to_onehot('gaussianPredictions')

    val_ids = np.random.choice(onehot_input.shape[0],
                               size=validation_games,
                               replace=False)
    train_ids = [i for i in range(onehot_input.shape[0]) if i not in val_ids]

    X_train = onehot_input[train_ids, :]
    y_train = y[train_ids]

    # X_train = onehot_input[0: -validation_games, :]
    # y_train = y[0: -validation_games]

    print("X train")

    print(X_train.shape)
    print(y_train.shape)

    X_test = onehot_input[val_ids, :]
    y_test = y[val_ids]

    # X_test = onehot_input[-validation_games:, :]
    # y_test = y[-validation_games:]

    print("X test")

    print(X_test.shape)
    print(y_test.shape)

    print(onehot_input.shape)
    print(onehot_input.shape[1])

    model = GStar4L4WNet(onehot_input.shape[1])
    print(model)

    optimizer = torch.optim.SGD(model.parameters(),
                                lr=LEARNING_RATE_DEFAULT,
                                momentum=0.9,
                                weight_decay=1e-5)

    accuracies = []
    losses = []
    vag_losses = []
    max_acc = 0
    min_loss = 1000

    vag_games = get_validation_ids()
    vag_games = np.array(vag_games)
    vag_ids = vag_games[-200:]
    vag_input = onehot_input[vag_ids, :]
    vag_targets = y[vag_ids]

    for epoch in range(1):
        val_ids = [
            i for i in range(onehot_input.shape[0] -
                             validation_games, onehot_input.shape[0])
        ]
        val_ids = np.append(val_ids, vag_ids)
        val_ids = np.unique(val_ids)
        val_ids = np.array(val_ids)
        print(len(val_ids), "val ids")
        print(val_ids)

        train_ids = [
            i for i in range(onehot_input.shape[0]) if i not in val_ids
        ]

        X_train = onehot_input[train_ids, :]
        y_train = y[train_ids]

        X_test = onehot_input[val_ids, :]
        y_test = y[val_ids]

        print("epoch " + str(epoch))

        for iteration in range(MAX_STEPS_DEFAULT):
            BATCH_SIZE_DEFAULT = 32

            if iteration % 50000 == 0:
                print("iteration: " + str(iteration))

            model.train()

            ids = np.random.choice(X_train.shape[0],
                                   size=BATCH_SIZE_DEFAULT,
                                   replace=False)

            X_train_batch = X_train[ids, :]
            y_train_batch = y_train[ids]

            X_train_batch = np.reshape(X_train_batch, (BATCH_SIZE_DEFAULT, -1))
            X_train_batch = Variable(torch.FloatTensor(X_train_batch))

            output = model.forward(X_train_batch)

            y_train_batch = np.reshape(y_train_batch, (BATCH_SIZE_DEFAULT, -1))
            y_train_batch = Variable(torch.FloatTensor(y_train_batch))
            loss = center_my_loss(output, y_train_batch)

            model.zero_grad()
            loss.backward(retain_graph=True)
            optimizer.step()

            if iteration % EVAL_FREQ_DEFAULT == 0:
                model.eval()

                ids = np.array(range(len(X_test)))
                x = X_test[ids, :]
                targets = y_test[ids]

                x = np.reshape(x, (len(X_test), -1))

                x = Variable(torch.FloatTensor(x))

                pred = model.forward(x)

                acc = accuracy(pred, targets)
                targets = np.reshape(targets, (len(X_test), -1))
                targets = Variable(torch.FloatTensor(targets))

                calc_loss = center_my_loss(pred, targets)

                accuracies.append(acc)
                losses.append(calc_loss.item())

                ###################

                if min_loss > calc_loss.item():
                    min_loss = calc_loss.item()
                    torch.save(model, model_to_train)

                    ids = np.array(range(len(X_train)))
                    x = X_train[ids, :]
                    targets = y_train[ids]

                    x = np.reshape(x, (len(X_train), -1))

                    x = Variable(torch.FloatTensor(x))

                    pred = model.forward(x)
                    train_acc = accuracy(pred, targets)

                    targets = np.reshape(targets, (len(X_train), -1))

                    targets = Variable(torch.FloatTensor(targets))

                    train_loss = center_my_loss(pred, targets)

                    print("iteration: " + str(iteration) + " train acc " +
                          str(train_acc) + " val acc " + str(acc) +
                          " train loss " + str(train_loss.item()) +
                          " val loss " + str(calc_loss.item()))

    #torch.save(model, model_to_train)
    test_nn.test_all(model_to_train)
    print(model_to_train)
    print("maxx acc")
    print(max_acc)
    plt.plot(accuracies)
    plt.ylabel('accuracies')
    plt.show()

    plt.plot(vag_losses, 'r')
    plt.plot(losses, 'b')
    plt.ylabel('losses')
    plt.show()
예제 #3
0
def load_models(onehot_encoded):
    filepath = 'models\\'
    script_directory = os.path.split(os.path.abspath(__file__))[0]

    grubby_star = os.path.join(script_directory, filepath + 'grubbyStar/grubbyStar.model')
    grubby_star2 = os.path.join(script_directory, filepath + 'grubbyStar2/grubbyStar2.model')
    grubby_star_3L3W = os.path.join(script_directory, filepath + 'grubbyStar3L-3W/grubbyStar3L-3W.model')
    grubby_star_4L3W = os.path.join(script_directory, filepath + 'grubbyStar4L-3W/grubbyStar4L-3W.model')
    grubby_star_4L4W = os.path.join(script_directory, filepath + 'grubbyStar4L4W/grubbyStar4L4W.model')

    grubby_star_cross_entropy = os.path.join(script_directory, filepath + 'cross_entropy/grubbyStarCrossEntropy.model')
    grubby_ce2 = os.path.join(script_directory, filepath + 'cross_entropy/grubbyStarCE2.model')
    grubby_ce3 = os.path.join(script_directory, filepath + 'cross_entropy/grubbyStarCE3.model')
    grubby_ce4 = os.path.join(script_directory, filepath + 'cross_entropy/grubbyStarCE4.model')

    metamodel = os.path.join(script_directory, "metamodel/" + 'grubbyStarMeta.model')
    sigmamodel = os.path.join(script_directory, "sigma_metamodel/" + 'grubbyStarSigma.model')
    enhanced = os.path.join(script_directory, filepath + "enhanced_features/" + 'enhancedStar.model')

    onehot_neural = copy.deepcopy(onehot_encoded)
    _, _, X_train = input_to_onehot('automagic')
    mean1, std1, mean2, std2 = get_means_and_stds(X_train)
    x = standardize_instance(onehot_neural, mean1, std1, mean2, std2)

    onehot_neural_cross = copy.deepcopy(onehot_encoded)
    _, _, X_train_cross = cross_entropy_input_to_onehot()
    mean1_cross, std1_cross, mean2_cross, std2_cross = get_means_and_stds(X_train_cross)
    x_cross = standardize_instance(onehot_neural_cross, mean1_cross, std1_cross, mean2_cross, std2_cross)

    # input->2, 2->2, 2->1
    model = torch.load(grubby_star)
    model.eval()
    pred = model.forward(x)
    neural_pred = pred.detach().numpy()

    # input->3, 3->3, 3->1
    model2 = torch.load(grubby_star2)
    model2.eval()
    pred2 = model2.forward(x)
    neural_pred2 = pred2.detach().numpy()

    # input->3, 3->3, 3->3, 3->1
    model3L3W = torch.load(grubby_star_3L3W)
    model3L3W.eval()
    pred3L3W = model3L3W.forward(x)
    neural_pred3L3W = pred3L3W.detach().numpy()

    # input->3, 3->3, 3->3, 3->3, 3->1
    model4L3W = torch.load(grubby_star_4L3W)
    model4L3W.eval()
    pred4L3W = model4L3W.forward(x)
    neural_pred4L3W = pred4L3W.detach().numpy()

    # input->4, 4->4, 4->1
    model4L4W = torch.load(grubby_star_4L4W)
    model4L4W.eval()
    pred4L4W = model4L4W.forward(x)
    neural_pred4L4W = pred4L4W.detach().numpy()


    ######### CROSS ##################


    modelCross = torch.load(grubby_star_cross_entropy)
    modelCross.eval()
    predCross = modelCross.forward(x)
    neural_predCross = predCross.detach().numpy()

    modelCE2 = torch.load(grubby_ce2)
    modelCE2.eval()
    predCross2 = modelCE2.forward(x)
    neural_predCross2 = predCross2.detach().numpy()

    modelCE3 = torch.load(grubby_ce3)
    modelCE3.eval()
    predCross3 = modelCE3.forward(x)
    neural_predCross3 = predCross3.detach().numpy()

    modelCE4 = torch.load(grubby_ce4)
    modelCE4.eval()
    predCross4 = modelCE4.forward(x)
    neural_predCross4 = predCross4.detach().numpy()

    # print("prediction", pred)
    # print("x.shape", x.shape)
    # print("pred.shape", pred.shape)
    third_tensor = torch.cat((x, pred2), 1)
    third_tensor = torch.cat((third_tensor, pred4L3W), 1)
    # third_tensor = torch.cat((third_tensor, pred3L3W), 1)
    # third_tensor = torch.cat((third_tensor, pred4L3W), 1)
    # third_tensor = torch.cat((third_tensor, pred4L4W), 1)

    third_tensor = torch.cat((third_tensor, predCross), 1)
    # third_tensor = torch.cat((third_tensor, predCross2), 1)
    third_tensor = torch.cat((third_tensor, predCross3), 1)
    # third_tensor = torch.cat((third_tensor, predCross4), 1)

    metaStar = torch.load(metamodel)
    metaStar.eval()
    pred_meta = metaStar.forward(third_tensor)
    coeffs = pred_meta.detach().numpy()

    third_tensor = torch.narrow(third_tensor, 1, 25, 4)

    result = torch.mul(third_tensor, pred_meta)
    result = torch.sum(result, dim=1)
    neural_meta = result.detach().numpy()


    sigma_tensor = torch.cat((x, pred), 1)
    sigma_tensor = torch.cat((sigma_tensor, pred2), 1)
    sigma_tensor = torch.cat((sigma_tensor, pred3L3W), 1)
    sigma_tensor = torch.cat((sigma_tensor, pred4L3W), 1)
    sigma_tensor = torch.cat((sigma_tensor, pred4L4W), 1)

    sigma_tensor = torch.cat((sigma_tensor, predCross), 1)
    sigma_tensor = torch.cat((sigma_tensor, predCross2), 1)
    sigma_tensor = torch.cat((sigma_tensor, predCross3), 1)
    sigma_tensor = torch.cat((sigma_tensor, predCross4), 1)

    sigmaStar = torch.load(sigmamodel)
    sigmaStar.eval()
    pred_sigma = sigmaStar.forward(sigma_tensor)

    train = torch.narrow(sigma_tensor, 1, 25, 9)

    mean = torch.mean(train, dim=1)
    s = torch.std(train, dim=1)

    result_sigma = mean + torch.mul(torch.transpose(pred_sigma, 0, 1), s)

    result_sigma = result_sigma.detach().numpy()


    enhancedStar = torch.load(enhanced)
    enhancedStar.eval()
    pred_enha = enhancedStar.forward(sigma_tensor)
    pred_enha = pred_enha.detach().numpy()

    return neural_pred, neural_pred2, neural_pred3L3W, neural_pred4L3W, \
           neural_pred4L4W, neural_predCross, neural_predCross2, neural_predCross3, neural_predCross4, neural_meta, coeffs[0], result_sigma[0], pred_enha