def test_exceptions(self): with self.assertRaises(ValueError): # Don't have learning rate algorithms.QuasiNewton((2, 3, 1), step=0.3) with self.assertRaises(ValueError): # Since it don't have learning rate, there is no need to set # up learning rate update algorithm algorithms.QuasiNewton((2, 3, 1), addons=[algorithms.LinearSearch])
def test_default_optimization(self): qnnet = algorithms.QuasiNewton((2, 3, 1)) self.assertEqual(qnnet.optimizations, [algorithms.WolfeSearch]) qnnet = algorithms.QuasiNewton((2, 3, 1), optimizations=[algorithms.WeightDecay]) self.assertEqual(qnnet.optimizations, [algorithms.WeightDecay, algorithms.WolfeSearch]) qnnet = algorithms.QuasiNewton( (2, 3, 1), optimizations=[algorithms.WeightDecay, algorithms.LinearSearch]) self.assertEqual(qnnet.optimizations, [algorithms.WeightDecay, algorithms.LinearSearch])
def test_exceptions(self): with self.assertRaises(ValueError): # Don't have learning rate algorithms.QuasiNewton( layers.Input(2) > layers.Sigmoid(3) > layers.Sigmoid(1), step=0.3, )
def test_bfgs(self): x_train, x_test, y_train, y_test = simple_classification() qnnet = algorithms.QuasiNewton( connection=[ layers.Sigmoid(10, init_method='ortho'), layers.Sigmoid(25, init_method='ortho'), layers.Output(1) ], shuffle_data=True, show_epoch='20 times', verbose=False, ) qnnet.train(x_train, y_train, x_test, y_test, epochs=20) result = qnnet.predict(x_test).round().astype(int) roc_curve_score = metrics.roc_auc_score(result, y_test) self.assertAlmostEqual(0.92, roc_curve_score, places=2)
def test_quasi_newton_psb(self): x_train, x_test, y_train, y_test = simple_classification() qnnet = algorithms.QuasiNewton( connection=[ layers.Input(10), layers.Sigmoid(30, weight=init.Orthogonal()), layers.Sigmoid(1, weight=init.Orthogonal()), ], shuffle_data=True, verbose=False, update_function='psb', h0_scale=2, ) qnnet.train(x_train, y_train, x_test, y_test, epochs=3) result = qnnet.predict(x_test).round() roc_curve_score = metrics.roc_auc_score(result, y_test) self.assertAlmostEqual(0.92, roc_curve_score, places=2)
def test_quasi_newton_bfgs(self): x_train, x_test, y_train, y_test = simple_classification() qnnet = algorithms.QuasiNewton( network=[ layers.Input(10), layers.Sigmoid(30, weight=init.Orthogonal()), layers.Sigmoid(1, weight=init.Orthogonal()), ], shuffle_data=True, show_epoch=10, update_function='bfgs', ) qnnet.train(x_train, y_train, x_test, y_test, epochs=50) result = qnnet.predict(x_test).round().astype(int) roc_curve_score = metrics.roc_auc_score(result, y_test) self.assertAlmostEqual(0.92, roc_curve_score, places=2)
def select_algorithm(self, algorithm, options=None): try: self.network = algorithms.LevenbergMarquardt(self.layers) opt = options print(opt[1]) print("Wybrano optymalizator: " + str(algorithm)) except RecursionError: print("Problem rekursji") return None if algorithm == 'GradientDescent': self.network = algorithms.GradientDescent(self.layers) if algorithm == 'LevenbergMarquardt': self.network = algorithms.LevenbergMarquardt(connection=self.layers, mu=opt[0], mu_update_factor=opt[1]) if algorithm == 'Adam': self.network = algorithms.Adam(self.layers) if algorithm == 'QuasiNewton': self.network = algorithms.QuasiNewton(self.layers) if algorithm == 'Quickprop': self.network = algorithms.Quickprop(self.layers) if algorithm == 'MinibatchGradientDescent': self.network = algorithms.MinibatchGradientDescent(self.layers) if algorithm == 'ConjugateGradient': self.network = algorithms.ConjugateGradient(self.layers) if algorithm == 'Hessian': self.network = algorithms.Hessian(self.layers) if algorithm == 'HessianDiagonal': self.network = algorithms.HessianDiagonal(self.layers) if algorithm == 'Momentum': self.network = algorithms.Momentum(self.layers) if algorithm == 'RPROP': self.network = algorithms.RPROP(self.layers) if algorithm == 'IRPROPPlus': self.network = algorithms.IRPROPPlus(self.layers) if algorithm == 'Adadelta': self.network = algorithms.Adadelta(self.layers) if algorithm == 'Adagrad': self.network = algorithms.Adagrad(self.layers) if algorithm == 'RMSProp': self.network = algorithms.RMSProp(self.layers) if algorithm == 'Adamax': self.network = algorithms.Adamax(self.layers)
def test_quasi_newton_sr1(self): x_train, x_test, y_train, y_test = simple_classification() qnnet = algorithms.QuasiNewton( connection=[ layers.Input(10), layers.Sigmoid(30, init_method='ortho'), layers.Sigmoid(1, init_method='ortho'), ], shuffle_data=True, show_epoch=20, verbose=False, update_function='sr1', h0_scale=2, gradient_tol=1e-5, ) qnnet.train(x_train, y_train, x_test, y_test, epochs=10) result = qnnet.predict(x_test).round() roc_curve_score = metrics.roc_auc_score(result, y_test) self.assertAlmostEqual(0.92, roc_curve_score, places=2)
def test_quasi_newton_bfgs(self): x_train, x_test, y_train, y_test = self.data qnnet = algorithms.QuasiNewton( connection=[ layers.SigmoidLayer(10, init_method='ortho'), layers.SigmoidLayer(20, init_method='ortho'), layers.OutputLayer(1) ], step=0.1, shuffle_data=True, show_epoch=20, verbose=False, update_function='bfgs', h0_scale=5, gradient_tol=1e-5, ) qnnet.train(x_train, y_train, x_test, y_test, epochs=10) result = qnnet.predict(x_test).round() roc_curve_score = metrics.roc_auc_score(result, y_test) self.assertAlmostEqual(0.92, roc_curve_score, places=2)
def QuasiNewton(col_predict, no_of_output_para, input_par, link, epoch, units, tf): global graph with graph.as_default(): dataset = pd.read_excel(link) #check for empty column cols_out = dataset.columns[col_predict:col_predict + 1] for col in cols_out: if "Unnamed" in col: return 0 X = dataset.iloc[:, no_of_output_para + 1:dataset.values[0].size].values Y = dataset.iloc[:, col_predict].values # np.random.seed(0) X_train = np.array(X) y_train = np.array(Y) X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0) sc = StandardScaler() X_train = sc.fit_transform(X_train) X_test = sc.transform(X_test) network = Input(input_par) >> Sigmoid(int(units / 10) + 1) >> Relu(1) optimizer = algorithms.QuasiNewton([network], update_function='bfgs', verbose=False, shuffle_data=False) optimizer.train(X_train, y_train, epochs=epoch) joblib.dump(optimizer, link + "-" + str(col_predict) + ".pkl")
def test_quasi_newton_assign_step_exception(self): with self.assertRaises(ValueError): algorithms.QuasiNewton((2, 3, 1), step=0.01)