Beispiel #1
0
def train():
    """
    Performs training and evaluation of MLP model.
    """
    # Set the random seeds for reproducibility
    # np.random.seed(42)

    onehot_input, y, _ = cross_entropy_input_to_onehot()

    LEARNING_RATE_DEFAULT = 3e-3
    MAX_STEPS_DEFAULT = 4000000

    cnet_a = CrossNet2(onehot_input.shape[1])
    script_directory = os.path.split(os.path.abspath(__file__))[0]
    filepath = 'grubbyStarCE2.model'
    model_to_train = os.path.join(script_directory, filepath)

    cnet_b = SimpleMLP(onehot_input.shape[1])
    script_directory_b = os.path.split(os.path.abspath(__file__))[0]
    filepath_b = 'grubbyStarCrossEntropy.model'
    model_b = os.path.join(script_directory_b, filepath_b)

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

    optimizer = torch.optim.SGD(cnet_a.parameters(), lr=1e-3, momentum=0.9, weight_decay=1e-5)
    optimizer_b = torch.optim.SGD(cnet_b.parameters(), lr=1e-3, momentum=0.9, weight_decay=1e-5)

    accuracies = []
    losses = []
    vag_losses = []
    min_loss = 100
    min_loss_b = 100

    vag_games = get_validation_ids()
    vag_games = np.array(vag_games)
    vag_ids = vag_games[-200:]
    validation_games = 100
    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, :]
        print(X_train.shape)
        y_train = y[train_ids]

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

        print("epoch " + str(epoch))
        saturation = 1
        p = 1
        bce = True
        ace = True

        for iteration in range(MAX_STEPS_DEFAULT):
            BATCH_SIZE_DEFAULT = 8
            cnet_a.train()
            cnet_b.train()
            if iteration % 20000 == 0:
                # saturation *= 0.5
                # saturation = max(0.5, saturation)
                print(iteration)
                print(saturation)

            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 = cnet_a.forward(X_train_batch)
            output_b = cnet_b.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))

            if iteration % 1 == 0:
                loss = center_my_loss(output, y_train_batch, output_b, saturation)
            else:
                loss = torch.nn.functional.binary_cross_entropy(output, y_train_batch)

            if True:
                loss_b = center_my_loss(output_b, y_train_batch, output, saturation)
            else:
                loss_b = torch.nn.functional.binary_cross_entropy(output_b, y_train_batch)

            ce_loss = torch.nn.functional.binary_cross_entropy(output, y_train_batch)
            ce_loss_b = torch.nn.functional.binary_cross_entropy(output_b, y_train_batch)

            if iteration % EVAL_FREQ_DEFAULT == 0:
                cnet_a.eval()
                cnet_b.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 = cnet_a.forward(x)
                pred_b = cnet_b.forward(x)
                acc = accuracy(pred, targets)

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

                calc_loss = torch.nn.functional.binary_cross_entropy(pred, targets)
                calc_loss_b = torch.nn.functional.binary_cross_entropy(pred_b, targets)

                accuracies.append(acc)

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

                if p*calc_loss.item()+(1-p)*ce_loss.item() < p*calc_loss_b.item()+(1-p)*ce_loss_b.item():
                    cnet_b.train()
                    cnet_b.zero_grad()
                    loss_b.backward(retain_graph=True)
                    optimizer_b.step()
                    cnet_b.eval()

                    if min_loss_b > calc_loss_b.item():
                        min_loss_b = calc_loss_b.item()
                        torch.save(cnet_b, model_b)

                        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_b = cnet_b.forward(x)

                        train_acc = accuracy(pred_b, targets)

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

                        train_loss = torch.nn.functional.binary_cross_entropy(pred_b, targets)
                        losses.append(train_loss.item())

                        print("iteration: " + str(iteration) + " train acc " + str(train_acc) + " val acc " + str(
                            acc) + " a " + str(round(calc_loss.item()*1000)/1000) + " b " + str(
                            round(calc_loss_b.item() * 1000)/1000))

                if p*calc_loss.item()+(1-p)*ce_loss.item() > p*calc_loss_b.item()+(1-p)*ce_loss_b.item():
                    cnet_a.train()
                    cnet_a.zero_grad()
                    loss.backward(retain_graph=True)
                    optimizer.step()
                    cnet_a.eval()

                    if min_loss > calc_loss.item():
                        min_loss = calc_loss.item()
                        torch.save(cnet_a, 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 = cnet_a.forward(x)

                        train_acc = accuracy(pred, targets)

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

                        print("iteration: " + str(iteration) + " train acc " + str(train_acc) + " val acc " + str(
                            acc) + " a " + str(round(calc_loss.item()*1000)/1000) + " b " + str(
                            round(calc_loss_b.item() * 1000)/1000))

    test_nn.test_all(model_to_train)
    print(model_to_train)

    plt.plot(accuracies)
    plt.ylabel('accuracies')
    plt.show()

    plt.plot(vag_losses, 'r')
    plt.plot(losses, 'b')
    plt.ylabel('losses')
    plt.show()
Beispiel #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()
Beispiel #3
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)

    onehot_input, y, non_standardized_input = cross_entropy_input_to_onehot()
    onehot_input = cluster_gmm(non_standardized_input)

    LEARNING_RATE_DEFAULT = 1e-3
    MAX_STEPS_DEFAULT = 300000
    BATCH_SIZE_DEFAULT = 5
    validation_games = 200

    model = GmmNET(onehot_input.shape[1])
    script_directory = os.path.split(os.path.abspath(__file__))[0]
    filepath = 'grubbyStarGmmNET.model'
    model_to_train = os.path.join(script_directory, filepath)

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

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

    accuracies = []
    losses = []
    min_loss = 100

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

    for epoch in range(1):
        # val_ids = np.random.choice(onehot_input.shape[0], size=validation_games, replace=False)
        # val_ids = np.append(val_ids, vag_ids)
        # val_ids = np.unique(val_ids)
        val_ids = np.array(vag_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 = 5
            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 = torch.nn.functional.binary_cross_entropy(
                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 = torch.nn.functional.binary_cross_entropy(
                    pred, targets)

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

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

                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 = torch.nn.functional.binary_cross_entropy(
                    pred, targets)

                ########## VAG #############

                BATCH_SIZE_DEFAULT = len(vag_ids)
                ids = np.array(range(BATCH_SIZE_DEFAULT))
                x = vag_input
                targets = vag_targets

                x = np.reshape(x, (BATCH_SIZE_DEFAULT, -1))

                x = Variable(torch.FloatTensor(x))

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

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

                vag_loss = torch.nn.functional.binary_cross_entropy(
                    pred, targets)

                p = 1
                if min_loss > (p * calc_loss.item() +
                               (1 - p) * train_loss.item()):
                    min_loss = (p * calc_loss.item() +
                                (1 - p) * train_loss.item())
                    torch.save(model, model_to_train)

                    print("iteration: " + str(iteration) + " train acc " +
                          str(train_acc) + " val acc " + str(acc) +
                          " train loss " +
                          str(round(train_loss.item() * 1000) / 1000) +
                          " val loss " +
                          str(round(calc_loss.item() * 1000) / 1000) +
                          " vag acc: " + str(vag_acc) + " vag loss: " +
                          str(round(vag_loss.item() * 1000) / 1000))

    test_nn.test_all(model_to_train)
    print(model_to_train)

    plt.plot(accuracies)
    plt.ylabel('accuracies')
    plt.show()

    plt.plot(losses)
    plt.ylabel('losses')
    plt.show()