if __name__ == '__main__': example = '5T' show_plots = True if len(sys.argv) == 2: # One arg: example example = sys.argv[1].upper() show_plots = False print('Example {} from Vignette paper'.format(example)) if example == '1': # 4 Bell MF model = vignette_ex1() train_data = jang_examples.make_sinc_xy_large() train_anfis(model, train_data, 100, show_plots) elif example == '1T': model = vignette_ex1_trained() test_data = jang_examples.make_sinc_xy() test_anfis(model, test_data, None, show_plots) elif example == '2': # 5 Bell MF model = vignette_ex2() 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])
train_data, idx = data(partition='train') folds = kfold_data(idx) rule_range = range(1) fold_eval = np.zeros((len(rule_range), len(folds))) for r, n_rules in enumerate(rule_range): for f, fold in enumerate(folds): fold_train_data = fold['train'] fold_test_data = fold['test'] anfis_model = model(fold_train_data, n_rules + 1) train_anfis(anfis_model, data=fold_train_data, epochs=20, show_plots=False) _, _, perc_loss = test_anfis(anfis_model, data=fold_test_data, show_plots=False) fold_eval[r, f] = perc_loss # particao de teste é avaliada com os parametros determinados best_n_rule = np.argmax(np.mean(fold_eval, axis=1)) + 1 anfis_model = model(train_data, best_n_rule) train_anfis(anfis_model, data=train_data, epochs=20, show_plots=False) test_data, _ = data(partition='test') _, _, error = test_anfis(anfis_model, data=test_data, show_plots=True) print('erro percentual ={:.2f}%'.format(error))
x[idx, 0] = torch.tensor(data[t - 18]) x[idx, 1] = torch.tensor(data[t - 12]) x[idx, 2] = torch.tensor(data[t - 6]) x[idx, 3] = torch.tensor(data[t - 0]) y[idx, 0] = torch.tensor(data[t + 6]) td = TensorDataset(x, y) return DataLoader(td) def model(): invardefs = [ # ainda n escolhi os centros e sigma ('x(t-18)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])), ('x(t-12)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])), ('x(t-6)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])), ('x(t)', make_gauss_mfs(sigma=0.2, mu_list=[0.4, 1.2])) ] outvars = ['ys'] model = anfis.AnfisNet('mackey-glass', invardefs, outvars) return model if __name__ == '__main__': model = model() train_data = data(partition='train') train_anfis(model, data=train_data, epochs=20, show_plots=True) test_data = data(partition='test') test_anfis(model, data=test_data, show_plots=True)
] outvars = ['y0'] return anfis.AnfisNet('SSIE616', invardefs, outvars) dlTrain, dlTest = MakeData() anfModel = MakeModel() trainRMSE = 99 lastRMSE = 100 testRMSE = 99 while testRMSE < lastRMSE: with open('output.txt', 'a') as f: f.write('TRAINING RMS error={:.5f}'.format(trainRMSE)) f.write('\n') f.write('TEST RMS error={:.5f}'.format(testRMSE)) f.write('\n') f.write('\n') f.write(str(anfModel.layer.fuzzify.__repr__)) f.write('\n') f.write('\n') f.write(str(anfModel.layer.consequent.coeff)) f.write('\n') f.write('\n') trainRMSE = train_anfis(anfModel, dlTrain, 500, show_plots=False) lastRMSE = testRMSE testRMSE = test_anfis(anfModel, dlTest) print('done')