x = torch.linspace(0, 2 * np.pi, 100).reshape(-1, 1) y = torch.tensor([noisy_sin(p) for p in x], dtype=dtype).reshape(-1, 1) plt.plot(x, y) plt.show() td = TensorDataset(x, y) return DataLoader(td, batch_size=batch_size, shuffle=True) def ex1_model(): ''' Define modelo e parametros para funcoes de pertinencia ''' # invardefs = [ # ('x0', make_bell_mfs(3.33333, 2, list(np.linspace(0, 2*np.pi, 3)))) # ] invardefs = [('x0', make_gauss_mfs(sigma=1.2, mu_list=np.linspace(0, 2 * np.pi, 3)))] outvars = ['y0'] anf = anfis.AnfisNet('Aproximacao senoidal', invardefs, outvars) return anf if __name__ == '__main__': model = ex1_model() train_data = make_noisy_sin(batch_size=100) train_anfis(model, train_data, 20, True)
], dtype=dtype).view(25, 3) anf.coeff = torch.stack([y0_coeff, y1_coeff], dim=1) return anf 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)
if partition == "train": data = data[:(partition_size*4)] if partition == "test": data = data[(partition_size*4):] num_samples = int(len(data)/5) x = torch.zeros((num_samples, 4)) y = torch.zeros((num_samples, 1)) i = 0 for index in range(num_samples): x[index] = torch.tensor(data[i:i+4]) y[index] = data[i+4] i += 5 assert len(x) == len(y), "tamanhos diferentes" dl = DataLoader(TensorDataset(x, y), batch_size=1024, shuffle=True) return dl if __name__ == '__main__': show_plots = True model = ex4_model() train_data = jang_ex4_data("train") train_anfis(model, train_data, 500, show_plots) test_data = jang_ex4_data("test") test_anfis(model, test_data, show_plots)
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)
####### 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)
BellMembFunc(p['S2alpha'], p['S2beta'], p['S2gamma']), BellMembFunc(p['M2alpha'], p['M2beta'], p['M2gamma']), BellMembFunc(p['L2alpha'], p['L2beta'], p['L2gamma']) ]), ] outvars = ['y0'] return anfis.AnfisNet('SSIE616', invardefs, outvars) dlTrain, dlTest = MakeData() anfModel = MakeModel() trainRMSE = 99 lastRMSE = 100 testRMSE = 99 epochs = 0 # while (testRMSE < lastRMSE) and epochs < 10000: trainRMSE = train_anfis(anfModel, dlTrain, num_epochs, show_plots=False) # validation not being conducted # ANFIS not as susceptible to overfitting as plain MLP # lastRMSE = testRMSE # testRMSE = test_anfis(anfModel, dlTest, show_plots=False) # Save the model torch.save(anfModel, './outputs/model.pth') print('done')
] 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')