Пример #1
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
Пример #2
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
        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)