예제 #1
0
def test_anfis(model, data, train=None, show_plots=False, mode="r"):
    """
    Do a single forward pass with x and compare with y_actual.
    :param model: [AnfisNet], anfis model after training.
    :param data: [DataLoader], test data set.
    :param train: Use training data or test data to plot membership function
    :param show_plots: [boolean], show membership function after training and result.
    :param mode: [str], choose "r" train the anfis regressor, choose "c" train the anfis classifier.
    :return:
    """

    x_test, y_actual = data.dataset.tensors
    if show_plots:
        if train is not None:
            x_train, _ = train.dataset.tensors
            plot_all_mfs(model, x_train, save=True, path="after")
        else:
            plot_all_mfs(model, x_test, save=True, path="after")
    print('### Testing for {} cases'.format(x_test.shape[0]))
    y_pred = model(x_test)
    if mode == "r":
        mse, rmse, perc_loss, r2 = calc_error(y_pred, y_actual)
        print(
            'R2 = {:.4f}, MS error={:.5f}, RMS error={:.5f}, percentage={:.2f}%'
            .format(r2, mse, rmse, perc_loss))
    else:
        ce, accuracy = calc_error_class(y_pred, y_actual)
        print('Cross Entropy = {:.4f}, accuracy = {:.4f}'.format(ce, accuracy))
    if show_plots:
        if mode == "r":
            plot_results(y_actual, y_pred)
        else:
            plot_results_class(y_actual, torch.argmax(y_pred, dim=1))
    return y_pred, y_actual
예제 #2
0
def train_hybrid(in_feat=2):
    '''
        Train a hybrid Anfis based on the Iris data.
        I use a 'resilient' BP optimiser here, as SGD was a little flakey.
    '''
    train_data = get_iris_data_one_hot(in_feat)
    x, y_actual = train_data.dataset.tensors
    model = make_anfis(x, num_mfs=3, num_out=3)
    # optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.99)
    optimizer = torch.optim.Rprop(model.parameters(), lr=1e-4)
    criterion = torch.nn.MSELoss(reduction='sum')
    experimental.train_anfis_with(model, train_data, optimizer, criterion, 5)
    experimental.plot_all_mfs(model, x)
    nc, tot = num_cat_correct(model, x, y_actual)
    print('{} of {} correct (={:5.2f}%)'.format(nc, tot, nc * 100 / tot))
    return model
예제 #3
0
def train_fuzzy(model, X, y, show_plots=True):
    X = torch.tensor(X, dtype=torch.float)
    y = torch.tensor(y, dtype=torch.float)
    net = skorch.NeuralNet(
        model,
        max_epochs=50,
        criterion=torch.nn.MSELoss,
        optimizer=torch.optim.SGD,
        optimizer__lr=1e-6,
        optimizer__momentum=0.99,
        callbacks=[FittingCallback()],
    )
    if show_plots:
        experimental.plot_all_mfs(model, X)
    net.fit(X, y)
    if show_plots:
        experimental.plot_all_mfs(model, X)
예제 #4
0
def train_non_hybrid(in_feat=2):
    '''
        Train a non-hybrid Anfis for the Iris data (so, no LSE).
        Loss criterion is CrossEntropy, and expects target to be categories.
        Note that the model still produces (float) scores for each category.
    '''
    train_data = get_iris_data(in_feat)
    x, y_actual = train_data.dataset.tensors
    model = make_anfis(x, num_mfs=3, num_out=3, hybrid=False)
    optimizer = torch.optim.SGD(model.parameters(), lr=1e-2, momentum=0.99)

    def criterion(input, target):  # change the dim and type
        return torch.nn.CrossEntropyLoss()(input, target.squeeze().long())

    experimental.train_anfis_with(model, train_data, optimizer, criterion, 250)
    y_pred = model(x)
    nc = torch.sum(y_actual.squeeze().long() == torch.argmax(y_pred, dim=1))
    tot = len(x)
    experimental.plot_all_mfs(model, x)
    print('{} of {} correct (={:5.2f}%)'.format(nc, tot, nc * 100 / tot))
    return model
예제 #5
0
def test_vignette(show_plots=True):
    model = vignette_examples.vignette_ex3()
    X, y = jang_examples.make_sinc_xy_large().dataset.tensors
    net = skorch.NeuralNet(
        model,
        max_epochs=50,
        # train_split=None,
        # window_size=1024,
        criterion=torch.nn.MSELoss,
        # criterion__reduction='sum',
        optimizer=torch.optim.SGD,
        optimizer__lr=1e-4,
        optimizer__momentum=0.99,
        callbacks=[FittingCallback()],
    )
    net.fit(X, y)
    if show_plots:
        experimental.plot_all_mfs(model, X)
        y_actual = y
        y_pred = model(X)
        experimental.plot_results(y_actual, y_pred)
예제 #6
0
def test_jang(show_plots=True):
    model = jang_examples.ex1_model()
    train_data = jang_examples.make_sinc_xy()
    X, y = train_data.dataset.tensors
    net = skorch.NeuralNet(
        model,
        max_epochs=100,
        train_split=None,
        criterion=torch.nn.MSELoss,
        #criterion__reduction='sum',
        optimizer=torch.optim.SGD,
        optimizer__lr=1e-4,
        optimizer__momentum=0.99,
        callbacks=[FittingCallback()],
    )
    net.fit(X, y)
    if show_plots:
        experimental.plot_all_mfs(model, X)
        y_actual = y
        y_pred = model(X)
        experimental.plot_results(y_actual, y_pred)
예제 #7
0
        train_data = jang_examples.make_sinc_xy_large(1000)
        train_anfis(model, train_data, 100, show_plots, metric="mse")
    elif example == '3':    # Gaussian MF
        model = vignette_ex3()
        train_data = jang_examples.make_sinc_xy_large()
        train_anfis(model, train_data, 50, show_plots)
    elif example == '3a':   # Triangular MF
        model = vignette_ex3a()
        train_data = jang_examples.make_sinc_xy_large(1000)
        # plot_all_mfs(model, train_data.dataset.tensors[0])
        # model.layer.fuzzify.show()
        train_anfis(model, train_data, 250, show_plots)
        # plot_all_mfs(model, train_data.dataset.tensors[0])
        # model.layer.fuzzify.show()
    elif example == '3b':   # Trapezoid MF
        model = vignette_ex3b()
        train_data = jang_examples.make_sinc_xy_large(1000)
        plot_all_mfs(model, train_data.dataset.tensors[0])
        train_anfis(model, train_data, 250, show_plots)
        plot_all_mfs(model, train_data.dataset.tensors[0])
    elif example == '5':    # Multi output: 2 output
        model = vignette_ex5()
        train_data = jang_examples.make_sinc_xy2()
        train_anfis(model, train_data, 50, show_plots)
    elif example == '5T':
        model = vignette_ex5_trained()
        test_data = jang_examples.make_sinc_xy2()
        test_anfis(model, test_data, None, show_plots)
    else:
        print('ERROR - no such example')
        plt.plot(range(len(error)), error, 'r', label='error')
        plt.xlabel('Time')
        plt.ylabel('Error in y(t)')
        plt.legend(loc='upper right')
        plt.hlines(y=0, xmin=0, xmax=size, linestyle=':', color='grey')
        plt.show()
    mse, rmse, perc_loss = experimental.calc_error(torch.tensor(y_act),
                                                   torch.tensor(y_desired))
    print('On {} test cases, MSE={:.5f}, RMSE={:.5f} ={:.2f}%'.format(
        size, mse, rmse, perc_loss))
    #model.layer.fuzzify.show()
    return (y_act, u)


model = plant_model_untrained()

print('### TRAINING ###')
train_data = make_training_data()
experimental.plot_all_mfs(model, train_data.dataset.tensors[0])
optimizer = torch.optim.Rprop(model.parameters(), lr=1e-3, etas=(0.9, 1.2))
criterion = torch.nn.MSELoss(reduction='sum')
experimental.train_anfis_with(model,
                              train_data,
                              optimizer,
                              criterion,
                              epochs=150,
                              show_plots=True)
experimental.plot_all_mfs(model, train_data.dataset.tensors[0])
print('### TESTING ###')
test_control_model(model)
예제 #9
0
    x_train, _ = training_data.dataset.tensors
    if not load:
        if model_type == "class":
            my_model = my_model_class1()
        elif model_type == "m1":
            my_model = my_model_m1()
        elif model_type == "k1":
            my_model = my_model_k1()
        elif model_type == "c1":
            my_model = my_model_c1()
        elif model_type == "class_rig":
            my_model = classifier_rig(window="small")
        print("mf before:", my_model.layer.fuzzify)
        with open("my_model/membership/before/premise.txt", "w") as text_file:
            print(f"{my_model.layer.fuzzify}", file=text_file)
        plot_all_mfs(my_model, x_train, save=True, path="before")

        # Training
        epoch = 2000
        metric = "ce" if mode == "c" else "rmse"
        _, y_train_tar, y_train_pre, y_test_tar, y_test_pre = \
            train_anfis_cv(my_model, [training_data, test_data], epoch=epoch, show_plots=True, \
                           metric=metric, mode=mode, save=save, name=save_path, detail=False)
        show_plot = True

    else:
        my_model = torch.load(load_path)
        show_plot = False

    # Visualization of validation set
    print("norm:", my_model.weights[2])
예제 #10
0
    return dl


if __name__ == '__main__':
    example = '1'
    show_plots = True
    if len(sys.argv) == 2:  # One arg: example
        example = sys.argv[1]
        show_plots = False
    print('Example {} from Jang\'s paper'.format(example))
    if example == '1':
        model = ex1_model()
        train_data = make_sinc_xy()
        a, b = train_data.dataset.tensors
        cv_data = make_sinc_xy_test()
        plot_all_mfs(model, a)
        # train_anfis(model, train_data, 20, show_plots)
        train_anfis_cv(model, [train_data, cv_data], 20, show_plots, metric="rmse")
    elif example == '2':
        model = ex2_model()
        train_data = ex2_training_data()
        train_anfis(model, train_data, 200, show_plots)
        test_data = ex2_testing_data()
        test_anfis(model, test_data, show_plots)
    elif example == '3':
        model = ex3_model()
        train_data = ex3_training_data()
        train_anfis(model, train_data, 500, show_plots)
        test_data = ex3_testing_data()
        test_anfis(model, test_data, show_plots)
    elif example == '4':