def test_regularized_model_selection_lambda(self): x_train = TEST_DATA['rms']['x_train'] y_train = TEST_DATA['rms']['y_train'] x_val = TEST_DATA['rms']['x_val'] y_val = TEST_DATA['rms']['y_val'] M = TEST_DATA['rms']['M'] lambda_values = TEST_DATA['rms']['lambda_values'] lambda_org = TEST_DATA['rms']['lambda'] _, _, _, lambda_computed = regularized_model_selection(x_train, y_train, x_val, y_val, M, lambda_values) self.assertAlmostEqual(lambda_org, lambda_computed, 6)
def test_regularized_model_selection_val_err(self): x_train = TEST_DATA['rms']['x_train'] y_train = TEST_DATA['rms']['y_train'] x_val = TEST_DATA['rms']['x_val'] y_val = TEST_DATA['rms']['y_val'] M = TEST_DATA['rms']['M'] lambda_values = TEST_DATA['rms']['lambda_values'] val_err = TEST_DATA['rms']['val_err'] _, _, val_err_computed, _ = regularized_model_selection(x_train, y_train, x_val, y_val, M, lambda_values) self.assertAlmostEqual(val_err, val_err_computed, 6)
def test_regularized_model_selection_w(self): x_train = TEST_DATA['rms']['x_train'] y_train = TEST_DATA['rms']['y_train'] x_val = TEST_DATA['rms']['x_val'] y_val = TEST_DATA['rms']['y_val'] M = TEST_DATA['rms']['M'] lambda_values = TEST_DATA['rms']['lambda_values'] w = TEST_DATA['rms']['w'] w_computed, _, _, _ = regularized_model_selection(x_train, y_train, x_val, y_val, M, lambda_values) max_diff = np.max(np.abs(w - w_computed)) self.assertAlmostEqual(max_diff, 0, 6)
def test_regularized_model_selection_w(self): x_train = TEST_DATA['rms']['x_train'] y_train = TEST_DATA['rms']['y_train'] x_val = TEST_DATA['rms']['x_val'] y_val = TEST_DATA['rms']['y_val'] M = TEST_DATA['rms']['M'] lambda_values = TEST_DATA['rms']['lambda_values'] w_expected = TEST_DATA['rms']['w'] w, _, _, _ = regularized_model_selection(x_train, y_train, x_val, y_val, M, lambda_values) self.assertEqual(np.shape(w), (8, 1)) np.testing.assert_almost_equal(w, w_expected)
def test_regularized_model_selection_lambda(self): x_train = TEST_DATA['rms']['x_train'] y_train = TEST_DATA['rms']['y_train'] x_val = TEST_DATA['rms']['x_val'] y_val = TEST_DATA['rms']['y_val'] M = TEST_DATA['rms']['M'] lambda_values = TEST_DATA['rms']['lambda_values'] lambda_expected = TEST_DATA['rms']['lambda'] _, _, _, lambda_ = regularized_model_selection( x_train, y_train, x_val, y_val, M, lambda_values) self.assertEqual(np.size(lambda_), 1) self.assertAlmostEqual(lambda_, lambda_expected)
plt.tight_layout() plt.draw() print('\n--- Wcisnij klawisz, aby kontynuowac ---') plt.waitforbuttonpress(0) print( '\n--- Selekcja modelu dla liniowego zadania najmniejszych kwadratow z regularyzacja ---' ) print( '-- Stopien M=7. Liczba punktow treningowych N=50. Liczba punktow walidacyjnych N=20 --' ) M = 7 lambdas = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100, 300] w, train_err, val_err, best_lambda = regularized_model_selection( data['x_train_50'], data['y_train_50'], data['x_val_20'], data['y_val_20'], M, lambdas) y_model = polynomial(x_plot, w) fig = plt.figure(figsize=(6, 5), num='Selekcja modelu dla parametru regularyzacji') sub = fig.add_subplot(1, 1, 1) sub.set_title('M={} Najlepsze $\lambda$={}'.format(M, best_lambda)) plot_model(data['x_train_50'], data['y_train_50'], x_plot, y_obj, y_model, data['x_val_20'], data['y_val_20'], train_err, val_err) plt.tight_layout() plt.draw() print('\n--- Wcisnij klawisz, aby kontynuowac ---') plt.waitforbuttonpress(0)